Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rewrite UrlGenerator

  • Loading branch information...
commit 3a0ef67280ae810693d245dcaa744496b974bbf5 1 parent 4370748
@golovanov authored
View
15 DependencyInjection/Compiler/OverrideRoutingUrlGeneratorPass.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace Go\RoutingBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+class OverrideRoutingUrlGeneratorPass implements CompilerPassInterface
+{
+ public function process(ContainerBuilder $container)
+ {
+ $container->setParameter('router.options.generator_class', 'Go\\RoutingBundle\\Routing\\Generator\\UrlGenerator');
+ $container->setParameter('router.options.generator_base_class', 'Go\\RoutingBundle\\Routing\\Generator\\UrlGenerator');
+ }
+}
View
21 DependencyInjection/GoRoutingExtension.php
@@ -0,0 +1,21 @@
+<?php
+namespace Go\RoutingBundle\DependencyInjection;
+
+use Symfony\Component\Config\FileLocator;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
+use Symfony\Component\HttpKernel\DependencyInjection\Extension;
+
+class GoRoutingExtension extends Extension
+{
+
+ public function load(array $configs, ContainerBuilder $container)
+ {
+ //$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
+ //$loader->load('services.xml');
+
+ $this->addClassesToCompile(array(
+ 'Go\\RoutingBundle\\Routing\\Generator\\UrlGenerator',
+ ));
+ }
+}
View
8 GoRoutingBundle.php
@@ -3,7 +3,15 @@
namespace Go\RoutingBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Go\RoutingBundle\DependencyInjection\Compiler\OverrideRoutingUrlGeneratorPass;
class GoRoutingBundle extends Bundle
{
+ public function build(ContainerBuilder $container)
+ {
+ $container->addCompilerPass(new OverrideRoutingUrlGeneratorPass());
+
+ parent::build($container);
+ }
}
View
35 Routing/Generator/Dumper/PhpGeneratorDumper.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Go\RoutingBundle\Routing\Generator\Dumper;
+
+use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper as BasePhpGeneratorDumper;
+
+class PhpGeneratorDumper extends BasePhpGeneratorDumper
+{
+ /**
+ * Dumps a set of routes to a PHP class.
+ *
+ * Available options:
+ *
+ * * class: The class name
+ * * base_class: The base class name
+ *
+ * @param array $options An array of options
+ *
+ * @return string A PHP class representing the generator class
+ */
+ public function dump(array $options = array())
+ {
+ $options = array_merge(array(
+ 'class' => 'ProjectUrlGenerator',
+ 'base_class' => 'Go\\RoutingBundle\\Routing\\Generator\\UrlGenerator',
+ ), $options);
+
+ return
+ $this->startClass($options['class'], $options['base_class']).
+ $this->addConstructor().
+ $this->addGenerator().
+ $this->endClass()
+ ;
+ }
+}
View
85 Routing/Generator/UrlGenerator.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Go\RoutingBundle\Routing\Generator;
+
+use Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator;
+use Symfony\Component\Routing\Exception\InvalidParameterException;
+use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
+
+class UrlGenerator extends BaseUrlGenerator
+{
+ /**
+ * @throws Symfony\Component\Routing\Exception\MissingMandatoryParametersException When route has some missing mandatory parameters
+ * @throws Symfony\Component\Routing\Exception\InvalidParameterException When a parameter value is not correct
+ */
+ protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute)
+ {
+ $variables = array_flip($variables);
+
+ $originParameters = $parameters;
+ $parameters = array_replace($this->context->getParameters(), $parameters);
+ $tparams = array_replace($defaults, $parameters);
+
+ // all params must be given
+ if ($diff = array_diff_key($variables, $tparams)) {
+ throw new MissingMandatoryParametersException(sprintf('The "%s" route has some missing mandatory parameters (%s).', $name, implode(', ', $diff)));
+ }
+
+ $url = '';
+ $optional = true;
+ foreach ($tokens as $token) {
+ if ('variable' === $token[0]) {
+ if (false === $optional || !isset($defaults[$token[3]]) || (isset($parameters[$token[3]]) && $parameters[$token[3]] != $defaults[$token[3]])) {
+ if (!$isEmpty = in_array($tparams[$token[3]], array(null, '', false), true)) {
+ // check requirement
+ if ($tparams[$token[3]] && !preg_match('#^'.$token[2].'$#', $tparams[$token[3]])) {
+ throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]]));
+ }
+ }
+
+ if (!$isEmpty || !$optional) {
+ // %2F is not valid in a URL, so we don't encode it (which is fine as the requirements explicitly allowed it)
+ $url = $token[1].str_replace('%2F', '/', rawurlencode($tparams[$token[3]])).$url;
+ }
+
+ $optional = false;
+ }
+ } elseif ('text' === $token[0]) {
+ $url = $token[1].$url;
+ $optional = false;
+ }
+ }
+
+ if (!$url) {
+ $url = '/';
+ }
+
+ // add a query string if needed
+ if ($extra = array_diff_key($originParameters, $variables, $defaults)) {
+ $url .= '?'.http_build_query($extra);
+ }
+
+ $url = $this->context->getBaseUrl().$url;
+
+ if ($this->context->getHost()) {
+ $scheme = $this->context->getScheme();
+ if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme != $req) {
+ $absolute = true;
+ $scheme = $req;
+ }
+
+ if ($absolute) {
+ $port = '';
+ if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
+ $port = ':'.$this->context->getHttpPort();
+ } elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
+ $port = ':'.$this->context->getHttpsPort();
+ }
+
+ $url = $scheme.'://'.$this->context->getHost().$port.$url;
+ }
+ }
+
+ return $url;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.