Skip to content

Commit

Permalink
fix multi-level
Browse files Browse the repository at this point in the history
- fix providers loader
- add main service provider
  • Loading branch information
MoamenEltouny committed Feb 10, 2022
1 parent c39af0f commit f68ef7a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Core/Commands/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function handle()
$this->path = module_path($this->name);

$this->nameNS = str_replace('/', '\\', $this->name);
// $this->nameNS = str_replace('-', '', $this->name);
$this->name = Str::studly($this->name);
$this->slug = studlyToSlug($this->name);

Expand All @@ -46,7 +47,6 @@ public function handle()
if (file_exists($this->tmp = $base . 'module-tmp'))
File::deleteDirectory($this->tmp);


if (File::copyDirectory($base . 'module', $this->tmp)) {
$this->comment('Creating a new module [' . $this->name . ']');

Expand Down
28 changes: 28 additions & 0 deletions src/Core/Commands/stubs/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Modules;

use Pharaonic\Laravel\Modulator\Core\ServiceProvider as PharaonicServiceProvider;

class ServiceProvider extends PharaonicServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Modules\{{ module-name-namespace }}\Providers;

use Pharaonic\Laravel\Modulator\Core\ServiceProvider;
use App\Modules\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -30,6 +30,7 @@ public function register()
*/
public function boot()
{
parent::boot();
//
}
}
4 changes: 2 additions & 2 deletions src/Core/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ protected function loadConfig()
if (file_exists($config = module_config_path(static::$module))) {
foreach (getFiles($config) as $file) {
$this->mergeConfigFrom(
module_config_path(static::$module) . DIRECTORY_SEPARATOR . $file,
'modules.' . studlyToSlug(static::$module) . '.' . str_replace('.php', '', $file)
$file,
'modules.' . studlyToSlug(static::$module) . '.' . str_replace('.php', '', $file->getRelativePathname())
);
}
}
Expand Down
37 changes: 13 additions & 24 deletions src/ModulatorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,32 +134,21 @@ public function console()
*/
protected function registerProviders()
{
foreach (modules() as $module) {
$this->loadModuleProviders($module);
}
}
$modules = modules();

/**
* Load the providers of a single module.
*
* @param string $module
* @param string|null $subModule
* @return void
*/
private function loadModuleProviders(string $module, ?string $subModule = null)
{
$path = $subModule ? $subModule . '/Providers' : 'Providers';
$namespace = 'App\Modules\\' . str_replace('/', '\\', $module) . ($subModule ? '\\' . $subModule : null) . '\Providers\\';
if (count($modules) > 0) {
// CREATE MAIN SERVICE PROVIDER
if (!file_exists($SP = app_path('Modules/ServiceProvider.php')))
File::copy(
str_replace('/', DIRECTORY_SEPARATOR, __DIR__ . '/Core/Commands/stubs/ServiceProvider.php'),
$SP
);

if (!file_exists($path = module_path($module, $path))) {
foreach (File::directories(module_path($module)) as $sm) {
$module = trim(str_replace(module_path(), '', $sm), DIRECTORY_SEPARATOR);
$this->loadModuleProviders($module, basename($subModule));
}
} else {
foreach (getFiles($path) as $provider) {
$provider = $namespace . substr($provider, 0, -4);
$this->app->register($provider);
foreach ($modules as $module) {
foreach (getFiles($path = module_path($module, 'Providers')) as $provider) {
$provider = str_replace($path . '/', '', substr($provider, 0, -4));
$this->app->register('App\Modules\\' . str_replace('/', '\\', $module) . '\Providers\\' . $provider);
}
}
}
}
Expand Down
38 changes: 34 additions & 4 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;


// Get Files List
if (!function_exists('getFiles')) {
function getFiles(string $dir)
{
if (!file_exists($dir)) return [];
return array_values(array_diff(scandir($dir), ['.', '..']));
return File::files($dir);
}
}

Expand All @@ -33,12 +33,42 @@ function module_path(?string $module = null, ?string $extra = null)
}
}

// Get Directories List of a module
if (!function_exists('getModuleDirectories')) {
function getModuleDirectories(string $module)
{
$dir = module_path($module);
if (!File::exists($dir)) return [];

$mainModulesPath = module_path();
return array_map(function ($m) use ($mainModulesPath) {
return str_replace($mainModulesPath . '/', '', $m);
}, File::directories($dir));
}
}

// Modules List
if (!function_exists('modules')) {
/**
* Get all modules names.
*
* @return array
*/
function modules()
{
$dir = module_path();
return file_exists($dir) ? getFiles($dir) : [];
if (!file_exists($dir = module_path())) return [];

$list = [];
foreach (File::directories($dir) as $module) {
$module = str_replace($dir . '/', '', $module);
if (file_exists(module_path($module, 'Providers'))) {
$list[] = $module;
} else {
$list = [...$list, ...getModuleDirectories($module)];
}
}

return $list;
}
}

Expand Down

0 comments on commit f68ef7a

Please sign in to comment.