1 | <?php namespace Limoncello\Commands; |
||
43 | class CommandsCommand extends BaseCommand |
||
44 | { |
||
45 | use CommandTrait, CommandSerializationTrait, CacheFilePathTrait; |
||
46 | |||
47 | /** |
||
48 | * Command name. |
||
49 | */ |
||
50 | const NAME = 'l:commands'; |
||
51 | |||
52 | /** Argument name */ |
||
53 | const ARG_ACTION = 'action'; |
||
54 | |||
55 | /** Command action */ |
||
56 | const ACTION_CONNECT = 'connect'; |
||
57 | |||
58 | /** Command action */ |
||
59 | const ACTION_CREATE = 'create'; |
||
60 | |||
61 | /** Argument name */ |
||
62 | const ARG_CLASS = 'class'; |
||
63 | |||
64 | /** |
||
65 | * Constructor. |
||
66 | */ |
||
67 | 3 | public function __construct() |
|
71 | |||
72 | /** |
||
73 | * @inheritdoc |
||
74 | */ |
||
75 | 3 | public function configure() |
|
94 | |||
95 | |||
96 | /** @noinspection PhpMissingParentCallCommonInspection |
||
97 | * @inheritdoc |
||
98 | */ |
||
99 | 2 | public function execute(InputInterface $input, OutputInterface $output) |
|
100 | { |
||
101 | 2 | $inOut = $this->wrapIo($input, $output); |
|
102 | 2 | $container = $this->createContainer($this->getComposer(), static::NAME); |
|
|
|||
103 | |||
104 | 2 | $argAction = static::ARG_ACTION; |
|
105 | 2 | $action = $inOut->getArgument($argAction); |
|
106 | switch ($action) { |
||
107 | 2 | case static::ACTION_CONNECT: |
|
108 | 1 | $this->executeConnect($container, $inOut); |
|
109 | 1 | break; |
|
110 | 1 | case static::ACTION_CREATE: |
|
111 | 1 | $this->executeCreate($container, $inOut); |
|
112 | 1 | break; |
|
113 | default: |
||
114 | $inOut->writeError("Unknown value `$action` for argument `$argAction`." . PHP_EOL); |
||
115 | break; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param ContainerInterface $container |
||
121 | * @param IoInterface $inOut |
||
122 | * |
||
123 | * @return void |
||
124 | */ |
||
125 | 1 | private function executeConnect(ContainerInterface $container, IoInterface $inOut): void |
|
126 | { |
||
127 | 1 | assert($container->has(CommandStorageInterface::class)); |
|
128 | /** @var CommandStorageInterface $commandStorage */ |
||
129 | 1 | $commandStorage = $container->get(CommandStorageInterface::class); |
|
130 | |||
131 | 1 | $commandClasses = []; |
|
132 | 1 | foreach ($commandStorage->getAll() as $commandClass) { |
|
133 | 1 | if (class_exists($commandClass) === false || |
|
134 | 1 | array_key_exists(CommandInterface::class, class_implements($commandClass)) === false |
|
135 | ) { |
||
136 | $inOut->writeWarning("Class `$commandClass` either do not exist or not a command class." . PHP_EOL); |
||
137 | continue; |
||
138 | } |
||
139 | |||
140 | 1 | $commandClasses[] = $this->commandClassToArray($commandClass); |
|
141 | } |
||
142 | |||
143 | 1 | if (empty($commandClasses) === false) { |
|
144 | 1 | $now = date(DATE_RFC2822); |
|
145 | 1 | $data = var_export($commandClasses, true); |
|
146 | $content = <<<EOT |
||
147 | <?php |
||
148 | |||
149 | // THIS FILE IS AUTO GENERATED. DO NOT EDIT IT MANUALLY. |
||
150 | 1 | // Generated at: $now |
|
151 | |||
152 | 1 | return $data; |
|
153 | |||
154 | EOT; |
||
155 | 1 | $cacheFilePath = $this->getCommandsCacheFilePath($this->getComposer()); |
|
156 | 1 | if (empty($cacheFilePath) === true) { |
|
157 | $inOut->writeError("Commands cache file path is not set. Check your `Application` settings." . PHP_EOL); |
||
158 | return; |
||
159 | } |
||
160 | |||
161 | 1 | $this->getFileSystem($container)->write($cacheFilePath, $content); |
|
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param ContainerInterface $container |
||
167 | * @param IoInterface $inOut |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | 1 | private function executeCreate(ContainerInterface $container, IoInterface $inOut): void |
|
172 | { |
||
173 | 1 | $argClass = static::ARG_CLASS; |
|
174 | 1 | if ($inOut->hasArgument($argClass) === false) { |
|
175 | $inOut->writeError("Argument `$argClass` is not provided." . PHP_EOL); |
||
176 | |||
177 | return; |
||
178 | } |
||
179 | 1 | $class = $inOut->getArgument($argClass); |
|
180 | |||
181 | 1 | $fileSystem = $this->getFileSystem($container); |
|
182 | 1 | $settings = $this->getAppSettings($container); |
|
183 | 1 | $commandsFolder = $settings[S::KEY_COMMANDS_FOLDER] ?? null; |
|
184 | 1 | if (empty($commandsFolder) === true || $fileSystem->isFolder($commandsFolder) === false) { |
|
185 | $inOut->writeError( |
||
186 | "Commands folder `$commandsFolder` is not valid. Check your `Application` settings." . PHP_EOL |
||
187 | ); |
||
188 | |||
189 | return; |
||
190 | } |
||
191 | |||
192 | 1 | $classPath = $commandsFolder . DIRECTORY_SEPARATOR . $class . '.php'; |
|
193 | 1 | if (ctype_alpha($class) === false || |
|
194 | 1 | $fileSystem->exists($classPath) === true |
|
195 | ) { |
||
196 | $inOut->writeError( |
||
197 | "Class name `$class` does not look valid for a command. " . |
||
198 | 'Can you please choose another one?' . PHP_EOL |
||
199 | ); |
||
200 | |||
201 | return; |
||
202 | } |
||
203 | |||
204 | 1 | $commandName = strtolower($class); |
|
205 | |||
206 | 1 | $templateContent = $fileSystem->read(__DIR__ . DIRECTORY_SEPARATOR . 'SampleCommand.txt'); |
|
207 | |||
208 | 1 | $tmpContent = $templateContent; |
|
209 | 1 | $tmpContent = str_replace('{CLASS_NAME}', $class, $tmpContent); |
|
210 | 1 | $tmpContent = str_replace('{COMMAND_NAME}', $commandName, $tmpContent); |
|
211 | 1 | $tmpContent = str_replace('{TO_DO}', 'TODO', $tmpContent); |
|
212 | 1 | $content = $tmpContent; |
|
213 | |||
214 | 1 | $fileSystem->write($classPath, $content); |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param ContainerInterface $container |
||
219 | * |
||
220 | * @return array |
||
221 | */ |
||
222 | 1 | private function getAppSettings(ContainerInterface $container): array |
|
232 | |||
233 | /** |
||
234 | * @param ContainerInterface $container |
||
235 | * |
||
236 | * @return FileSystemInterface |
||
237 | */ |
||
238 | 2 | private function getFileSystem(ContainerInterface $container): FileSystemInterface |
|
247 | } |
||
248 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: