Skip to content

Commit e27d7f6

Browse files
author
Lupacescu Eduard
authored
Custom middleware and prefix per resource (#74)
* Custom middleware and prefix per resource * Apply fixes from StyleCI (#73)
1 parent c1e050a commit e27d7f6

File tree

5 files changed

+155
-5
lines changed

5 files changed

+155
-5
lines changed

src/Commands/RepositoryCommand.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class RepositoryCommand extends GeneratorCommand
3636
* Execute the console command.
3737
*
3838
* @return bool|null
39+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
3940
*/
4041
public function handle()
4142
{
@@ -47,6 +48,7 @@ public function handle()
4748
*
4849
* @param string $name
4950
* @return string
51+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
5052
*/
5153
protected function buildClass($name)
5254
{
@@ -60,6 +62,19 @@ protected function buildClass($name)
6062
$model = $this->laravel->getNamespace().$model;
6163
}
6264

65+
if ($this->option('all')) {
66+
$this->call('make:model', [
67+
'name' => $this->argument('name'),
68+
'--factory' => true,
69+
'--migration' => true,
70+
'--controller' => true,
71+
]);
72+
73+
$this->call('make:policy', [
74+
'name' => $this->argument('name').'Policy',
75+
]);
76+
}
77+
6378
return str_replace(
6479
'DummyFullModel', $model, parent::buildClass($name)
6580
);
@@ -94,6 +109,7 @@ protected function getDefaultNamespace($rootNamespace)
94109
protected function getOptions()
95110
{
96111
return [
112+
['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, factory, and controller for the repository'],
97113
['model', 'm', InputOption::VALUE_REQUIRED, 'The model class being represented.'],
98114
];
99115
}

src/Commands/stubs/policy.stub

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use App\User;
6+
use Illuminate\Auth\Access\HandlesAuthorization;
7+
8+
class DummyClass
9+
{
10+
use HandlesAuthorization;
11+
12+
/**
13+
* Determine whether the user can view any models.
14+
*
15+
* @param \App\User $user
16+
* @return mixed
17+
*/
18+
public function viewAny(User $user = null)
19+
{
20+
return true;
21+
}
22+
23+
/**
24+
* Determine whether the user can view the model.
25+
*
26+
* @param \App\User $user
27+
* @param \App\User $model
28+
* @return mixed
29+
*/
30+
public function view(User $user = null, User $model = null)
31+
{
32+
return true;
33+
}
34+
35+
/**
36+
* Determine whether the user can create models.
37+
*
38+
* @param \App\User $user
39+
* @return mixed
40+
*/
41+
public function create(User $user = null)
42+
{
43+
return true;
44+
}
45+
46+
/**
47+
* Determine whether the user can update the model.
48+
*
49+
* @param \App\User $user
50+
* @param \App\User $model
51+
* @return mixed
52+
*/
53+
public function update(User $user, User $model)
54+
{
55+
return true;
56+
}
57+
58+
/**
59+
* Determine whether the user can delete the model.
60+
*
61+
* @param \App\User $user
62+
* @param \App\User $model
63+
* @return mixed
64+
*/
65+
public function delete(User $user, User $model)
66+
{
67+
//
68+
}
69+
70+
/**
71+
* Determine whether the user can restore the model.
72+
*
73+
* @param \App\User $user
74+
* @param \App\User $model
75+
* @return mixed
76+
*/
77+
public function restore(User $user, User $model)
78+
{
79+
return true;
80+
}
81+
82+
/**
83+
* Determine whether the user can permanently delete the model.
84+
*
85+
* @param \App\User $user
86+
* @param \App\User $model
87+
* @return mixed
88+
*/
89+
public function forceDelete(User $user, User $model)
90+
{
91+
return true;
92+
}
93+
}

src/LaravelRestifyServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class LaravelRestifyServiceProvider extends ServiceProvider
1616
public function boot()
1717
{
1818
if ($this->app->runningInConsole()) {
19-
$this->commands([CheckPassport::class]);
19+
$this->commands([
20+
CheckPassport::class,
21+
]);
2022
$this->registerPublishing();
2123

2224
$this->app->register(RestifyServiceProvider::class);

src/Restify.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static function repositoryForKey($key)
5151
/**
5252
* Get the repository class name for a given key.
5353
*
54-
* @param string $model
54+
* @param string $model
5555
* @return string
5656
*/
5757
public static function repositoryForModel($model)
@@ -109,11 +109,16 @@ public static function repositoriesFrom($directory)
109109
/**
110110
* Get the URI path prefix utilized by Restify.
111111
*
112+
* @param null $plus
112113
* @return string
113114
*/
114-
public static function path()
115+
public static function path($plus = null)
115116
{
116-
return config('restify.base', '/restify-api');
117+
if (isset($plus)) {
118+
return config('restify.base', '/restify-api').'/'.$plus;
119+
} else {
120+
return config('restify.base', '/restify-api');
121+
}
117122
}
118123

119124
/**
@@ -130,7 +135,7 @@ public static function starting($callback)
130135
}
131136

132137
/**
133-
* @param \Closure|string $callback
138+
* @param \Closure|string $callback
134139
*/
135140
public static function beforeEach($callback)
136141
{

src/RestifyServiceProvider.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Binaryk\LaravelRestify;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\Support\Facades\Route;
67
use Illuminate\Support\ServiceProvider;
78

@@ -33,8 +34,41 @@ protected function registerRoutes()
3334
'middleware' => config('restify.middleware', []),
3435
];
3536

37+
$this->customDefinitions($config)
38+
->defaultRoutes($config);
39+
}
40+
41+
/**
42+
* @param $config
43+
* @return RestifyServiceProvider
44+
*/
45+
public function customDefinitions($config)
46+
{
47+
collect(Restify::$repositories)->filter(function ($repository) {
48+
return isset($repository::$middleware) || isset($repository::$prefix);
49+
})
50+
->each(function ($repository) use ($config) {
51+
$config['middleware'] = array_merge(config('restify.middleware', []), Arr::wrap($repository::$middleware));
52+
$config['prefix'] = Restify::path($repository::$prefix);
53+
54+
Route::group($config, function () {
55+
$this->loadRoutesFrom(__DIR__.'/../routes/api.php');
56+
});
57+
});
58+
59+
return $this;
60+
}
61+
62+
/**
63+
* @param $config
64+
* @return RestifyServiceProvider
65+
*/
66+
public function defaultRoutes($config)
67+
{
3668
Route::group($config, function () {
3769
$this->loadRoutesFrom(__DIR__.'/../routes/api.php');
3870
});
71+
72+
return $this;
3973
}
4074
}

0 commit comments

Comments
 (0)