From b26b54488170e1046ebd985f8884aab722c8dd4e Mon Sep 17 00:00:00 2001 From: Ali Sasani Date: Thu, 19 Sep 2024 20:40:39 +0330 Subject: [PATCH 1/4] [feat] inject activator class into manifest --- src/LaravelModulesServiceProvider.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/LaravelModulesServiceProvider.php b/src/LaravelModulesServiceProvider.php index ee124f5f..f325e075 100644 --- a/src/LaravelModulesServiceProvider.php +++ b/src/LaravelModulesServiceProvider.php @@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Event; use Illuminate\Translation\Translator; use Nwidart\Modules\Constants\ModuleEvent; +use Nwidart\Modules\Contracts\ActivatorInterface; use Nwidart\Modules\Contracts\RepositoryInterface; use Nwidart\Modules\Exceptions\InvalidActivatorClass; use Nwidart\Modules\Support\Stub; @@ -30,7 +31,8 @@ public function boot() fn () => new ModuleManifest( new Filesystem(), app(Contracts\RepositoryInterface::class)->getScanPaths(), - $this->getCachedModulePath() + $this->getCachedModulePath(), + app(ActivatorInterface::class) ) ); From 7b25dda0cca5294c8e1170b36441605d27e18bb7 Mon Sep 17 00:00:00 2001 From: Ali Sasani Date: Thu, 19 Sep 2024 20:41:39 +0330 Subject: [PATCH 2/4] [feat] update hasStatus of Activator, to accept name of module --- src/Activators/FileActivator.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Activators/FileActivator.php b/src/Activators/FileActivator.php index 9cf3403e..c669d3a0 100644 --- a/src/Activators/FileActivator.php +++ b/src/Activators/FileActivator.php @@ -2,7 +2,6 @@ namespace Nwidart\Modules\Activators; -use Illuminate\Cache\CacheManager; use Illuminate\Config\Repository as Config; use Illuminate\Container\Container; use Illuminate\Contracts\Filesystem\FileNotFoundException; @@ -86,13 +85,15 @@ public function disable(Module $module): void /** * {@inheritDoc} */ - public function hasStatus(Module $module, bool $status): bool + public function hasStatus(Module|string $module, bool $status): bool { - if (! isset($this->modulesStatuses[$module->getName()])) { + $name = $module instanceof Module ? $module->getName() : $module; + + if (! isset($this->modulesStatuses[$name])) { return $status === false; } - return $this->modulesStatuses[$module->getName()] === $status; + return $this->modulesStatuses[$name] === $status; } /** From 704eea8267ea48dce0698b4ca2f75ba0bea80f4e Mon Sep 17 00:00:00 2001 From: Ali Sasani Date: Thu, 19 Sep 2024 20:42:21 +0330 Subject: [PATCH 3/4] [feat] update manifest create, filter disable modules --- src/ModuleManifest.php | 82 ++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/src/ModuleManifest.php b/src/ModuleManifest.php index 7d9c60a3..be7aee5b 100644 --- a/src/ModuleManifest.php +++ b/src/ModuleManifest.php @@ -4,6 +4,8 @@ use Exception; use Illuminate\Filesystem\Filesystem; +use Illuminate\Support\Collection; +use Nwidart\Modules\Contracts\ActivatorInterface; class ModuleManifest { @@ -12,7 +14,7 @@ class ModuleManifest * * @var \Illuminate\Filesystem\Filesystem */ - public $files; + private $files; /** * The base path. @@ -28,25 +30,36 @@ class ModuleManifest */ public $manifestPath; + /** + * The manifestData + */ + private static ?Collection $manifestData; + /** * The loaded manifest array. * * @var array */ - public $manifest; + private $manifest; + + /** + * module activator class + */ + private ActivatorInterface $activator; /** * Create a new package manifest instance. * - * @param \Illuminate\Support\Collection $paths + * @param Collection $paths * @param string $manifestPath * @return void */ - public function __construct(Filesystem $files, $paths, $manifestPath) + public function __construct(Filesystem $files, $paths, $manifestPath, ActivatorInterface $activator) { $this->files = $files; $this->paths = collect($paths); $this->manifestPath = $manifestPath; + $this->activator = $activator; } /** @@ -88,7 +101,7 @@ public function aliases() public function config($key) { return collect($this->getManifest())->flatMap(function ($configuration) use ($key) { - return (array) ($configuration[$key] ?? []); + return (array)($configuration[$key] ?? []); })->filter()->all(); } @@ -116,19 +129,9 @@ protected function getManifest() * * @return void */ - public function build() + public function build(): void { - $providers = $this->paths - ->flatMap(function ($path) { - $manifests = $this->files->glob("{$path}/module.json"); - is_array($manifests) || $manifests = []; - - return collect($manifests) - ->map(function ($manifest) { - return $this->files->json($manifest); - }); - }) - ->sortBy(fn ($module) => $module['priority'] ?? 0) + $providers = $this->getModulesData() ->pluck('providers') ->flatten() ->filter() @@ -150,7 +153,7 @@ public function build() * * @throws \Exception */ - protected function write(array $manifest) + protected function write(array $manifest): void { if (! is_writable($dirname = dirname($this->manifestPath))) { throw new Exception("The {$dirname} directory must be present and writable."); @@ -164,29 +167,40 @@ protected function write(array $manifest) public function registerFiles(): void { //todo check this section store on module.php or not? - $this->paths + $this->getModulesData() + ->each(function (array $manifest) { + if (empty($manifest['files'])) { + return; + } + + foreach ($manifest['files'] as $file) { + include_once $manifest['module_directory'].DIRECTORY_SEPARATOR.$file; + } + }); + } + + public function getModulesData(): Collection + { + if (! empty(self::$manifestData)) { + return self::$manifestData; + } + + self::$manifestData = $this->paths ->flatMap(function ($path) { $manifests = $this->files->glob("{$path}/module.json"); is_array($manifests) || $manifests = []; return collect($manifests) ->map(function ($manifest) { - $json = $this->files->json($manifest); - - if (! empty($json['files'])) { - return [ - 'module_directory' => dirname($manifest), - ...$this->files->json($manifest), - ]; - } + return [ + 'module_directory' => dirname($manifest), + ...$this->files->json($manifest), + ]; }); }) - ->filter() - ->sortBy(fn ($module) => $module['priority'] ?? 0) - ->each(function (array $manifest) { - foreach ($manifest['files'] as $file) { - include_once $manifest['module_directory'].DIRECTORY_SEPARATOR.$file; - } - }); + ->filter(fn ($module) => $this->activator->hasStatus($module['name'], true)) + ->sortBy(fn ($module) => $module['priority'] ?? 0); + + return self::$manifestData; } } From f5ac56bdd5bd267391c197c014e5042c80fb7f2d Mon Sep 17 00:00:00 2001 From: Ali Sasani Date: Thu, 19 Sep 2024 21:50:45 +0330 Subject: [PATCH 4/4] [test] fix test --- src/ModuleManifest.php | 2 +- tests/LaravelModulesServiceProviderTest.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ModuleManifest.php b/src/ModuleManifest.php index be7aee5b..b5eb6914 100644 --- a/src/ModuleManifest.php +++ b/src/ModuleManifest.php @@ -181,7 +181,7 @@ public function registerFiles(): void public function getModulesData(): Collection { - if (! empty(self::$manifestData)) { + if (! empty(self::$manifestData) && ! app()->runningUnitTests()) { return self::$manifestData; } diff --git a/tests/LaravelModulesServiceProviderTest.php b/tests/LaravelModulesServiceProviderTest.php index 45cf6790..14b0e672 100644 --- a/tests/LaravelModulesServiceProviderTest.php +++ b/tests/LaravelModulesServiceProviderTest.php @@ -25,6 +25,8 @@ public function test_it_throws_exception_if_config_is_invalid() $this->app['config']->set('modules.activators.file', ['class' => null]); + app()->forgetInstance(ActivatorInterface::class); + $this->assertInstanceOf(ActivatorInterface::class, app(ActivatorInterface::class)); } }