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