Skip to content

Commit 3f57dff

Browse files
committed
feat(module provider):
add module provider. With module provider you can simply add your module provider to app.php and the other thinks work automaticaly.
1 parent 77cc9ae commit 3f57dff

File tree

6 files changed

+86
-13
lines changed

6 files changed

+86
-13
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
vendor
1+
/vendor
2+
composer.lock
3+
/phpunit.xml
4+
.phpunit.result.cache
5+
/.idea

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"license": "MIT",
77
"require": {
88
"php": "^7.3|^8.0",
9-
"illuminate/filesystem": "^8.42|^9.0"
9+
"illuminate/filesystem": "^8.42|^9.0",
10+
"illuminate/support": "^8.42|^9.0",
11+
"illuminate/validation": "^8.42|^9.0",
12+
"illuminate/console": "^8.42|^9.0"
1013
},
1114
"autoload": {
1215
"psr-4": {

src/Console/AppMaker.php

+40-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function handle()
5353
}
5454

5555
// this is your Modules path.
56-
$this->appPath = base_path("Modules/" . $this->appName);
56+
$this->appPath = base_path("modules/" . $this->appName);
5757

5858

5959
if (file_exists($this->appPath)) {
@@ -63,7 +63,10 @@ public function handle()
6363
// make App Dir
6464
(new Filesystem())->ensureDirectoryExists($this->appPath);
6565

66-
// provider...
66+
// module provider...
67+
$this->createModuleProvider();
68+
69+
// app provider...
6770
$this->createProvider();
6871

6972
// Routes..
@@ -87,17 +90,49 @@ public function handle()
8790

8891
$this->info('** Everything is ready Build Something Amazing **');
8992
$this->alert(self::ALERT_MESSAGE);
93+
$this->info('** test url: ' . 'http://127.0.0.1:8000/' . strtolower($this->appName) . '/test' . ' **');
94+
9095
$bar->finish();
9196
$this->newLine();
9297
}
9398

99+
private function createModuleProvider()
100+
{
101+
$moudleServiceProviderPath = base_path("modules/ModulesProvider");
102+
$moudleServiceProviderFile = $moudleServiceProviderPath . '/ModuleServiceProvider.php';
103+
104+
if (!file_exists($moudleServiceProviderFile)) {
105+
(new Filesystem())->ensureDirectoryExists($moudleServiceProviderPath);
106+
(new Filesystem())->copy(__DIR__ . '/../../stubs/ModulesProvider/ModuleServiceProvider.php',
107+
$moudleServiceProviderFile);
108+
(new Filesystem())->replaceInFile('Example', ucfirst($this->appName ), $moudleServiceProviderFile);
109+
} else {
110+
$fileContent = (new Filesystem())->get($moudleServiceProviderFile);
111+
112+
$firsStrPosForNameSpace = strrpos($fileContent, 'use Modules\{') + strlen('use Modules\{');
113+
114+
$appServiceProviderClassNameSpace = ucfirst($this->appName) . '\Provider\\' . ucfirst($this->appName) . 'Provider';
115+
116+
$newFileContent = substr($fileContent, 0, $firsStrPosForNameSpace) . PHP_EOL . "\t" . $appServiceProviderClassNameSpace . ',' . "\t \t". substr($fileContent, $firsStrPosForNameSpace);
117+
118+
$firsStrPosForRegisterApp = strpos($newFileContent, ' $this->app->register(');
119+
120+
$appServiceProviderClass = $this->appName . 'Provider::class';
121+
122+
$newFileContent = substr($newFileContent, 0, $firsStrPosForRegisterApp) . '$this->app->register('. $appServiceProviderClass . ');' . PHP_EOL . "\t \t". substr($newFileContent, $firsStrPosForRegisterApp);
123+
124+
(new Filesystem())->put($moudleServiceProviderFile, $newFileContent);
125+
}
126+
}
127+
94128
private function createProvider()
95129
{
96130
(new Filesystem())->ensureDirectoryExists($this->appPath . '/Provider');
97131
(new Filesystem())->copy(__DIR__ . '/../../stubs/app/Provider/ExampleProvider.php',
98-
$this->appPath . '/Provider/' . ucfirst($this->appName ) . 'Provider.php');
99-
(new Filesystem())->replaceInFile('Example', ucfirst($this->appName ), $this->appPath . '/Provider/' . ucfirst($this->appName ) . 'Provider.php');
100-
(new Filesystem())->replaceInFile('AppPath', 'Modules/' . ucfirst($this->appName ), $this->appPath . '/Provider/' . ucfirst($this->appName ) . 'Provider.php');
132+
$this->appPath . '/Provider/' . ucfirst($this->appName) . 'Provider.php');
133+
(new Filesystem())->replaceInFile('Example', ucfirst($this->appName), $this->appPath . '/Provider/' . ucfirst($this->appName ) . 'Provider.php');
134+
(new Filesystem())->replaceInFile('ROUTE-NAME', strtolower($this->appName), $this->appPath . '/Provider/' . ucfirst($this->appName ) . 'Provider.php');
135+
(new Filesystem())->replaceInFile('AppPath', 'Modules/' . ucfirst($this->appName), $this->appPath . '/Provider/' . ucfirst($this->appName ) . 'Provider.php');
101136
}
102137

103138
private function createRoute()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Modules\ModulesProvider;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Modules\{
7+
Example\Provider\ExampleProvider,
8+
};
9+
10+
class ModuleServiceProvider extends ServiceProvider
11+
{
12+
/**
13+
* Register any application services.
14+
*
15+
* @return void
16+
*/
17+
public function register()
18+
{
19+
// register module providers here
20+
$this->app->register(ExampleProvider::class);
21+
}
22+
23+
/**
24+
* Bootstrap any application services.
25+
*
26+
* @return void
27+
*/
28+
public function boot()
29+
{
30+
//
31+
}
32+
}

stubs/app/Provider/ExampleProvider.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public function boot()
3131

3232
private function routeMap()
3333
{
34-
Route::prefix('Example')
34+
Route::prefix('ROUTE-NAME')
3535
->middleware('web')
36-
->name('Example.')
36+
->name('ROUTE-NAME.')
3737
->group(base_path('AppPath/routes/web.php'));
3838
}
39-
}
39+
}

stubs/app/routes/web.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
|
1414
*/
1515

16-
Route::get('/Example', function () {
16+
Route::get('/test', function () {
1717
return view('Example::Example');
18-
});
19-
18+
});

0 commit comments

Comments
 (0)