Skip to content

Create module() helper function and @module() blade directive #2044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/LaravelModulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,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);
});
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
<?php

use Illuminate\Foundation\Vite;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Vite as ViteFacade;
use Nwidart\Modules\Laravel\Module;

if (! function_exists('module')) {
/**
* Retrieves a module status or its instance.
*
* @param string $name The name of the module.
* @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 $instance = false): bool|Module
{
$modules = app('modules');
if (! $modules->has($name)) {
Log::error("Module '$name' not found.");

return false;
}

return $instance ? $modules->find($name) : $modules->isEnabled($name);
}
}

if (! function_exists('module_path')) {
function module_path(string $name, string $path = ''): string
Expand Down
96 changes: 96 additions & 0 deletions tests/ModuleHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

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;

class ModuleHelperTest extends BaseTestCase
{
/**
* @var \Illuminate\Filesystem\Filesystem
*/
private $finder;

/**
* @var string
*/
private $modulePath;

protected function setUp(): void
{
parent::setUp();
$this->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_true_when_found()
{
$this->assertTrue(module('Blog'));
}

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_and_log_error_when_not_found_and_instance_parameter_is_true()
{
Log::shouldReceive('error')->once()->with("Module 'Blogs' not found.");

$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_false_when_disabled()
{
Artisan::call('module:disable Blog');

$this->assertFalse(module('Blog'));
}

public function test_module_returns_instance_when_disabled_and_instance_parameter_is_true()
{
Artisan::call('module:disable Blog');

$module = module('Blog', true);

$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));
}
}