Skip to content

Commit

Permalink
Merge pull request #35 from yepwoo/dev
Browse files Browse the repository at this point in the history
fixed: plugins module is not being loaded
  • Loading branch information
mahmoodahmad100 authored Apr 21, 2024
2 parents 1140f52 + 3444e61 commit f80683e
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 43 deletions.
41 changes: 22 additions & 19 deletions src/Core/Base/Module.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
6 changes: 3 additions & 3 deletions src/Core/Base/ModuleServiceProvider.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Core\Base;
namespace #SELECTED_DIRECTORY#\Base;

use Illuminate\Support\ServiceProvider;
use Core\Base\Traits\ServiceProvider\File;
Expand All @@ -17,8 +17,8 @@ class ModuleServiceProvider extends ServiceProvider
*/
public function register()
{
$this->loadFiles(__DIR__);
$this->registerModules();
$this->loadFiles(__DIR__#MODULE_AND_IS_PLUGINS#);
$this->registerModules(#IS_PLUGINS#);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Generators/Payloads/Commands/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
33 changes: 27 additions & 6 deletions src/Generators/Payloads/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

Expand All @@ -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'));
}
}

11 changes: 0 additions & 11 deletions src/Logic/FileManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/ModuleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
/**
Expand All @@ -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;
}
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Traits/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

if (!class_exists(SendResponse::class, false)) {
module_autoloader();
module_autoloader('Plugins', config('laragine.plugins_dir'));
}

trait Handler
Expand Down
1 change: 1 addition & 0 deletions src/autoload.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

module_autoloader('Core', './core');
module_autoloader('Plugins', './plugins');

require 'vendor/autoload.php';
16 changes: 16 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@
'layouts/partials' => ['_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
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f80683e

Please sign in to comment.