Skip to content

Commit

Permalink
Merge branch 'feature/update-api-routes' of https://github.com/hskras…
Browse files Browse the repository at this point in the history
…ek/api into hskrasek-feature/update-api-routes

Conflicts:
	src/Console/ApiRoutesCommand.php
  • Loading branch information
jasonlewis committed Sep 10, 2014
2 parents 4389c62 + 4e703e7 commit f5c199d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 13 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
Expand Down
96 changes: 83 additions & 13 deletions src/Console/ApiRoutesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}

0 comments on commit f5c199d

Please sign in to comment.