|
4 | 4 |
|
5 | 5 | namespace Tempest\Http\Static; |
6 | 6 |
|
| 7 | +use RecursiveDirectoryIterator; |
| 8 | +use RecursiveIteratorIterator; |
| 9 | +use SplFileInfo; |
7 | 10 | use Tempest\Console\Console; |
8 | 11 | use Tempest\Console\ConsoleCommand; |
9 | 12 | use Tempest\Console\HasConsole; |
10 | | -use Tempest\Container\Container; |
| 13 | +use Tempest\Console\Middleware\CautionMiddleware; |
| 14 | +use Tempest\Console\Middleware\ForceMiddleware; |
11 | 15 | use Tempest\Core\Kernel; |
12 | | -use Tempest\Http\DataProvider; |
13 | | -use function Tempest\path; |
14 | | -use function Tempest\uri; |
15 | 16 |
|
16 | 17 | final readonly class StaticCleanCommand |
17 | 18 | { |
|
20 | 21 | public function __construct( |
21 | 22 | private Console $console, |
22 | 23 | private Kernel $kernel, |
23 | | - private Container $container, |
24 | | - private StaticPageConfig $staticPageConfig, |
25 | 24 | ) { |
26 | 25 | } |
27 | 26 |
|
28 | 27 | #[ConsoleCommand( |
29 | | - name: 'static:clean' |
| 28 | + name: 'static:clean', |
| 29 | + middleware: [ForceMiddleware::class, CautionMiddleware::class] |
30 | 30 | )] |
31 | 31 | public function __invoke(): void |
32 | 32 | { |
33 | | - $publicPath = path($this->kernel->root, 'public'); |
| 33 | + /** @var SplFileInfo[] $files */ |
| 34 | + $files = []; |
34 | 35 |
|
35 | | - foreach ($this->staticPageConfig->staticPages as $staticPage) { |
36 | | - /** @var DataProvider $dataProvider */ |
37 | | - $dataProvider = $this->container->get($staticPage->dataProviderClass ?? GenericDataProvider::class); |
| 36 | + $directoryIterator = new RecursiveDirectoryIterator($this->kernel->root . '/public'); |
38 | 37 |
|
39 | | - foreach ($dataProvider->provide() as $params) { |
40 | | - $uri = parse_url(uri($staticPage->handler, ...$params), PHP_URL_PATH); |
41 | | - |
42 | | - $file = path($publicPath, $uri . '/index.html'); |
| 38 | + /** @var SplFileInfo $file */ |
| 39 | + foreach (new RecursiveIteratorIterator($directoryIterator) as $file) { |
| 40 | + if ($file->getExtension() === 'html') { |
| 41 | + $files[] = $file; |
| 42 | + } |
| 43 | + } |
43 | 44 |
|
44 | | - if (! file_exists($file)) { |
45 | | - continue; |
46 | | - } |
| 45 | + foreach ($files as $file) { |
| 46 | + unlink($file->getPathname()); |
47 | 47 |
|
48 | | - unlink($file); |
| 48 | + $pathName = str_replace('\\', '/', $file->getPathname()); |
49 | 49 |
|
50 | | - $this->writeln("- <u>{$file}</u> removed"); |
51 | | - } |
| 50 | + $this->writeln("- <u>{$pathName}</u> removed"); |
52 | 51 | } |
53 | 52 |
|
54 | 53 | $this->success('Done'); |
|
0 commit comments