Skip to content

Commit 94a8efa

Browse files
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.
1 parent a6b3dac commit 94a8efa

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

src/LaravelModulesServiceProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public function boot()
3838
AboutCommand::add('Laravel-Modules', [
3939
'Version' => fn () => InstalledVersions::getPrettyVersion('nwidart/laravel-modules'),
4040
]);
41+
42+
// Create @module() blade directive.
43+
Blade::if('module', function (string $name) {
44+
return module($name);
45+
});
4146
}
4247

4348
/**
@@ -53,11 +58,6 @@ public function register()
5358
$this->registerTranslations();
5459

5560
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'modules');
56-
57-
// Create @module() blade directive.
58-
Blade::if('module', function (string $name) {
59-
return module($name, true);
60-
});
6161
}
6262

6363
/**

src/helpers.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
if (! function_exists('module')) {
99
/**
10-
* Retrieves a module instance or its status.
10+
* Retrieves a module status or its instance.
1111
*
1212
* @param string $name The name of the module.
13-
* @param bool $status Whether to return the module's status instead of the instance. Defaults to false.
14-
* @return Module|bool The module instance or its status.
13+
* @param bool $instance Whether to return the module's instance instead of the status. Defaults to false [status].
14+
* @return bool|Module The module instance or its status.
1515
*/
16-
function module(string $name, bool $status = false): Module|bool
16+
function module(string $name, bool $instance = false): bool|Module
1717
{
1818
$modules = app('modules');
1919
if (! $modules->has($name)) {
@@ -22,7 +22,7 @@ function module(string $name, bool $status = false): Module|bool
2222
return false;
2323
}
2424

25-
return $status ? $modules->isEnabled($name) : $modules->find($name);
25+
return $instance ? $modules->find($name) : $modules->isEnabled($name);
2626
}
2727
}
2828

tests/ModuleHelperTest.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,47 @@ protected function tearDown(): void
3333
parent::tearDown();
3434
}
3535

36-
public function test_module_returns_instance_when_exists()
36+
public function test_module_returns_true_when_found()
3737
{
38-
$module = module('Blog');
39-
40-
$this->assertInstanceOf(Module::class, $module);
41-
$this->assertEquals('Blog', $module->getName());
38+
$this->assertTrue(module('Blog'));
4239
}
4340

44-
public function test_module_returns_false_when_not_found()
41+
public function test_module_returns_false_and_log_error_when_not_found()
4542
{
4643
Log::shouldReceive('error')->once()->with("Module 'Blogs' not found.");
4744

4845
$this->assertFalse(module('Blogs'));
4946
}
5047

51-
public function test_module_returns_false_when_not_found_and_status_parameter_is_true()
48+
public function test_module_returns_false_and_log_error_when_not_found_and_instance_parameter_is_true()
5249
{
5350
Log::shouldReceive('error')->once()->with("Module 'Blogs' not found.");
5451

55-
$this->assertFalse(module('Blogs'));
52+
$this->assertFalse(module('Blogs', true));
53+
}
54+
55+
public function test_module_returns_instance_when_instance_parameter_is_true()
56+
{
57+
$module = module('Blog', true);
58+
59+
$this->assertInstanceOf(Module::class, $module);
60+
$this->assertEquals('Blog', $module->getName());
5661
}
5762

58-
public function test_module_returns_status_when_status_parameter_is_true()
63+
public function test_module_returns_false_when_disabled()
5964
{
60-
$this->assertTrue(module('Blog', true));
65+
Artisan::call('module:disable Blog');
66+
67+
$this->assertFalse(module('Blog'));
6168
}
6269

63-
public function test_module_returns_status_when_status_parameter_is_true_and_module_is_disabled()
70+
public function test_module_returns_instance_when_disabled_and_instance_parameter_is_true()
6471
{
6572
Artisan::call('module:disable Blog');
66-
$this->assertFalse(module('Blog', true));
73+
74+
$module = module('Blog', true);
75+
76+
$this->assertInstanceOf(Module::class, $module);
77+
$this->assertEquals('Blog', $module->getName());
6778
}
6879
}

0 commit comments

Comments
 (0)