From f3f9522fccebd2c1c0c53f17a3c0d7a1a2526a2e Mon Sep 17 00:00:00 2001 From: Hunter Skrasek Date: Wed, 9 Jul 2014 21:52:50 -0500 Subject: [PATCH 1/5] Allow API route filtering by Scope(s) and / or version --- src/Console/ApiRoutesCommand.php | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/Console/ApiRoutesCommand.php b/src/Console/ApiRoutesCommand.php index 7df79c67..e86fb07b 100644 --- a/src/Console/ApiRoutesCommand.php +++ b/src/Console/ApiRoutesCommand.php @@ -5,6 +5,8 @@ use Dingo\Api\Routing\Router; use Illuminate\Routing\Route; use Illuminate\Foundation\Console\RoutesCommand; +use Illuminate\Support\Facades\Input; +use Symfony\Component\Console\Input\InputOption; class ApiRoutesCommand extends RoutesCommand { @@ -83,6 +85,40 @@ protected function getRouteInformation(Route $route) )); } + /** + * Filter the route by URI, Version and / or name. + * + * @param array $route + * @return array|null + */ + protected function filterRoute(array $route) + { + if (($this->option('name') && !str_contains($route['name'], $this->option('name'))) || + $this->option('path') && !str_contains($route['uri'], $this->option('path')) || + ($this->option('vers') && !str_contains($route['version'], $this->option('vers'))) || + ($this->option('scopes') && !$this->scopeFilter($route['scopes'])) + ) + { + return null; + } + else + { + return $route; + } + } + + protected function scopeFilter($scopes) + { + foreach($this->option('scopes') as $scope) { + if (str_contains($scopes, $scope)) + { + return true; + } + } + + return false; + } + /** * Get the scopes of a route. * @@ -95,4 +131,18 @@ protected function getScopes($route) return is_array($scopes) ? implode(', ', $scopes) : $scopes; } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return array_merge(parent::getOptions(), array( + array('vers', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by version.'), + + array('scopes', 'S', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Filter the routes by scope(s)', null), + )); + } } From 570368180c834e8ec052585c16556ff2bc6baf59 Mon Sep 17 00:00:00 2001 From: Hunter Skrasek Date: Thu, 10 Jul 2014 20:08:01 -0500 Subject: [PATCH 2/5] PSR-2 Code format Signed-off-by: Hunter Skrasek --- src/Console/ApiRoutesCommand.php | 131 ++++++++++++++++--------------- 1 file changed, 69 insertions(+), 62 deletions(-) diff --git a/src/Console/ApiRoutesCommand.php b/src/Console/ApiRoutesCommand.php index e86fb07b..7471818e 100644 --- a/src/Console/ApiRoutesCommand.php +++ b/src/Console/ApiRoutesCommand.php @@ -3,9 +3,8 @@ namespace Dingo\Api\Console; use Dingo\Api\Routing\Router; -use Illuminate\Routing\Route; use Illuminate\Foundation\Console\RoutesCommand; -use Illuminate\Support\Facades\Input; +use Illuminate\Routing\Route; use Symfony\Component\Console\Input\InputOption; class ApiRoutesCommand extends RoutesCommand @@ -34,7 +33,8 @@ class ApiRoutesCommand extends RoutesCommand /** * Create a new route command instance. * - * @param \Illuminate\Routing\Router $router + * @param \Illuminate\Routing\Router $router + * * @return void */ public function __construct(Router $router) @@ -55,7 +55,7 @@ protected function getRoutes() foreach ($this->routes as $collection) { foreach ($collection->getRoutes() as $route) { - $results[] = $this->getRouteInformation($route); + $results[ ] = $this->getRouteInformation($route); } } @@ -67,62 +67,61 @@ protected function getRoutes() /** * Get the route information for a given route. * - * @param string $name - * @param \Illuminate\Routing\Route $route + * @param \Illuminate\Routing\Route $route * * @return array */ protected function getRouteInformation(Route $route) { - return $this->filterRoute(array( - 'host' => $route->domain(), - 'uri' => implode('|', $route->methods()).' '.$route->uri(), - 'name' => $route->getName(), - 'action' => $route->getActionName(), - 'version' => implode(', ', array_get($route->getAction(), 'version')), - 'protected' => array_get($route->getAction(), 'protected') ? 'Yes' : 'No', - 'scopes' => $this->getScopes($route) - )); + return $this->filterRoute( + array( + 'host' => $route->domain(), + 'uri' => implode('|', $route->methods()) . ' ' . $route->uri(), + 'name' => $route->getName(), + 'action' => $route->getActionName(), + 'version' => implode(', ', array_get($route->getAction(), 'version')), + 'protected' => array_get($route->getAction(), 'protected') ? 'Yes' : 'No', + 'scopes' => $this->getScopes($route) + ) + ); } - /** - * Filter the route by URI, Version and / or name. - * - * @param array $route - * @return array|null - */ - protected function filterRoute(array $route) - { - if (($this->option('name') && !str_contains($route['name'], $this->option('name'))) || - $this->option('path') && !str_contains($route['uri'], $this->option('path')) || - ($this->option('vers') && !str_contains($route['version'], $this->option('vers'))) || - ($this->option('scopes') && !$this->scopeFilter($route['scopes'])) - ) - { - return null; - } - else - { - return $route; - } - } - - protected function scopeFilter($scopes) - { - foreach($this->option('scopes') as $scope) { - if (str_contains($scopes, $scope)) - { - return true; - } - } - - return false; - } + /** + * Filter the route by URI, Version and / or name. + * + * @param array $route + * + * @return array|null + */ + protected function filterRoute(array $route) + { + if (( $this->option('name') && ! str_contains($route[ 'name' ], $this->option('name')) ) || + $this->option('path') && ! str_contains($route[ 'uri' ], $this->option('path')) || + ( $this->option('vers') && ! str_contains($route[ 'version' ], $this->option('vers')) ) || + ( $this->option('scopes') && ! $this->scopeFilter($route[ 'scopes' ]) ) + ) { + return null; + } else { + return $route; + } + } + + protected function scopeFilter($scopes) + { + foreach ($this->option('scopes') as $scope) { + if (str_contains($scopes, $scope)) { + return true; + } + } + + return false; + } /** * Get the scopes of a route. * - * @param \Illuminate\Routing\Route $route + * @param \Illuminate\Routing\Route $route + * * @return string */ protected function getScopes($route) @@ -132,17 +131,25 @@ protected function getScopes($route) return is_array($scopes) ? implode(', ', $scopes) : $scopes; } - /** - * Get the console command options. - * - * @return array - */ - protected function getOptions() - { - return array_merge(parent::getOptions(), array( - array('vers', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by version.'), - - array('scopes', 'S', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Filter the routes by scope(s)', null), - )); - } + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return array_merge( + parent::getOptions(), + array( + array('vers', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by version.'), + array( + 'scopes', + 'S', + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, + 'Filter the routes by scope(s)', + null + ), + ) + ); + } } From 2ab2e409ad2b4fbf6587c13918a1b54c9d7af4a7 Mon Sep 17 00:00:00 2001 From: Hunter Skrasek Date: Thu, 10 Jul 2014 20:19:34 -0500 Subject: [PATCH 3/5] Update filterRoute docblock, refractor various filters into individual methods Signed-off-by: Hunter Skrasek --- src/Console/ApiRoutesCommand.php | 47 +++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Console/ApiRoutesCommand.php b/src/Console/ApiRoutesCommand.php index 7471818e..03bdd286 100644 --- a/src/Console/ApiRoutesCommand.php +++ b/src/Console/ApiRoutesCommand.php @@ -87,7 +87,7 @@ protected function getRouteInformation(Route $route) } /** - * Filter the route by URI, Version and / or name. + * Filter the route by URI, Version, Scopes and / or name. * * @param array $route * @@ -95,10 +95,10 @@ protected function getRouteInformation(Route $route) */ protected function filterRoute(array $route) { - if (( $this->option('name') && ! str_contains($route[ 'name' ], $this->option('name')) ) || - $this->option('path') && ! str_contains($route[ 'uri' ], $this->option('path')) || - ( $this->option('vers') && ! str_contains($route[ 'version' ], $this->option('vers')) ) || - ( $this->option('scopes') && ! $this->scopeFilter($route[ 'scopes' ]) ) + if (( $this->option('name') && ! $this->nameFilter($route) ) || + ( $this->option('path') && ! $this->pathFilter($route) ) || + ( $this->option('vers') && ! $this->versionFilter($route) ) || + ( $this->option('scopes') && ! $this->scopeFilter($route) ) ) { return null; } else { @@ -106,17 +106,6 @@ protected function filterRoute(array $route) } } - protected function scopeFilter($scopes) - { - foreach ($this->option('scopes') as $scope) { - if (str_contains($scopes, $scope)) { - return true; - } - } - - return false; - } - /** * Get the scopes of a route. * @@ -152,4 +141,30 @@ protected function getOptions() ) ); } + + protected function pathFilter(array $route) + { + return str_contains($route[ 'uri' ], $this->option('path')); + } + + protected function versionFilter(array $route) + { + return str_contains($route[ 'version' ], $this->option('vers')); + } + + protected function nameFilter(array $route) + { + return str_contains($route[ 'name' ], $this->option('name')); + } + + protected function scopeFilter(array $route) + { + foreach ($this->option('scopes') as $scope) { + if (str_contains($route[ 'scopes' ], $scope)) { + return true; + } + } + + return false; + } } From d51609cc943e23b33613c5ab5098f4870c6173cd Mon Sep 17 00:00:00 2001 From: Hunter Skrasek Date: Thu, 10 Jul 2014 20:36:34 -0500 Subject: [PATCH 4/5] Goodbye `array` syntax. Good riddance Signed-off-by: Hunter Skrasek --- src/Console/ApiRoutesCommand.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Console/ApiRoutesCommand.php b/src/Console/ApiRoutesCommand.php index 03bdd286..7a59a8c5 100644 --- a/src/Console/ApiRoutesCommand.php +++ b/src/Console/ApiRoutesCommand.php @@ -129,16 +129,16 @@ protected function getOptions() { return array_merge( parent::getOptions(), - array( - array('vers', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by version.'), - array( + [ + ['vers', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by version.'], + [ 'scopes', 'S', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Filter the routes by scope(s)', null - ), - ) + ], + ] ); } From 4e703e7db1f8f34e168760513c1c505fb33b8d65 Mon Sep 17 00:00:00 2001 From: Hunter Skrasek Date: Fri, 11 Jul 2014 21:52:14 -0500 Subject: [PATCH 5/5] Add illuminate/console to require-dev, so that things will work again. --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 91467b58..d3563518 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "illuminate/auth": "~4.1", "illuminate/database": "~4.1", "illuminate/pagination": "~4.1", + "illuminate/console": "~4.1", "phpunit/phpunit": "~4.0", "mockery/mockery": "~0.9", "squizlabs/php_codesniffer": "1.*"