22
33namespace FlixtechsLabs \LaravelAuthorizer \Commands ;
44
5+ use FlixtechsLabs \LaravelAuthorizer \Commands \Traits \LocatesModels ;
56use Illuminate \Console \Command ;
6- use Illuminate \Database \Eloquent \Model ;
7- use Illuminate \Support \Facades \File ;
87use Illuminate \Support \Str ;
9- use ReflectionClass ;
10- use ReflectionException ;
11- use SplFileInfo ;
128use Symfony \Component \Filesystem \Filesystem ;
139
1410class LaravelAuthorizerCommand extends Command
1511{
12+ use LocatesModels;
13+
1614 /**
1715 * The name and signature of the console command.
1816 *
@@ -67,31 +65,9 @@ public function handle(): int
6765 */
6866 public function generateAllPolicies (): void
6967 {
70- $ models = collect (File::allFiles (app_path ()))
71- ->map (function (SplFileInfo $ info ) {
72- $ path = $ info ->getRelativePathname ();
73-
74- return sprintf (
75- '\%s%s ' ,
76- app ()->getNamespace (),
77- Str::replace ('/ ' , '\\' , Str::beforeLast ($ path , '. ' ))
78- );
79- })
80- ->filter (function (string $ class ) {
81- try {
82- $ reflection = new ReflectionClass ($ class );
83- } catch (ReflectionException $ throwable ) {
84- return false ;
85- }
86-
87- return $ reflection ->isSubclassOf (Model::class) &&
88- !$ reflection ->isAbstract ();
89- })
90- ->map (fn ($ model ) => Str::afterLast ($ model , '\\' ));
91-
92- $ models ->each (function (string $ model ) {
93- $ this ->generatePolicy ($ model , $ model );
94- });
68+ $ this ->getModels ()->each (
69+ fn (string $ model ) => $ this ->generatePolicy ($ model , $ model )
70+ );
9571 }
9672
9773 /**
@@ -104,7 +80,7 @@ public function generatePlainPolicy(string $name): void
10480 {
10581 if (
10682 file_exists ($ this ->getPolicyPath ($ name )) &&
107- !$ this ->option ('force ' )
83+ ! $ this ->option ('force ' )
10884 ) {
10985 $ this ->error (sprintf ('Policy "%s" already exists! ' , $ name ));
11086
@@ -116,8 +92,8 @@ public function generatePlainPolicy(string $name): void
11692 'namespace ' => $ this ->getNamespace (),
11793 'class ' => $ this ->getClassName ($ name ),
11894 ])->reduce (
119- fn ($ carry , $ value , $ key ) => Str::replace (
120- '{{ ' . $ key . ' }} ' ,
95+ fn ($ carry , $ value , $ key ) => Str::replace (
96+ '{{ ' . $ key. ' }} ' ,
12197 $ value ,
12298 $ carry
12399 )
@@ -137,7 +113,7 @@ private function generatePolicy(string $name, string $model): void
137113 {
138114 if (
139115 file_exists ($ this ->getPolicyPath ($ name )) &&
140- !$ this ->option ('force ' )
116+ ! $ this ->option ('force ' )
141117 ) {
142118 $ this ->error (sprintf ('Policy "%s" already exists! ' , $ name ));
143119
@@ -147,8 +123,7 @@ private function generatePolicy(string $name, string $model): void
147123 $ compiled = collect ([
148124 'name ' => $ name ,
149125 'model ' => $ model ,
150- 'modelVariable ' =>
151- strtolower ($ model ) ===
126+ 'modelVariable ' => strtolower ($ model ) ===
152127 strtolower (
153128 Str::afterLast ($ this ->getNamespacedUserModel (), '\\' )
154129 )
@@ -162,8 +137,8 @@ private function generatePolicy(string $name, string $model): void
162137 'namespacedUserModel ' => $ this ->getNamespacedUserModel (),
163138 'user ' => Str::afterLast ($ this ->getNamespacedUserModel (), '\\' ),
164139 ])->reduce (
165- static fn ($ old , $ value , $ key ) => Str::replace (
166- '{{ ' . $ key . ' }} ' ,
140+ static fn ($ old , $ value , $ key ) => Str::replace (
141+ '{{ ' . $ key. ' }} ' ,
167142 $ value ,
168143 $ old
169144 ),
@@ -176,22 +151,22 @@ private function generatePolicy(string $name, string $model): void
176151 /**
177152 * Get the path to the policy.
178153 *
179- * @param string $name
154+ * @param string $name
180155 * @return string
181156 */
182157 public function getPolicyPath (string $ name ): string
183158 {
184- return app_path ('Policies/ ' . $ this ->getClassName ($ name ) . '.php ' );
159+ return app_path ('Policies/ ' . $ this ->getClassName ($ name ). '.php ' );
185160 }
186161
187162 /**
188- * Get the policies namespace.
163+ * Get the policies' namespace.
189164 *
190165 * @return string
191166 */
192167 public function getNamespace (): string
193168 {
194- return app ()->getNamespace () . 'Policies ' ;
169+ return app ()->getNamespace (). 'Policies ' ;
195170 }
196171
197172 /**
@@ -206,7 +181,7 @@ public function getClassName(string $name): string
206181 return Str::studly ($ name );
207182 }
208183
209- return Str::studly ($ name ) . 'Policy ' ;
184+ return Str::studly ($ name ). 'Policy ' ;
210185 }
211186
212187 /**
@@ -217,7 +192,7 @@ public function getClassName(string $name): string
217192 */
218193 public function getNamespacedModel (string $ model ): string
219194 {
220- return app ()->getNamespace () . 'Models \\' . Str::studly ($ model );
195+ return app ()->getNamespace (). 'Models \\' . Str::studly ($ model );
221196 }
222197
223198 /**
@@ -237,6 +212,6 @@ public function getNamespacedUserModel(): string
237212 */
238213 public function getStub (): string
239214 {
240- return __DIR__ . '/stubs/policy.stub ' ;
215+ return __DIR__ . '/stubs/policy.stub ' ;
241216 }
242217}
0 commit comments