From f06b641b5565261e810ea17cec2e32e0dd379ff4 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Mon, 6 Jan 2020 21:03:00 +0200 Subject: [PATCH 1/2] Custom middleware and prefix per resource --- src/Commands/RepositoryCommand.php | 16 +++++ src/Commands/stubs/policy.stub | 93 +++++++++++++++++++++++++++ src/LaravelRestifyServiceProvider.php | 12 ++-- src/Restify.php | 20 ++++-- src/RestifyServiceProvider.php | 37 ++++++++++- 5 files changed, 166 insertions(+), 12 deletions(-) create mode 100644 src/Commands/stubs/policy.stub diff --git a/src/Commands/RepositoryCommand.php b/src/Commands/RepositoryCommand.php index 802995503..81ebe8afa 100644 --- a/src/Commands/RepositoryCommand.php +++ b/src/Commands/RepositoryCommand.php @@ -36,6 +36,7 @@ class RepositoryCommand extends GeneratorCommand * Execute the console command. * * @return bool|null + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function handle() { @@ -47,6 +48,7 @@ public function handle() * * @param string $name * @return string + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ protected function buildClass($name) { @@ -60,6 +62,19 @@ protected function buildClass($name) $model = $this->laravel->getNamespace().$model; } + if ($this->option('all')) { + $this->call('make:model', [ + 'name' => $this->argument('name'), + '--factory' => true, + '--migration' => true, + '--controller' => true, + ]); + + $this->call('make:policy', [ + 'name' => $this->argument('name').'Policy', + ]); + } + return str_replace( 'DummyFullModel', $model, parent::buildClass($name) ); @@ -94,6 +109,7 @@ protected function getDefaultNamespace($rootNamespace) protected function getOptions() { return [ + ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, factory, and controller for the repository'], ['model', 'm', InputOption::VALUE_REQUIRED, 'The model class being represented.'], ]; } diff --git a/src/Commands/stubs/policy.stub b/src/Commands/stubs/policy.stub new file mode 100644 index 000000000..6780f3dec --- /dev/null +++ b/src/Commands/stubs/policy.stub @@ -0,0 +1,93 @@ +app->runningInConsole()) { - $this->commands([CheckPassport::class]); + $this->commands([ + CheckPassport::class, + ]); $this->registerPublishing(); $this->app->register(RestifyServiceProvider::class); @@ -48,15 +50,15 @@ public function register() protected function registerPublishing() { $this->publishes([ - __DIR__.'/Commands/stubs/RestifyServiceProvider.stub' => app_path('Providers/RestifyServiceProvider.php'), + __DIR__ . '/Commands/stubs/RestifyServiceProvider.stub' => app_path('Providers/RestifyServiceProvider.php'), ], 'restify-provider'); $this->publishes([ - __DIR__.'/../config/config.php' => config_path('restify.php'), + __DIR__ . '/../config/config.php' => config_path('restify.php'), ], 'restify-config'); - if (! $this->app->configurationIsCached()) { - $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-restify'); + if ( ! $this->app->configurationIsCached()) { + $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'laravel-restify'); } } } diff --git a/src/Restify.php b/src/Restify.php index 96beb11dc..0b8d22469 100644 --- a/src/Restify.php +++ b/src/Restify.php @@ -6,6 +6,7 @@ use Binaryk\LaravelRestify\Events\RestifyStarting; use Binaryk\LaravelRestify\Repositories\Repository; use Binaryk\LaravelRestify\Traits\AuthorizesRequests; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Event; use Illuminate\Support\Str; use ReflectionClass; @@ -51,7 +52,7 @@ public static function repositoryForKey($key) /** * Get the repository class name for a given key. * - * @param string $model + * @param string $model * @return string */ public static function repositoryForModel($model) @@ -90,10 +91,10 @@ public static function repositoriesFrom($directory) $repositories = []; foreach ((new Finder)->in($directory)->files() as $repository) { - $repository = $namespace.str_replace( + $repository = $namespace . str_replace( ['/', '.php'], ['\\', ''], - Str::after($repository->getPathname(), app_path().DIRECTORY_SEPARATOR) + Str::after($repository->getPathname(), app_path() . DIRECTORY_SEPARATOR) ); if (is_subclass_of($repository, Repository::class) && (new ReflectionClass($repository))->isInstantiable()) { @@ -109,11 +110,18 @@ public static function repositoriesFrom($directory) /** * Get the URI path prefix utilized by Restify. * + * @param null $plus * @return string */ - public static function path() + public static function path($plus = null) { - return config('restify.base', '/restify-api'); + if (isset($plus)) { + + return config('restify.base', '/restify-api') . '/' . $plus; + } else { + + return config('restify.base', '/restify-api'); + } } /** @@ -130,7 +138,7 @@ public static function starting($callback) } /** - * @param \Closure|string $callback + * @param \Closure|string $callback */ public static function beforeEach($callback) { diff --git a/src/RestifyServiceProvider.php b/src/RestifyServiceProvider.php index 89a62b098..e2461e2d6 100644 --- a/src/RestifyServiceProvider.php +++ b/src/RestifyServiceProvider.php @@ -2,6 +2,7 @@ namespace Binaryk\LaravelRestify; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; @@ -33,8 +34,42 @@ protected function registerRoutes() 'middleware' => config('restify.middleware', []), ]; + $this->customDefinitions($config) + ->defaultRoutes($config); + + } + + /** + * @param $config + * @return RestifyServiceProvider + */ + public function customDefinitions($config) + { + collect(Restify::$repositories)->filter(function ($repository) { + return isset($repository::$middleware) || isset($repository::$prefix); + }) + ->each(function ($repository) use ($config) { + $config['middleware'] = array_merge(config('restify.middleware', []), Arr::wrap($repository::$middleware)); + $config['prefix'] = Restify::path($repository::$prefix); + + Route::group($config, function () { + $this->loadRoutesFrom(__DIR__ . '/../routes/api.php'); + }); + }); + + return $this; + } + + /** + * @param $config + * @return RestifyServiceProvider + */ + public function defaultRoutes($config) + { Route::group($config, function () { - $this->loadRoutesFrom(__DIR__.'/../routes/api.php'); + $this->loadRoutesFrom(__DIR__ . '/../routes/api.php'); }); + + return $this; } } From 0a36e96b1392b635f2963d37c4a2094d87bc0054 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Mon, 6 Jan 2020 21:03:19 +0200 Subject: [PATCH 2/2] Apply fixes from StyleCI (#73) --- src/LaravelRestifyServiceProvider.php | 8 ++++---- src/Restify.php | 9 +++------ src/RestifyServiceProvider.php | 5 ++--- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/LaravelRestifyServiceProvider.php b/src/LaravelRestifyServiceProvider.php index dcea76d09..527ac19f9 100644 --- a/src/LaravelRestifyServiceProvider.php +++ b/src/LaravelRestifyServiceProvider.php @@ -50,15 +50,15 @@ public function register() protected function registerPublishing() { $this->publishes([ - __DIR__ . '/Commands/stubs/RestifyServiceProvider.stub' => app_path('Providers/RestifyServiceProvider.php'), + __DIR__.'/Commands/stubs/RestifyServiceProvider.stub' => app_path('Providers/RestifyServiceProvider.php'), ], 'restify-provider'); $this->publishes([ - __DIR__ . '/../config/config.php' => config_path('restify.php'), + __DIR__.'/../config/config.php' => config_path('restify.php'), ], 'restify-config'); - if ( ! $this->app->configurationIsCached()) { - $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'laravel-restify'); + if (! $this->app->configurationIsCached()) { + $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-restify'); } } } diff --git a/src/Restify.php b/src/Restify.php index 0b8d22469..7dc61c727 100644 --- a/src/Restify.php +++ b/src/Restify.php @@ -6,7 +6,6 @@ use Binaryk\LaravelRestify\Events\RestifyStarting; use Binaryk\LaravelRestify\Repositories\Repository; use Binaryk\LaravelRestify\Traits\AuthorizesRequests; -use Illuminate\Support\Collection; use Illuminate\Support\Facades\Event; use Illuminate\Support\Str; use ReflectionClass; @@ -91,10 +90,10 @@ public static function repositoriesFrom($directory) $repositories = []; foreach ((new Finder)->in($directory)->files() as $repository) { - $repository = $namespace . str_replace( + $repository = $namespace.str_replace( ['/', '.php'], ['\\', ''], - Str::after($repository->getPathname(), app_path() . DIRECTORY_SEPARATOR) + Str::after($repository->getPathname(), app_path().DIRECTORY_SEPARATOR) ); if (is_subclass_of($repository, Repository::class) && (new ReflectionClass($repository))->isInstantiable()) { @@ -116,10 +115,8 @@ public static function repositoriesFrom($directory) public static function path($plus = null) { if (isset($plus)) { - - return config('restify.base', '/restify-api') . '/' . $plus; + return config('restify.base', '/restify-api').'/'.$plus; } else { - return config('restify.base', '/restify-api'); } } diff --git a/src/RestifyServiceProvider.php b/src/RestifyServiceProvider.php index e2461e2d6..0dcc9dcd9 100644 --- a/src/RestifyServiceProvider.php +++ b/src/RestifyServiceProvider.php @@ -36,7 +36,6 @@ protected function registerRoutes() $this->customDefinitions($config) ->defaultRoutes($config); - } /** @@ -53,7 +52,7 @@ public function customDefinitions($config) $config['prefix'] = Restify::path($repository::$prefix); Route::group($config, function () { - $this->loadRoutesFrom(__DIR__ . '/../routes/api.php'); + $this->loadRoutesFrom(__DIR__.'/../routes/api.php'); }); }); @@ -67,7 +66,7 @@ public function customDefinitions($config) public function defaultRoutes($config) { Route::group($config, function () { - $this->loadRoutesFrom(__DIR__ . '/../routes/api.php'); + $this->loadRoutesFrom(__DIR__.'/../routes/api.php'); }); return $this;