|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Framework |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
|
7
|
|
|
* PHP Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine Framework |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @file BaseCommand.php |
|
34
|
|
|
* |
|
35
|
|
|
* The Base Command class |
|
36
|
|
|
* |
|
37
|
|
|
* @package Platine\Framework\Console |
|
38
|
|
|
* @author Platine Developers team |
|
39
|
|
|
* @copyright Copyright (c) 2020 |
|
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
41
|
|
|
* @link https://www.platine-php.com |
|
42
|
|
|
* @version 1.0.0 |
|
43
|
|
|
* @filesource |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
declare(strict_types=1); |
|
47
|
|
|
|
|
48
|
|
|
namespace Platine\Framework\Console; |
|
49
|
|
|
|
|
50
|
|
|
use InvalidArgumentException; |
|
51
|
|
|
use Platine\Console\Command\Command; |
|
52
|
|
|
use Platine\Console\Input\Reader; |
|
53
|
|
|
use Platine\Filesystem\Filesystem; |
|
54
|
|
|
use Platine\Framework\App\Application; |
|
55
|
|
|
use Platine\Stdlib\Helper\Path; |
|
56
|
|
|
use Platine\Stdlib\Helper\Str; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @class BaseCommand |
|
60
|
|
|
* @package Platine\Framework\Console |
|
61
|
|
|
*/ |
|
62
|
|
|
abstract class BaseCommand extends Command |
|
63
|
|
|
{ |
|
64
|
|
|
/** |
|
65
|
|
|
* The Application instance |
|
66
|
|
|
* @var Application |
|
67
|
|
|
*/ |
|
68
|
|
|
protected Application $application; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The file system to use |
|
72
|
|
|
* @var Filesystem |
|
73
|
|
|
*/ |
|
74
|
|
|
protected Filesystem $filesystem; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* The application root name space |
|
78
|
|
|
* @var string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected string $rootNamespace; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Create new instance |
|
84
|
|
|
* @param Application $application |
|
85
|
|
|
* @param Filesystem $filesystem |
|
86
|
|
|
*/ |
|
87
|
|
|
public function __construct( |
|
88
|
|
|
Application $application, |
|
89
|
|
|
Filesystem $filesystem |
|
90
|
|
|
) { |
|
91
|
|
|
parent::__construct('make', 'Command to generate class skeleton'); |
|
92
|
|
|
$this->application = $application; |
|
93
|
|
|
$this->filesystem = $filesystem; |
|
94
|
|
|
$this->rootNamespace = $application->getNamespace(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Return the base class name |
|
99
|
|
|
* @param string|object $fullClassName |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getClassBaseName(string|object $fullClassName): string |
|
103
|
|
|
{ |
|
104
|
|
|
if (is_object($fullClassName)) { |
|
105
|
|
|
$fullClassName = get_class($fullClassName); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$temp = explode('\\', $fullClassName); |
|
109
|
|
|
|
|
110
|
|
|
return end($temp); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Return the real path for the given name |
|
115
|
|
|
* @param string $className |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getPath(string $className): string |
|
119
|
|
|
{ |
|
120
|
|
|
$class = Str::replaceFirst($this->rootNamespace, '', $className); |
|
121
|
|
|
|
|
122
|
|
|
$path = sprintf( |
|
123
|
|
|
'%s/%s.php', |
|
124
|
|
|
$this->application->getAppPath(), |
|
125
|
|
|
str_replace('\\', '/', $class) |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
return Path::normalizePathDS($path); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Return the class name with the root name space |
|
133
|
|
|
* @param string $name |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getFullClassName(string $name): string |
|
137
|
|
|
{ |
|
138
|
|
|
$classClean = ltrim($name, '/\\'); |
|
139
|
|
|
$class = str_replace('/', '\\', $classClean); |
|
140
|
|
|
|
|
141
|
|
|
if (Str::startsWith($this->rootNamespace, $class)) { |
|
142
|
|
|
return $class; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$fullyClass = $this->getFullClassName(sprintf( |
|
146
|
|
|
'%s\\%s', |
|
147
|
|
|
trim($this->rootNamespace, '\\'), |
|
148
|
|
|
$class |
|
149
|
|
|
)); |
|
150
|
|
|
|
|
151
|
|
|
return $fullyClass; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Return the full name space for the given class |
|
156
|
|
|
* @param string $className |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getNamespace(string $className): string |
|
160
|
|
|
{ |
|
161
|
|
|
$class = str_replace('/', '\\', $className); |
|
162
|
|
|
|
|
163
|
|
|
return $this->rootNamespace . trim(implode( |
|
164
|
|
|
'\\', |
|
165
|
|
|
array_slice(explode('\\', $class), 0, -1) |
|
166
|
|
|
), '\\'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Whether the file for the given name already exists |
|
171
|
|
|
* @param string $className |
|
172
|
|
|
* @return bool |
|
173
|
|
|
*/ |
|
174
|
|
|
public function fileExists(string $className): bool |
|
175
|
|
|
{ |
|
176
|
|
|
$path = $this->getPath($className); |
|
177
|
|
|
|
|
178
|
|
|
return $this->filesystem->file($path)->exists(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Return the short class name |
|
183
|
|
|
* @param string $className |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getShortClassName(string $className): string |
|
187
|
|
|
{ |
|
188
|
|
|
$namespace = $this->getNamespace($className); |
|
189
|
|
|
|
|
190
|
|
|
return Str::replaceFirst( |
|
191
|
|
|
$namespace . '\\', |
|
192
|
|
|
'', |
|
193
|
|
|
$this->getFullClassName($className) |
|
194
|
|
|
); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Create the class parent(s) directory if it does not exist |
|
199
|
|
|
* @param string $path |
|
200
|
|
|
* @return void |
|
201
|
|
|
*/ |
|
202
|
|
|
public function createParentDirectory(string $path): void |
|
203
|
|
|
{ |
|
204
|
|
|
$file = $this->filesystem->file($path); |
|
205
|
|
|
$location = $file->getLocation(); |
|
206
|
|
|
if (!empty($location)) { |
|
207
|
|
|
$directory = $this->filesystem->directory($location); |
|
208
|
|
|
if ($directory->exists() === false) { |
|
209
|
|
|
$directory->create('', 0775, true); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Set reader stream content |
|
216
|
|
|
* @param Reader $reader |
|
217
|
|
|
* @param string $filename |
|
218
|
|
|
* @param string|array<string> $data |
|
219
|
|
|
* @return void |
|
220
|
|
|
*/ |
|
221
|
|
|
public function setReaderContent(Reader $reader, string $filename, string|array $data): void |
|
222
|
|
|
{ |
|
223
|
|
|
if (is_array($data)) { |
|
|
|
|
|
|
224
|
|
|
$data = implode(PHP_EOL, $data); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
file_put_contents($filename, $data, FILE_APPEND); |
|
228
|
|
|
|
|
229
|
|
|
$resource = fopen($filename, 'r'); |
|
230
|
|
|
if ($resource === false) { |
|
231
|
|
|
throw new InvalidArgumentException(sprintf( |
|
232
|
|
|
'Could not open filename [%s] for reading', |
|
233
|
|
|
$filename |
|
234
|
|
|
)); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
$reader->setStream($resource); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|