1 | <?php |
||
9 | |||
10 | abstract class GeneratorCommand extends Command |
||
11 | { |
||
12 | /** |
||
13 | * The name of 'name' argument. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $argumentName = ''; |
||
18 | |||
19 | /** |
||
20 | * Get template contents. |
||
21 | * |
||
22 | * @return string |
||
23 | */ |
||
24 | abstract protected function getTemplateContents(); |
||
25 | |||
26 | /** |
||
27 | * Get the destination file path. |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | abstract protected function getDestinationFilePath(); |
||
32 | |||
33 | /** |
||
34 | * Execute the console command. |
||
35 | */ |
||
36 | public function fire() |
||
37 | { |
||
38 | $path = str_replace('\\', '/', $this->getDestinationFilePath()); |
||
39 | |||
40 | if (!$this->laravel['files']->isDirectory($dir = dirname($path))) { |
||
41 | $this->laravel['files']->makeDirectory($dir, 0777, true); |
||
42 | } |
||
43 | |||
44 | $contents = $this->getTemplateContents(); |
||
45 | |||
46 | try { |
||
47 | with(new FileGenerator($path, $contents))->generate(); |
||
48 | |||
49 | $this->info("Created : {$path}"); |
||
50 | } catch (FileAlreadyExistException $e) { |
||
51 | $this->error("File : {$path} already exists."); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Get class name. |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | public function getClass() |
||
61 | { |
||
62 | return class_basename($this->argument($this->argumentName)); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Get default namespace. |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | public function getDefaultNamespace() |
||
71 | { |
||
72 | return ''; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get class namespace. |
||
77 | * |
||
78 | * @param \Nwidart\Module\Module $module |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getClassNamespace($module) |
||
83 | { |
||
84 | $extra = str_replace($this->getClass(), '', $this->argument($this->argumentName)); |
||
85 | |||
86 | $extra = str_replace('/', '\\', $extra); |
||
87 | |||
88 | $namespace = $this->laravel['modules']->config('namespace'); |
||
89 | |||
90 | $namespace .= '\\'.$module->getStudlyName(); |
||
91 | |||
92 | $namespace .= '\\'.$this->getDefaultNamespace(); |
||
93 | |||
94 | $namespace .= '\\'.$extra; |
||
95 | |||
96 | return rtrim($namespace, '\\'); |
||
97 | } |
||
98 | } |
||
99 |