Skip to content

Commit

Permalink
feat: view command clear and cache (compailer)
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Dec 1, 2023
1 parent 2a28eb6 commit f8efb33
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/System/Integrate/Console/ViewCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace System\Integrate\Console;

use System\Console\Command;
use System\Console\Traits\PrintHelpTrait;

use function System\Console\ok;
use function System\Console\warn;

class ViewCommand extends Command
{
use PrintHelpTrait;

/**
* Register command.
*
* @var array<int, array<string, mixed>>
*/
public static $command = [
[
'pattern' => 'view:cache',
'fn' => [ViewCommand::class, 'cache'],
], [
'pattern' => 'view:clear',
'fn' => [ViewCommand::class, 'clear'],
],
];

/**
* @return array<string, array<string, string|string[]>>
*/
public function printHelp()
{
return [
'commands' => [
'view:cache' => 'Create all templator template (optimize)',
'view:clear' => 'Clear all cached view file',
],
'options' => [],
'relation' => [],
];
}

public function cache(): int
{
return 0;
}

public function clear(): int
{
warn('Clear cache file in `{cache_path()}`.')->out(false);
$files = glob(cache_path() . '/*.php');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}

ok('Finish clear cache.')->out();

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

declare(strict_types=1);

namespace System\Test\Integrate\Commands;

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

final class ViewCommandsTest extends TestCase
{
/**
* @test
*/
public function itCanCompileFromTemplatorFiles(): void
{
$this->markTestIncomplete('Wait new PR for handle tempaltor::compailer.');
$view_command = new ViewCommand(['php', 'cli', 'view:cache']);
ob_start();
$exit = $view_command->cache();
ob_get_clean();
$this->assertEquals(0, $exit);
}

/**
* @test
*/
public function itCanClearCompiledViewFile(): void
{
// tests\Integrate\Commands\assets\view_cache
(new Application(''))->setCachePath(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'view_cache' . DIRECTORY_SEPARATOR);

file_put_contents(cache_path() . 'test01.php', '');
file_put_contents(cache_path() . 'test02.php', '');
$view_command = new ViewCommand(['php', 'cli', 'view:clear']);
ob_start();
$exit = $view_command->clear();
ob_get_clean();
$this->assertEquals(0, $exit);
}
}
1 change: 1 addition & 0 deletions tests/Integrate/Commands/assets/view_cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.php

0 comments on commit f8efb33

Please sign in to comment.