1 <?php
2
3 namespace Skimia\ApiFusion\Console\Commands;
4
5 use Illuminate\Console\Command;
6 use Illuminate\Foundation\Inspiring;
7
8 class GenerateDomainApi extends Command
9 {
10 11 12 13 14
15 protected $signature = 'api-fusion:generate.domain';
16
17 18 19 20 21
22 protected $description = 'génère toute les classes necessaires pour gérér une nouvelle resource';
23
24 25 26 27 28
29 public function handle()
30 {
31 $camelCasedSingularName = $this->ask('Nom de la resource en camelCase [maNouvelleResourceEnCamelCase]');
32 $ucFirstSingularName = ucfirst($camelCasedSingularName);
33 $camelCasedPluralName = str_plural($camelCasedSingularName);
34 $ucFirstPluralName = str_plural($ucFirstSingularName);
35
36
37 $namespace = $this->ask('Namespace du domaine de la resource', 'App\Domain\\'.$ucFirstSingularName);
38
39 $directory = $this->ask('Répertoire de la resource', 'app/Domain/'.$ucFirstSingularName);
40
41
42 $namespaceController = $this->ask('Namespace du controller de la resource', 'App\Http\Controllers\Api\v1');
43
44 $directoryController = $this->ask('Répertoire du controlleur', 'app/Http/Controllers/Api/v1');
45
46 $this->generateModel($namespace, $directory, $ucFirstSingularName);
47
48 $this->info('Model généré !');
49
50 $this->generateValidator($namespace, $directory, $ucFirstSingularName);
51
52 $this->info('Validator généré !');
53
54 $this->generateService($namespace, $directory, $ucFirstSingularName);
55
56 $this->info('Service généré !');
57
58 $this->generateTransformer($namespace, $directory, $ucFirstSingularName, $camelCasedSingularName, $camelCasedPluralName);
59
60 $this->info('Transformer généré !');
61
62 $this->generateController($namespaceController, $directoryController, $ucFirstPluralName, $namespace, $ucFirstSingularName, $camelCasedPluralName);
63
64 $this->info('Controller généré !');
65
66 }
67
68 public function generateModel($namespace, $directory, $name)
69 {
70 $contents = $this->getStub('resource_model');
71
72 $contents = str_replace(
73 [
74 '{{NAMESPACE}}',
75 '{{CLASS}}',
76 ],
77 [
78 $namespace,
79 $name,
80 ], $contents);
81
82 $this->write($directory.'/'.$name.'.php', $contents);
83 }
84
85 public function generateValidator($namespace, $directory, $name)
86 {
87 $contents = $this->getStub('resource_validator');
88
89 $contents = str_replace(
90 [
91 '{{NAMESPACE}}',
92 '{{CLASS}}',
93 ],
94 [
95 $namespace,
96 $name,
97 ], $contents);
98
99 $this->write($directory.'/'.$name.'Validator.php', $contents);
100 }
101
102 public function generateService($namespace, $directory, $name)
103 {
104 $contents = $this->getStub('resource_service');
105
106 $contents = str_replace(
107 [
108 '{{NAMESPACE}}',
109 '{{CLASS}}',
110 ],
111 [
112 $namespace,
113 $name,
114 ], $contents);
115
116 $this->write($directory.'/'.$name.'Service.php', $contents);
117 }
118
119 public function generateTransformer($namespace, $directory, $name, $camel, $camelplu)
120 {
121 $contents = $this->getStub('resource_transformer');
122
123 $contents = str_replace(
124 [
125 '{{NAMESPACE}}',
126 '{{CLASS}}',
127 '{{NAME}}',
128 '{{NAME_PLU}}',
129 ],
130 [
131 $namespace,
132 $name,
133 $camel,
134 $camelplu,
135 ], $contents);
136
137 $this->write($directory.'/'.$name.'Transformer.php', $contents);
138 }
139
140 public function generateController($namespaceCtrl, $directory, $nameCtrl, $namespace, $class, $pluriel)
141 {
142 $contents = $this->getStub('resource_controller');
143
144 $contents = str_replace(
145 [
146 '{{NAMESPACE_CTRL}}',
147 '{{CLASS_CTRL}}',
148 '{{NAMESPACE}}',
149 '{{CLASS}}',
150 '{{URL}}',
151 ],
152 [
153 $namespaceCtrl,
154 $nameCtrl,
155 $namespace,
156 $class,
157 $pluriel,
158 ], $contents);
159
160 $this->write($directory.'/'.$nameCtrl.'Controller.php', $contents);
161 }
162
163 protected function getStub($name)
164 {
165 $path = __DIR__.'/stubs/'.$name.'.stub';
166
167 return \File::get($path);
168 }
169
170 protected function write($path, $content)
171 {
172 $dir = dirname($path);
173
174 if (! \File::exists($dir)) {
175 \File::makeDirectory($dir, 0777, true);
176 }
177 \File::put($path, $content);
178 }
179 }
180