| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfAppRoutesTask extends sfBaseTask |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$routes = array(); |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
* @see sfTask |
|---|
| 26 |
*/ |
|---|
| 27 |
protected function configure() |
|---|
| 28 |
{ |
|---|
| 29 |
$this->addArguments(array( |
|---|
| 30 |
new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'), |
|---|
| 31 |
new sfCommandArgument('name', sfCommandArgument::OPTIONAL, 'A route name'), |
|---|
| 32 |
)); |
|---|
| 33 |
|
|---|
| 34 |
$this->namespace = 'app'; |
|---|
| 35 |
$this->name = 'routes'; |
|---|
| 36 |
$this->briefDescription = 'Displays current routes for an application'; |
|---|
| 37 |
|
|---|
| 38 |
$this->detailedDescription = <<<EOF |
|---|
| 39 |
The [app:routes|INFO] displays the current routes for a given application: |
|---|
| 40 |
|
|---|
| 41 |
[./symfony app:routes frontend|INFO] |
|---|
| 42 |
EOF; |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
* @see sfTask |
|---|
| 47 |
*/ |
|---|
| 48 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 49 |
{ |
|---|
| 50 |
|
|---|
| 51 |
$config = sfFactoryConfigHandler::getConfiguration($this->configuration->getConfigPaths('config/factories.yml')); |
|---|
| 52 |
$params = array_merge($config['routing']['param'], array('load_configuration' => false, 'logging' => false)); |
|---|
| 53 |
|
|---|
| 54 |
$config = new sfRoutingConfigHandler(); |
|---|
| 55 |
$routes = $config->evaluate($this->configuration->getConfigPaths('config/routing.yml')); |
|---|
| 56 |
|
|---|
| 57 |
$routing = new sfPatternRouting($this->dispatcher, null, $params); |
|---|
| 58 |
$routing->setRoutes($routes); |
|---|
| 59 |
|
|---|
| 60 |
$this->dispatcher->notify(new sfEvent($routing, 'routing.load_configuration')); |
|---|
| 61 |
|
|---|
| 62 |
$this->routes = $routing->getRoutes(); |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
$arguments['name'] ? $this->outputRoute($arguments['application'], $arguments['name']) : $this->outputRoutes($arguments['application']); |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
protected function outputRoutes($application) |
|---|
| 69 |
{ |
|---|
| 70 |
$this->logSection('app', sprintf('Current routes for application "%s"', $application)); |
|---|
| 71 |
|
|---|
| 72 |
$maxName = 4; |
|---|
| 73 |
$maxMethod = 6; |
|---|
| 74 |
foreach ($this->routes as $name => $route) |
|---|
| 75 |
{ |
|---|
| 76 |
$requirements = $route->getRequirements(); |
|---|
| 77 |
$method = isset($requirements['sf_method']) ? strtoupper(is_array($requirements['sf_method']) ? implode(', ', $requirements['sf_method']) : $requirements['sf_method']) : 'ANY'; |
|---|
| 78 |
|
|---|
| 79 |
if (strlen($name) > $maxName) |
|---|
| 80 |
{ |
|---|
| 81 |
$maxName = strlen($name); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
if (strlen($method) > $maxMethod) |
|---|
| 85 |
{ |
|---|
| 86 |
$maxMethod = strlen($method); |
|---|
| 87 |
} |
|---|
| 88 |
} |
|---|
| 89 |
$format = '%-'.$maxName.'s %-'.$maxMethod.'s %s'; |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
$format1 = '%-'.($maxName + 9).'s %-'.($maxMethod + 9).'s %s'; |
|---|
| 93 |
$this->log(sprintf($format1, $this->formatter->format('Name', 'COMMENT'), $this->formatter->format('Method', 'COMMENT'), $this->formatter->format('Pattern', 'COMMENT'))); |
|---|
| 94 |
foreach ($this->routes as $name => $route) |
|---|
| 95 |
{ |
|---|
| 96 |
$requirements = $route->getRequirements(); |
|---|
| 97 |
$method = isset($requirements['sf_method']) ? strtoupper(is_array($requirements['sf_method']) ? implode(', ', $requirements['sf_method']) : $requirements['sf_method']) : 'ANY'; |
|---|
| 98 |
$this->log(sprintf($format, $name, $method, $route->getPattern())); |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
protected function outputRoute($application, $name) |
|---|
| 103 |
{ |
|---|
| 104 |
$this->logSection('app', sprintf('Route "%s" for application "%s"', $name, $application)); |
|---|
| 105 |
|
|---|
| 106 |
if (!isset($this->routes[$name])) |
|---|
| 107 |
{ |
|---|
| 108 |
throw new sfCommandException(sprintf('The route "%s" does not exist.', $name)); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
$route = $this->routes[$name]; |
|---|
| 112 |
$this->log(sprintf('%s %s', $this->formatter->format('Name', 'COMMENT'), $name)); |
|---|
| 113 |
$this->log(sprintf('%s %s', $this->formatter->format('Pattern', 'COMMENT'), $route->getPattern())); |
|---|
| 114 |
$this->log(sprintf('%s %s', $this->formatter->format('Class', 'COMMENT'), get_class($route))); |
|---|
| 115 |
|
|---|
| 116 |
$defaults = ''; |
|---|
| 117 |
$d = $route->getDefaults(); |
|---|
| 118 |
ksort($d); |
|---|
| 119 |
foreach ($d as $name => $value) |
|---|
| 120 |
{ |
|---|
| 121 |
$defaults .= ($defaults ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value); |
|---|
| 122 |
} |
|---|
| 123 |
$this->log(sprintf('%s %s', $this->formatter->format('Defaults', 'COMMENT'), $defaults)); |
|---|
| 124 |
|
|---|
| 125 |
$requirements = ''; |
|---|
| 126 |
$r = $route->getRequirements(); |
|---|
| 127 |
ksort($r); |
|---|
| 128 |
foreach ($r as $name => $value) |
|---|
| 129 |
{ |
|---|
| 130 |
$requirements .= ($requirements ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value); |
|---|
| 131 |
} |
|---|
| 132 |
$this->log(sprintf('%s %s', $this->formatter->format('Requirements', 'COMMENT'), $requirements)); |
|---|
| 133 |
|
|---|
| 134 |
$options = ''; |
|---|
| 135 |
$o = $route->getOptions(); |
|---|
| 136 |
ksort($o); |
|---|
| 137 |
foreach ($o as $name => $value) |
|---|
| 138 |
{ |
|---|
| 139 |
$options .= ($options ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value); |
|---|
| 140 |
} |
|---|
| 141 |
$this->log(sprintf('%s %s', $this->formatter->format('Options', 'COMMENT'), $options)); |
|---|
| 142 |
$this->log(sprintf('%s %s', $this->formatter->format('Regex', 'COMMENT'), preg_replace('/^ /', '', preg_replace('/^/m', ' ', $route->getRegex())))); |
|---|
| 143 |
|
|---|
| 144 |
$tokens = ''; |
|---|
| 145 |
foreach ($route->getTokens() as $token) |
|---|
| 146 |
{ |
|---|
| 147 |
if (!$tokens) |
|---|
| 148 |
{ |
|---|
| 149 |
$tokens = $this->displayToken($token); |
|---|
| 150 |
} |
|---|
| 151 |
else |
|---|
| 152 |
{ |
|---|
| 153 |
$tokens .= "\n".str_repeat(' ', 13).$this->displayToken($token); |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
$this->log(sprintf('%s %s', $this->formatter->format('Tokens', 'COMMENT'), $tokens)); |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
protected function displayToken($token) |
|---|
| 160 |
{ |
|---|
| 161 |
$type = array_shift($token); |
|---|
| 162 |
array_shift($token); |
|---|
| 163 |
|
|---|
| 164 |
return sprintf('%-10s %s', $type, $this->formatValue($token)); |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
protected function formatValue($value) |
|---|
| 168 |
{ |
|---|
| 169 |
if (is_object($value)) |
|---|
| 170 |
{ |
|---|
| 171 |
return sprintf('object(%s)', get_class($value)); |
|---|
| 172 |
} |
|---|
| 173 |
else |
|---|
| 174 |
{ |
|---|
| 175 |
return preg_replace("/\n\s*/s", '', var_export($value, true)); |
|---|
| 176 |
} |
|---|
| 177 |
} |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|