Skip to content

Commit bec78e4

Browse files
committed
Correcting DemoCommand issue by introducing new runCommandExternally() method and applying overall refactoring
1 parent 2cd081a commit bec78e4

3 files changed

Lines changed: 93 additions & 27 deletions

File tree

shared/Commands/DemoCommand.php

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
use Quantum\Libraries\Database\Database;
2727
use Bluemmb\Faker\PicsumPhotosProvider;
2828
use Quantum\Di\Exceptions\DiException;
29+
use Symfony\Component\Process\Process;
2930
use Quantum\Console\QtCommand;
3031
use Ottaviano\Faker\Gravatar;
3132
use ReflectionException;
3233
use Shared\Enums\Role;
3334
use Faker\Generator;
3435
use ErrorException;
3536
use Faker\Factory;
36-
use Exception;
3737

3838
/**
3939
* Class DemoCommand
@@ -146,6 +146,7 @@ public function exec()
146146
foreach ($steps as $step) {
147147
$this->updateProgress($progress, $step['message']);
148148
$step['action']();
149+
$progress->advance();
149150
}
150151

151152
$progress->finish();
@@ -219,17 +220,15 @@ private function initProgressBar(int $steps): ProgressBar
219220
* @param string $moduleName
220221
* @param string $template
221222
* @param bool $withAssets
222-
* @return void
223-
* @throws ExceptionInterface
224223
*/
225224
private function createModule(string $moduleName, string $template, bool $withAssets): void
226225
{
227-
$this->runExternalCommand(self::COMMANDS['module_generate'], [
226+
$this->runCommandExternally(self::COMMANDS['module_generate'], [
228227
'module' => $moduleName,
229228
'--yes' => true,
230229
'--template' => $template,
231230
'--with-assets' => $withAssets
232-
]);
231+
], true);
233232
}
234233

235234
/**
@@ -248,7 +247,7 @@ private function generateUsers(): array
248247
$users = [];
249248
for ($i = 0; $i < self::COUNTS['users']; $i++) {
250249
$user = $this->generateUserData();
251-
$this->runExternalCommand(self::COMMANDS['user_create'], $user);
250+
$this->runCommandInternally(self::COMMANDS['user_create'], $user);
252251
$users[] = $user;
253252
}
254253
return $users;
@@ -273,7 +272,7 @@ private function generatePosts(array $users): array
273272
foreach ($users as $user) {
274273
for ($i = 0; $i < self::COUNTS['posts_per_user']; $i++) {
275274
$post = $this->generatePostData($user);
276-
$this->runExternalCommand(self::COMMANDS['post_create'], $post);
275+
$this->runCommandInternally(self::COMMANDS['post_create'], $post);
277276
$posts[] = $post;
278277
}
279278
}
@@ -294,7 +293,7 @@ private function generateComments(array $users, array $posts): void
294293
for ($i = 0; $i < self::COUNTS['comments_per_post']; $i++) {
295294
$commentUser = $this->getRandomUserExcept($users, $post['user_uuid']);
296295

297-
$this->runExternalCommand(self::COMMANDS['comment_create'], [
296+
$this->runCommandInternally(self::COMMANDS['comment_create'], [
298297
'post_uuid' => $post['uuid'],
299298
'user_uuid' => $commentUser['uuid'],
300299
'content' => textCleanUp($this->faker->realText(rand(20, 100))),
@@ -316,7 +315,7 @@ private function generateComments(array $users, array $posts): void
316315
private function generateUserData(): array
317316
{
318317
$userUuid = $this->faker->uuid();
319-
$email = textCleanUp($this->faker->email());
318+
$email = $this->faker->safeEmail();
320319

321320
create_user_directory($userUuid);
322321

@@ -327,19 +326,19 @@ private function generateUserData(): array
327326
);
328327

329328
return [
330-
'uuid' => $userUuid,
329+
'email' => $email,
330+
'password' => self::DEFAULT_PASSWORD,
331331
'firstname' => $this->faker->name(),
332332
'lastname' => $this->faker->lastName(),
333+
'uuid' => $userUuid,
333334
'role' => Role::EDITOR,
334-
'email' => $email,
335-
'password' => self::DEFAULT_PASSWORD,
336335
'image' => $imageName
337336
];
338337
}
339338

340339
/**
341340
* Generates data for post
342-
* @param $user
341+
* @param array $user
343342
* @return array
344343
* @throws BaseException
345344
* @throws ConfigException
@@ -348,7 +347,7 @@ private function generateUserData(): array
348347
* @throws HttpClientException
349348
* @throws ReflectionException
350349
*/
351-
private function generatePostData($user): array
350+
private function generatePostData(array $user): array
352351
{
353352
$postUuid = $this->faker->uuid();
354353
$title = textCleanUp($this->faker->realText(50));
@@ -360,11 +359,11 @@ private function generatePostData($user): array
360359
);
361360

362361
return [
363-
'uuid' => $postUuid,
364362
'title' => $title,
365363
'description' => textCleanUp($this->faker->realText(1000)),
366-
'image' => $imageName,
367364
'user_uuid' => $user['uuid'],
365+
'uuid' => $postUuid,
366+
'image' => $imageName,
368367
];
369368
}
370369

@@ -393,25 +392,25 @@ private function updateProgress(ProgressBar $progress, string $message): void
393392
{
394393
$progress->setMessage($message, 'item');
395394
$progress->display();
396-
$progress->advance();
397395
}
398396

399397
/**
400398
* Rebuilds the database
399+
* @return void
401400
* @throws ExceptionInterface
402401
*/
403402
private function rebuildDatabase(): void
404403
{
405404
switch (Database::getInstance()->getConfigs()['driver']) {
406405
case 'mysql':
407-
$this->runExternalCommand(self::COMMANDS['migrate'], ['direction' => 'down']);
408-
$this->runExternalCommand(self::COMMANDS['migrate'], ['direction' => 'up']);
406+
$this->runCommandInternally(self::COMMANDS['migrate'], ['direction' => 'down']);
407+
$this->runCommandInternally(self::COMMANDS['migrate'], ['direction' => 'up']);
409408
break;
410409

411410
case 'sleekdb':
412-
$this->runExternalCommand(self::COMMANDS['comment_delete'], ['--yes' => true]);
413-
$this->runExternalCommand(self::COMMANDS['post_delete'], ['--yes' => true]);
414-
$this->runExternalCommand(self::COMMANDS['user_delete'], ['--yes' => true]);
411+
$this->runCommandInternally(self::COMMANDS['comment_delete'], ['--yes' => true]);
412+
$this->runCommandInternally(self::COMMANDS['post_delete'], ['--yes' => true]);
413+
$this->runCommandInternally(self::COMMANDS['user_delete'], ['--yes' => true]);
415414
break;
416415
}
417416
}
@@ -452,13 +451,78 @@ private function resetDatabase(): void
452451
}
453452

454453
/**
455-
* Runs an external command
456-
* @throws Exception
454+
* @param string $commandName
455+
* @param array $arguments
457456
* @throws ExceptionInterface
458457
*/
459-
private function runExternalCommand(string $commandName, ?array $arguments = [])
458+
private function runCommandInternally(string $commandName, array $arguments = [])
460459
{
461460
$command = $this->getApplication()->find($commandName);
462461
$command->run(new ArrayInput($arguments), new NullOutput);
463462
}
463+
464+
/**
465+
* @param string $commandName
466+
* @param array $arguments
467+
*/
468+
private function runCommandExternally(string $commandName, array $arguments = [])
469+
{
470+
$command = $this->buildCommand($commandName, $arguments);
471+
$this->runExternalProcess($command);
472+
}
473+
474+
/**
475+
* Runs separate process
476+
* @param array $command
477+
* @return void
478+
*/
479+
private function runExternalProcess(array $command): void
480+
{
481+
$process = new Process($command, base_dir());
482+
$process->setTimeout(null);
483+
484+
$process->mustRun();
485+
}
486+
487+
/**
488+
* Builds command string
489+
* @param string $commandName
490+
* @param array $arguments
491+
* @return string[]
492+
*/
493+
private function buildCommand(string $commandName, array $arguments): array
494+
{
495+
$command = ['php', 'qt', $commandName];
496+
497+
foreach ($arguments as $key => $value) {
498+
499+
if (!is_int($key) && !str_starts_with($key, '--')) {
500+
$command[] = (string)$value;
501+
continue;
502+
}
503+
504+
if (is_bool($value)) {
505+
if ($value) $command[] = $key;
506+
continue;
507+
}
508+
509+
if (is_array($value)) {
510+
foreach ($value as $item) {
511+
$command[] = $key;
512+
$command[] = (string)$item;
513+
}
514+
continue;
515+
}
516+
517+
$command[] = $key;
518+
519+
if ($value === null) {
520+
continue;
521+
}
522+
523+
$command[] = (string)$value;
524+
}
525+
526+
return $command;
527+
}
464528
}

shared/resources/lang/en/common.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
// ───── About framework ───────────────────────────────────
66
'description' => 'Very fast and extremely simple, next generation PHP MVC framework with modular structure.',
7-
'about_framework' => 'Quantum is a free, open-source PHP web framework under MIT license...',
7+
'about_framework' => 'Quantum is a free, open-source PHP web framework under MIT license, specially designed to develop very fast web applications with modular structure. With Quantum it’s easy to start any kind of project immediately, whether it\'s regular websites or complex API based service, at the same time it allows to keep the code clean, organized through all the development process.',
88
'version' => 'Version',
99
'current_version' => 'Current version is: {%1}',
10+
'cli_commands' => 'Quantum CLI Commands',
1011

1112
// ───── Navigation ─────────────────────────────────────────
1213
'home' => 'Home',

shared/resources/lang/es/common.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
// ───── About framework ───────────────────────────────────
66
'description' => 'Framework MVC en PHP de nueva generación, muy rápido y extremadamente simple, con una estructura modular.',
7-
'about_framework' => 'Quantum es un framework web en PHP gratuito y de código abierto bajo la licencia MIT...',
7+
'about_framework' => 'Quantum es un framework web PHP gratuito y de código abierto bajo la licencia MIT, diseñado especialmente para desarrollar aplicaciones web muy rápidas con una estructura modular. Con Quantum es fácil iniciar de inmediato cualquier tipo de proyecto, ya sean sitios web tradicionales o servicios complejos basados en API, y al mismo tiempo permite mantener el código limpio y organizado durante todo el proceso de desarrollo.',
88
'version' => 'Versión',
99
'current_version' => 'La versión actual es: {%1}',
10+
'cli_commands' => 'Comandos CLI de Quantum',
1011

1112
// ───── Navigation ─────────────────────────────────────────
1213
'home' => 'Inicio',

0 commit comments

Comments
 (0)