Skip to content

Commit

Permalink
refactor: command to display data in tables is refactored (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleon4 authored Oct 18, 2024
1 parent c347ead commit ee2d8d0
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 82 deletions.
19 changes: 17 additions & 2 deletions lion
Original file line number Diff line number Diff line change
Expand Up @@ -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();
125 changes: 67 additions & 58 deletions src/LionBundle/Commands/Lion/Schedule/UpScheduleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -161,8 +160,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

/** @var array<string> $commands */
$commands = [];
$data = [];

foreach ($files as $scheduleInterface) {
$schedule = new Schedule();
Expand Down Expand Up @@ -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()}";
Expand All @@ -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;
}
Expand Down
Empty file added storage/logs/cron/.gitignore
Empty file.
81 changes: 59 additions & 22 deletions tests/Commands/Lion/Schedule/UpScheduleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,89 @@
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
{
$this->rmdirRecursively('./app/');
}

public function testExecute(): void
#[Testing]
public function execute(): void
{
$this->assertSame(
Command::SUCCESS,
Expand All @@ -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 */
Expand Down

0 comments on commit ee2d8d0

Please sign in to comment.