From 4feac6466d5c35091fbed13ad26ff8308bae4129 Mon Sep 17 00:00:00 2001 From: Solomon Ochepa Date: Tue, 25 Mar 2025 20:15:38 +0100 Subject: [PATCH 1/5] Create `module()` helper function --- src/helpers.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/helpers.php b/src/helpers.php index af4803001..2547ebafd 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,7 +1,30 @@ has($name)) { + Log::error("Module '$name' not found."); + + return false; + } + + return $status ? $modules->isEnabled($name) : $modules->find($name); + } +} if (! function_exists('module_path')) { function module_path(string $name, string $path = ''): string From f31cef3c026af338ef6ab6cf269f0cdf3c50e27f Mon Sep 17 00:00:00 2001 From: Solomon Ochepa Date: Tue, 25 Mar 2025 20:17:00 +0100 Subject: [PATCH 2/5] Add feature tests for `module()` helper function --- tests/ModuleHelperTest.php | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/ModuleHelperTest.php diff --git a/tests/ModuleHelperTest.php b/tests/ModuleHelperTest.php new file mode 100644 index 000000000..2f1fb2e72 --- /dev/null +++ b/tests/ModuleHelperTest.php @@ -0,0 +1,68 @@ +finder = $this->app['files']; + $this->createModule(); + $this->modulePath = $this->getModuleAppPath(); + } + + protected function tearDown(): void + { + $this->app[RepositoryInterface::class]->delete('Blog'); + parent::tearDown(); + } + + public function test_module_returns_instance_when_exists() + { + $module = module('Blog'); + + $this->assertInstanceOf(Module::class, $module); + $this->assertEquals('Blog', $module->getName()); + } + + public function test_module_returns_false_when_not_found() + { + Log::shouldReceive('error')->once()->with("Module 'Blogs' not found."); + + $this->assertFalse(module('Blogs')); + } + + public function test_module_returns_false_when_not_found_and_status_parameter_is_true() + { + Log::shouldReceive('error')->once()->with("Module 'Blogs' not found."); + + $this->assertFalse(module('Blogs')); + } + + public function test_module_returns_status_when_status_parameter_is_true() + { + $this->assertTrue(module('Blog', true)); + } + + public function test_module_returns_status_when_status_parameter_is_true_and_module_is_disabled() + { + Artisan::call('module:disable Blog'); + $this->assertFalse(module('Blog', true)); + } +} From a6b3dac1be68a43d00734fb1c673107b0e40203b Mon Sep 17 00:00:00 2001 From: Solomon Ochepa Date: Tue, 25 Mar 2025 20:26:01 +0100 Subject: [PATCH 3/5] Create `@module()` blade directive. ## Usage ```blade @module('blog')

Blog module exists!

@endModule ``` --- src/LaravelModulesServiceProvider.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/LaravelModulesServiceProvider.php b/src/LaravelModulesServiceProvider.php index 41ff01f4f..e8e336746 100644 --- a/src/LaravelModulesServiceProvider.php +++ b/src/LaravelModulesServiceProvider.php @@ -7,6 +7,7 @@ use Illuminate\Database\Migrations\Migrator; use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\Console\AboutCommand; +use Illuminate\Support\Facades\Blade; use Illuminate\Translation\Translator; use Nwidart\Modules\Contracts\ActivatorInterface; use Nwidart\Modules\Contracts\RepositoryInterface; @@ -52,6 +53,11 @@ public function register() $this->registerTranslations(); $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'modules'); + + // Create @module() blade directive. + Blade::if('module', function (string $name) { + return module($name, true); + }); } /** From 94a8efa012b2aa9c5c206f63e58f1a304d0ce189 Mon Sep 17 00:00:00 2001 From: Solomon Ochepa Date: Wed, 26 Mar 2025 09:06:00 +0100 Subject: [PATCH 4/5] Refactor module() helper function and Blade directive to improve flexibility and consistency The helper function should return the module status by default instead of the instance, as this is the common usage. --- src/LaravelModulesServiceProvider.php | 10 ++++---- src/helpers.php | 10 ++++---- tests/ModuleHelperTest.php | 35 ++++++++++++++++++--------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/LaravelModulesServiceProvider.php b/src/LaravelModulesServiceProvider.php index e8e336746..fefcaa216 100644 --- a/src/LaravelModulesServiceProvider.php +++ b/src/LaravelModulesServiceProvider.php @@ -38,6 +38,11 @@ public function boot() AboutCommand::add('Laravel-Modules', [ 'Version' => fn () => InstalledVersions::getPrettyVersion('nwidart/laravel-modules'), ]); + + // Create @module() blade directive. + Blade::if('module', function (string $name) { + return module($name); + }); } /** @@ -53,11 +58,6 @@ public function register() $this->registerTranslations(); $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'modules'); - - // Create @module() blade directive. - Blade::if('module', function (string $name) { - return module($name, true); - }); } /** diff --git a/src/helpers.php b/src/helpers.php index 2547ebafd..74005413e 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -7,13 +7,13 @@ if (! function_exists('module')) { /** - * Retrieves a module instance or its status. + * Retrieves a module status or its instance. * * @param string $name The name of the module. - * @param bool $status Whether to return the module's status instead of the instance. Defaults to false. - * @return Module|bool The module instance or its status. + * @param bool $instance Whether to return the module's instance instead of the status. Defaults to false [status]. + * @return bool|Module The module instance or its status. */ - function module(string $name, bool $status = false): Module|bool + function module(string $name, bool $instance = false): bool|Module { $modules = app('modules'); if (! $modules->has($name)) { @@ -22,7 +22,7 @@ function module(string $name, bool $status = false): Module|bool return false; } - return $status ? $modules->isEnabled($name) : $modules->find($name); + return $instance ? $modules->find($name) : $modules->isEnabled($name); } } diff --git a/tests/ModuleHelperTest.php b/tests/ModuleHelperTest.php index 2f1fb2e72..bffe4c076 100644 --- a/tests/ModuleHelperTest.php +++ b/tests/ModuleHelperTest.php @@ -33,36 +33,47 @@ protected function tearDown(): void parent::tearDown(); } - public function test_module_returns_instance_when_exists() + public function test_module_returns_true_when_found() { - $module = module('Blog'); - - $this->assertInstanceOf(Module::class, $module); - $this->assertEquals('Blog', $module->getName()); + $this->assertTrue(module('Blog')); } - public function test_module_returns_false_when_not_found() + public function test_module_returns_false_and_log_error_when_not_found() { Log::shouldReceive('error')->once()->with("Module 'Blogs' not found."); $this->assertFalse(module('Blogs')); } - public function test_module_returns_false_when_not_found_and_status_parameter_is_true() + public function test_module_returns_false_and_log_error_when_not_found_and_instance_parameter_is_true() { Log::shouldReceive('error')->once()->with("Module 'Blogs' not found."); - $this->assertFalse(module('Blogs')); + $this->assertFalse(module('Blogs', true)); + } + + public function test_module_returns_instance_when_instance_parameter_is_true() + { + $module = module('Blog', true); + + $this->assertInstanceOf(Module::class, $module); + $this->assertEquals('Blog', $module->getName()); } - public function test_module_returns_status_when_status_parameter_is_true() + public function test_module_returns_false_when_disabled() { - $this->assertTrue(module('Blog', true)); + Artisan::call('module:disable Blog'); + + $this->assertFalse(module('Blog')); } - public function test_module_returns_status_when_status_parameter_is_true_and_module_is_disabled() + public function test_module_returns_instance_when_disabled_and_instance_parameter_is_true() { Artisan::call('module:disable Blog'); - $this->assertFalse(module('Blog', true)); + + $module = module('Blog', true); + + $this->assertInstanceOf(Module::class, $module); + $this->assertEquals('Blog', $module->getName()); } } From 4bfa1c20483446187114a3e6214e114f542abe39 Mon Sep 17 00:00:00 2001 From: Solomon Ochepa Date: Wed, 26 Mar 2025 09:16:38 +0100 Subject: [PATCH 5/5] [test] Add tests for `@module()` blade directive --- tests/ModuleHelperTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/ModuleHelperTest.php b/tests/ModuleHelperTest.php index bffe4c076..89f7efea8 100644 --- a/tests/ModuleHelperTest.php +++ b/tests/ModuleHelperTest.php @@ -3,6 +3,7 @@ namespace Nwidart\Modules\Tests; use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Log; use Nwidart\Modules\Contracts\RepositoryInterface; use Nwidart\Modules\Laravel\Module; @@ -76,4 +77,20 @@ public function test_module_returns_instance_when_disabled_and_instance_paramete $this->assertInstanceOf(Module::class, $module); $this->assertEquals('Blog', $module->getName()); } + + public function test_module_directive_renders_content_when_module_is_enabled() + { + $blade = "@module('Blog') Enabled @endmodule"; + + $this->assertStringContainsString('Enabled', Blade::render($blade)); + } + + public function test_module_directive_does_not_render_content_when_module_is_disabled() + { + Artisan::call('module:disable Blog'); + + $blade = "@module('Blog') Enabled @endmodule"; + + $this->assertStringNotContainsString('Enabled', Blade::render($blade)); + } }