Skip to content

Commit 2a56838

Browse files
committed
Make policy stubs customizable
1 parent 261e74c commit 2a56838

4 files changed

Lines changed: 78 additions & 50 deletions

File tree

src/Illuminate/Foundation/Console/PolicyMakeCommand.php

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,46 @@ protected function replaceModel($stub, $model)
7878
{
7979
$model = str_replace('/', '\\', $model);
8080

81-
$namespaceModel = $this->laravel->getNamespace().$model;
82-
8381
if (Str::startsWith($model, '\\')) {
84-
$stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub);
82+
$namespacedModel = trim($model, '\\');
8583
} else {
86-
$stub = str_replace('NamespacedDummyModel', $namespaceModel, $stub);
84+
$namespacedModel = $this->laravel->getNamespace().$model;
8785
}
8886

89-
$stub = str_replace(
90-
"use {$namespaceModel};\nuse {$namespaceModel};", "use {$namespaceModel};", $stub
91-
);
92-
9387
$model = class_basename(trim($model, '\\'));
9488

9589
$dummyUser = class_basename($this->userProviderModel());
9690

9791
$dummyModel = Str::camel($model) === 'user' ? 'model' : $model;
9892

99-
$stub = str_replace('DocDummyModel', Str::snake($dummyModel, ' '), $stub);
100-
101-
$stub = str_replace('DummyModel', $model, $stub);
102-
103-
$stub = str_replace('dummyModel', Str::camel($dummyModel), $stub);
93+
$replace = [
94+
'NamespacedDummyModel' => $namespacedModel,
95+
'{{ namespacedModel }}' => $namespacedModel,
96+
'{{namespacedModel}}' => $namespacedModel,
97+
'DummyModel' => $model,
98+
'{{ model }}' => $model,
99+
'{{model}}' => $model,
100+
'dummyModel' => Str::camel($dummyModel),
101+
'{{ modelVariable }}' => Str::camel($dummyModel),
102+
'{{modelVariable}}' => Str::camel($dummyModel),
103+
'DocDummyModel' => Str::snake($dummyModel, ' '),
104+
'{{ modelDoc }}' => Str::snake($dummyModel, ' '),
105+
'{{modelDoc}}' => Str::snake($dummyModel, ' '),
106+
'DocDummyPluralModel' => Str::snake(Str::pluralStudly($dummyModel), ' '),
107+
'{{ pluralModelDoc }}' => Str::snake(Str::pluralStudly($dummyModel), ' '),
108+
'{{pluralModelDoc}}' => Str::snake(Str::pluralStudly($dummyModel), ' '),
109+
'DummyUser' => $dummyUser,
110+
'{{ user }}' => $dummyUser,
111+
'{{user}}' => $dummyUser,
112+
];
104113

105-
$stub = str_replace('DummyUser', $dummyUser, $stub);
114+
$stub = str_replace(
115+
array_keys($replace), array_values($replace), $stub
116+
);
106117

107-
return str_replace('DocDummyPluralModel', Str::snake(Str::pluralStudly($dummyModel), ' '), $stub);
118+
return str_replace(
119+
"use {$namespacedModel};\nuse {$namespacedModel};", "use {$namespacedModel};", $stub
120+
);
108121
}
109122

110123
/**
@@ -115,8 +128,21 @@ protected function replaceModel($stub, $model)
115128
protected function getStub()
116129
{
117130
return $this->option('model')
118-
? __DIR__.'/stubs/policy.stub'
119-
: __DIR__.'/stubs/policy.plain.stub';
131+
? $this->resolveStubPath('/stubs/policy.stub')
132+
: $this->resolveStubPath('/stubs/policy.plain.stub');
133+
}
134+
135+
/**
136+
* Resolve the fully-qualified path to the stub.
137+
*
138+
* @param string $stub
139+
* @return string
140+
*/
141+
protected function resolveStubPath($stub)
142+
{
143+
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
144+
? $customPath
145+
: __DIR__.$stub;
120146
}
121147

122148
/**

src/Illuminate/Foundation/Console/StubPublishCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function handle()
4343
realpath(__DIR__.'/../../Database/Migrations/stubs/migration.create.stub') => $stubsPath.'/migration.create.stub',
4444
realpath(__DIR__.'/../../Database/Migrations/stubs/migration.stub') => $stubsPath.'/migration.stub',
4545
realpath(__DIR__.'/../../Database/Migrations/stubs/migration.update.stub') => $stubsPath.'/migration.update.stub',
46+
realpath(__DIR__.'/../../Foundation/Console/stubs/policy.plain.stub') => $stubsPath.'/policy.plain.stub',
47+
realpath(__DIR__.'/../../Foundation/Console/stubs/policy.stub') => $stubsPath.'/policy.stub',
4648
realpath(__DIR__.'/../../Routing/Console/stubs/controller.api.stub') => $stubsPath.'/controller.api.stub',
4749
realpath(__DIR__.'/../../Routing/Console/stubs/controller.invokable.stub') => $stubsPath.'/controller.invokable.stub',
4850
realpath(__DIR__.'/../../Routing/Console/stubs/controller.model.api.stub') => $stubsPath.'/controller.model.api.stub',

src/Illuminate/Foundation/Console/stubs/policy.plain.stub

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3-
namespace DummyNamespace;
3+
namespace {{ namespace }};
44

55
use Illuminate\Auth\Access\HandlesAuthorization;
6-
use NamespacedDummyUserModel;
6+
use {{ namespacedUserModel }};
77

8-
class DummyClass
8+
class {{ class }}
99
{
1010
use HandlesAuthorization;
1111

src/Illuminate/Foundation/Console/stubs/policy.stub

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,93 @@
11
<?php
22

3-
namespace DummyNamespace;
3+
namespace {{ namespace }};
44

55
use Illuminate\Auth\Access\HandlesAuthorization;
6-
use NamespacedDummyModel;
7-
use NamespacedDummyUserModel;
6+
use {{ namespacedModel }};
7+
use {{ namespacedUserModel }};
88

9-
class DummyClass
9+
class {{ class }}
1010
{
1111
use HandlesAuthorization;
1212

1313
/**
14-
* Determine whether the user can view any DocDummyPluralModel.
14+
* Determine whether the user can view any {{ pluralModelDoc }}.
1515
*
16-
* @param \NamespacedDummyUserModel $user
16+
* @param \{{ namespacedUserModel }} $user
1717
* @return mixed
1818
*/
19-
public function viewAny(DummyUser $user)
19+
public function viewAny({{ user }} $user)
2020
{
2121
//
2222
}
2323

2424
/**
25-
* Determine whether the user can view the DocDummyModel.
25+
* Determine whether the user can view the {{ modelDoc }}.
2626
*
27-
* @param \NamespacedDummyUserModel $user
28-
* @param \NamespacedDummyModel $dummyModel
27+
* @param \{{ namespacedUserModel }} $user
28+
* @param \{{ namespacedModel }} ${{ modelVariable }}
2929
* @return mixed
3030
*/
31-
public function view(DummyUser $user, DummyModel $dummyModel)
31+
public function view({{ user }} $user, {{ model }} ${{ modelVariable }})
3232
{
3333
//
3434
}
3535

3636
/**
37-
* Determine whether the user can create DocDummyPluralModel.
37+
* Determine whether the user can create {{ pluralModelDoc }}.
3838
*
39-
* @param \NamespacedDummyUserModel $user
39+
* @param \{{ namespacedUserModel }} $user
4040
* @return mixed
4141
*/
42-
public function create(DummyUser $user)
42+
public function create({{ user }} $user)
4343
{
4444
//
4545
}
4646

4747
/**
48-
* Determine whether the user can update the DocDummyModel.
48+
* Determine whether the user can update the {{ modelDoc }}.
4949
*
50-
* @param \NamespacedDummyUserModel $user
51-
* @param \NamespacedDummyModel $dummyModel
50+
* @param \{{ namespacedUserModel }} $user
51+
* @param \{{ namespacedModel }} ${{ modelVariable }}
5252
* @return mixed
5353
*/
54-
public function update(DummyUser $user, DummyModel $dummyModel)
54+
public function update({{ user }} $user, {{ model }} ${{ modelVariable }})
5555
{
5656
//
5757
}
5858

5959
/**
60-
* Determine whether the user can delete the DocDummyModel.
60+
* Determine whether the user can delete the {{ modelDoc }}.
6161
*
62-
* @param \NamespacedDummyUserModel $user
63-
* @param \NamespacedDummyModel $dummyModel
62+
* @param \{{ namespacedUserModel }} $user
63+
* @param \{{ namespacedModel }} ${{ modelVariable }}
6464
* @return mixed
6565
*/
66-
public function delete(DummyUser $user, DummyModel $dummyModel)
66+
public function delete({{ user }} $user, {{ model }} ${{ modelVariable }})
6767
{
6868
//
6969
}
7070

7171
/**
72-
* Determine whether the user can restore the DocDummyModel.
72+
* Determine whether the user can restore the {{ modelDoc }}.
7373
*
74-
* @param \NamespacedDummyUserModel $user
75-
* @param \NamespacedDummyModel $dummyModel
74+
* @param \{{ namespacedUserModel }} $user
75+
* @param \{{ namespacedModel }} ${{ modelVariable }}
7676
* @return mixed
7777
*/
78-
public function restore(DummyUser $user, DummyModel $dummyModel)
78+
public function restore({{ user }} $user, {{ model }} ${{ modelVariable }})
7979
{
8080
//
8181
}
8282

8383
/**
84-
* Determine whether the user can permanently delete the DocDummyModel.
84+
* Determine whether the user can permanently delete the {{ modelDoc }}.
8585
*
86-
* @param \NamespacedDummyUserModel $user
87-
* @param \NamespacedDummyModel $dummyModel
86+
* @param \{{ namespacedUserModel }} $user
87+
* @param \{{ namespacedModel }} ${{ modelVariable }}
8888
* @return mixed
8989
*/
90-
public function forceDelete(DummyUser $user, DummyModel $dummyModel)
90+
public function forceDelete({{ user }} $user, {{ model }} ${{ modelVariable }})
9191
{
9292
//
9393
}

0 commit comments

Comments
 (0)