Skip to content

Commit

Permalink
adding support for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
mahmoodahmad100 committed Apr 18, 2024
1 parent b547853 commit 94ee001
Show file tree
Hide file tree
Showing 30 changed files with 173 additions and 71 deletions.
6 changes: 3 additions & 3 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ Summary of all the commands:

### Create new module

`php artisan laragine:module ModuleName`
`php artisan laragine:module ModuleName` | optional: add `--plugins` to be in the Plugins directory

### Initialize the unit

`php artisan laragine:unit UnitName --module=ModuleName --init`
`php artisan laragine:unit UnitName --module=ModuleName --init` | optional: add `--plugins` to be in the Plugins directory

### Create all the related stuff for the unit

`php artisan laragine:unit UnitName --module=ModuleName`
`php artisan laragine:unit UnitName --module=ModuleName` | optional: add `--plugins` to be in the Plugins directory
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## Config

You will notice in any module you generate, a `config` directory, so basically you can add configuration for the module, and you can access it in this form: `config('core_modulename.some_key')` for example: `config('core_base.api_key')`
You will notice in any module you generate, a `config` directory, so basically you can add configuration for the module, and you can access it in this form: `config('coreOrplugins_moduleName.some_key')` for example: `config('core_base.api_key')` another example `config('plugins_sale.tax')`
6 changes: 4 additions & 2 deletions docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ It's very important to know why to use Laragine, here is why:

![Structure](structure.png)

The `Base` module is the main module in which every new module will be created (keep reading the other sections to know how), will be derived from.
The `Base` module in the `core` directory is the main module in which every new module will be created (keep reading the other sections to know how), will be derived from.

Also it's worth mentioning that there is another directory called `plugins` for overriding current modules, adding extra functionalities or adding new modules to the system.

It's also very important to understand the following terms:

Expand All @@ -34,7 +36,7 @@ It's also very important to understand the following terms:

* Laragine currently is working on **Laravel 8.x, 9.x, 10.x and 11.x**

* Laragine directory will be in the root directory under `Core` directory
* Laragine directory will be in the root directory under `core` directory and as mentioned above there is also `plugins` directory

* The system response (including errors response if you applied what's in `Error Handling` section) to any request will be as in below examples (`status_code` is the http status code):

Expand Down
20 changes: 20 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Plugins

**Note:** it's important to read the module and unit sections first

To create a new module or a new unit to the plugins directory you can do so by adding `--plugins` option to the commands

Here is a complete example:

Creating New module:
```bash
php artisan laragine:module Todo --plugins
```
Initializing a unit:
```bash
php artisan laragine:unit Task --module=Todo --init --plugins
```
Publishing the unit:
```bash
php artisan laragine:unit Task --module=Todo --plugins
```
Binary file modified docs/structure.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/Commands/MakeModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class MakeModule extends Command
*
* @var string
*/
protected $signature = 'laragine:module {name}';
protected $signature = 'laragine:module {name} {--P|plugins}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create new module';
protected $description = 'Create a new module';

/**
* Create a new command instance.
Expand All @@ -33,7 +33,7 @@ public function __construct()

public function handle()
{
$command = GeneratorFactory::create($this, 'MakeModule', $this->argument('name'));
$command = GeneratorFactory::create($this, 'MakeModule', $this->argument('name'), $this->option('plugins'));
$command->run();
}
}
7 changes: 4 additions & 3 deletions src/Commands/MakeUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class MakeUnit extends Command
*
* @var string
*/
protected $signature = 'laragine:unit {name} {--module=} {--I|init}';
protected $signature = 'laragine:unit {name} {--module=} {--I|init} {--P|plugins}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create new unit';
protected $description = 'Create a new unit';

/**
* Create a new command instance.
Expand Down Expand Up @@ -48,7 +48,8 @@ public function handle()
'MakeUnit',
$this->argument('name'),
$this->option('module'),
$this->option('init')
$this->option('init'),
$this->option('plugins')
);

$command->run();
Expand Down
6 changes: 3 additions & 3 deletions src/Core/Base/File.stub
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ trait File
*
* @param string $dir
* @param string $module
* @param boolean $is_core
* @param boolean $is_plugins
*/
protected function loadFiles($dir, $module = 'base', $is_core = true)
protected function loadFiles($dir, $module = 'base', $is_plugins = false)
{
$module_root_dir = $is_core ? 'core' : 'plugins';
$module_root_dir = $is_plugins ? 'plugins' : 'core';

$this->loadRoutesFrom($dir . '/routes/web.php');
$this->loadRoutesFrom($dir . '/routes/api.php');
Expand Down
38 changes: 29 additions & 9 deletions src/Core/Base/Module.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,43 @@

namespace Core\Base\Traits\ServiceProvider;

use Illuminate\Support\Str;

trait Module
{
/**
* register the service provider for the module
*
* @param string $excluded_directory
* @param string $root_namespace
*/
private function registerModules($excluded_directory = 'Base', $root_namespace = 'Core')
private function registerModules()
{
$base_path = base_path() . '/core';
foreach (glob($base_path.'/*/ModuleServiceProvider.php') as $file) {
// Register 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)) {
$namespace = str_replace('/', '\\', str_replace('.php', '', $file));
$namespace = explode('core\\', $namespace)[1];
$this->app->register($root_namespace . '\\' . $namespace);
$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);
}
}

/**
* handle registering the module service provider
*/
private function registerModuleServiceProvider($file, $root_namespace)
{
$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);
}
}
4 changes: 2 additions & 2 deletions src/Core/Module/ModuleServiceProvider.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Core\#MODULE_NAME#;
namespace #SELECTED_DIRECTORY#\#MODULE_NAME#;

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Module/Unit.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Core\#MODULE_NAME#\Models;
namespace #SELECTED_DIRECTORY#\#MODULE_NAME#\Models;

use Core\Base\Models\Base;

Expand Down
9 changes: 5 additions & 4 deletions src/Core/Module/UnitApiController.stub
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php

namespace Core\#MODULE_NAME#\Controllers\API\V1;
namespace #SELECTED_DIRECTORY#\#MODULE_NAME#\Controllers\API\V1;

use Core\#MODULE_NAME#\Requests\#UNIT_NAME#Request as FormRequest;
use Core\#MODULE_NAME#\Models\#UNIT_NAME# as Model;
use Core\#MODULE_NAME#\Resources\#UNIT_NAME#Resource as Resource;
use #SELECTED_DIRECTORY#\#MODULE_NAME#\Requests\#UNIT_NAME#Request as FormRequest;
use #SELECTED_DIRECTORY#\#MODULE_NAME#\Models\#UNIT_NAME# as Model;
use #SELECTED_DIRECTORY#\#MODULE_NAME#\Resources\#UNIT_NAME#Resource as Resource;

class #UNIT_NAME#Controller extends \Core\Base\Controllers\API\Controller
{
/**
* Init.
*
* @param FormRequest $request
* @param Model $model
* @param string $resource
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Module/UnitFactory.stub
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Core\#MODULE_NAME#\Database\Factories;
namespace #SELECTED_DIRECTORY#\#MODULE_NAME#\Database\Factories;

use Core\#MODULE_NAME#\Models\#UNIT_NAME# as Model;
use #SELECTED_DIRECTORY#\#MODULE_NAME#\Models\#UNIT_NAME# as Model;
use Illuminate\Database\Eloquent\Factories\Factory;

class #UNIT_NAME#Factory extends Factory
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Module/UnitRequest.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Core\#MODULE_NAME#\Requests;
namespace #SELECTED_DIRECTORY#\#MODULE_NAME#\Requests;

use Illuminate\Foundation\Http\FormRequest;

Expand Down
2 changes: 1 addition & 1 deletion src/Core/Module/UnitResource.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Core\#MODULE_NAME#\Resources;
namespace #SELECTED_DIRECTORY#\#MODULE_NAME#\Resources;

use Illuminate\Http\Resources\Json\JsonResource as Resource;

Expand Down
4 changes: 2 additions & 2 deletions src/Core/Module/UnitTest.stub
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Core\#MODULE_NAME#\Tests\Feature;
namespace #SELECTED_DIRECTORY#\#MODULE_NAME#\Tests\Feature;

use Core\Base\Tests\ApiTestCase;
use Core\#MODULE_NAME#\Models\#UNIT_NAME# as Model;
use #SELECTED_DIRECTORY#\#MODULE_NAME#\Models\#UNIT_NAME# as Model;

class #UNIT_NAME#Test extends ApiTestCase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Module/UnitWebController.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Core\#MODULE_NAME#\Controllers\Web;
namespace #SELECTED_DIRECTORY#\#MODULE_NAME#\Controllers\Web;

class #UNIT_NAME#Controller extends \Core\Base\Controllers\Web\Controller
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Module/api.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Route::group(['prefix' => 'api', 'middleware' => []], function () {
# V1
Route::namespace('Core\#MODULE_NAME#\Controllers\API\V1')->prefix('v1')->name('api.v1.')->group(function () {
Route::namespace('#SELECTED_DIRECTORY#\#MODULE_NAME#\Controllers\API\V1')->prefix('v1')->name('api.v1.')->group(function () {
#*** Ex: START: #UNIT_NAME# ***#
// Route::apiResource('#UNIT_NAME_PLURAL_LOWER_CASE#', '#UNIT_NAME#Controller');
#*** END: #UNIT_NAME# ***#
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Module/web.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

Route::namespace('Core\#MODULE_NAME#\Controllers\Web')->prefix('admin')->name('admin.')->group(function () {
Route::namespace('#SELECTED_DIRECTORY#\#MODULE_NAME#\Controllers\Web')->prefix('admin')->name('admin.')->group(function () {
#*** Ex: START: #UNIT_NAME# ***#
// Route::resource('#UNIT_NAME_PLURAL_LOWER_CASE#', '#UNIT_NAME#Controller')->except([
// 'store', 'update', 'destroy'
Expand Down
8 changes: 8 additions & 0 deletions src/Generators/Payloads/Commands/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class Base implements GeneratorInterface
*/
protected $root_dir;

/**
* the plugins dir
*
* @var string
*/
protected $plugins_dir;

/**
* init
*
Expand All @@ -40,6 +47,7 @@ public function __construct(Command $command, $args)
$this->command = $command;
$this->args = $args;
$this->root_dir = config('laragine.root_dir');
$this->plugins_dir = config('laragine.plugins_dir');
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Generators/Payloads/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function run()
{
$allow_publish = true;
if (FileManipulator::exists($this->root_dir)) {
if ($this->command->confirm(__('laragine::install.root_dir_exists'), true)) {
if ($this->command->confirm(__('laragine::install.already_installed'), true)) {
$allow_publish = true;
} else {
$allow_publish = false;
Expand All @@ -25,6 +25,8 @@ public function run()

if ($allow_publish) {
$this->publishRootDirectory();

FileManipulator::generateDir($this->plugins_dir);
}
}

Expand Down
Loading

0 comments on commit 94ee001

Please sign in to comment.