-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2f9d148
Showing
5 changed files
with
341 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
<?php | ||
namespace Widnyana\LDRoutesList; | ||
|
||
|
||
use Dingo\Api\Routing\Router; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
class CommandProvider extends Command | ||
{ | ||
|
||
/** @var Router $router Dingo Router */ | ||
protected $router; | ||
|
||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'api:list-route'; | ||
|
||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Display all registered routes on Dingo.'; | ||
|
||
/** | ||
* The table headers for the command. | ||
* | ||
* @var array | ||
*/ | ||
protected $headers = [ | ||
'Host', 'Method', 'URI', 'Name', 'Action', 'Middleware', 'Protected', 'Version(s)', 'Scope(s)', 'Rate Limit' | ||
]; | ||
|
||
protected $routes; | ||
|
||
|
||
/** | ||
* Create a new console command instance. | ||
* | ||
* @param Router $router | ||
*/ | ||
public function __construct(Router $router) | ||
{ | ||
Command::__construct(); | ||
$this->router = $router; | ||
} | ||
|
||
|
||
public function fire() | ||
{ | ||
|
||
$this->routes = $this->router->getRoutes(); | ||
|
||
if (count($this->routes) == 0) { | ||
$this->error("Your application doesn't have any routes."); | ||
return; | ||
} | ||
|
||
$this->displayRoutes($this->getRoutes()); | ||
} | ||
|
||
|
||
protected function displayRoutes(array $routes) | ||
{ | ||
$this->table($this->headers, $routes); | ||
} | ||
|
||
/** | ||
* Compile the routes into a displayable format. | ||
* | ||
* @return array | ||
*/ | ||
protected function getRoutes() | ||
{ | ||
$routes = []; | ||
foreach ($this->routes as $collection) { | ||
foreach ($collection->getRoutes() as $route) { | ||
$uses = array_key_exists('uses', $route->getAction()) | ||
? $route->getAction()['uses'] | ||
: $route->getActionName(); | ||
|
||
$routes[] = $this->filterRoute([ | ||
'host' => $route->domain(), | ||
'method' => implode('|', $route->methods()), | ||
'uri' => $route->uri(), | ||
'name' => $route->getName(), | ||
'action' => $uses, | ||
'middleware' => $route->getMiddleware() ? implode(", ", $route->getMiddleware()) : "No", | ||
'protected' => $route->isProtected() ? 'Yes' : 'No', | ||
'versions' => implode(', ', $route->versions()), | ||
'scopes' => implode(', ', $route->scopes()), | ||
'rate' => $this->routeRateLimit($route), | ||
]); | ||
} | ||
} | ||
|
||
if ($sort = $this->option('sort')) { | ||
$routes = Arr::sort($routes, function ($value) use ($sort) { | ||
return $value[$sort]; | ||
}); | ||
} | ||
if ($this->option('short')) { | ||
$this->headers = ['Method', 'URI', 'Name', 'Version(s)']; | ||
$routes = array_map(function ($item) { | ||
return array_only($item, ['method', 'uri', 'name', 'versions']); | ||
}, $routes); | ||
} | ||
return array_filter(array_unique($routes, SORT_REGULAR)); | ||
} | ||
|
||
protected function routeRateLimit($route) | ||
{ | ||
list($limit, $expires) = [$route->getRateLimit(), $route->getRateLimitExpiration()]; | ||
if ($limit && $expires) { | ||
return sprintf('%s req/s', round($limit / ($expires * 60), 2)); | ||
} | ||
} | ||
|
||
|
||
protected function filterRoute(array $route) | ||
{ | ||
$filters = ['protected', 'unprotected', 'versions', 'scopes']; | ||
foreach ($filters as $filter) { | ||
if ($this->option($filter) && !$this->{'filterBy' . ucfirst($filter)}($route)) { | ||
return false; | ||
} | ||
} | ||
return $route; | ||
} | ||
|
||
|
||
/** | ||
* Get the console command options. | ||
* | ||
* @return array | ||
*/ | ||
protected function getOptions() | ||
{ | ||
$options = parent::getOptions(); | ||
foreach ($options as $key => $option) { | ||
if ($option[0] == 'sort') { | ||
unset($options[$key]); | ||
} | ||
} | ||
$options = array_merge( | ||
$options, | ||
[ | ||
['sort', null, InputOption::VALUE_OPTIONAL, 'The column (domain, method, uri, name, action) to sort by'], | ||
['versions', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Filter the routes by version'], | ||
['scopes', 'S', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Filter the routes by scopes'], | ||
['protected', null, InputOption::VALUE_NONE, 'Filter the protected routes'], | ||
['unprotected', null, InputOption::VALUE_NONE, 'Filter the unprotected routes'], | ||
['short', null, InputOption::VALUE_NONE, 'Get an abridged version of the routes'], | ||
] | ||
); | ||
|
||
return $options; | ||
} | ||
|
||
protected function filterByPath(array $route) | ||
{ | ||
return Str::contains($route['uri'], $this->option('path')); | ||
} | ||
|
||
|
||
protected function filterByProtected(array $route) | ||
{ | ||
return $this->option('protected') && $route['protected'] == 'Yes'; | ||
} | ||
/** | ||
* Filter the route by whether or not it is unprotected. | ||
* | ||
* @param array $route | ||
* | ||
* @return bool | ||
*/ | ||
protected function filterByUnprotected(array $route) | ||
{ | ||
return $this->option('unprotected') && $route['protected'] == 'No'; | ||
} | ||
/** | ||
* Filter the route by its versions. | ||
* | ||
* @param array $route | ||
* | ||
* @return bool | ||
*/ | ||
protected function filterByVersions(array $route) | ||
{ | ||
foreach ($this->option('versions') as $version) { | ||
if (Str::contains($route['versions'], $version)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Filter the route by its name. | ||
* | ||
* @param array $route | ||
* | ||
* @return bool | ||
*/ | ||
protected function filterByName(array $route) | ||
{ | ||
return Str::contains($route['name'], $this->option('name')); | ||
} | ||
/** | ||
* Filter the route by its scopes. | ||
* | ||
* @param array $route | ||
* | ||
* @return bool | ||
*/ | ||
protected function filterByScopes(array $route) | ||
{ | ||
foreach ($this->option('scopes') as $scope) { | ||
if (Str::contains($route['scopes'], $scope)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
namespace Widnyana\LDRoutesList; | ||
|
||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class CommandServiceProvider extends ServiceProvider | ||
{ | ||
|
||
/** | ||
* Register any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->commands(CommandProvider::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Copyright (c) 2016, Widnyana | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of Dingo API nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
Lumen Dingo Route List | ||
====================== | ||
This code bring Dingo's `api:route` command to Lumen's `artisan`. | ||
|
||
## Howto | ||
Acquire the source code: | ||
``` | ||
composer require widnyana/lumen-dingo-routes-list | ||
``` | ||
|
||
and Register the service provider: | ||
``` | ||
// file: bootstrap/app.php | ||
$app->register(Widnyana\LDRoutesList\CommandServiceProvider::class); | ||
``` | ||
|
||
call it like this: | ||
``` | ||
$ ./artisan api:list-route | ||
``` | ||
for avoiding any command conflict with Dingo nor Lumen, the command is located at `api:list-route `. | ||
|
||
![lumen-dingo-routes-list in action](http://i.imgur.com/ExAtLXW.png) | ||
|
||
### Help wanted | ||
|
||
There is an "*undefined behaviour*" that I can't exactly tell you why it will always say `GET|HEAD` for the `Method` columns no matter what HTTP Method you define for each route, as far as I try, there is no HTTP Method passed down to `Dingo\Api\Routing\Route`, and the function call to `$route->getMethods()` will always return `GET|HEAD`. If you know how, please send me a pull request :) | ||
|
||
#### Disclaimer | ||
This code is blatantly stolen from [this](https://github.com/dingo/api/blob/master/src/Console/Command/Routes.php) file. | ||
|
||
#### License | ||
|
||
see [license](LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "widnyana/lumen-dingo-routes-list", | ||
"license": "BSD-3-Clause", | ||
"description": "Lumen Routes List Dingo API Endpoint", | ||
"keywords": [ | ||
"lumen", | ||
"dingo", | ||
"route list" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "Widnyana", | ||
"email": "me@widnyana.web.id", | ||
"homepage": "http://widnyana.web.id" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.5.9", | ||
"dingo/api": "1.0.x@dev", | ||
"illuminate/support": "~5.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Widnyana\\LDRoutesList\\": "" | ||
} | ||
}, | ||
"minimum-stability": "stable", | ||
"prefer-stable": true | ||
} |