From ee2d8d04e04198d7b65d0dbe1792a210b72e1ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Le=C3=B3n?= <56183278+Sleon4@users.noreply.github.com> Date: Fri, 18 Oct 2024 08:01:17 -0500 Subject: [PATCH] refactor: command to display data in tables is refactored (#93) --- lion | 19 ++- .../Lion/Schedule/UpScheduleCommand.php | 125 ++++++++++-------- storage/logs/cron/.gitignore | 0 .../Lion/Schedule/UpScheduleCommandTest.php | 81 +++++++++--- 4 files changed, 143 insertions(+), 82 deletions(-) create mode 100644 storage/logs/cron/.gitignore diff --git a/lion b/lion index 38190f48..020793bc 100644 --- a/lion +++ b/lion @@ -110,7 +110,22 @@ Mailer::initialize([ * ----------------------------------------------------------------------------- **/ -(new CommandHandler('Lion-Bundle')) - ->registerCommands('./src/LionBundle/Commands/Lion/', 'Lion\\Bundle\\Commands\\Lion\\', 'Commands/Lion/') +$commandHandler = (new CommandHandler('Lion-Bundle')); + +$commandHandler->registerCommands( + './src/LionBundle/Commands/Lion/', + 'Lion\\Bundle\\Commands\\Lion\\', + 'Commands/Lion/' +); + +if ((new Store())->exist('./app/Console/Commands/')) { + $commandHandler->registerCommands( + './app/Console/Commands/', + 'App\\Console\\Commands\\', + 'Commands/' + ); +} + +$commandHandler ->getApplication() ->run(); diff --git a/src/LionBundle/Commands/Lion/Schedule/UpScheduleCommand.php b/src/LionBundle/Commands/Lion/Schedule/UpScheduleCommand.php index 46c09089..013e2ef2 100644 --- a/src/LionBundle/Commands/Lion/Schedule/UpScheduleCommand.php +++ b/src/LionBundle/Commands/Lion/Schedule/UpScheduleCommand.php @@ -8,93 +8,94 @@ use Lion\Bundle\Helpers\Commands\Schedule\Schedule; use Lion\Bundle\Interface\ScheduleInterface; use Lion\Command\Command; -use Lion\Command\Kernel; use Lion\Dependency\Injection\Container; use Lion\Files\Store; -use Lion\Helpers\Arr; use Lion\Helpers\Str; +use LogicException; +use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Helper\TableSeparator; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; /** * Add scheduled tasks in crontab * - * @property ClassFactory $classFactory [ClassFactory class object] - * @property Container $container [Container class object] - * @property Store $store [Store class object] - * @property Kernel $kernel [Kernel class object] - * @property Str $str [Str class object] - * @property Arr $arr [Arr class object] + * @property ClassFactory $classFactory [Fabricates the data provided to + * manipulate information (folder, class, namespace)] + * @property Container $container [Container to generate dependency injection] + * @property Store $store [Manipulate system files] + * @property Str $str [Modify and construct strings with different formats] * * @package Lion\Bundle\Commands\Lion\Schedule */ class UpScheduleCommand extends Command { /** - * [ClassFactory class object] + * [Fabricates the data provided to manipulate information (folder, class, + * namespace)] * * @var ClassFactory $classFactory */ private ClassFactory $classFactory; /** - * [Container class object] + * [Container to generate dependency injection] * * @var Container $container */ private Container $container; /** - * [Store class object] + * [Manipulate system files] * * @var Store $store */ private Store $store; /** - * [Kernel class object] - * - * @var Kernel $kernel - */ - private Kernel $kernel; - - /** - * [Str class object] + * [Modify and construct strings with different formats] * * @var Str $str */ private Str $str; /** - * [Arr class object] - * - * @var Arr $arr + * @required */ - private Arr $arr; + public function setClassFactory(ClassFactory $classFactory): UpScheduleCommand + { + $this->classFactory = $classFactory; + + return $this; + } /** * @required */ - public function setInject( - ClassFactory $classFactory, - Container $container, - Store $store, - Kernel $kernel, - Str $str, - Arr $arr - ): UpScheduleCommand { - $this->classFactory = $classFactory; - + public function setContainer(Container $container): UpScheduleCommand + { $this->container = $container; + return $this; + } + + /** + * @required + */ + public function setStore(Store $store): UpScheduleCommand + { $this->store = $store; - $this->kernel = $kernel; + return $this; + } + /** + * @required + */ + public function setStr(Str $str): UpScheduleCommand + { $this->str = $str; - $this->arr = $arr; - return $this; } @@ -123,11 +124,9 @@ protected function configure(): void * @param OutputInterface $output [OutputInterface is the interface * implemented by all Output classes] * - * @return int 0 if everything went fine, or an exit code - * - * @throws LogicException When this abstract method is not implemented + * @return int [0 if everything went fine, or an exit code] * - * @see setCode() + * @throws LogicException [When this abstract method is not implemented] */ protected function execute(InputInterface $input, OutputInterface $output): int { @@ -161,8 +160,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return Command::SUCCESS; } - /** @var array $commands */ - $commands = []; + $data = []; foreach ($files as $scheduleInterface) { $schedule = new Schedule(); @@ -192,8 +190,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** @var Command $commandObject */ $commandObject = new $config['command']; - $output->writeln($this->warningOutput("\t>> SCHEDULE: {$config['command']}")); - $command = "{$config['cron']} cd {$_ENV['CRONTAB_PROJECT_PATH']}"; $command .= " && {$_ENV['CRONTAB_PHP_PATH']} {$_ENV['CRONTAB_PROJECT_PATH']}lion {$commandObject->getName()}"; @@ -202,26 +198,39 @@ protected function execute(InputInterface $input, OutputInterface $output): int $command .= " >> {$_ENV['CRONTAB_PROJECT_PATH']}storage/logs/cron/{$config['logName']}.log 2>&1"; + $data[] = [ + $config['command'], + $this->infoOutput($command), + ]; + + $data[] = new TableSeparator(); $this->store->folder($this->store->normalizePath('./storage/logs/cron/')); - $this->classFactory - ->create( - $config['logName'], - ClassFactory::LOG_EXTENSION, - $this->store->normalizePath('./storage/logs/cron/') - ) - ->close(); + if (isError($this->store->exist($this->store->normalizePath('./storage/logs/cron/') . $config['logName']))) { + $this->classFactory + ->create( + $config['logName'], + ClassFactory::LOG_EXTENSION, + $this->store->normalizePath('./storage/logs/cron/') + ) + ->close(); + } + } - $this->classFactory->classFactory( - $this->store->normalizePath('./storage/logs/cron/'), - $config['logName'] - ); + $output->writeln($this->warningOutput("\t>> CRONTAB PATH: " . env('CRONTAB_PATH') . 'crontab')); - $commands[] = trim($command); - } + $output->writeln($this->infoOutput("\t>> CRON: sudo service cron status")); + + $output->writeln($this->infoOutput("\t>> CRON: sudo service cron stop")); + + $output->writeln($this->infoOutput("\t>> CRON: sudo service cron start")); - $this->kernel->execute('(echo "' . $this->arr->of($commands)->join("\n") . '") | crontab -', false); + (new Table($output)) + ->setHeaderTitle($this->successOutput(' CRONTAB ')) + ->setHeaders(['CLASS', 'CRON']) + ->setRows($data) + ->render(); return Command::SUCCESS; } diff --git a/storage/logs/cron/.gitignore b/storage/logs/cron/.gitignore new file mode 100644 index 00000000..e69de29b diff --git a/tests/Commands/Lion/Schedule/UpScheduleCommandTest.php b/tests/Commands/Lion/Schedule/UpScheduleCommandTest.php index 8f6a39cf..0a9283e3 100644 --- a/tests/Commands/Lion/Schedule/UpScheduleCommandTest.php +++ b/tests/Commands/Lion/Schedule/UpScheduleCommandTest.php @@ -7,44 +7,80 @@ use Lion\Bundle\Commands\Lion\New\CommandsCommand; use Lion\Bundle\Commands\Lion\New\CronCommand; use Lion\Bundle\Commands\Lion\Schedule\UpScheduleCommand; +use Lion\Bundle\Helpers\Commands\ClassFactory; use Lion\Bundle\Interface\ScheduleInterface; use Lion\Command\Command; -use Lion\Command\Kernel; use Lion\Dependency\Injection\Container; +use Lion\Files\Store; +use Lion\Helpers\Str; use Lion\Test\Test; +use PHPUnit\Framework\Attributes\Test as Testing; +use Symfony\Component\Console\Application; use Symfony\Component\Console\Tester\CommandTester; class UpScheduleCommandTest extends Test { - const URL_PATH = './app/Console/Cron/'; - const URL_PATH_COMMAND = './app/Console/Commands/'; - const NAMESPACE_CLASS = 'App\\Console\\Cron\\'; - const CLASS_NAME = 'TestCron'; - const CLASS_NAME_COMMAND = 'ExampleCommand'; - const OBJECT_NAME = self::NAMESPACE_CLASS . self::CLASS_NAME; - const FILE_NAME = self::CLASS_NAME . '.php'; - const FILE_NAME_COMMAND = self::CLASS_NAME_COMMAND . '.php'; - const OUTPUT_MESSAGE = 'cron has been generated'; - const OUTPUT_MESSAGE_COMMAND = 'command has been generated'; - const CONFIGURE_OUTPUT_MESSAGE = "SCHEDULE: App\Console\Commands\ExampleCommand"; - - private CommandTester $commandTester; + private const string URL_PATH = './app/Console/Cron/'; + private const string URL_PATH_COMMAND = './app/Console/Commands/'; + private const string NAMESPACE_CLASS = 'App\\Console\\Cron\\'; + private const string CLASS_NAME = 'TestCron'; + private const string CLASS_NAME_COMMAND = 'ExampleCommand'; + private const string OBJECT_NAME = self::NAMESPACE_CLASS . self::CLASS_NAME; + private const string FILE_NAME = self::CLASS_NAME . '.php'; + private const string FILE_NAME_COMMAND = self::CLASS_NAME_COMMAND . '.php'; + private const string OUTPUT_MESSAGE = 'cron has been generated'; + private const string OUTPUT_MESSAGE_COMMAND = 'command has been generated'; + private const string CONFIGURE_OUTPUT_MESSAGE = 'App\Console\Commands\ExampleCommand'; + + private CommandTester $commandTesterCron; private CommandTester $commandTesterCommand; private CommandTester $commandTesterUp; + private UpScheduleCommand $upScheduleCommand; protected function setUp(): void { - $application = (new Kernel())->getApplication(); + $application = new Application(); + + $application->add( + (new CommandsCommand()) + ->setClassFactory( + (new ClassFactory()) + ->setStore(new Store()) + ) + ->setStore(new Store()) + ); + + $application->add( + (new CronCommand()) + ->setClassFactory( + (new ClassFactory()) + ->setStore(new Store()) + ) + ->setStore(new Store()) + ); - $application->add((new Container())->injectDependencies(new CommandsCommand())); - $application->add((new Container())->injectDependencies(new CronCommand())); - $application->add((new Container())->injectDependencies(new UpScheduleCommand())); + $this->upScheduleCommand = new UpScheduleCommand(); + + $application->add( + $this->upScheduleCommand + ->setClassFactory( + (new ClassFactory()) + ->setStore(new Store()) + ) + ->setContainer(new Container()) + ->setStore(new Store()) + ->setStr(new Str()) + ); $this->commandTesterCommand = new CommandTester($application->find('new:command')); - $this->commandTester = new CommandTester($application->find('new:cron')); + + $this->commandTesterCron = new CommandTester($application->find('new:cron')); + $this->commandTesterUp = new CommandTester($application->find('schedule:up')); $this->createDirectory(self::URL_PATH); + + $this->initReflection($this->upScheduleCommand); } protected function tearDown(): void @@ -52,7 +88,8 @@ protected function tearDown(): void $this->rmdirRecursively('./app/'); } - public function testExecute(): void + #[Testing] + public function execute(): void { $this->assertSame( Command::SUCCESS, @@ -61,8 +98,8 @@ public function testExecute(): void $this->assertStringContainsString(self::OUTPUT_MESSAGE_COMMAND, $this->commandTesterCommand->getDisplay()); $this->assertFileExists(self::URL_PATH_COMMAND . self::FILE_NAME_COMMAND); - $this->assertSame(Command::SUCCESS, $this->commandTester->execute(['cron' => self::CLASS_NAME])); - $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); + $this->assertSame(Command::SUCCESS, $this->commandTesterCron->execute(['cron' => self::CLASS_NAME])); + $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTesterCron->getDisplay()); $this->assertFileExists(self::URL_PATH . self::FILE_NAME); /** @var ScheduleInterface $cronObject */