[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/routing/Symfony/Component/Routing/Generator/Dumper/ -> PhpGeneratorDumper.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Routing\Generator\Dumper;
  13  
  14  /**
  15   * PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.
  16   *
  17   * @author Fabien Potencier <fabien@symfony.com>
  18   * @author Tobias Schultze <http://tobion.de>
  19   */
  20  class PhpGeneratorDumper extends GeneratorDumper
  21  {
  22      /**
  23       * Dumps a set of routes to a PHP class.
  24       *
  25       * Available options:
  26       *
  27       *  * class:      The class name
  28       *  * base_class: The base class name
  29       *
  30       * @param array $options An array of options
  31       *
  32       * @return string A PHP class representing the generator class
  33       */
  34      public function dump(array $options = array())
  35      {
  36          $options = array_merge(array(
  37              'class' => 'ProjectUrlGenerator',
  38              'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
  39          ), $options);
  40  
  41          return <<<EOF
  42  <?php
  43  
  44  use Symfony\Component\Routing\RequestContext;
  45  use Symfony\Component\Routing\Exception\RouteNotFoundException;
  46  use Psr\Log\LoggerInterface;
  47  
  48  /**
  49   * {$options['class']}
  50   *
  51   * This class has been auto-generated
  52   * by the Symfony Routing Component.
  53   */
  54  class {$options['class']} extends {$options['base_class']}
  55  {
  56      private static \$declaredRoutes;
  57  
  58      /**
  59       * Constructor.
  60       */
  61      public function __construct(RequestContext \$context, LoggerInterface \$logger = null)
  62      {
  63          \$this->context = \$context;
  64          \$this->logger = \$logger;
  65          if (null === self::\$declaredRoutes) {
  66              self::\$declaredRoutes = {$this->generateDeclaredRoutes()};
  67          }
  68      }
  69  
  70  {$this->generateGenerateMethod()}
  71  }
  72  
  73  EOF;
  74      }
  75  
  76      /**
  77       * Generates PHP code representing an array of defined routes
  78       * together with the routes properties (e.g. requirements).
  79       *
  80       * @return string PHP code
  81       */
  82      private function generateDeclaredRoutes()
  83      {
  84          $routes = "array(\n";
  85          foreach ($this->getRoutes()->all() as $name => $route) {
  86              $compiledRoute = $route->compile();
  87  
  88              $properties = array();
  89              $properties[] = $compiledRoute->getVariables();
  90              $properties[] = $route->getDefaults();
  91              $properties[] = $route->getRequirements();
  92              $properties[] = $compiledRoute->getTokens();
  93              $properties[] = $compiledRoute->getHostTokens();
  94  
  95              $routes .= sprintf("        '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true)));
  96          }
  97          $routes .= '    )';
  98  
  99          return $routes;
 100      }
 101  
 102      /**
 103       * Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface.
 104       *
 105       * @return string PHP code
 106       */
 107      private function generateGenerateMethod()
 108      {
 109          return <<<'EOF'
 110      public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
 111      {
 112          if (!isset(self::$declaredRoutes[$name])) {
 113              throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
 114          }
 115  
 116          list($variables, $defaults, $requirements, $tokens, $hostTokens) = self::$declaredRoutes[$name];
 117  
 118          return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens);
 119      }
 120  EOF;
 121      }
 122  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1