Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 63afd286ebc31b861948ffc6b52dab62218b7013 0 parents
Jirgn Mssnr authored
92 README.md
@@ -0,0 +1,92 @@
+# itCrossAppRouting Plugin
+
+## Requirements
+* sfDependencyInjectionPlugin
+
+## Installation
+
+## copy and activate
+Copy the plugin to your projects plugins folder.
+You have to activate the Plugin in your global ProjectConfiguration.
+ class ProjectConfiguration extends sfProjectConfiguration {
+ ...
+ public function setup() {
+ ...
+ $this->enablePlugins(array(
+ ...
+ 'itCrossAppRoutingPlugin',
+ ...
+ //notice that the sfDependencyInjectionPlugin has to be enabled AFTER the itCrossAppRoutingPlguin
+ 'sfDependencyInjectionPlugin',
+ );
+ }
+ }
+
+### register helper
+The helper itCrossAppRoutingHelper can also be globally enabeld.
+This will be done by defining it as standard_helper in settings.yml @see http://www.symfony-project.org/reference/1_4/en/04-Settings#chapter_04_sub_standard_helpers
+
+### set app_routing.yml
+The Plugin entroduces a new configurationfile 'app_routing.yml'.
+This is an Environment specific yml Config like the most symfony config files.
+
+The following settings are supported
+Example:
+ ../config/app_routing.yml
+ all:
+ # key is the application name
+ # optionally you can use the :sf_culture placeholder to inject the cluture in path prefix
+ online_guard:
+ domain: http://ifb.berlinale.de
+ route_prefix: :domain/:sf_culture/path/to/online_guard
+ online_boa:
+ domain: http://ifb.berlinale.de
+ route_prefix: :domain/:sf_culture/path/to/online_boa
+
+## Usage
+The Routing can be called due to the configured service 'app_routing'
+
+### Example Actions / Components
+ ../actions.class.php
+ public function executeMyAction() {
+ $this->getService('app_routing')
+ ->generateApplicationUrl(
+ 'application_name',
+ 'route_name',
+ array('param'=>'123test',
+ $absolute)
+ );
+ ...
+ }
+
+### Example anywhere in the symfony context (eg. filter, ..)
+
+ public function getAnApplicationLink(){
+ $appRouting = sfContext::getInstance()->getConfiguration()->getService('app_routing');
+ return $appRouting->generateApplicationUrl(
+ 'application_name',
+ 'route_name',
+ array('param'=>'123test', $absolute);
+ }
+
+### Example template
+ ../myActionSuccess.php
+ //include helper if not already defined in standard_helpers
+ use_helper('itCrossAppRouting');
+
+ echo it_cross_app_url_for('app_name', 'route_name', array('param'=>'test123'));
+
+### Usage via string notation
+
+for using in routing.yml there is an altenative syntax to descripe app routings
+the convention is like
+
+ $alternativeNotation = '@@appname?app_param=value@routename?routeparam=othervalue'
+
+so you can call
+
+ $appRouting->generateApplicationUrlByString($alternativeNotation);
+
+
+
+
65 config/app_routing.yml
@@ -0,0 +1,65 @@
+all: &default
+ domain: http://www.cornelsen.de
+ duden_domain: http://www.duden-schulverlage.de
+ duden_sbk_base:
+ # domain: can/be/specified/here
+ route_prefix: :duden_domain/:alias/
+ sbk_base:
+# domain: can/be/specified/here
+ route_prefix: :domain/:alias/
+ lehrkraefte:
+# domain: can/be/specified/here
+ route_prefix: :domain/lehrkraefte
+ home:
+# domain: can/be/specified/here
+ route_prefix: :domain/home
+
+
+dev_test: &dev_test
+ <<: *default
+ domain: http://test.cornelsen.de
+ duden_domain: http://test.cornelsen.de
+
+test:
+ <<: *dev_test
+
+dev_test2: &dev_test2
+ <<: *default
+ domain: http://test2.cornelsen.de
+ duden_domain: http://test2.cornelsen.de
+
+test2:
+ <<: *dev_test2
+
+dev_referenz: &dev_referenz
+ <<: *default
+ domain: http://referenz.cornelsen.de
+ duden_domain: http://referenz.cornelsen.de
+
+referenz:
+ <<: *dev_referenz
+
+dev_entwicklung: &dev_entwicklung
+ <<: *default
+ domain: http://entwicklung.cornelsen.de
+ duden_domain: http://entwicklung.cornelsen.de
+
+entwicklung:
+ <<: *dev_entwicklung
+
+dev_entwicklung2: &dev_entwicklung2
+ <<: *default
+ domain: http://entwicklung2.cornelsen.de
+ duden_domain: http://entwicklung2.cornelsen.de
+
+entwicklung2:
+ <<: *dev_entwicklung2
+
+
+dev_local: &dev_local
+ <<: *default
+ domain: http://cornelsen.vbox
+ duden_domain: http://cornelsen.vbox
+
+prod_local:
+ <<: *dev_local
5 config/config_handlers.yml
@@ -0,0 +1,5 @@
+config/app_routing.yml:
+ class: itCrossAppConfigHandler
+ file: %SF_PLUGINS_DIR%/itCrossAppRoutingPlugin/lib/config/itCrossAppConfigHandler.class.php
+ param:
+ prefix: sf_app_routing
30 config/itCrossAppRoutingPluginConfiguration.class.php
@@ -0,0 +1,30 @@
+<?php
+class itCrossAppRoutingPluginConfiguration extends sfPluginConfiguration {
+
+ public function initialize() {
+ $this->initAppRoutingConfig();
+ $this->dispatcher->connect('service_container.load_configuration', array($this, 'listenToServiceContainerLoadConfiguration'));
+ }
+
+ public function listenToServiceContainerLoadConfiguration(sfEvent $event) {
+ $container = $event->getSubject();
+ $loader = new sfServiceContainerLoaderFileYaml($container);
+ $loader->load(dirname(__FILE__).'/services.yml');
+ }
+
+ /**
+ * inits application route config app_routing.yml
+ */
+ protected function initAppRoutingConfig() {
+
+ $config = 'config/app_routing.yml';
+ if($this->configuration instanceof sfApplicationConfiguration) {
+ $cacheConfig = new sfConfigCache($this->configuration);
+ $cacheConfig->import($config, true);
+ }
+ else {
+ $configFiles = $this->configuration->getConfigPaths($config);
+ sfConfig::set(itCrossAppConfigHandler::DEFAULT_PREFIX, itCrossAppConfigHandler::getConfiguration($configFiles));
+ }
+ }
+}
7 config/services.yml
@@ -0,0 +1,7 @@
+parameters:
+ testparam: foo
+
+services:
+ app_routing:
+ class: itCrossAppRoutingService
+ shared: true
30 lib/config/itCrossAppConfigHandler.class.php
@@ -0,0 +1,30 @@
+<?php
+
+class itCrossAppConfigHandler extends sfYamlConfigHandler {
+
+ const DEFAULT_PREFIX = 'sf_app_routing';
+
+ public function execute($configFiles)
+ {
+
+ $prefix = strtolower($this->getParameterHolder()->get('prefix', self::DEFAULT_PREFIX));
+ $config = self::getConfiguration($configFiles);
+
+ // compile data
+ $retval = sprintf("<?php\n".
+ "// auto-generated by %s\n".
+ "// date: %s\nsfConfig::set('{$prefix}', \n%s\n);\n?>",
+ __CLASS__, date('Y/m/d H:i:s'), var_export($config,true));
+
+ return $retval;
+ }
+
+ /**
+ * @see sfConfigHandler
+ */
+ static public function getConfiguration(array $configFiles)
+ {
+ return self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles)));
+ }
+
+}
41 lib/helper/itCrossAppRoutingHelper.php
@@ -0,0 +1,41 @@
+<?php
+
+/**
+ *
+ * generates a url for given app route
+ * @param string $app_name
+ * @param string $route_name
+ * @param array $params
+ * @return string absolute url to application route
+ */
+function it_cross_app_url_for($app_name, $route_name, $params = array(), $absolute = false, $prefixParams = array())
+{
+ return sfContext::getInstance()
+ ->getConfiguration()
+ ->getService('app_routing')
+ ->generateApplicationUrl(
+ $app_name,
+ $route_name,
+ $params,
+ $absolute,
+ $prefixParams
+ );
+}
+
+/**
+ *
+ * generates url based on mofified uri string
+ * @param string $uridef in form @@appname?prefixparam1=value&prefixparam2=...@routename?routeparam1=value&routeparam2=value
+ * @return string
+ */
+function it_cross_app_url_for2($uridef, $absolute = false)
+{
+ if (preg_match('(^\w?://)', $uridef)) {
+ return $uridef;
+ }
+
+ return sfContext::getInstance()
+ ->getConfiguration()
+ ->getService('app_routing')
+ ->generateApplicationUrlByString($uridef, $absolute);
+}
122 lib/service/itCrossAppRoutingService.php
@@ -0,0 +1,122 @@
+<?php
+class itCrossAppRoutingService
+{
+
+
+ protected $appRoutings = array();
+
+ /**
+ *
+ * generates url for application based route
+ * @param string $app_name
+ * @param string $route_name
+ * @param array $parameters
+ * @param boolean $absolute (default is false)
+ * @param array $prefixParams
+ * @throws sfConfigurationException
+ * @return string
+ */
+ public function generateApplicationUrl($app_name, $route_name, $parameters = array(), $absolute = false, $prefixParams = array())
+ {
+ $context = sfContext::getInstance();
+
+ $name = 'sf_app_routing_' . $app_name;
+ $config = sfConfig::get('sf_app_routing');
+ if (!$config[$app_name]) {
+ throw new sfConfigurationException('no application routing defined for app ' . $app_name);
+ }
+ //add some system params for replacement in prefix
+ $routeDomain = '';
+ $routeDudenDomain = '';
+ if ($absolute) {
+ if (!empty($config['domain'])) {
+ $routeDomain = $config['domain'];
+ }
+ if (!empty($config[$app_name]['domain'])) {
+ $routeDomain = $config[$app_name]['domain'];
+ }
+ if (!empty($config['duden_domain'])) {
+ $routeDudenDomain = $config['duden_domain'];
+ }
+ if (!empty($config[$app_name]['duden_domain'])) {
+ $routeDudenDomain = $config[$app_name]['duden_domain'];
+ }
+ }
+ $replacement = array(
+ 'sf_culture' => isset($prefixParams['sf_culture']) ? $prefixParams['sf_culture'] : $context->getUser()->getCulture(),
+ 'domain' => isset($prefixParams['domain']) ? $prefixParams['domain'] : $routeDomain,
+ 'duden_domain' => isset($prefixParams['duden_domain']) ? $prefixParams['duden_domain'] : $routeDudenDomain,
+ );
+ foreach ($prefixParams as $param => $value) {
+ $replacement[$param] = $value;
+ }
+ $routePrefix = $config[$app_name]['route_prefix'];
+
+ foreach ($replacement as $paramName => $paramValue) {
+ $routePrefix = preg_replace('@:' . $paramName . '@', $paramValue, $routePrefix);
+ }
+
+ $scriptName = (sfConfig::get('sf_no_script_name', false) ? '' : '/'.basename($context->getRequest()->getScriptName()));
+
+ return rtrim($routePrefix, '/') . $scriptName . $this->getApplicationRouting($app_name)->generate($route_name, $parameters);
+ }
+
+ /**
+ *
+ * @param string $uridef in form @@appname?prefixparam1=value&prefixparam2=...@routename?routeparam1=value&routeparam2=value
+ * @param boolean indicating whether should generate absolute url
+ * @return string
+ *
+ */
+ public function generateApplicationUrlByString($uri_string, $absolute = false)
+ {
+ $isAppRoute = substr($uri_string, 0, 2) === '@@';
+
+ $config = sfContext::getInstance()->getConfiguration();
+ if (!$isAppRoute) {
+ throw new Exception('given route is no app route');
+ }
+
+ /* @var $app_routing itCrossAppRoutingService */
+ $matches;
+ preg_match('%@@(\w+)\??(.*)@(\w+)\??(.*)$%', $uri_string, $matches);
+ $appName = $matches[1];
+ parse_str($matches[2], $prefixParams);
+ $routeName = $matches[3];
+ parse_str($matches[4], $routeParams);
+
+ // replace :values with values from current request context
+ $tmpParams = $routeParams;
+ $request = sfContext::getInstance()->getRequest();
+ foreach ($tmpParams as $name => $value) {
+ if (strlen($value && $value[0] === ':')) {
+ $refParamName = substr($value, 1);
+ $routeParams[$name] = $request->getParameter($refParamName);
+ }
+ }
+ return $this->generateApplicationUrl($appName, $routeName, $routeParams, $absolute, $prefixParams);
+ }
+
+ /**
+ *
+ * gets the routing class for application given
+ * @param string $app_name
+ */
+ protected function getApplicationRouting($app_name)
+ {
+ if (!isset($this->appRoutings[$app_name])) {
+ $routing = new sfPatternRouting(new sfEventDispatcher());
+ $config = new sfRoutingConfigHandler();
+ $routingPath = array(sfConfig::get('sf_apps_dir'),
+ $app_name,
+ 'config',
+ 'routing.yml'
+ );
+ $routingPath = implode(DIRECTORY_SEPARATOR, $routingPath);
+ $routing->setRoutes($config->evaluate(array($routingPath)));
+ $this->appRoutings[$app_name] = $routing;
+ }
+ return $this->appRoutings[$app_name];
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.