Skip to content

Commit c091d66

Browse files
committed
Cache: add option to recreate folder after purging [closes #39]
1 parent 1dd823a commit c091d66

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎src/Command/Cache/CachePurgeCommand.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Contributte\Console\Extra\Command\Cache;
44

55
use Contributte\Console\Extra\Utils\Files;
6+
use Contributte\Console\Extra\Utils\Utils;
67
use Symfony\Component\Console\Attribute\AsCommand;
78
use Symfony\Component\Console\Command\Command;
89
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Input\InputOption;
911
use Symfony\Component\Console\Output\OutputInterface;
1012
use Symfony\Component\Console\Style\SymfonyStyle;
1113

@@ -29,6 +31,11 @@ public function __construct(array $dirs)
2931
$this->dirs = $dirs;
3032
}
3133

34+
protected function configure(): void
35+
{
36+
$this->addOption('recreate', null, InputOption::VALUE_OPTIONAL, 'Recreate folders', false);
37+
}
38+
3239
protected function execute(InputInterface $input, OutputInterface $output): int
3340
{
3441
$style = new SymfonyStyle($input, $output);
@@ -37,6 +44,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3744
foreach ($this->dirs as $directory) {
3845
$style->text(sprintf('Purging: %s', $directory));
3946
Files::purge($directory);
47+
48+
if (Utils::boolenize($input->getOption('recreate'))) {
49+
$style->text(sprintf('Recreating: %s', $directory));
50+
Files::mkdir($directory);
51+
}
4052
}
4153

4254
$style->success(sprintf('Purging done. Total %d folders purged.', count($this->dirs)));

‎src/Utils/Files.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Contributte\Console\Extra\Utils;
44

5+
use Contributte\Console\Extra\Exception\LogicalException;
56
use Contributte\Console\Extra\Exception\RuntimeException;
67
use RecursiveDirectoryIterator;
78
use RecursiveIteratorIterator;
@@ -10,6 +11,13 @@
1011
class Files
1112
{
1213

14+
public static function mkdir(string $dir, int $mode = 0777): void
15+
{
16+
if (!is_dir($dir) && !@mkdir($dir, $mode, recursive: true) && !is_dir($dir)) { // @ - dir may already exist
17+
throw new LogicalException(sprintf('Directory "%s" was not created with mode %s', $dir, $mode));
18+
}
19+
}
20+
1321
/**
1422
* @param string[] $ignored
1523
*/

‎src/Utils/Utils.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ public static function numerize(mixed $input): int
2929
throw new LogicalException(sprintf('Cannot numerize %s', gettype($input)));
3030
}
3131

32+
public static function boolenize(mixed $input): bool
33+
{
34+
if (is_scalar($input)) {
35+
return filter_var($input, FILTER_VALIDATE_BOOLEAN);
36+
}
37+
38+
throw new LogicalException(sprintf('Cannot boolenize %s', gettype($input)));
39+
}
40+
3241
}

‎tests/Cases/Command/Latte/LatteWarmupCommand.phpt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ require_once __DIR__ . '/../../../bootstrap.php';
1717
Toolkit::test(function (): void {
1818
$templateFactory = new TemplateFactory(
1919
new class implements LatteFactory {
20+
2021
public function create(): Engine
2122
{
2223
$latte = new Engine();
2324
$latte->setTempDirectory(Environment::getTestDir());
2425

2526
return $latte;
2627
}
28+
2729
}
2830
);
2931

0 commit comments

Comments
 (0)