diff --git a/src/Core/Base/Module.stub b/src/Core/Base/Module.stub index 6fc06fa..265687d 100644 --- a/src/Core/Base/Module.stub +++ b/src/Core/Base/Module.stub @@ -8,37 +8,40 @@ trait Module { /** * register the service provider for the module + * + * @param boolean $is_plugins */ - private function registerModules() + private function registerModules($is_plugins = false) { - // Register the core modules + // the core modules $root_namespace = 'Core'; - $excluded_directory = 'Base'; $root_path = config('laragine.root_dir'); - - foreach (glob($root_path.'/*/ModuleServiceProvider.php') as $file) { - if (!preg_match("/core\/{$excluded_directory}/i", $file)) { - $this->registerModuleServiceProvider($file, $root_namespace); - } - } - - // Register the plugins modules - $plugins_namespace = 'Plugins'; - $plugins_path = config('laragine.plugins_dir'); - foreach (glob($plugins_path.'/*/ModuleServiceProvider.php') as $file) { - $this->registerModuleServiceProvider($file, $plugins_namespace); + // the plugins modules + if ($is_plugins) { + $root_namespace = 'Plugins'; + $root_path = config('laragine.plugins_dir'); } + + $this->registerModuleServiceProvider($root_namespace, $root_path); } /** * handle registering the module service provider + * + * @param string $root_namespace + * @param string $root_path + * @param string $excluded_directory */ - private function registerModuleServiceProvider($file, $root_namespace) + private function registerModuleServiceProvider($root_namespace, $root_path, $excluded_directory = 'Base') { $root_namespace_lower_case = Str::lower($root_namespace); - $namespace = str_replace('/', '\\', str_replace('.php', '', $file)); - $namespace = explode("{$root_namespace_lower_case}\\", $namespace)[1]; - $this->app->register($root_namespace . '\\' . $namespace); + foreach (glob($root_path.'/*/ModuleServiceProvider.php') as $file) { + if (!preg_match("/{$root_namespace_lower_case}\/{$excluded_directory}/i", $file)) { + $namespace = str_replace('/', '\\', str_replace('.php', '', $file)); + $namespace = explode("{$root_namespace_lower_case}\\", $namespace)[1]; + $this->app->register($root_namespace . '\\' . $namespace); + } + } } } diff --git a/src/Core/Base/ModuleServiceProvider.stub b/src/Core/Base/ModuleServiceProvider.stub index d219341..74e950c 100644 --- a/src/Core/Base/ModuleServiceProvider.stub +++ b/src/Core/Base/ModuleServiceProvider.stub @@ -1,6 +1,6 @@ loadFiles(__DIR__); - $this->registerModules(); + $this->loadFiles(__DIR__#MODULE_AND_IS_PLUGINS#); + $this->registerModules(#IS_PLUGINS#); } /** diff --git a/src/Generators/Payloads/Commands/Base.php b/src/Generators/Payloads/Commands/Base.php index 1f24af1..234cc11 100644 --- a/src/Generators/Payloads/Commands/Base.php +++ b/src/Generators/Payloads/Commands/Base.php @@ -44,9 +44,9 @@ class Base implements GeneratorInterface */ public function __construct(Command $command, $args) { - $this->command = $command; - $this->args = $args; - $this->root_dir = config('laragine.root_dir'); + $this->command = $command; + $this->args = $args; + $this->root_dir = config('laragine.root_dir'); $this->plugins_dir = config('laragine.plugins_dir'); } diff --git a/src/Generators/Payloads/Commands/Install.php b/src/Generators/Payloads/Commands/Install.php index a0923c2..296d7c8 100644 --- a/src/Generators/Payloads/Commands/Install.php +++ b/src/Generators/Payloads/Commands/Install.php @@ -25,8 +25,8 @@ public function run() if ($allow_publish) { $this->publishRootDirectory(); - - FileManipulator::generateDir($this->plugins_dir); + $this->publishPluginsDirectory(); + $this->command->info(__('laragine::install.success')); } } @@ -41,14 +41,35 @@ protected function publishRootDirectory() { $this->root_dir . '/Base', config('laragine.base'), [ - 'file' => ['stub'], + 'file' => ['stub'], + 'content' => ['#SELECTED_DIRECTORY#', '#IS_PLUGINS#', '#MODULE_AND_IS_PLUGINS#'] + ], + [ + 'file' => ['php'], + 'content' => ['Core', '', ''] + ] + ); + } + + /** + * publish plugins directory + * + * @return void + */ + protected function publishPluginsDirectory() { + FileManipulator::generate( + __DIR__ . '/../../../Core/Base', + $this->plugins_dir . '/Base', + config('laragine.plugins_base'), + [ + 'file' => ['stub'], + 'content' => ['#SELECTED_DIRECTORY#', '#IS_PLUGINS#', '#MODULE_AND_IS_PLUGINS#'] ], [ - 'file' => ['php'], + 'file' => ['php'], + 'content' => ['Plugins', 'true', ", 'base', true"] ] ); - - $this->command->info(__('laragine::install.success')); } } diff --git a/src/Logic/FileManipulator.php b/src/Logic/FileManipulator.php index 06e4741..9040b04 100644 --- a/src/Logic/FileManipulator.php +++ b/src/Logic/FileManipulator.php @@ -66,17 +66,6 @@ public static function generate($source_dir, $destination_dir, $files, $search = } } - /** - * generate directory or directories - * - * @param string $destination_dir - * @param integer $mode - */ - public static function generateDir($destination_dir, $mode = 0775) : void - { - File::makeDirectory("$destination_dir", $mode, true, true); - } - protected static function deleteFilesWithMatchSpecificPrefix($destination, $prefix) { foreach (glob("$destination".'/*') as $migration_file) { if(strpos($migration_file, $prefix) !== false) { diff --git a/src/ModuleServiceProvider.php b/src/ModuleServiceProvider.php index 943080f..ca48c3d 100644 --- a/src/ModuleServiceProvider.php +++ b/src/ModuleServiceProvider.php @@ -16,6 +16,7 @@ public function register() $this->mergeConfigFrom(__DIR__ . '/config.php', 'laragine'); module_autoloader(); + module_autoloader('Plugins', config('laragine.plugins_dir')); if (class_exists('Core\\Base\\ModuleServiceProvider')) { /** @@ -28,6 +29,17 @@ public function register() //throw $th; } } + + if (class_exists('Plugins\\Base\\ModuleServiceProvider')) { + /** + * it's the same error as in the previous try/catch block + */ + try { + $this->app->register('Plugins\\Base\\ModuleServiceProvider'); + } catch (\Throwable $th) { + //throw $th; + } + } } /** diff --git a/src/Traits/Exceptions/Handler.php b/src/Traits/Exceptions/Handler.php index 461e3ce..07402a3 100644 --- a/src/Traits/Exceptions/Handler.php +++ b/src/Traits/Exceptions/Handler.php @@ -10,6 +10,7 @@ if (!class_exists(SendResponse::class, false)) { module_autoloader(); + module_autoloader('Plugins', config('laragine.plugins_dir')); } trait Handler diff --git a/src/autoload.php b/src/autoload.php index b92f986..9a05857 100644 --- a/src/autoload.php +++ b/src/autoload.php @@ -1,5 +1,6 @@ ['_navbar.blade.php', '_sidebar.blade.php'] ], + /* + |-------------------------------------------------------------------------- + | The Base (Plugins) + |-------------------------------------------------------------------------- + | + | The stubs and files for the Base Module (Plugins) + | + */ + + 'plugins_base' => [ + 'main.php' => 'config/main.php', + 'api.php' => 'routes/api.php', + 'web.php' => 'routes/web.php', + 'ModuleServiceProvider.stub' => 'ModuleServiceProvider.php' + ], + /* |-------------------------------------------------------------------------- | The Module diff --git a/src/helpers.php b/src/helpers.php index 13676e5..9eb8bea 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -44,7 +44,7 @@ function client_validation_response($validations, &$start_code = 4101) */ function module_autoloader($namespace = 'Core', $dir = '') { - $dir = empty($dir) ? base_path().'/core' : $dir; + $dir = empty($dir) ? config('laragine.root_dir') : $dir; // instantiate the loader $loader = new \Yepwoo\Laragine\Support\Psr4AutoloaderClass;