Skip to content

Implement new api:routes command. #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions src/ApiServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
<?php namespace Dingo\Api;

use RuntimeException;
use Dingo\Api\Auth\ProviderManager;
use Dingo\Api\Auth\Shield;
use Dingo\Api\Commands\ApiRoutesCommand;
use Dingo\Api\Http\Response;
use Dingo\Api\Routing\Router;
use Dingo\Api\Auth\ProviderManager;
use League\Fractal\Manager as Fractal;
use Illuminate\Support\ServiceProvider;
use League\Fractal\Manager as Fractal;
use RuntimeException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class ApiServiceProvider extends ServiceProvider {
class ApiServiceProvider extends ServiceProvider
{

/**
* Boot the service provider.
*
*
* @return void
*/
public function boot()
{
$this->package('dingo/api', 'api', __DIR__);

$this->app['Dingo\Api\Dispatcher'] = function($app) { return $app['dingo.api.dispatcher']; };
$this->app['Dingo\Api\Auth\Shield'] = function($app) { return $app['dingo.api.auth']; };
$this->app['Dingo\Api\Dispatcher'] = function ($app)
{
return $app['dingo.api.dispatcher'];
};
$this->app['Dingo\Api\Auth\Shield'] = function ($app)
{
return $app['dingo.api.auth'];
};

$formats = $this->prepareResponseFormats();

Expand All @@ -31,7 +39,7 @@ public function boot()

/**
* Prepare the response formats.
*
*
* @return array
*/
protected function prepareResponseFormats()
Expand All @@ -58,7 +66,7 @@ protected function prepareResponseFormats()

/**
* Register bindings for the service provider.
*
*
* @return void
*/
public function register()
Expand All @@ -75,7 +83,9 @@ public function register()

$this->registerMiddlewares();

$this->app->booting(function($app)
$this->registerCommands();

$this->app->booting(function ($app)
{
$router = $app['router'];

Expand All @@ -90,12 +100,12 @@ public function register()

/**
* Register and replace the bound router.
*
*
* @return void
*/
protected function registerRouter()
{
$this->app['router'] = $this->app->share(function($app)
$this->app['router'] = $this->app->share(function ($app)
{
$router = new Router($app['events'], $app);

Expand All @@ -110,51 +120,51 @@ protected function registerRouter()

/**
* Register the API dispatcher.
*
*
* @return void
*/
protected function registerDispatcher()
{
$this->app['dingo.api.dispatcher'] = $this->app->share(function($app)
$this->app['dingo.api.dispatcher'] = $this->app->share(function ($app)
{
return new Dispatcher($app['request'], $app['url'], $app['router'], $app['dingo.api.auth']);
});
}

/**
* Register the API transformer.
*
*
* @return void
*/
protected function registerTransformer()
{
$this->app['dingo.api.transformer'] = $this->app->share(function($app)
$this->app['dingo.api.transformer'] = $this->app->share(function ($app)
{
return new Transformer(new Fractal, $app, $app['config']['api::embeds.key'], $app['config']['api::embeds.separator']);
});
}

/**
* Register the exception handler.
*
*
* @return void
*/
protected function registerExceptionHandler()
{
$this->app['dingo.api.exception'] = $this->app->share(function($app)
$this->app['dingo.api.exception'] = $this->app->share(function ($app)
{
return new ExceptionHandler;
});
}

/**
* Register the API authentication.
*
*
* @return void
*/
protected function registerAuthentication()
{
$this->app['dingo.api.auth'] = $this->app->share(function($app)
$this->app['dingo.api.auth'] = $this->app->share(function ($app)
{
$providers = [];

Expand All @@ -174,7 +184,7 @@ protected function registerAuthentication()

/**
* Register the middlewares.
*
*
* @return void
*/
protected function registerMiddlewares()
Expand All @@ -184,4 +194,14 @@ protected function registerMiddlewares()
$this->app->middleware('Dingo\Api\Http\Middleware\RateLimit', [$this->app]);
}

protected function registerCommands()
{
$this->app['dingo.api.command.routes'] = $this->app->share(function ($app)
{
return new ApiRoutesCommand($app['router']);
});

$this->commands('dingo.api.command.routes');
}

}
116 changes: 116 additions & 0 deletions src/Commands/ApiRoutesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php namespace Dingo\Api\Commands;

use Dingo\Api\Routing\Router;
use Illuminate\Foundation\Console\RoutesCommand;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Symfony\Component\Console\Input\InputOption;

class ApiRoutesCommand extends RoutesCommand
{

/**
* The console command name.
*
* @var string
*/
protected $name = 'api:routes';

/**
* The console command description.
*
* @var string
*/
protected $description = 'List all registered API routes';

/**
* The table headers for the command.
*
* @var array
*/
protected $headers = array(
'Domain', 'URI', 'Name', 'Action', 'Version(s)', 'Protected', 'Scope(s)'
);

/**
* Create a new route command instance.
*
* @param \Illuminate\Routing\Router $router
*
* @return void
*/
public function __construct(Router $router)
{
parent::__construct($router);

$this->routes = $router->getApiRoutes();
}

/**
* Compile the routes into a displayable format.
*
* @return array
*/
protected function getRoutes()
{
$results = array();
foreach ($this->routes as $apiCollection)
{
foreach ($apiCollection->getRoutes() as $route)
{
$results[] = $this->getRouteInformation($route);
}
}

$results = array_unique($results, SORT_REGULAR);

return array_filter($results);
}

/**
* Get the route information for a given route.
*
* @param string $name
* @param \Illuminate\Routing\Route $route
*
* @return array
*/
protected function getRouteInformation(Route $route)
{
$uri = implode('|', $route->methods()) . ' ' . $route->uri();
$versions = implode(',', array_get($route->getAction(), 'version'));
$protected = array_get($route->getAction(), 'protected') ? 'Yes' : 'No';

return $this->filterRoute(array(
'host' => $route->domain(),
'uri' => $uri,
'name' => $route->getName(),
'action' => $route->getActionName(),
'version' => $versions,
'protected' => $protected,
'scopes' => $this->getScopes($route)
));
}

/**
* Get scopes
*
* @param \Illuminate\Routing\Route $route
* @return string
*/
protected function getScopes($route)
{
$scopes = array_get($route->getAction(), 'scopes');

if (is_string($scopes))
{
return $scopes;
}

if (is_array($scopes))
{
return implode(',', $scopes);
}
}

}
4 changes: 4 additions & 0 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,4 +666,8 @@ public function setCurrentRoute(Route $route)
$this->current = $route;
}

public function getApiRoutes()
{
return $this->api;
}
}