-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: config command with build and clear cache config (#331)
* feat: config command with build and clear cache config * change command description
- Loading branch information
1 parent
97c89c5
commit 6fae4d1
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
config.php |