55use Illuminate \Support \Str ;
66use Illuminate \Console \Command ;
77use Illuminate \Container \Container ;
8- use Illuminate \Filesystem \Filesystem ;
98
109
1110abstract class BaseCommand extends Command
1211{
13-
14- /**
15- * @var string
16- */
17- protected $ template_path = '' ;
12+ /** @var string */
13+ private $ template_path ;
1814
1915
20- protected $ filesystem ;
2116
2217
2318 public function __construct ()
2419 {
2520 parent ::__construct ();
2621
27- $ this ->template_path = (dirname (dirname (__DIR__ ))) . "/templates " ;
28- $ this ->filesystem = new Filesystem ;
22+ $ this ->template_path = dirname (dirname (__DIR__ )) . "/stubs " ;
2923 }
3024
3125
32- /**
33- * Parse guard name
34- * Get the guard name in different cases
35- * @param string $name
36- * @return array
37- */
38- protected function parseName ($ name = null ) :array
26+ protected function getTemplatePath (string $ path = '' ): string
27+ {
28+ $ path = Str::start ($ path , '/ ' );
29+
30+ return $ this ->template_path . $ path ;
31+ }
32+
33+ protected function parseName () :array
3934 {
4035 return [
4136 '{{namespace}} ' => $ this ->getNamespace (),
42- // '{{pluralCamel}}' => Str::plural(Str::camel($name)),
43- // '{{pluralSlug}}' => Str::plural(Str::slug($name)),
44- // '{{pluralSnake}}' => Str::plural(Str::snake($name)),
45- // '{{pluralClass}}' => Str::plural(Str::studly($name)),
46- // '{{singularCamel}}' => Str::singular(Str::camel($name)),
47- // '{{singularSlug}}' => Str::singular(Str::slug($name)),
48- // '{{singularSnake}}' => Str::singular(Str::snake($name)),
49- // '{{singularClass}}' => Str::singular(Str::studly($name)),
5037 ];
5138 }
5239
@@ -58,121 +45,8 @@ protected function parseName($name = null) :array
5845 protected function getNamespace ()
5946 {
6047 $ namespace = Container::getInstance ()->getNamespace ();
61- return rtrim ($ namespace , '\\' );
62- }
63-
6448
65- /**
66- * @param string|array $files
67- * @param string $path
68- * @return void
69- */
70- protected function compliedAndWriteFile ($ files , string $ path ): void
71- {
72-
73- if (is_array ($ files )) {
74- foreach ($ files as $ file ) {
75- $ this ->compliedAndWriteFile ($ file , $ path );
76- }
77- return ;
78- }
79-
80- $ data_map = $ this ->parseName ();
81-
82- $ stub = $ this ->isSingleFile ($ files ) ? $ files : $ this ->filesystem ->get ($ files ->getRealPath ());
83-
84- $ this ->createDirectoryIfNotExists (
85- $ path ,
86- !$ this ->isSingleFile ($ files )
87- );
88- $ complied = strtr ($ stub , $ data_map );
89-
90- $ this ->writeFile (
91- $ complied ,
92- $ this ->isSingleFile ($ files ) ? $ path : $ path . '/ ' . $ files ->getFilenameWithoutExtension () . '.php '
93- );
94- }
95-
96-
97- protected function compliedAndWriteFileRecursively ($ files , string $ path )
98- {
99- if (is_array ($ files )) {
100- foreach ($ files as $ file ) {
101- $ this ->compliedAndWriteFileRecursively ($ file , $ path );
102- }
103- return ;
104- }
105-
106- $ this ->compliedAndWriteFile (
107- $ this ->filesystem ->get ($ files ),
108- $ path . '/ ' . $ files ->getRelativePath () . '/ ' . $ files ->getFilenameWithoutExtension () . '.php '
109- );
110- }
111-
112- /**
113- * @param string|array $files
114- * @param string $search
115- * @param string $path
116- * @return void
117- */
118- protected function replaceAndWriteFile ($ files , string $ search , $ replace , string $ path )
119- {
120- if (is_array ($ files )) {
121- foreach ($ files as $ file ) {
122- $ this ->replaceAndWriteFile ($ file , $ search , $ replace , $ path );
123- }
124- return ;
125- }
126-
127- $ stub = $ this ->isSingleFile ($ files ) ? $ files : $ this ->filesystem ->get ($ files ->getRealPath ());
128- // $stub = $this->filesystem->get($files->getRealPath());
129- $ this ->createDirectoryIfNotExists (
130- $ path ,
131- !$ this ->isSingleFile ($ files )
132- );
133- $ complied = str_replace ($ search , $ replace , $ stub );
134-
135- $ this ->writeFile (
136- $ complied ,
137- $ this ->isSingleFile ($ files ) ? $ path : $ path . '/ ' . $ files ->getFilenameWithoutExtension () . '.php '
138- );
139- }
140-
141-
142- /**
143- * Permet de créer un dossier
144- * @param string $path
145- * @param boolean $folder Permet de savoir si le chemin passé est un dossier ou fichier
146- * @return void
147- */
148- protected function createDirectoryIfNotExists (string $ path , bool $ folder = true ): void
149- {
150-
151- $ dir = $ folder ? $ path : $ this ->filesystem ->dirname ($ path );
152-
153- if (!$ this ->filesystem ->exists ($ dir )) {
154- $ this ->filesystem ->makeDirectory ($ dir , 0755 , true );
155- }
156- }
157-
158- /**
159- * @param mixte $compiled
160- * @param string $path
161- * @return void
162- */
163- protected function writeFile ($ compiled , string $ path ): void
164- {
165- // dd($path, $compiled);
166- $ this ->filesystem ->put (
167- $ path ,
168- $ compiled
169- );
170- }
171-
172-
173- protected function isSingleFile ($ file ): bool
174- {
175- return is_string ($ file );
49+ return rtrim ($ namespace , '\\' );
17650 }
17751
17852}
0 commit comments