ansible-api/app/Providers/ApiKeyServiceProvider.php

68 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2019-10-29 15:22:32 -04:00
<?php
namespace App\Providers;
use App\Console\Commands\ActivateApiKey;
use App\Console\Commands\DeactivateApiKey;
use App\Console\Commands\DeleteApiKey;
use App\Console\Commands\GenerateApiKey;
use App\Console\Commands\ListApiKeys;
use App\Http\Middleware\AuthorizeApiKey;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
class ApiKeyServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @param Router $router
* @return void
*/
public function boot(Router $router)
{
$this->registerMiddleware($router);
$this->registerMigrations(__DIR__ . '/../../database/migrations');
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->commands([
ActivateApiKey::class,
DeactivateApiKey::class,
DeleteApiKey::class,
GenerateApiKey::class,
ListApiKeys::class,
]);
}
/**
* Register middleware
*
* Support added for different Laravel versions
*
* @param Router $router
*/
protected function registerMiddleware(Router $router)
{
$versionComparison = version_compare(app()->version(), '5.4.0');
if ($versionComparison >= 0) {
$router->aliasMiddleware('auth.apikey', AuthorizeApiKey::class);
} else {
$router->middleware('auth.apikey', AuthorizeApiKey::class);
}
}
/**
* Register migrations
*/
protected function registerMigrations($migrationsDirectory)
{
$this->publishes([
$migrationsDirectory => database_path('migrations')
], 'migrations');
}
}