class PhpGeneratorDumper

PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.

@author Fabien Potencier <fabien@symfony.com> @author Tobias Schultze <http://tobion.de>

@api

Hierarchy

Expanded class hierarchy of PhpGeneratorDumper

1 file declares its use of PhpGeneratorDumper
PhpGeneratorDumperTest.php in drupal/core/vendor/symfony/routing/Symfony/Component/Routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php

File

drupal/core/vendor/symfony/routing/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php, line 22

Namespace

Symfony\Component\Routing\Generator\Dumper
View source
class PhpGeneratorDumper extends GeneratorDumper {

  /**
   * 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
   *
   * @api
   */
  public function dump(array $options = array()) {
    $options = array_merge(array(
      'class' => 'ProjectUrlGenerator',
      'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
    ), $options);
    return <<<EOF
<?php

use Symfony\\Component\\Routing\\RequestContext;
use Symfony\\Component\\Routing\\Exception\\RouteNotFoundException;
use Psr\\Log\\LoggerInterface;

/**
 * {<span class="php-variable">$options</span>[<span class="php-string">'class'</span>]}
 *
 * This class has been auto-generated
 * by the Symfony Routing Component.
 */
class {<span class="php-variable">$options</span>[<span class="php-string">'class'</span>]} extends {<span class="php-variable">$options</span>[<span class="php-string">'base_class'</span>]}
{
    static private \$declaredRoutes = {<span class="php-variable">$this</span>
  -&gt;<span class="php-function-or-constant function member-of-self">generateDeclaredRoutes</span>()};

    /**
     * Constructor.
     */
    public function __construct(RequestContext \$context, LoggerInterface \$logger = null)
    {
        \$this->context = \$context;
        \$this->logger = \$logger;
    }

{<span class="php-variable">$this</span>
  -&gt;<span class="php-function-or-constant function member-of-self">generateGenerateMethod</span>()}
}

EOF;
  }

  /**
   * Generates PHP code representing an array of defined routes
   * together with the routes properties (e.g. requirements).
   *
   * @return string PHP code
   */
  private function generateDeclaredRoutes() {
    $routes = "array(\n";
    foreach ($this
      ->getRoutes()
      ->all() as $name => $route) {
      $compiledRoute = $route
        ->compile();
      $properties = array();
      $properties[] = $compiledRoute
        ->getVariables();
      $properties[] = $route
        ->getDefaults();
      $properties[] = $route
        ->getRequirements();
      $properties[] = $compiledRoute
        ->getTokens();
      $properties[] = $compiledRoute
        ->getHostTokens();
      $routes .= sprintf("        '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true)));
    }
    $routes .= '    )';
    return $routes;
  }

  /**
   * Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface.
   *
   * @return string PHP code
   */
  private function generateGenerateMethod() {
    return <<<EOF
    public function generate(\$name, \$parameters = array(), \$referenceType = self::ABSOLUTE_PATH)
    {
        if (!isset(self::\$declaredRoutes[\$name])) {
            throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', \$name));
        }

        list(\$variables, \$defaults, \$requirements, \$tokens, \$hostTokens) = self::\$declaredRoutes[\$name];

        return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$referenceType, \$hostTokens);
    }
EOF;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GeneratorDumper::$routes private property
GeneratorDumper::getRoutes public function Gets the routes to dump. Overrides GeneratorDumperInterface::getRoutes
GeneratorDumper::__construct public function Constructor.
PhpGeneratorDumper::dump public function Dumps a set of routes to a PHP class. Overrides GeneratorDumperInterface::dump
PhpGeneratorDumper::generateDeclaredRoutes private function Generates PHP code representing an array of defined routes together with the routes properties (e.g. requirements).
PhpGeneratorDumper::generateGenerateMethod private function Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface.