1 | <?php namespace Limoncello\Commands; |
||
48 | class CommandsCommand extends BaseCommand |
||
49 | { |
||
50 | use CommandTrait, CommandSerializationTrait, CacheFilePathTrait; |
||
51 | |||
52 | /** |
||
53 | * Command name. |
||
54 | */ |
||
55 | const NAME = 'l:commands'; |
||
56 | |||
57 | /** Argument name */ |
||
58 | const ARG_ACTION = 'action'; |
||
59 | |||
60 | /** Command action */ |
||
61 | const ACTION_CONNECT = 'connect'; |
||
62 | |||
63 | /** Command action */ |
||
64 | const ACTION_CREATE = 'create'; |
||
65 | |||
66 | /** Argument name */ |
||
67 | const ARG_CLASS = 'class'; |
||
68 | |||
69 | /** |
||
70 | * Constructor. |
||
71 | */ |
||
72 | 7 | public function __construct() |
|
76 | |||
77 | /** |
||
78 | * @inheritdoc |
||
79 | */ |
||
80 | 7 | public function configure() |
|
99 | |||
100 | |||
101 | /** @noinspection PhpMissingParentCallCommonInspection |
||
102 | * @inheritdoc |
||
103 | * |
||
104 | * @throws ReflectionException |
||
105 | */ |
||
106 | 6 | public function execute(InputInterface $input, OutputInterface $output) |
|
125 | |||
126 | /** |
||
127 | * @param ContainerInterface $container |
||
128 | * @param IoInterface $inOut |
||
129 | * |
||
130 | * @return void |
||
131 | * |
||
132 | * @throws ContainerExceptionInterface |
||
133 | * @throws NotFoundExceptionInterface |
||
134 | */ |
||
135 | 1 | private function executeConnect(ContainerInterface $container, IoInterface $inOut): void |
|
136 | { |
||
137 | 1 | assert($container->has(CommandStorageInterface::class)); |
|
138 | /** @var CommandStorageInterface $commandStorage */ |
||
139 | 1 | $commandStorage = $container->get(CommandStorageInterface::class); |
|
140 | |||
141 | 1 | $commandClasses = []; |
|
142 | 1 | foreach ($commandStorage->getAll() as $commandClass) { |
|
143 | 1 | if (class_exists($commandClass) === false || |
|
144 | 1 | array_key_exists(CommandInterface::class, class_implements($commandClass)) === false |
|
145 | ) { |
||
146 | 1 | $inOut->writeWarning("Class `$commandClass` either do not exist or not a command class." . PHP_EOL); |
|
147 | 1 | continue; |
|
148 | } |
||
149 | |||
150 | $inOut->writeInfo("Found command class `$commandClass`." . PHP_EOL, IoInterface::VERBOSITY_VERBOSE); |
||
151 | |||
152 | $commandClasses[] = $this->commandClassToArray($commandClass); |
||
153 | } |
||
154 | |||
155 | 1 | if (empty($commandClasses) === false) { |
|
156 | $now = date(DATE_RFC2822); |
||
157 | $data = var_export($commandClasses, true); |
||
158 | $content = <<<EOT |
||
159 | <?php |
||
160 | |||
161 | // THIS FILE IS AUTO GENERATED. DO NOT EDIT IT MANUALLY. |
||
162 | // Generated at: $now |
||
163 | |||
164 | return $data; |
||
165 | |||
166 | EOT; |
||
167 | $cacheFilePath = $this->getCommandsCacheFilePath($this->getComposer()); |
||
168 | if (empty($cacheFilePath) === true) { |
||
169 | $inOut->writeError("Commands cache file path is not set. Check your `Application` settings." . PHP_EOL); |
||
170 | |||
171 | return; |
||
172 | } |
||
173 | |||
174 | $this->getFileSystem($container)->write($cacheFilePath, $content); |
||
175 | |||
176 | $inOut->writeInfo('Commands connected.' . PHP_EOL); |
||
177 | |||
178 | return; |
||
179 | } |
||
180 | |||
181 | 1 | $inOut->writeWarning('No commands found.' . PHP_EOL); |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param ContainerInterface $container |
||
186 | * @param IoInterface $inOut |
||
187 | * |
||
188 | * @return void |
||
189 | * |
||
190 | * @throws ContainerExceptionInterface |
||
191 | * @throws NotFoundExceptionInterface |
||
192 | */ |
||
193 | 4 | private function executeCreate(ContainerInterface $container, IoInterface $inOut): void |
|
241 | |||
242 | /** |
||
243 | * @param ContainerInterface $container |
||
244 | * |
||
245 | * @return string |
||
246 | * |
||
247 | * @throws ContainerExceptionInterface |
||
248 | * @throws NotFoundExceptionInterface |
||
249 | */ |
||
250 | 3 | private function getCommandsFolder(ContainerInterface $container): string |
|
261 | |||
262 | /** |
||
263 | * @param ContainerInterface $container |
||
264 | * |
||
265 | * @return FileSystemInterface |
||
266 | * |
||
267 | * @throws ContainerExceptionInterface |
||
268 | * @throws NotFoundExceptionInterface |
||
269 | */ |
||
270 | 3 | private function getFileSystem(ContainerInterface $container): FileSystemInterface |
|
279 | } |
||
280 |
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: