Skip to content

Commit

Permalink
Add console commands for the Routing component
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorbrandao committed Jul 22, 2016
1 parent 2820f41 commit 0765029
Show file tree
Hide file tree
Showing 24 changed files with 496 additions and 51 deletions.
6 changes: 1 addition & 5 deletions README.md
Expand Up @@ -8,9 +8,6 @@ Noiselabs ZfDebugModule

WebUI and Console commands for debugging ZF2 apps.

DISCLAIMER: ZfDebugModule is still at an early development stage.
No Console commands were added so far.

Installation
------------

Expand All @@ -21,12 +18,11 @@ composer require noiselabs/zf-debug-utils
```


Then enable this module by adding it and `AssetManager` (a dependency) to `application.config.php`.
Then enable this module by adding it to `application.config.php`.

```php
<?php
'modules' => [
'AssetManager',
'Noiselabs\ZfDebugModule',
],
```
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -13,9 +13,9 @@
"php": "^5.5 || ^7.0",
"zendframework/zend-console": "~2.4",
"zendframework/zend-modulemanager": "~2.4",
"zendframework/zend-mvc": "~2.4",
"zendframework/zend-mvc": "~2.4",
"zendframework/zend-servicemanager": ">=2.4 <2.8",
"zendframework/zend-view": "~2.4",
"zendframework/zend-view": "~2.4",
"rwoverdijk/assetmanager": "^1.6"
},
"require-dev": {
Expand Down
Binary file added docs/_static/images/console/route-export.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_static/images/console/route-list.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_static/images/console/route-match.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_static/images/console/summary.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
55 changes: 39 additions & 16 deletions docs/getting-started.md
Expand Up @@ -11,55 +11,78 @@ composer require noiselabs/zf-debug-utils
```


Then enable this module by adding it and `AssetManager` (a dependency) to `application.config.php`.
Then enable this module by adding it to `application.config.php`.

```php
<?php
'modules' => [
'AssetManager',
'Noiselabs\ZfDebugModule',
],
```

Configuration
-------------
## Configuration

Nothing so far. It just works. Soon we will add the possibility of defining a custom root URL for ZfDebugModule URLs.

What's inside?
--------------
## What's inside?

ZfDebugModule provides a _WebUI and Console commands for debugging ZF2 apps_. However, as stated in the DISCLAIMER:
_"ZfDebugModule is still at an early development stage. No Console commands were added so far"_. So for now we are
going to focus on WebUI features only.
ZfDebugModule provides a _WebUI and Console commands for debugging ZF2 apps_.

### WebUI

Web URLs are by default mounted at `/_debug`. After installing this module simply navigate to `/_debug` and you should
see the home screen. If you are using the ZendSkeletonApplication the home URL should look similar to this:
`http://127.0.0.1:8080/_debug`.

### Console

If you prefer to use the Console type `php public/index.php` at the terminal to see a summary of the available commands.

![Console - Summary](_static/images/console/summary.png "Console - Summary")

### Routing
## Routing

Routing is the first debug component made available. You have the possibility to list all routes or match a URL to a
route.

![Home - Routing](_static/images/index-routing.png "Home - Routing")
![Home - Routing](_static/images/webui/index-routing.png "Home - Routing")

### List all Routes

#### List all Routes
`WebUI:`

Navigate to `/_debug/routes/list` or in the Home screen click in `List all Routes`. These are all the routes available
in current application. Each column is sortable and you can filter results by using the search input.

![List all routes](_static/images/routing-list-all-routes.png "List all routes")
![List all routes](_static/images/webui/routing-list-all-routes.png "List all routes")

`Console:`

$ php zfdebug routes list

![Console - List routes](_static/images/console/route-list.png "Console - List routes")


#### Match a Route
### Match a Route

Navigate to `/_debug/routes/match` or in the Home screen click in `Match a Route`. Provide an HTTP method such as "GET"
or "POST" plus a base URL (omit the scheme) and hit `Find Route`. If the URL matches a route in your application you
should see something similar to the following picture. You can see details such as the route URL, controller and action.

![Match a Route](_static/images/routing-match-route.png "Match a Route")
![Match a Route](_static/images/webui/routing-match-route.png "Match a Route")


`Console:`

$ php zfdebug routes match GET /_debug/routes/match

![Console - Route match](_static/images/console/route-match.png "Console - Route match")

### Export all routes in CSV format

It is possible to have the output of the [List all Routes](#list-all-routes) feature saved into a CSV file. The generated file can then be loaded by spreadsheet applications and used in reports.

Note that the export feature is at the moment only available via the Console.

$ php zfdebug routes export

![Console - Route export](_static/images/console/route-export.png "Console - Export all routes")
94 changes: 91 additions & 3 deletions src/Controller/Console/RoutesController.php
Expand Up @@ -8,13 +8,101 @@

namespace Noiselabs\ZfDebugModule\Controller\Console;

use RuntimeException;
use Noiselabs\ZfDebugModule\Util\Routing\CsvExporter;
use Noiselabs\ZfDebugModule\Util\Routing\RouteCollection;
use Noiselabs\ZfDebugModule\Util\Routing\RouteMatcher;
use Zend\Console\Adapter\AdapterInterface as Console;
use Zend\Console\ColorInterface;
use Zend\Console\Request;
use Zend\Mvc\Controller\AbstractActionController;

class RoutesController extends AbstractActionController
{
public function listAll()
/**
* @var Console
*/
private $console;

/**
* @var RouteCollection
*/
private $routeCollection;

/**
* @var RouteMatcher
*/
private $routeMatcher;

/**
* RoutesController constructor.
*
* @param RouteCollection $routeCollection
* @param RouteMatcher $routeMatcher
* @param Console $console
*/
public function __construct(RouteCollection $routeCollection, RouteMatcher $routeMatcher, Console $console)
{
$this->routeCollection = $routeCollection;
$this->routeMatcher = $routeMatcher;
$this->console = $console;
}

public function exportAction()
{
$csvExporter = new CsvExporter($this->routeCollection, sys_get_temp_dir());
$this->console->write('Exporting all routes...');
$fileName = $csvExporter->export();
$this->console->writeLine(' done.');

$this->console->write('CSV file now available at ');
$this->console->writeLine($fileName, ColorInterface::LIGHT_BLUE);
}

public function listAction()
{
$this->console->write('{ ', ColorInterface::GRAY);
$this->console->write('ROUTE', ColorInterface::GREEN);
$this->console->write(', ', ColorInterface::GRAY);
$this->console->write('URL ', ColorInterface::BLUE);
$this->console->write(', ', ColorInterface::GRAY);
$this->console->write('CONTROLLER::ACTION', ColorInterface::RED);
$this->console->writeLine(' }', ColorInterface::GRAY);
$this->console->writeLine('');

foreach ($this->routeCollection->getRoutes() as $route) {
$this->console->write('{ ', ColorInterface::GRAY);
$this->console->write($route->getName(), ColorInterface::GREEN);
$this->console->write(', ', ColorInterface::GRAY);
$this->console->write($route->getUrl(), ColorInterface::BLUE);
$this->console->write(', ', ColorInterface::GRAY);
$this->console->write(sprintf('%s::%s', $route->getController(), $route->getAction()),
ColorInterface::RED);
$this->console->writeLine(' }', ColorInterface::GRAY);
}
}

public function matchAction()
{
throw new RuntimeException('not there yet');
/** @var Request $request */
$request = $this->getRequest();
$method = strtoupper($request->getParam('method'));
$url = $request->getParam('url');

$match = $this->routeMatcher->match($method, $url);
if (null !== $match) {
$route = $this->routeCollection->getRoute($match);

$this->console->writeLine(sprintf('A match was found for %s "%s"', $method, $url), ColorInterface::GREEN);
$this->console->write(' Name: ');
$this->console->writeLine($route->getName(), ColorInterface::LIGHT_WHITE);
$this->console->write(' URL: ');
$this->console->writeLine($route->getUrl(), ColorInterface::LIGHT_WHITE);
$this->console->write(' Controller: ');
$this->console->writeLine($route->getController(), ColorInterface::LIGHT_WHITE);
$this->console->write(' Action: ');
$this->console->writeLine($route->getAction(), ColorInterface::LIGHT_WHITE);
} else {
$this->console->writeLine(sprintf('No match found for %s "%s"', $method, $url), ColorInterface::RED);
}
}
}
20 changes: 18 additions & 2 deletions src/Factory/Controller/Console/RoutesControllerFactory.php
Expand Up @@ -9,6 +9,11 @@
namespace Noiselabs\ZfDebugModule\Factory\Controller\Console;

use Noiselabs\ZfDebugModule\Controller\Console\RoutesController;
use Noiselabs\ZfDebugModule\Factory\Util\Routing\RouteCollectionFactory;
use Noiselabs\ZfDebugModule\Factory\Util\Routing\RouteMatcherFactory;
use Noiselabs\ZfDebugModule\Util\Routing\RouteCollection;
use Noiselabs\ZfDebugModule\Util\Routing\RouteMatcher;
use Zend\Mvc\Controller\ControllerManager;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

Expand All @@ -20,10 +25,21 @@ class RoutesControllerFactory implements FactoryInterface
const SERVICE_NAME = RoutesController::class;

/**
* {@inheritdoc}
* @param ServiceLocatorInterface|ControllerManager $serviceLocator
*
* @return RoutesController
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return new RoutesController();
/** @var ServiceLocatorInterface $mainServiceManager */
$mainServiceManager = $serviceLocator->getServiceLocator();

/** @var RouteCollection $routeCollection */
$routeCollection = $mainServiceManager->get(RouteCollectionFactory::SERVICE_NAME);
/** @var RouteMatcher $routeMatcher */
$routeMatcher = $mainServiceManager->get(RouteMatcherFactory::SERVICE_NAME);
$console = $mainServiceManager->get('Console');

return new RoutesController($routeCollection, $routeMatcher, $console);
}
}
2 changes: 1 addition & 1 deletion src/Factory/Util/Routing/RouteMatcherFactory.php
Expand Up @@ -25,7 +25,7 @@ class RouteMatcherFactory implements FactoryInterface
public function createService(ServiceLocatorInterface $serviceLocator)
{
/** @var RouteStackInterface $router */
$router = $serviceLocator->get('router');
$router = $serviceLocator->get('HttpRouter');

return new RouteMatcher($router);
}
Expand Down
48 changes: 43 additions & 5 deletions src/Module.php
Expand Up @@ -8,10 +8,14 @@

namespace Noiselabs\ZfDebugModule;

use Zend\Console\Adapter\AdapterInterface as Console;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
use Zend\ModuleManager\Feature\ConsoleBannerProviderInterface;
use Zend\ModuleManager\Feature\ConsoleUsageProviderInterface;
use Zend\ModuleManager\Feature\InitProviderInterface;
use Zend\ModuleManager\ModuleManagerInterface;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteInterface;
use Zend\Mvc\Router\RouteMatch;
Expand All @@ -23,16 +27,17 @@
*
* @author Vítor Brandão <vitor@noiselabs.org>
*/
class Module implements BootstrapListenerInterface, ConfigProviderInterface, DependencyIndicatorInterface
class Module implements BootstrapListenerInterface, ConfigProviderInterface, ConsoleBannerProviderInterface,
ConsoleUsageProviderInterface, InitProviderInterface
{
const DEFAULT_LAYOUT = 'noiselabs/zf-debug-utils/layout';

/**
* {@inheritdoc}
*/
public function getModuleDependencies()
public function init(ModuleManagerInterface $manager)
{
return ['AssetManager'];
$manager->loadModule('AssetManager');
}

/**
Expand All @@ -48,7 +53,7 @@ public function getConfig()
*
* @param EventInterface|MvcEvent $e
*
* @return array|null
* @return array|void
*/
public function onBootstrap(EventInterface $e)
{
Expand All @@ -60,6 +65,39 @@ public function onBootstrap(EventInterface $e)
$e->getViewModel()->setVariables(['__currentRouteName' => $currentRouteName]);
}

/**
* {@inheritdoc}
*
* @param Console $console
*
* @return string
*/
public function getConsoleBanner(Console $console)
{
return sprintf('%s v%s', Package::NAME, Package::VERSION);
}

/**
* @param Console $console
*
* @return array
*/
public function getConsoleUsage(Console $console)
{
return [
'[ Routing ]',
'',
'zfdebug routes export' => 'Exports all routes in CSV format',
'zfdebug routes list' => 'Lists all routes',
'zfdebug routes match [METHOD] [URL]' => 'Matches a URL to a Route',
['Examples:'],
['$ zfdebug routes export', ''],
['$ zfdebug routes list', ''],
['$ zfdebug routes match GET /users/123', ''],
['$ zfdebug routes match POST /login', ''],
];
}

/**
* @param ServiceLocatorInterface $serviceLocator
*
Expand Down
2 changes: 1 addition & 1 deletion src/Package.php
Expand Up @@ -17,5 +17,5 @@ class Package
const NAME = 'zf-debug-utils';
const FQPN = 'noiselabs/zf-debug-utils'; // Fully Qualified Package Name
const DESCRIPTION = 'WebUI and Console commands for debugging ZF2 apps';
const VERSION = '0.2.0';
const VERSION = '0.3.0-DEV';
}

0 comments on commit 0765029

Please sign in to comment.