diff --git a/composer.json b/composer.json index ecc5a957..16e361cd 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.*" diff --git a/src/Console/ApiRoutesCommand.php b/src/Console/ApiRoutesCommand.php index 428b4d85..9686625f 100644 --- a/src/Console/ApiRoutesCommand.php +++ b/src/Console/ApiRoutesCommand.php @@ -5,6 +5,7 @@ use Dingo\Api\Routing\Router; use Illuminate\Routing\Route; use Illuminate\Foundation\Console\RoutesCommand; +use Symfony\Component\Console\Input\InputOption; class ApiRoutesCommand extends RoutesCommand { @@ -65,28 +66,49 @@ 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([ - '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, Scopes and / or name. + * + * @param array $route + * + * @return array|null + */ + protected function filterRoute(array $route) + { + 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 { + return $route; + } } /** * Get the scopes of a route. * - * @param \Illuminate\Routing\Route $route + * @param \Illuminate\Routing\Route $route + * * @return string */ protected function getScopes($route) @@ -95,4 +117,52 @@ 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(), + [ + ['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 + ], + ] + ); + } + + 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; + } }