Skip to content

Commit 8097896

Browse files
authored
Merge pull request #2044 from solomon-ochepa/create-module-helper-function
Create `module()` helper function and `@module()` blade directive
2 parents e56a298 + 4bfa1c2 commit 8097896

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

src/LaravelModulesServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Migrations\Migrator;
88
use Illuminate\Filesystem\Filesystem;
99
use Illuminate\Foundation\Console\AboutCommand;
10+
use Illuminate\Support\Facades\Blade;
1011
use Illuminate\Translation\Translator;
1112
use Nwidart\Modules\Contracts\ActivatorInterface;
1213
use Nwidart\Modules\Contracts\RepositoryInterface;
@@ -37,6 +38,11 @@ public function boot()
3738
AboutCommand::add('Laravel-Modules', [
3839
'Version' => fn () => InstalledVersions::getPrettyVersion('nwidart/laravel-modules'),
3940
]);
41+
42+
// Create @module() blade directive.
43+
Blade::if('module', function (string $name) {
44+
return module($name);
45+
});
4046
}
4147

4248
/**

src/helpers.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
<?php
22

33
use Illuminate\Foundation\Vite;
4+
use Illuminate\Support\Facades\Log;
45
use Illuminate\Support\Facades\Vite as ViteFacade;
6+
use Nwidart\Modules\Laravel\Module;
7+
8+
if (! function_exists('module')) {
9+
/**
10+
* Retrieves a module status or its instance.
11+
*
12+
* @param string $name The name of the module.
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.
15+
*/
16+
function module(string $name, bool $instance = false): bool|Module
17+
{
18+
$modules = app('modules');
19+
if (! $modules->has($name)) {
20+
Log::error("Module '$name' not found.");
21+
22+
return false;
23+
}
24+
25+
return $instance ? $modules->find($name) : $modules->isEnabled($name);
26+
}
27+
}
528

629
if (! function_exists('module_path')) {
730
function module_path(string $name, string $path = ''): string

tests/ModuleHelperTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace Nwidart\Modules\Tests;
4+
5+
use Illuminate\Support\Facades\Artisan;
6+
use Illuminate\Support\Facades\Blade;
7+
use Illuminate\Support\Facades\Log;
8+
use Nwidart\Modules\Contracts\RepositoryInterface;
9+
use Nwidart\Modules\Laravel\Module;
10+
11+
class ModuleHelperTest extends BaseTestCase
12+
{
13+
/**
14+
* @var \Illuminate\Filesystem\Filesystem
15+
*/
16+
private $finder;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $modulePath;
22+
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
$this->finder = $this->app['files'];
27+
$this->createModule();
28+
$this->modulePath = $this->getModuleAppPath();
29+
}
30+
31+
protected function tearDown(): void
32+
{
33+
$this->app[RepositoryInterface::class]->delete('Blog');
34+
parent::tearDown();
35+
}
36+
37+
public function test_module_returns_true_when_found()
38+
{
39+
$this->assertTrue(module('Blog'));
40+
}
41+
42+
public function test_module_returns_false_and_log_error_when_not_found()
43+
{
44+
Log::shouldReceive('error')->once()->with("Module 'Blogs' not found.");
45+
46+
$this->assertFalse(module('Blogs'));
47+
}
48+
49+
public function test_module_returns_false_and_log_error_when_not_found_and_instance_parameter_is_true()
50+
{
51+
Log::shouldReceive('error')->once()->with("Module 'Blogs' not found.");
52+
53+
$this->assertFalse(module('Blogs', true));
54+
}
55+
56+
public function test_module_returns_instance_when_instance_parameter_is_true()
57+
{
58+
$module = module('Blog', true);
59+
60+
$this->assertInstanceOf(Module::class, $module);
61+
$this->assertEquals('Blog', $module->getName());
62+
}
63+
64+
public function test_module_returns_false_when_disabled()
65+
{
66+
Artisan::call('module:disable Blog');
67+
68+
$this->assertFalse(module('Blog'));
69+
}
70+
71+
public function test_module_returns_instance_when_disabled_and_instance_parameter_is_true()
72+
{
73+
Artisan::call('module:disable Blog');
74+
75+
$module = module('Blog', true);
76+
77+
$this->assertInstanceOf(Module::class, $module);
78+
$this->assertEquals('Blog', $module->getName());
79+
}
80+
81+
public function test_module_directive_renders_content_when_module_is_enabled()
82+
{
83+
$blade = "@module('Blog') Enabled @endmodule";
84+
85+
$this->assertStringContainsString('Enabled', Blade::render($blade));
86+
}
87+
88+
public function test_module_directive_does_not_render_content_when_module_is_disabled()
89+
{
90+
Artisan::call('module:disable Blog');
91+
92+
$blade = "@module('Blog') Enabled @endmodule";
93+
94+
$this->assertStringNotContainsString('Enabled', Blade::render($blade));
95+
}
96+
}

0 commit comments

Comments
 (0)