Skip to content
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

[3.3.] Nut debug tweaks #6543

Merged
merged 6 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions src/Nut/DebugEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Bolt\Nut;

use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand All @@ -20,45 +22,48 @@ protected function configure()
{
$this
->setName('debug:events')
->setDescription('System events, and target callable debug dumper.')
->setDescription('Dumps event listeners.')
->addOption('sort-listener', null, InputOption::VALUE_NONE, 'Sort events in order of callable name.')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->displayListeners($output);
}

/**
* Output a table of listeners, and their callbacks.
*
* @param OutputInterface $output
*/
private function displayListeners(OutputInterface $output)
{
$table = new Table($output);
$table->setHeaders(array(
'Event Name',
'Priority',
'Listener',
));
$rightAligned = new TableStyle();
$rightAligned->setPadType(STR_PAD_LEFT);
$table->setHeaders([
['Event Name', 'Listener', 'Priority'],
]);
$table->setColumnStyle(2, $rightAligned);
$dispatcher = $this->app['dispatcher'];
$listeners = $dispatcher->getListeners();

foreach ($listeners as $eventName => $eventListeners) {
if ($input->getOption('sort-listener')) {
uasort($eventListeners, function ($a, $b) {
$a = is_array($a) ? get_class($a[0]) : get_class($a);
$b = is_array($b) ? get_class($b[0]) : get_class($b);
if ($a === $b) {
return 0;
}

return ($a < $b) ? -1 : 1;
});
}
foreach ($eventListeners as $callable) {
$priority = $dispatcher->getListenerPriority($eventName, $callable);
if (is_array($callable)) {
$table->addRow([
$eventName,
sprintf('%s::%s()', get_class($callable[0]), $callable[1]),
$priority,
sprintf('%s::%s', get_class($callable[0]), $callable[1]),
]);
} else {
$table->addRow([$eventName, $priority, get_class($callable)]);
$table->addRow([$eventName, get_class($callable), $priority]);
}
}
}
Expand Down
18 changes: 8 additions & 10 deletions src/Nut/DebugRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ protected function configure()
$this
->setName('debug:routes')
->setDescription('System route debug dumper.')
->addOption('sort-bind', null, InputOption::VALUE_NONE, 'Sort in order of bind name (default).')
->addOption('sort-route', null, InputOption::VALUE_NONE, 'Sort in order of route name (default).')
->addOption('sort-pattern', null, InputOption::VALUE_NONE, 'Sort in order of URI patterns.')
->addOption('sort-methods', null, InputOption::VALUE_NONE, 'Sort in order of HTTP method grouping allowed.')
->addOption('sort-method', null, InputOption::VALUE_NONE, 'Sort in order of HTTP method grouping allowed.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where I was saying it seems to make more sense to have a value here instead of an option for each sorting. --sort name/path/method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but was thinking of consistency with the others

;
}

Expand All @@ -36,19 +36,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$table = new Table($output);
$table->setHeaders([
'Bind name',
'Path matching parameter',
'Method(s)',
['Route Name', 'Path', 'Method(s)']
]);
$routes = (array) $this->app['routes']->getIterator();

if ($input->getOption('sort-bind')) {
$routes = $this->sortBind($routes);
if ($input->getOption('sort-route')) {
$routes = $this->sortRotues($routes);
}
if ($input->getOption('sort-pattern')) {
$routes = $this->sortPattern($routes);
}
if ($input->getOption('sort-methods')) {
if ($input->getOption('sort-method')) {
$routes = $this->sortMethods($routes);
}

Expand All @@ -64,13 +62,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/**
* Sort routes by their binding name.
* Sort routes by their route binding name.
*
* @param Route[] $routes
*
* @return Route[]
*/
private function sortBind(array $routes)
private function sortRotues(array $routes)
{
ksort($routes);

Expand Down
68 changes: 68 additions & 0 deletions src/Nut/DebugServiceProviders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Bolt\Nut;

use Silex\Application;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Nut command to dump system provider registration order.
*
* @author Gawain Lynch <gawain.lynch@gmail.com>
*/
class DebugServiceProviders extends BaseCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('debug:service-providers')
->setAliases(['debug:providers'])
->setDescription('Dumps service providers and their order.')
->addOption('sort-class', null, InputOption::VALUE_NONE, 'Sort by provider class names.')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$table = new Table($output);
$rightAligned = new TableStyle();
$rightAligned->setPadType(STR_PAD_LEFT);

$table->setHeaders([
['Provider Class Name', 'Order']
]);
$table->setColumnStyle(1, $rightAligned);

$reflect = new \ReflectionProperty(Application::class, 'providers');
$reflect->setAccessible(true);
$registrations = $reflect->getValue($this->app);

if ($input->getOption('sort-class')) {
uasort($registrations, function ($a, $b) {
$a = get_class($a);
$b = get_class($b);
if ($a === $b) {
return 0;
}

return ($a < $b) ? -1 : 1;
});
}

foreach ($registrations as $index => $registration) {
$table->addRow([get_class($registration), $index + 1]);
}

$table->render();
}
}
58 changes: 0 additions & 58 deletions src/Nut/DebugServiceRegistration.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Provider/NutServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function ($app) {
new Nut\UserRoleAdd($app),
new Nut\UserRoleRemove($app),
new Nut\DebugEvents($app),
new Nut\DebugServiceRegistration($app),
new Nut\DebugServiceProviders($app),
new Nut\DebugRoutes($app),
];
}
Expand Down
59 changes: 59 additions & 0 deletions tests/phpunit/unit/Nut/DebugEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Bolt\Tests\Nut;

use Bolt\Nut\DebugEvents;
use Bolt\Tests\BoltUnitTest;
use Symfony\Component\Console\Tester\CommandTester;

/**
* Test for \Bolt\Nut\DebugEvents
*
* @author Gawain Lynch <gawain.lynch@gmail.com>
*/
class DebugEventsTest extends BoltUnitTest
{
use TableHelperTrait;

protected $regexExpectedA = '/(kernel\.request).+(Symfony.Component.HttpKernel.EventListener.RouterListener::onKernelRequest).+(32)/';
protected $regexExpectedB = '/(Bolt.Routing.Canonical::onRequest).+(31)/';

public function testRunNormal()
{
$tester = $this->getCommandTester();

$tester->execute([]);
$result = $tester->getDisplay();

$this->assertRegExp($this->regexExpectedA, $result);
$this->assertRegExp($this->regexExpectedB, $result);
}

public function testSortListener()
{
$tester = $this->getCommandTester();

$expectedOutput = $this->getNormalOuput();
$expectedA = $this->getMatchingLineNumber($this->regexExpectedA, $expectedOutput);
$expectedB = $this->getMatchingLineNumber($this->regexExpectedB, $expectedOutput);
$this->assertGreaterThan($expectedA, $expectedB);

$tester->execute(['--sort-listener' => true]);
$result = $tester->getDisplay();

$expectedA = $this->getMatchingLineNumber($this->regexExpectedA, $result);
$expectedB = $this->getMatchingLineNumber($this->regexExpectedB, $result);
$this->assertLessThan($expectedA, $expectedB);
}

/**
* @return CommandTester
*/
protected function getCommandTester()
{
$app = $this->getApp();
$command = new DebugEvents($app);

return new CommandTester($command);
}
}
Loading