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 Platine\Console\Command\Command; |
51
|
|
|
use Platine\Filesystem\Filesystem; |
52
|
|
|
use Platine\Framework\App\Application; |
53
|
|
|
use Platine\Stdlib\Helper\Path; |
54
|
|
|
use Platine\Stdlib\Helper\Str; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @class BaseCommand |
58
|
|
|
* @package Platine\Framework\Console |
59
|
|
|
*/ |
60
|
|
|
abstract class BaseCommand extends Command |
61
|
|
|
{ |
62
|
|
|
/** |
63
|
|
|
* The Application instance |
64
|
|
|
* @var Application |
65
|
|
|
*/ |
66
|
|
|
protected Application $application; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The file system to use |
70
|
|
|
* @var Filesystem |
71
|
|
|
*/ |
72
|
|
|
protected Filesystem $filesystem; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The application root name space |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
protected string $rootNamespace; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create new instance |
82
|
|
|
* @param Application $application |
83
|
|
|
* @param Filesystem $filesystem |
84
|
|
|
*/ |
85
|
|
|
public function __construct( |
86
|
|
|
Application $application, |
87
|
|
|
Filesystem $filesystem |
88
|
|
|
) { |
89
|
|
|
parent::__construct('make', 'Command to generate class skeleton'); |
90
|
|
|
$this->application = $application; |
91
|
|
|
$this->filesystem = $filesystem; |
92
|
|
|
$this->rootNamespace = $application->getNamespace(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return the base class name |
97
|
|
|
* @param string|object $fullClassName |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getClassBaseName($fullClassName): string |
101
|
|
|
{ |
102
|
|
|
if (is_object($fullClassName)) { |
103
|
|
|
$fullClassName = get_class($fullClassName); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$temp = explode('\\', $fullClassName); |
107
|
|
|
|
108
|
|
|
return end($temp); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return the real path for the given name |
113
|
|
|
* @param string $className |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getPath(string $className): string |
117
|
|
|
{ |
118
|
|
|
$class = Str::replaceFirst($this->rootNamespace, '', $className); |
119
|
|
|
|
120
|
|
|
$path = sprintf( |
121
|
|
|
'%s/%s.php', |
122
|
|
|
$this->application->getAppPath(), |
123
|
|
|
str_replace('\\', '/', $class) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
return Path::normalizePathDS($path); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Return the class name with the root name space |
131
|
|
|
* @param string $name |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getFullClassName(string $name): string |
135
|
|
|
{ |
136
|
|
|
$classClean = ltrim($name, '/\\'); |
137
|
|
|
$class = str_replace('/', '\\', $classClean); |
138
|
|
|
|
139
|
|
|
if (Str::startsWith($this->rootNamespace, $class)) { |
140
|
|
|
return $class; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$fullyClass = $this->getFullClassName(sprintf( |
144
|
|
|
'%s\\%s', |
145
|
|
|
trim($this->rootNamespace, '\\'), |
146
|
|
|
$class |
147
|
|
|
)); |
148
|
|
|
|
149
|
|
|
return $fullyClass; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Return the full name space for the given class |
154
|
|
|
* @param string $className |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getNamespace(string $className): string |
158
|
|
|
{ |
159
|
|
|
$class = str_replace('/', '\\', $className); |
160
|
|
|
|
161
|
|
|
return $this->rootNamespace . trim(implode( |
162
|
|
|
'\\', |
163
|
|
|
array_slice(explode('\\', $class), 0, -1) |
164
|
|
|
), '\\'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Whether the file for the given name already exists |
169
|
|
|
* @param string $className |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
|
|
public function fileExists(string $className): bool |
173
|
|
|
{ |
174
|
|
|
$path = $this->getPath($className); |
175
|
|
|
|
176
|
|
|
return $this->filesystem->file($path)->exists(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Return the short class name |
181
|
|
|
* @param string $className |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function getShortClassName(string $className): string |
185
|
|
|
{ |
186
|
|
|
$namespace = $this->getNamespace($className); |
187
|
|
|
|
188
|
|
|
return Str::replaceFirst( |
189
|
|
|
$namespace . '\\', |
190
|
|
|
'', |
191
|
|
|
$this->getFullClassName($className) |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Create the class parent(s) directory if it does not exist |
197
|
|
|
* @param string $path |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
public function createParentDirectory(string $path): void |
201
|
|
|
{ |
202
|
|
|
$file = $this->filesystem->file($path); |
203
|
|
|
$location = $file->getLocation(); |
204
|
|
|
if (!empty($location)) { |
205
|
|
|
$directory = $this->filesystem->directory($location); |
206
|
|
|
if (!$directory->exists()) { |
207
|
|
|
$directory->create('', 0775, true); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|