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