66use Illuminate \Support \Str ;
77use Symfony \Component \Console \Input \InputOption ;
88
9- /**
10- * @author Eduard Lupacescu <[email protected] > 11- */
129class PolicyCommand extends GeneratorCommand
1310{
14- /**
15- * The console command name.
16- *
17- * @var string
18- */
1911 protected $ name = 'restify:policy ' ;
2012
21- /**
22- * The console command description.
23- *
24- * @var string
25- */
2613 protected $ description = 'Create a new policy for a specific model. ' ;
2714
28- /**
29- * The type of class being generated.
30- *
31- * @var string
32- */
33- protected $ type = 'Policy ' ;
34-
35- /**
36- * Execute the console command.
37- *
38- * @return bool|null
39- * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
40- */
4115 public function handle ()
4216 {
43- parent ::handle ();
17+ if (parent ::handle () === false && ! $ this ->option ('force ' )) {
18+ return false ;
19+ }
4420 }
4521
46- /**
47- * Build the class with the given name.
48- *
49- * @param string $name
50- * @return string
51- * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
52- */
5322 protected function buildClass ($ name )
5423 {
55- $ namespacedModel = null ;
56- $ model = $ this ->option ('model ' );
24+ $ class = $ this ->replaceModel (parent ::buildClass ($ name ));
5725
58- if (is_null ($ model )) {
59- $ model = $ this ->argument ('name ' );
60- }
26+ $ class = $ this ->replaceQualifiedModel ($ class );
6127
62- if ($ model && ! Str::startsWith ($ model , [$ this ->laravel ->getNamespace (), '\\' ])) {
63- $ namespacedModel = $ this ->laravel ->getNamespace ().$ model ;
64- }
28+ return $ class ;
29+ }
6530
66- $ name .= 'Policy ' ;
31+ protected function replaceClass ($ stub , $ name )
32+ {
33+ $ class = str_replace ($ this ->getNamespace ($ name ).'\\' , '' , $ this ->guessPolicyName ());
6734
68- $ rendered = str_replace (
69- 'UseDummyModel ' , $ namespacedModel ?? $ model , parent ::buildClass ($ name )
70- );
35+ return str_replace (['{{ class }} ' , '{{class}} ' ], $ class , $ stub );
36+ }
7137
72- $ rendered = str_replace (
73- 'DummyModel ' , $ model , $ rendered
74- );
38+ protected function replaceModel ($ stub )
39+ {
40+ return str_replace (['{{ model }} ' , '{{model}} ' ], class_basename ($ this ->guessQualifiedModel ()), $ stub );
41+ }
42+
43+ protected function replaceQualifiedModel ($ stub )
44+ {
45+ return str_replace ('{{ modelQualified }} ' , $ this ->guessQualifiedModel (), $ stub );
46+ }
47+
48+ protected function guessQualifiedModel (): string
49+ {
50+ $ model = Str::singular (class_basename (Str::beforeLast ($ this ->getNameInput (), 'Policy ' )));
7551
76- return $ rendered ;
52+ return str_replace ( ' / ' , '\\' , $ this -> rootNamespace (). ' Models/ ' . $ model ) ;
7753 }
7854
79- public function nameWithEnd ()
55+ protected function guessPolicyName ()
8056 {
81- $ model = $ this ->option ( ' model ' );
57+ $ name = $ this ->getNameInput ( );
8258
83- if (is_null ( $ model )) {
84- $ model = $ this -> argument ( ' name ' ) ;
59+ if (false === Str:: endsWith ( $ name , ' Policy ' )) {
60+ $ name .= ' Policy ' ;
8561 }
8662
87- return $ model . ' Policy ' ;
63+ return $ name ;
8864 }
8965
9066 protected function getPath ($ name )
9167 {
92- return $ this ->laravel ['path ' ].'/Policies/ ' .$ this ->nameWithEnd ().'.php ' ;
68+ return $ this ->laravel ['path ' ].'/Policies/ ' .$ this ->guessPolicyName ().'.php ' ;
9369 }
9470
9571 /**
@@ -105,12 +81,12 @@ protected function getStub()
10581 /**
10682 * Get the default namespace for the class.
10783 *
108- * @param string $rootNamespace
84+ * @param string $rootNamespace
10985 * @return string
11086 */
11187 protected function getDefaultNamespace ($ rootNamespace )
11288 {
113- return $ rootNamespace .'\Restify ' ;
89+ return $ rootNamespace .'\Policies ' ;
11490 }
11591
11692 /**
@@ -122,6 +98,7 @@ protected function getOptions()
12298 {
12399 return [
124100 ['model ' , 'm ' , InputOption::VALUE_REQUIRED , 'The model class being protected. ' ],
101+ ['force ' , null , InputOption::VALUE_NONE , 'Create the class even if the model already exists. ' ],
125102 ];
126103 }
127104}
0 commit comments