Skip to content

Applied the new styles to the router: commands #14132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Prev Previous commit
Next Next commit
Move the logic to display route information back to the text descriptor
  • Loading branch information
javiereguiluz committed Apr 1, 2015
commit 9ebdcc1326f9aa816a9fd71407b6fcf22368941a
111 changes: 10 additions & 101 deletions src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,117 +96,26 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->convertController($route);

if ('txt' === $input->getOption('format')) {
$this->displayRouteInformation($name, $route, $output);
} else {
$helper->describe($output, $route, array(
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'name' => $name,
));
}
$helper->describe($output, $route, array(
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'name' => $name,
));
} else {
$routes = $this->getContainer()->get('router')->getRouteCollection();

foreach ($routes as $route) {
$this->convertController($route);
}

if ('txt' === $input->getOption('format')) {
$this->displayRouteCollectionInformation($routes, $input->getOption('show-controllers'), $output);
} else {
$helper->describe($output, $routes, array(
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'show_controllers' => $input->getOption('show-controllers'),
));
}
$helper->describe($output, $routes, array(
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'show_controllers' => $input->getOption('show-controllers'),
));
}
}

private function displayRouteInformation($name, Route $route, OutputInterface $output)
{
$requirements = $route->getRequirements();

$output->table(
array('Property', 'Value'),
array(
array('Route Name', $name),
array('Path', $route->getPath()),
array('Path Regex', $route->compile()->getRegex()),
array('Host', ('' !== $route->getHost() ? $route->getHost() : 'ANY')),
array('Host Regex', ('' !== $route->getHost() ? $route->compile()->getHostRegex() : '')),
array('Scheme', ($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY')),
array('Method', ($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY')),
array('Requirements', ($requirements ? $this->formatRouterConfig($requirements) : 'NO CUSTOM')),
array('Class', get_class($route)),
array('Defaults', $this->formatRouterConfig($route->getDefaults())),
array('Options', $this->formatRouterConfig($route->getOptions())),
)
);
}

private function displayRouteCollectionInformation(RouteCollection $routes, $showControllers, OutputInterface $output)
{
$tableHeaders = array('Name', 'Method', 'Scheme', 'Host', 'Path');
if ($showControllers) {
$tableHeaders[] = 'Controller';
}

$tableRows = array();
foreach ($routes->all() as $name => $route) {
$row = array(
$name,
$route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
$route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
'' !== $route->getHost() ? $route->getHost() : 'ANY',
$route->getPath(),
);

if ($showControllers) {
$controller = $route->getDefault('_controller');
if ($controller instanceof \Closure) {
$controller = 'Closure';
} elseif (is_object($controller)) {
$controller = get_class($controller);
}
$row[] = $controller;
}

$tableRows[] = $row;
}

$output->table($tableHeaders, $tableRows);
}

private function formatRouterConfig(array $config)
{
if (!count($config)) {
return 'NONE';
}

$string = '';
ksort($config);
foreach ($config as $name => $value) {
$string .= sprintf("\n%s: %s", $name, $this->formatValue($value));
}

return trim($string);
}

private function formatValue($value)
{
if (is_object($value)) {
return sprintf('object(%s)', get_class($value));
}

if (is_string($value)) {
return $value;
}

return preg_replace("/\n\s*/s", '', var_export($value, true));
}

private function convertController(Route $route)
{
$nameParser = $this->getContainer()->get('controller_name_converter');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ class TextDescriptor extends Descriptor
protected function describeRouteCollection(RouteCollection $routes, array $options = array())
{
$showControllers = isset($options['show_controllers']) && $options['show_controllers'];
$headers = array('Name', 'Method', 'Scheme', 'Host', 'Path');
$table = new Table($this->getOutput());
$table->setStyle('compact');
$table->setHeaders($showControllers ? array_merge($headers, array('Controller')) : $headers);

$tableHeaders = array('Name', 'Method', 'Scheme', 'Host', 'Path');
if ($showControllers) {
$tableHeaders[] = 'Controller';
}

$tableRows = array();
foreach ($routes->all() as $name => $route) {
$row = array(
$name,
Expand All @@ -58,11 +60,10 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
$row[] = $controller;
}

$table->addRow($row);
$tableRows[] = $row;
}

$this->writeText($this->formatSection('router', 'Current routes')."\n", $options);
$table->render();
$this->getOutput()->table($tableHeaders, $tableRows);
}

/**
Expand All @@ -73,26 +74,22 @@ protected function describeRoute(Route $route, array $options = array())
$requirements = $route->getRequirements();
unset($requirements['_scheme'], $requirements['_method']);

// fixme: values were originally written as raw
$description = array(
'<comment>Path</comment> '.$route->getPath(),
'<comment>Path Regex</comment> '.$route->compile()->getRegex(),
'<comment>Host</comment> '.('' !== $route->getHost() ? $route->getHost() : 'ANY'),
'<comment>Host Regex</comment> '.('' !== $route->getHost() ? $route->compile()->getHostRegex() : ''),
'<comment>Scheme</comment> '.($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'),
'<comment>Method</comment> '.($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'),
'<comment>Class</comment> '.get_class($route),
'<comment>Defaults</comment> '.$this->formatRouterConfig($route->getDefaults()),
'<comment>Requirements</comment> '.($requirements ? $this->formatRouterConfig($requirements) : 'NO CUSTOM'),
'<comment>Options</comment> '.$this->formatRouterConfig($route->getOptions()),
$tableHeaders = array('Property', 'Value');
$tableRows = array(
array('Route Name', $options['name']),
array('Path', $route->getPath()),
array('Path Regex', $route->compile()->getRegex()),
array('Host', ('' !== $route->getHost() ? $route->getHost() : 'ANY')),
array('Host Regex', ('' !== $route->getHost() ? $route->compile()->getHostRegex() : '')),
array('Scheme', ($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY')),
array('Method', ($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY')),
array('Requirements', ($requirements ? $this->formatRouterConfig($requirements) : 'NO CUSTOM')),
array('Class', get_class($route)),
array('Defaults', $this->formatRouterConfig($route->getDefaults())),
array('Options', $this->formatRouterConfig($route->getOptions())),
);

if (isset($options['name'])) {
array_unshift($description, '<comment>Name</comment> '.$options['name']);
array_unshift($description, $this->formatSection('router', sprintf('Route "%s"', $options['name'])));
}

$this->writeText(implode("\n", $description)."\n", $options);
$this->getOutput()->table($tableHeaders, $tableRows);
}

/**
Expand Down Expand Up @@ -376,23 +373,24 @@ protected function describeCallable($callable, array $options = array())
}

/**
* @param array $array
* @param array $config
*
* @return string
*/
private function formatRouterConfig(array $array)
private function formatRouterConfig(array $config)
{
if (!count($array)) {
if (empty($config)) {
return 'NONE';
}

$string = '';
ksort($array);
foreach ($array as $name => $value) {
$string .= ($string ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
ksort($config);

$configAsString = '';
foreach ($config as $key => $value) {
$configAsString .= sprintf("\n%s: %s", $key, $this->formatValue($value));
}

return $string;
return trim($configAsString);
}

/**
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy