Skip to content

Commit abaf052

Browse files
authored
Merge pull request #3 from mhh1422/master
Initial version
2 parents 806ee9e + 55674e7 commit abaf052

18 files changed

+1064
-2
lines changed

config/defaults.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
return [
4+
5+
/**-----------------------------------------------------------
6+
* Classes and Structure
7+
------------------------------------------------------------*/
8+
/*
9+
* For each module you create, there should be a main module class which defines different module configurations
10+
* and settings. If you named it 'Module', then in the module root folder there should be a file called 'Module.php'
11+
* which defines a class called 'Module' which extends the core ItvisionSy\Laravel\Modules\Module class.
12+
*/
13+
'class_name' => 'Module',
14+
/*
15+
* Next two configs defines:
16+
* - What is the namespace of the modules directory.
17+
* - Where is the modules directory located.
18+
* Note that your composer autoloader should be able to locate and autoload your files and classes.
19+
*/
20+
'namespace' => '\\App\\Modules\\',
21+
'directory' => app_path('Modules'),
22+
23+
24+
/**-----------------------------------------------------------
25+
* Store Handler
26+
------------------------------------------------------------*/
27+
/*
28+
* A store handler is a class full name that implements the interface \ItvisionSy\Laravel\Modules\Interfaces\KeyValueStoreInterface
29+
* or a callable that will return a class name or an object implements it. Avoid using closures in the config.
30+
* If you want to use a closure, in your kernel.php call the
31+
* \ItvisionSy\Laravel\Modules\Modules::setStoreHandler(closure())
32+
*/
33+
'store_handler' => \ItvisionSy\Laravel\Modules\StoreHandlers\SimpleDbStoreHandler::class,
34+
/*
35+
* If you do not provide a store handler, then the following connection will be used to store the settings for you.
36+
* You will need to initiate the required table by issuing the modules:db:init command.
37+
* Null means using the default connection
38+
*/
39+
'default_store_handler_connection' => null,
40+
/*
41+
* A key prefix to all non-module entries
42+
*/
43+
'store_public_prefix_key' => 'modules',
44+
/*
45+
* True will make the default status of the modules is enabled if the store can not find an entry for the module
46+
*/
47+
'modules_enabled_by_default' => false,
48+
49+
50+
/**-----------------------------------------------------------
51+
* Routes and URL Generating
52+
------------------------------------------------------------*/
53+
/*
54+
* A prefix to be added to the routes generated by the module
55+
* i.e. setting it to modules will prefix the 'sales' module's generated url show/{id} to modules/sales/show/{id}
56+
*/
57+
'route_prefix' => null,
58+
/*
59+
* A prefix to be added to the route names generated by the module
60+
* i.e. setting it to modules will prefix the 'sales' module's generated url 'show' to 'modules.sales.show'
61+
*/
62+
'route_name_prefix' => null
63+
];

config/published.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
return [
4+
5+
/**-----------------------------------------------------------
6+
* Classes and Structure
7+
------------------------------------------------------------*/
8+
/*
9+
* For each module you create, there should be a main module class which defines different module configurations
10+
* and settings. If you named it 'Module', then in the module root folder there should be a file called 'Module.php'
11+
* which defines a class called 'Module' which extends the core ItvisionSy\Laravel\Modules\Module class.
12+
*/
13+
'class_name' => 'Module',
14+
/*
15+
* Next two configs defines:
16+
* - What is the namespace of the modules directory.
17+
* - Where is the modules directory located.
18+
* Note that your composer autoloader should be able to locate and autoload your files and classes.
19+
*/
20+
'namespace' => '\\App\\Modules\\',
21+
'directory' => app_path('Modules'),
22+
23+
24+
/**-----------------------------------------------------------
25+
* Store Handler
26+
------------------------------------------------------------*/
27+
/*
28+
* A store handler is a class full name that implements the interface \ItvisionSy\Laravel\Modules\Interfaces\KeyValueStoreInterface
29+
* or a callable that will return a class name or an object implements it. Avoid using closures in the config.
30+
* If you want to use a closure, in your kernel.php call the
31+
* \ItvisionSy\Laravel\Modules\Modules::setStoreHandler(closure())
32+
*/
33+
'store_handler' => \ItvisionSy\Laravel\Modules\StoreHandlers\SimpleDbStoreHandler::class,
34+
/*
35+
* If you do not provide a store handler, then the following connection will be used to store the settings for you.
36+
* You will need to initiate the required table by issuing the modules:db:init command.
37+
* Null means using the default connection
38+
*/
39+
'default_store_handler_connection' => null,
40+
/*
41+
* A key prefix to all non-module entries
42+
*/
43+
'store_public_prefix_key' => 'modules',
44+
/*
45+
* True will make the default status of the modules is enabled if the store can not find an entry for the module
46+
*/
47+
'modules_enabled_by_default' => false,
48+
49+
50+
/**-----------------------------------------------------------
51+
* Routes and URL Generating
52+
------------------------------------------------------------*/
53+
/*
54+
* A prefix to be added to the routes generated by the module
55+
* i.e. setting it to modules will prefix the 'sales' module's generated url show/{id} to modules/sales/show/{id}
56+
*/
57+
'route_prefix' => null,
58+
/*
59+
* A prefix to be added to the route names generated by the module
60+
* i.e. setting it to modules will prefix the 'sales' module's generated url 'show' to 'modules.sales.show'
61+
*/
62+
'route_name_prefix' => null
63+
];
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace ItvisionSy\Laravel\Modules\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Contracts\Bus\SelfHandling;
7+
use ItvisionSy\Laravel\Modules\StoreHandlers\SimpleDbStoreHandler;
8+
9+
class InitiateDatabaseTable extends Command implements SelfHandling
10+
{
11+
12+
protected $signature = 'modules:db:init';
13+
protected $description = 'Initiate the database table for storage';
14+
15+
/**
16+
* Create a new command instance.
17+
*/
18+
public function __construct()
19+
{
20+
parent::__construct();
21+
}
22+
23+
/**
24+
* Execute the command.
25+
*
26+
* @return void
27+
*/
28+
public function handle()
29+
{
30+
try {
31+
$result = SimpleDbStoreHandler::createTable();
32+
if($result){
33+
$this->info("Table modules_storage created successfully!");
34+
}else{
35+
$this->error("Something went wrong!");
36+
}
37+
}catch(\Exception $e){
38+
$this->error($e->getMessage());
39+
}
40+
}
41+
}

src/Commands/MakeModule.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace ItvisionSy\Laravel\Modules\Commands;
4+
5+
use Illuminate\Contracts\Bus\SelfHandling;
6+
use Illuminate\Filesystem\Filesystem;
7+
8+
class MakeModule extends \Illuminate\Console\Command implements SelfHandling
9+
{
10+
11+
protected $signature = 'modules:make
12+
{id : the ID of the module. Should be unique across modules}
13+
{name : the display name of the module}
14+
{--url= : the URL/route-names part for the module}
15+
';
16+
protected $description = 'Makes a new module';
17+
18+
/**
19+
* @var Filesystem
20+
*/
21+
protected $fileSystem;
22+
23+
/**
24+
* Create a new command instance.
25+
* @param Filesystem $fileSystem
26+
*/
27+
public function __construct(Filesystem $fileSystem)
28+
{
29+
parent::__construct();
30+
$this->fileSystem = $fileSystem;
31+
}
32+
33+
/**
34+
* Execute the command.
35+
*
36+
* @return void
37+
*/
38+
public function handle()
39+
{
40+
//input
41+
$id = $this->argument('id');
42+
$name = $this->argument('name');
43+
$url = $this->option('url') ?: str_slug($id);
44+
45+
//subs
46+
$path = rtrim(config('modules.directory'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $id . DIRECTORY_SEPARATOR;
47+
$className = config('modules.class_name');
48+
$ds = DIRECTORY_SEPARATOR;
49+
50+
//stub data
51+
$stubData = [
52+
"id" => $id,
53+
"name" => $name,
54+
"namespace" => trim(config("modules.namespace"), "\\"),
55+
"class" => $className,
56+
"url_name" => $url,
57+
];
58+
59+
$this->makeDirectory($path);
60+
$this->makeDirectory("{$path}Views");
61+
$this->makeDirectory("{$path}Models");
62+
$this->makeDirectory("{$path}Http{$ds}Controllers");
63+
$this->copyStub("Module.php.stub", "{$path}{$className}.php", $stubData + []);
64+
$this->copyStub("routes.php.stub", "{$path}Http{$ds}routes.php", $stubData + []);
65+
$this->copyStub("Controller.php.stub", "{$path}Http{$ds}Controllers{$ds}WelcomeController.php", $stubData + []);
66+
$this->copyStub("index.blade.php.stub", "{$path}Views{$ds}index.blade.php", $stubData + []);
67+
68+
$this->info("Module {$id} has been created in {$path}");
69+
70+
}
71+
72+
protected function makeDirectory($path, $mode = 0777)
73+
{
74+
if (!$this->fileSystem->isDirectory($path)) {
75+
$this->fileSystem->makeDirectory($path, $mode, true, true);
76+
}
77+
}
78+
79+
protected function copyStub($stubName, $filePath, array $values)
80+
{
81+
$path = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'MakeModule' . DIRECTORY_SEPARATOR . $stubName;
82+
$content = preg_replace_callback("/\{\{([a-zA-Z_\-]+)\}\}/", function ($matches) use ($values) {
83+
return $values[$matches[1]];
84+
}, file_get_contents($path));
85+
if ($this->fileSystem->exists($filePath)) {
86+
$this->fileSystem->delete($filePath);
87+
}
88+
$this->fileSystem->put($filePath, $content);
89+
}
90+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace {{namespace}}\{{id}}\Http\Controllers;
4+
5+
use ItvisionSy\Laravel\Modules\Controller as BaseController;
6+
7+
class WelcomeController extends BaseController
8+
{
9+
10+
public function index(){
11+
return $this->renderView('index');
12+
}
13+
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace {{namespace}}\{{id}};
4+
5+
use ItvisionSy\Laravel\Modules\Module as BaseModule;
6+
7+
class {{class}} extends BaseModule
8+
{
9+
10+
protected $moduleId='{{id}}';
11+
protected $moduleName='{{name}}';
12+
protected $moduleRouteNamePrefix='{{url_name}}';
13+
protected $moduleUrlPrefix='{{url_name}}';
14+
15+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php /** @var {{namespace}}\{{id}}\{{class}} $this_module */ ?>
2+
<h1>Welcome</h1>
3+
<p>Sample view <a href="{!! $this_module->getPathForRoute('index' /*,['query_param1'=>1,...]*/) !!}">Sample generated path</a></p>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$module = {{namespace}}\{{id}}\{{class}}::make();
4+
5+
//Route::get($module->getRoutePath('/'), [
6+
// 'as' => $module->getRouteName('index'),
7+
// 'uses' => App\Modules\TestModule\Http\Controllers\WelcomeController::class . '@index'
8+
//]);

src/Controller.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Muhannad Shelleh <muhannad.shelleh@live.com>
5+
* Date: 3/15/17
6+
* Time: 5:15 AM
7+
*/
8+
9+
namespace ItvisionSy\Laravel\Modules;
10+
11+
use App\Http\Controllers\Controller as BaseController;
12+
use ErrorException;
13+
14+
abstract class Controller extends BaseController
15+
{
16+
17+
/** @var Module */
18+
protected $module;
19+
20+
/**
21+
* @param $viewName
22+
* @param array $data
23+
* @param array $mergeData
24+
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
25+
*/
26+
protected function renderView($viewName, array $data = [], array $mergeData = [])
27+
{
28+
return $this->module()->renderView($viewName, $data, $mergeData);
29+
}
30+
31+
/**
32+
* @return Module
33+
* @throws ErrorException
34+
*/
35+
public function module()
36+
{
37+
if (!$this->module) {
38+
$ns = trim(get_class($this), "\\");
39+
$baseNs = trim(config('modules.namespace'), "\\");
40+
if (!starts_with($ns, $baseNs)) {
41+
throw new ErrorException('Module controllers should exist inside module root folder');
42+
}
43+
$moduleId = explode("\\", trim(substr($ns, strlen($baseNs)), "\\"))[0];
44+
$moduleFullName = join("\\", ["", $baseNs, $moduleId, config('modules.class_name')]);
45+
if (!class_exists($moduleFullName)) {
46+
throw new ErrorException("Module class does not exist");
47+
}
48+
$this->module = $moduleFullName::make();
49+
}
50+
return $this->module;
51+
}
52+
53+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Muhannad Shelleh <muhannad.shelleh@live.com>
5+
* Date: 3/14/17
6+
* Time: 6:43 PM
7+
*/
8+
9+
namespace ItvisionSy\Laravel\Modules\Interfaces;
10+
11+
interface KeyValueStoreInterface
12+
{
13+
14+
public function set($key, $value = null);
15+
16+
public function get($key, $default = null);
17+
18+
}

0 commit comments

Comments
 (0)