Skip to content

Commit aff9f18

Browse files
authored
Merge pull request #1865 from Blair2004/v5.0.x
Changelog:
2 parents dbfa0a1 + d251f5c commit aff9f18

File tree

9 files changed

+82
-18
lines changed

9 files changed

+82
-18
lines changed

app/Console/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ protected function schedule( Schedule $schedule )
117117
* We want to make sure Modules Kernel get injected
118118
* on the process so that modules jobs can also be scheduled.
119119
*/
120-
collect( $modules->getEnabled() )->each( function ( $module ) use ( $schedule ) {
120+
collect( $modules->getEnabledAndAutoloadedModules() )->each( function ( $module ) use ( $schedule ) {
121121
$filePath = $module[ 'path' ] . 'Console' . DIRECTORY_SEPARATOR . 'Kernel.php';
122122

123123
if ( is_file( $filePath ) ) {

app/Http/Controllers/UpdateController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function updateDatabase()
2727
return view( 'pages.database.update', [
2828
'title' => __( 'Database Update' ),
2929
'redirect' => session( 'after_update', ns()->route( 'ns.dashboard.home' ) ),
30-
'modules' => collect( $this->modulesService->getEnabled() )->filter( fn( $module ) => count( $module[ 'migrations' ] ) > 0 )->toArray(),
30+
'modules' => collect( $this->modulesService->getEnabledAndAutoloadedModules() )->filter( fn( $module ) => count( $module[ 'migrations' ] ) > 0 )->toArray(),
3131
] );
3232
}
3333

app/Providers/CrudServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function register()
5252
*/
5353
$modulesService = app()->make( ModulesService::class );
5454

55-
$classes = collect( $modulesService->getEnabled() )->map( function ( $module ) use ( $namespace ) {
55+
$classes = collect( $modulesService->getEnabledAndAutoloadedModules() )->map( function ( $module ) use ( $namespace ) {
5656
$classes = Cache::get( 'modules-crud-classes-' . $module[ 'namespace' ], function () use ( $module ) {
5757
$files = collect( Storage::disk( 'ns' )->files( 'modules' . DIRECTORY_SEPARATOR . $module[ 'namespace' ] . DIRECTORY_SEPARATOR . 'Crud' ) );
5858

app/Providers/EventServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ protected function discoverEventsWithin()
4141
*/
4242
$modulesServices = app()->make( ModulesService::class );
4343

44-
$paths = collect( $modulesServices->getEnabled() )->map( function ( $module ) {
45-
return base_path( 'modules' . DIRECTORY_SEPARATOR . $module[ 'namespace' ] . DIRECTORY_SEPARATOR . 'Listeners' );
46-
} )
44+
$paths = $modulesServices->getEnabledAndAutoloadedModules()->map( function ( $module ) {
45+
return base_path( 'modules' . DIRECTORY_SEPARATOR . $module[ 'namespace' ] . DIRECTORY_SEPARATOR . 'Listeners' );
46+
})
4747
->values()
4848
->push( $this->app->path( 'Listeners' ) )
4949
->toArray();

app/Providers/ModulesServiceProvider.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public function boot( ModulesService $modules )
2525
* trigger boot method only for enabled modules
2626
* service providers that extends ModulesServiceProvider.
2727
*/
28-
collect( $modules->getEnabled() )->each( function ( $module ) use ( $modules ) {
29-
$modules->triggerServiceProviders( $module, 'boot', ServiceProvider::class );
30-
} );
28+
collect( $modules->getEnabledAndAutoloadedModules() )
29+
->each( function ( $module ) use ( $modules ) {
30+
$modules->triggerServiceProviders( $module, 'boot', ServiceProvider::class );
31+
} );
3132

3233
$this->commands( $this->modulesCommands );
3334

@@ -58,13 +59,13 @@ public function register()
5859
*/
5960
$this->modules->loadModulesMigrations();
6061

61-
collect( $this->modules->getEnabled() )->each( fn( $module ) => $this->modules->boot( $module ) );
62+
$this->modules->getEnabledAndAutoloadedModules()->each( fn( $module ) => $this->modules->boot( $module ) );
6263

6364
/**
6465
* trigger register method only for enabled modules
6566
* service providers that extends ModulesServiceProvider.
6667
*/
67-
collect( $this->modules->getEnabled() )->each( function ( $module ) {
68+
$this->modules->getEnabledAndAutoloadedModules()->each( function ( $module ) {
6869
/**
6970
* register module commands
7071
*/

app/Providers/SettingsPageProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function boot()
5353
*/
5454
$modulesService = app()->make( ModulesService::class );
5555

56-
$classes = collect( $modulesService->getEnabled() )->map( function ( $module ) use ( $identifier ) {
56+
$classes = collect( $modulesService->getEnabledAndAutoloadedModules() )->map( function ( $module ) use ( $identifier ) {
5757
$classes = Cache::get( 'modules-crud-classes-' . $module[ 'namespace' ], function () use ( $module ) {
5858
$files = collect( Storage::disk( 'ns' )->files( 'modules' . DIRECTORY_SEPARATOR . $module[ 'namespace' ] . DIRECTORY_SEPARATOR . 'Settings' ) );
5959

app/Services/ModulesService.php

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Illuminate\Http\JsonResponse;
2121
use Illuminate\Http\UploadedFile;
2222
use Illuminate\Support\Arr;
23+
use Illuminate\Support\Collection;
2324
use Illuminate\Support\Facades\Artisan;
2425
use Illuminate\Support\Facades\Cache;
2526
use Illuminate\Support\Facades\Log;
@@ -34,6 +35,7 @@
3435
class ModulesService
3536
{
3637
private $modules = [];
38+
private $autoloadedNamespace = [];
3739

3840
private Options $options;
3941

@@ -46,6 +48,8 @@ public function __construct()
4648
* We can only enable a module if the database is installed.
4749
*/
4850
$this->options = app()->make( Options::class );
51+
52+
$this->autoloadedNamespace = explode( ',', env( 'AUTOLOAD_MODULES' ) );
4953
}
5054

5155
/**
@@ -193,6 +197,7 @@ public function __init( string $dir ): void
193197
$config[ 'routes-file' ] = is_file( $webRoutesPath ) ? $webRoutesPath : false;
194198
$config[ 'views-path' ] = $currentModulePath . 'Resources' . DIRECTORY_SEPARATOR . 'Views';
195199
$config[ 'views-relativePath' ] = 'modules' . DIRECTORY_SEPARATOR . ucwords( $config[ 'namespace' ] ) . DIRECTORY_SEPARATOR . 'Views';
200+
$config[ 'autoloaded' ] = in_array( $config[ 'namespace' ], $this->autoloadedNamespace );
196201

197202
/**
198203
* If the system is installed, then we can check if the module is enabled or not
@@ -437,11 +442,11 @@ public function dependenciesCheck( ?array $module = null ): void
437442
*/
438443
public function boot( $module = null ): void
439444
{
440-
if ( ! empty( $module ) && $module[ 'enabled' ] ) {
445+
if ( ! empty( $module ) && ($module[ 'enabled' ] || $module[ 'autoloaded' ] ) ) {
441446
$this->__boot( $module );
442447
} else {
443448
foreach ( $this->modules as $module ) {
444-
if ( ! $module[ 'enabled' ] ) {
449+
if ( ! ( $module[ 'enabled' ] || $module[ 'autoloaded' ] ) ) {
445450
continue;
446451
}
447452
$this->__boot( $module );
@@ -495,6 +500,47 @@ public function getIfEnabled( string $namespace ): bool|array
495500
return $module;
496501
}
497502

503+
/**
504+
* Get all modules that are enabled and all modules
505+
* that are set to autload without repeating them.
506+
* @return Collection
507+
*/
508+
public function getEnabledAndAutoloadedModules()
509+
{
510+
/**
511+
* trigger boot method only for enabled modules
512+
* service providers that extends ModulesServiceProvider.
513+
*/
514+
$autoloadedModulesNamespace = [];
515+
516+
/**
517+
* We might manually set some module
518+
* to always autoload, even if it's disabled.
519+
*/
520+
if ( env( 'AUTOLOAD_MODULES' ) ) {
521+
$autoloadedModulesNamespace = explode( ',', env( 'AUTOLOAD_MODULES' ) );
522+
$autoloadedModulesNamespace = collect( $autoloadedModulesNamespace )->filter( function( $namespace ) {
523+
$module = $this->get( $namespace );
524+
525+
return empty( $module[ 'requires' ] );
526+
})->toArray();
527+
}
528+
529+
/**
530+
* 1 - Get all enabled modules
531+
* 2 - Filter out the modules that are not autoloaded
532+
* 3 - Merge the autoloaded modules with the filtered modules
533+
*/
534+
$result = collect( $this->getEnabled() )
535+
->filter( fn( $module ) => ! in_array( $module[ 'namespace' ], $autoloadedModulesNamespace ) )
536+
->merge(
537+
collect( $this->get() )
538+
->filter( fn( $module ) => in_array( $module[ 'namespace' ], $autoloadedModulesNamespace ) )
539+
);
540+
541+
return $result;
542+
}
543+
498544
/**
499545
* Returns the list of active module as an array
500546
*/
@@ -925,6 +971,10 @@ public function delete( string $namespace ): array
925971
* Check if module exists first
926972
*/
927973
if ( $module = $this->get( $namespace ) ) {
974+
if ( $module[ 'autoloaded' ] ) {
975+
throw new NotAllowedException( sprintf( __( 'The module "%s" is autoloaded and can\'t be deleted.' ), $module[ 'name' ] ) );
976+
}
977+
928978
/**
929979
* Disable the module first
930980
*/
@@ -1108,6 +1158,14 @@ public function enable( string $namespace ): array|JsonResponse
11081158
$this->checkManagementStatus();
11091159

11101160
if ( $module = $this->get( $namespace ) ) {
1161+
if ( $module[ 'autoloaded' ] ) {
1162+
return response()->json([
1163+
'status' => 'error',
1164+
'code' => 'autoloaded_module',
1165+
'message' => sprintf( __( 'The module "%s" is autoloaded and cannot be enabled.' ), $module[ 'name' ] )
1166+
], 403 );
1167+
}
1168+
11111169
/**
11121170
* get all the modules that are
11131171
* enabled.
@@ -1234,6 +1292,10 @@ public function disable( string $namespace ): array
12341292

12351293
// check if module exists
12361294
if ( $module = $this->get( $namespace ) ) {
1295+
if ( $module[ 'autoloaded' ] ) {
1296+
throw new NotAllowedException( sprintf( __( 'The module "%s" is autoloaded and cannot be disabled.' ), $module[ 'name' ] ) );
1297+
}
1298+
12371299
ModulesBeforeDisabledEvent::dispatch( $module );
12381300

12391301
// @todo sandbox to test if the module runs

resources/ts/components/ns-table-row.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<div class="py-1" role="menu" aria-orientation="vertical" aria-labelledby="options-menu">
1414
<template :key="index" v-for="(action,index) of row.$actions">
1515
<a :href="action.url" v-if="action.type === 'GOTO'" class="ns-action-button block px-4 py-2 text-sm leading-5" role="menuitem" v-html="sanitizeHTML( action.label )"></a>
16+
<a :href="action.url" v-if="action.type === 'TAB'" target="_blank" class="ns-action-button block px-4 py-2 text-sm leading-5" role="menuitem" v-html="sanitizeHTML( action.label )"></a>
1617
<a href="javascript:void(0)" @click="triggerAsync( action )" v-if="[ 'GET', 'DELETE', 'POPUP' ].includes( action.type )" class="ns-action-button block px-4 py-2 text-sm leading-5" role="menuitem" v-html="sanitizeHTML( action.label )"></a>
1718
</template>
1819
</div>

resources/ts/pages/dashboard/modules.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@
5454
</p>
5555
</div>
5656
<div class="ns-box-footer border-t p-2 flex justify-between">
57-
<ns-button v-if="! moduleObject.enabled" @click="enableModule( moduleObject )" type="info">{{ __( 'Enable' ) }}</ns-button>
58-
<ns-button v-if="moduleObject.enabled" @click="disableModule( moduleObject )" type="success">{{ __( 'Disable' ) }}</ns-button>
57+
<ns-button :disabled="moduleObject.autoloaded" v-if="! moduleObject.enabled" @click="enableModule( moduleObject )" type="info">{{ __( 'Enable' ) }}</ns-button>
58+
<ns-button :disabled="moduleObject.autoloaded" v-if="moduleObject.enabled" @click="disableModule( moduleObject )" type="success">{{ __( 'Disable' ) }}</ns-button>
5959
<div class="flex -mx-1">
6060
<div class="px-1 flex -mx-1">
6161
<div class="px-1 flex">
62-
<ns-button @click="download( moduleObject )" type="info">
62+
<ns-button :disabled="moduleObject.autoloaded" @click="download( moduleObject )" type="info">
6363
<i class="las la-archive"></i>
6464
</ns-button>
6565
</div>
6666
<div class="px-1 flex">
67-
<ns-button @click="removeModule( moduleObject )" type="error"><i class="las la-trash"></i></ns-button>
67+
<ns-button :disabled="moduleObject.autoloaded" @click="removeModule( moduleObject )" type="error"><i class="las la-trash"></i></ns-button>
6868
</div>
6969
</div>
7070
</div>

0 commit comments

Comments
 (0)