Skip to content

Commit 56bd91c

Browse files
committed
Adding the ability to publish pre-defiend stubds.
1 parent 6276ddf commit 56bd91c

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

src/CrudGeneratorMakeCommand.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,19 @@ protected function getStub() {
8989

9090
$model = $this->option('model');
9191

92+
$basePath = File::exists($this->laravel->basePath('stubs/crud'))
93+
? $this->laravel->basePath('stubs/crud')
94+
: base_path('stubs');
95+
9296
if($model and $this->option('validation') and $this->getFillables($model)) {
93-
return base_path('stubs/controller.model.validation.stub');
97+
$path = '/controller.model.validation.stub';
9498
} elseif($model) {
95-
return base_path('stubs/controller.model.stub');
99+
$path = '/controller.model.stub';
96100
} else {
97-
return base_path('stubs/controller.plain.stub');
101+
$path = '/controller.plain.stub';
98102
}
103+
104+
return $basePath.$path;
99105
}
100106

101107
protected function getArguments() {

src/CrudGeneratorServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ class CrudGeneratorServiceProvider extends ServiceProvider {
99
public function boot()
1010
{
1111
if($this->app->runningInConsole()) {
12-
$this->commands([CrudGeneratorMakeCommand::class]);
12+
$this->commands([
13+
CrudGeneratorMakeCommand::class,
14+
PublishCrudStubCommand::class
15+
]);
1316
}
1417
}
1518
}

src/PublishCrudStubCommand.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Mehradsadeghi\CrudGenerator;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
8+
class PublishCrudStubCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'crud:publish';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Publish all CRUD stubs';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return void
28+
*/
29+
public function handle()
30+
{
31+
if (! is_dir($stubsPath = $this->laravel->basePath('stubs/crud'))) {
32+
(new Filesystem)->makeDirectory($stubsPath, 0755, true);
33+
}
34+
35+
$files = [
36+
__DIR__.'/stubs/controller.model.stub' => $stubsPath.'/controller.model.stub',
37+
__DIR__.'/stubs/controller.model.validation.stub' => $stubsPath.'/controller.model.validation.stub',
38+
__DIR__.'/stubs/controller.plain.stub' => $stubsPath.'/controller.plain.stub',
39+
];
40+
41+
foreach ($files as $from => $to) {
42+
if (! file_exists($to)) {
43+
file_put_contents($to, file_get_contents($from));
44+
}
45+
}
46+
47+
$this->info('Stubs published successfully.');
48+
}
49+
}

src/stubs/controller.plain.stub

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace DummyNamespace;
44

55
use DummyRootNamespaceHttp\Controllers\Controller;
6-
use Illuminate\Http\Request;
76

87
class DummyClass extends Controller
98
{

0 commit comments

Comments
 (0)