1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace LaravelUi5\OData\Console;
6:
7: use Illuminate\Console\Command;
8: use LaravelUi5\OData\Console\Concerns\ResolvesServices;
9: use LaravelUi5\OData\Service\Cache\EdmxLoader;
10: use LaravelUi5\OData\Service\Contracts\ODataServiceRegistryInterface;
11:
12: class ClearCommand extends Command
13: {
14: use ResolvesServices;
15:
16: protected $signature = 'odata:clear {--class= : Comma-separated FQCNs of additional OData services to clear}';
17:
18: protected $description = 'Remove cached Edm PHP classes for OData services — the registry, plus any --class services (dev only)';
19:
20: public function handle(ODataServiceRegistryInterface $registry): int
21: {
22: if (app()->environment('production', 'staging')) {
23: $this->error('odata:clear must not be run in production or staging.');
24: $this->error('The generated Edm/ cache is committed to version control and deployed as-is.');
25: $this->error('Run odata:clear on your development machine only.');
26:
27: return self::FAILURE;
28: }
29:
30: $services = $this->resolveServices($registry);
31:
32: if ($services === null) {
33: return self::FAILURE;
34: }
35:
36: foreach ($services as $service) {
37: $cacheDir = EdmxLoader::cacheDir($service);
38:
39: if (!is_dir($cacheDir)) {
40: continue;
41: }
42:
43: $this->deleteDirectory($cacheDir);
44: $this->info("Cleared: {$cacheDir}");
45: }
46:
47: $this->info('OData cache cleared.');
48:
49: return self::SUCCESS;
50: }
51:
52: private function deleteDirectory(string $dir): void
53: {
54: $items = new \RecursiveIteratorIterator(
55: new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS),
56: \RecursiveIteratorIterator::CHILD_FIRST,
57: );
58:
59: foreach ($items as $item) {
60: $item->isDir() ? rmdir($item->getPathname()) : unlink($item->getPathname());
61: }
62:
63: rmdir($dir);
64: }
65: }
66: