diff --git a/src/Contracts/RepositoryInterface.php b/src/Contracts/RepositoryInterface.php index 3741dbeee..37acd59bc 100644 --- a/src/Contracts/RepositoryInterface.php +++ b/src/Contracts/RepositoryInterface.php @@ -14,13 +14,6 @@ interface RepositoryInterface */ public function all(); - /** - * Get cached modules. - * - * @return array - */ - public function getCached(); - /** * Scan & get all available modules. * diff --git a/src/FileRepository.php b/src/FileRepository.php index 5a0ae7991..1adf5e704 100644 --- a/src/FileRepository.php +++ b/src/FileRepository.php @@ -67,6 +67,8 @@ abstract class FileRepository implements Countable, RepositoryInterface */ private $cache; + private static $modules = []; + /** * The constructor. * @@ -140,6 +142,10 @@ abstract protected function createModule(...$args); */ public function scan() { + if (! empty(self::$modules) && ! $this->app->runningUnitTests()) { + return self::$modules; + } + $paths = $this->getScanPaths(); $modules = []; @@ -150,13 +156,16 @@ public function scan() is_array($manifests) || $manifests = []; foreach ($manifests as $manifest) { - $name = Json::make($manifest)->get('name'); + $json = Json::make($manifest); + $name = $json->get('name'); - $modules[$name] = $this->createModule($this->app, $name, dirname($manifest)); + $modules[strtolower($name)] = $this->createModule($this->app, $name, dirname($manifest)); } } - return $modules; + self::$modules = $modules; + + return self::$modules; } /** @@ -164,42 +173,7 @@ public function scan() */ public function all(): array { - if (! $this->config('cache.enabled')) { - return $this->scan(); - } - - return $this->formatCached($this->getCached()); - } - - /** - * Format the cached data as array of modules. - * - * @param array $cached - * @return array - */ - protected function formatCached($cached) - { - $modules = []; - - foreach ($cached as $name => $module) { - $path = $module['path']; - - $modules[$name] = $this->createModule($this->app, $name, $path); - } - - return $modules; - } - - /** - * Get cached modules. - * - * @return array - */ - public function getCached() - { - return $this->cache->store($this->config->get('modules.cache.driver'))->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () { - return $this->toCollection()->toArray(); - }); + return $this->scan(); } /** @@ -232,7 +206,7 @@ public function getByStatus($status): array */ public function has($name): bool { - return array_key_exists($name, $this->all()); + return array_key_exists(strtolower($name), $this->all()); } /** @@ -316,12 +290,7 @@ public function boot(): void */ public function find(string $name) { - foreach ($this->all() as $module) { - if ($module->getLowerName() === strtolower($name)) { - return $module; - } - } - + return $this->all()[strtolower($name)] ?? null; } /** @@ -578,4 +547,11 @@ public function setStubPath($stubPath) return $this; } + + public function resetModules(): static + { + self::$modules = []; + + return $this; + } } diff --git a/src/Generators/ModuleGenerator.php b/src/Generators/ModuleGenerator.php index 2cf399bb3..8d4c70979 100644 --- a/src/Generators/ModuleGenerator.php +++ b/src/Generators/ModuleGenerator.php @@ -278,8 +278,6 @@ public function generate(): int { $name = $this->getName(); - Event::dispatch(sprintf('modules.%s.%s', strtolower($name), ModuleEvent::CREATING)); - if ($this->module->has($name)) { if ($this->force) { $this->module->delete($name); @@ -289,6 +287,9 @@ public function generate(): int return E_ERROR; } } + + Event::dispatch(sprintf('modules.%s.%s', strtolower($name), ModuleEvent::CREATING)); + $this->component->info("Creating module: [$name]"); $this->generateFolders(); diff --git a/src/Json.php b/src/Json.php index ff1d1a0cd..357eff2e7 100644 --- a/src/Json.php +++ b/src/Json.php @@ -136,7 +136,7 @@ public function decodeContents() */ public function getAttributes() { - return $this->decodeContents(); + return $this->attributes ?? $this->decodeContents(); } /** diff --git a/src/Traits/CanClearModulesCache.php b/src/Traits/CanClearModulesCache.php index 52c9db5a4..b6ea2e499 100644 --- a/src/Traits/CanClearModulesCache.php +++ b/src/Traits/CanClearModulesCache.php @@ -12,5 +12,7 @@ public function clearCache() if (config('modules.cache.enabled') === true) { app('cache')->forget(config('modules.cache.key')); } + + $this->laravel['modules']->resetModules(); } } diff --git a/tests/Commands/Actions/ModuleDeleteCommandTest.php b/tests/Commands/Actions/ModuleDeleteCommandTest.php index faba96e00..59bca661b 100644 --- a/tests/Commands/Actions/ModuleDeleteCommandTest.php +++ b/tests/Commands/Actions/ModuleDeleteCommandTest.php @@ -30,6 +30,14 @@ public function setUp(): void $this->activator = new FileActivator($this->app); } + public function tearDown(): void + { + $this->artisan('module:delete', ['--all' => true, '--force' => true]); + + $this->activator->reset(); + parent::tearDown(); + } + public function test_it_can_delete_a_module_from_disk(): void { $this->artisan('module:make', ['name' => ['WrongModule']]);