Skip to content

Commit

Permalink
feat: config command with build and clear cache config (#331)
Browse files Browse the repository at this point in the history
* feat: config command with build and clear cache config

* change command description
  • Loading branch information
SonyPradana committed Jun 20, 2024
1 parent 97c89c5 commit 6fae4d1
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/System/Integrate/Console/ConfigCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace System\Integrate\Console;

use System\Console\Command;
use System\Integrate\Application;
use System\Integrate\Bootstrap\ConfigProviders;
use System\Integrate\ConfigRepository;

use function System\Console\fail;
use function System\Console\ok;

class ConfigCommand extends Command
{
/**
* Register command.
*
* @var array<int, array<string, mixed>>
*/
public static array $command = [
[
'pattern' => 'config:cache',
'fn' => [self::class, 'main'],
], [
'pattern' => 'config:clear',
'fn' => [self::class, 'clear'],
],
];

/**
* @return array<string, array<string, string|string[]>>
*/
public function printHelp()
{
return [
'commands' => [
'config:cache' => 'Build cache application config',
'config:clear' => 'Remove cached application config',
],
'options' => [],
'relation' => [],
];
}

public function main(): int
{
$app = Application::getIntance();
(new ConfigProviders())->bootstrap($app);

$this->clear();
$config = $app->get(ConfigRepository::class)->toArray();
$cached_config = '<?php return ' . var_export($config, true) . ';' . PHP_EOL;
if (file_put_contents($app->getApplicationCachePath() . 'config.php', $cached_config)) {
ok('Config file has successfully created.')->out();

return 0;
}
fail('Cant build config cache.')->out();

return 1;
}

public function clear(): int
{
if (file_exists($file = Application::getIntance()->getApplicationCachePath() . 'config.php')) {
@unlink($file);
ok('Clear config file has successfully.')->out();

return 0;
}

return 1;
}
}
63 changes: 63 additions & 0 deletions tests/Integrate/Commands/ConfigCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace System\Integrate\Commands;

use PHPUnit\Framework\TestCase;
use System\Integrate\Application;
use System\Integrate\Console\ConfigCommand;

class ConfigCommandTest extends TestCase
{
protected function tearDown(): void
{
// tests\Integrate\bootsrap\cache\config.php
if (file_exists($file = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'bootsrap' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'config.php')) {
@unlink($file);
}
}

/**
* @test
*/
public function itCanCreateConfigFile()
{
$app = new Application(dirname(__DIR__) . DIRECTORY_SEPARATOR);

$app->setConfigPath(DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR);

$command = new ConfigCommand([]);

ob_start();
$status = $command->main();
$out = ob_get_clean();

$this->assertEquals(0, $status);
$this->assertStringContainsString('Config file has successfully created.', $out);

$app->flush();
}

/**
* @test
*/
public function itCanRemoveConfigFile()
{
$app = new Application(dirname(__DIR__) . DIRECTORY_SEPARATOR);

$app->setConfigPath(DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR);

$command = new ConfigCommand([]);

ob_start();
$command->main();
$status = $command->clear();
$out = ob_get_clean();

$this->assertEquals(0, $status);
$this->assertStringContainsString('Config file has successfully created.', $out);

$app->flush();
}
}
1 change: 1 addition & 0 deletions tests/Integrate/bootsrap/cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.php

0 comments on commit 6fae4d1

Please sign in to comment.