Skip to content

Commit b72d8dc

Browse files
committed
Make facade, more trait templates, signatures
1 parent 0bb184f commit b72d8dc

13 files changed

+225
-10
lines changed

src/Config/laravel-stubs.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
],
5050
/**
5151
* make:scope
52+
*
53+
* Allows you to change the namespace (and so the folder) where the scope will be stored
5254
*/
5355
'scope' => [
5456
'namespace' => '\App\Scopes'
@@ -63,9 +65,17 @@
6365
],
6466
/**
6567
* make:view:composer
68+
*
69+
* Allows you to change the namespace (and so the folder) where the view composer will be stored
6670
*/
6771
'view:composer' => [
6872
'namespace' => '\App\Http\View\Composers'
73+
],
74+
/**
75+
* make:view:composer
76+
*/
77+
'facade' => [
78+
'namespace' => '\App\Facades'
6979
]
7080
]
7181
];

src/Console/Create/CreateUser.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CreateUser extends Command
1212
*
1313
* @var string
1414
*/
15-
protected $signature = 'create:user';
15+
protected $name = 'create:user';
1616

1717
/**
1818
* The console command description.
@@ -21,6 +21,8 @@ class CreateUser extends Command
2121
*/
2222
protected $description = 'Create an user';
2323

24+
protected $signature = 'create:user';
25+
2426
/**
2527
* Create a new command instance.
2628
*
@@ -43,7 +45,7 @@ public function handle()
4345

4446
if (!\class_exists($model)) {
4547

46-
return $this->error('Model does not exist.');
48+
return $this->error('Model does not exist');
4749
}
4850

4951
$fields = config('laravel-stubs.create.user.fields');
@@ -93,7 +95,7 @@ public function handle()
9395
$model::where(config('laravel-stubs.create.user.unique'),
9496
$saving[config('laravel-stubs.create.user.unique')])->first()) {
9597

96-
$this->error('This user already exists.');
98+
$this->error('This user already exists');
9799
}
98100
else {
99101

src/Console/Make/MakeFacade.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Creativeorange\LaravelStubs\Console\Make;
4+
5+
use Creativeorange\LaravelStubs\Console\CustomGeneratorCommand;
6+
use Illuminate\Support\Str;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use Symfony\Component\Console\Input\InputOption;
9+
10+
class MakeFacade extends CustomGeneratorCommand
11+
{
12+
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
protected $name = 'make:facade';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Create a new facade';
26+
27+
protected $signature = 'make:facade
28+
{name : The name of the facade}
29+
{accessor : The accessor for the facade}';
30+
31+
protected $type = 'Facade';
32+
33+
public function handle()
34+
{
35+
parent::handle();
36+
}
37+
38+
protected function getStub()
39+
{
40+
41+
return $this->resolveStubPath('/../stubs/facade.stub');
42+
}
43+
44+
protected function replaceNamespace(&$stub, $name)
45+
{
46+
$this->parseAccessor($stub);
47+
48+
return parent::replaceNamespace($stub, $name);
49+
}
50+
51+
protected function parseAccessor(&$stub)
52+
{
53+
54+
$accessor = $this->argument('accessor');
55+
$accessor = Str::endsWith($accessor, '/')
56+
? $accessor
57+
: '/' . $accessor;
58+
59+
$accessor = \str_replace('/', '\\', $accessor);
60+
61+
$stub = \str_replace('DummyAccessor', $accessor, $stub);
62+
}
63+
64+
protected function getDefaultNamespace($rootNamespace)
65+
{
66+
67+
return (empty(config('laravel-stubs.make.facade.namespace')))
68+
? $rootNamespace
69+
: config('laravel-stubs.make.facade.namespace');
70+
}
71+
72+
protected function getNameInput()
73+
{
74+
75+
$name = trim($this->argument('name'));
76+
77+
$name = Str::endsWith($name, 'Facade')
78+
? $name
79+
: $name . 'Facade';
80+
81+
return $name;
82+
}
83+
84+
protected function getArguments()
85+
{
86+
return [
87+
['name', InputArgument::REQUIRED, 'The name of the facade'],
88+
['accessor', InputArgument::REQUIRED, 'The accessor for the facade']
89+
];
90+
}
91+
}

src/Console/Make/MakeInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class MakeInterface extends CustomGeneratorCommand
2323
*/
2424
protected $description = 'Create a new interface';
2525

26+
protected $signature = 'make:interface
27+
{name : The name of the interface}';
28+
2629
protected $type = 'Interface';
2730

2831
public function handle()

src/Console/Make/MakeScope.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class MakeScope extends CustomGeneratorCommand
2424
*/
2525
protected $description = 'Create a new scope';
2626

27+
protected $signature = 'make:scope
28+
{name : The name of the scope}';
29+
2730
protected $type = 'Scope';
2831

2932
public function handle()

src/Console/Make/MakeTrait.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class MakeTrait extends CustomGeneratorCommand
2424
*/
2525
protected $description = 'Create a new trait';
2626

27+
protected $signature = 'make:trait
28+
{name : The name of the trait}
29+
{--b|boot : Create a boot trait}
30+
{--a|anonymous : Create a trait to anonymous data}
31+
{--u|uuid : Create a trait to generate an uuid field}';
32+
2733
protected $type = 'Trait';
2834

2935
public function handle()
@@ -34,9 +40,19 @@ public function handle()
3440
protected function getStub()
3541
{
3642

37-
return $this->option('boot')
38-
? $this->resolveStubPath('/../stubs/trait-boot.stub')
39-
: $this->resolveStubPath('/../stubs/trait.stub');
43+
if ($this->option('boot')) {
44+
return $this->resolveStubPath('/../stubs/trait-boot.stub');
45+
}
46+
47+
if ($this->option('anonymous')) {
48+
return $this->resolveStubPath('/../stubs/trait-anonymous.stub');
49+
}
50+
51+
if ($this->option('uuid')) {
52+
return $this->resolveStubPath('/../stubs/trait-uuid.stub');
53+
}
54+
55+
return $this->resolveStubPath('/../stubs/trait.stub');
4056
}
4157

4258
protected function getDefaultNamespace($rootNamespace)
@@ -69,7 +85,9 @@ protected function getArguments()
6985
protected function getOptions()
7086
{
7187
return [
72-
['boot', 'b', InputOption::VALUE_NONE, 'Create a boot trait.'],
88+
['boot', 'b', InputOption::VALUE_NONE, 'Create a boot trait'],
89+
['anonymous', 'a', InputOption::VALUE_NONE, 'Create a trait to anonymous data'],
90+
['uuid', 'u', InputOption::VALUE_NONE, 'Create a trait to generate an uuid field'],
7391
];
7492
}
7593
}

src/Console/Make/MakeViewComposer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class MakeViewComposer extends CustomGeneratorCommand
2424
*/
2525
protected $description = 'Create a new view composer';
2626

27+
protected $signature = 'make:view:composer
28+
{name : The name of the view composer}';
29+
2730
protected $type = 'View composer';
2831

2932
public function handle()

src/Console/Publish/PublishStubs.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class PublishStubs extends Command
2323
*/
2424
protected $description = 'Publish the stubs of this package';
2525

26+
protected $signature = 'publish:stubs
27+
{--f|force : Overwrite any existing files}';
28+
2629
/**
2730
* Create a new command instance.
2831
*
@@ -47,7 +50,7 @@ public function handle()
4750

4851
File::copyDirectory(__DIR__.'/../../stubs/', $stubsPath);
4952

50-
return $this->info('Stubs published successfully.');
53+
return $this->info('Stubs published successfully');
5154
}
5255

5356
protected function getOptions()

src/Console/Run/RunFactory.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class RunFactory extends Command
2222
*/
2323
protected $description = 'Run a factory';
2424

25+
protected $signature = 'run:factory
26+
{model= : The model to run the factory for}
27+
{amount= : The amount of records generated by the factory}';
28+
2529
/**
2630
* Create a new command instance.
2731
*
@@ -100,8 +104,8 @@ protected function getAmountOption()
100104
protected function getArguments()
101105
{
102106
return [
103-
['model', InputArgument::OPTIONAL, 'The model to run the factory for.'],
104-
['amount', InputArgument::OPTIONAL, 'The amount of records generated by the factory.'],
107+
['model', InputArgument::OPTIONAL, 'The model to run the factory for'],
108+
['amount', InputArgument::OPTIONAL, 'The amount of records generated by the factory'],
105109
];
106110
}
107111
}

src/LaravelStubsServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function boot() {
1919
Console\Make\MakeScope::class,
2020
Console\Make\MakeInterface::class,
2121
Console\Make\MakeViewComposer::class,
22+
Console\Make\MakeFacade::class,
2223
/** Publish */
2324
Console\Publish\PublishStubs::class,
2425
/** Run */

src/stubs/facade.stub

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* Class DummyClass
9+
* @package DummyNamespace
10+
*/
11+
class DummyClass extends Facade
12+
{
13+
14+
/**
15+
* @return string
16+
*/
17+
protected static function getFacadeAccessor()
18+
{
19+
return DummyAccessor::class;
20+
}
21+
}

src/stubs/trait-anonymous.stub

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
trait DummyClass
6+
{
7+
8+
/**
9+
* Bootstrap the trait.
10+
*
11+
* The following functions can be called statically:
12+
* retrieved, creating, created, updating, updated,
13+
* saving, saved, deleting, deleted, restoring, restored
14+
*
15+
* @return void
16+
*/
17+
public static function bootDummyClass(): void
18+
{
19+
20+
static::deleted(function ($model) {
21+
22+
foreach (self::$anonymousFields as $field) {
23+
$model->$field = null;
24+
}
25+
26+
$model->save();
27+
});
28+
}
29+
}

src/stubs/trait-uuid.stub

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use Illuminate\Support\Str;
6+
7+
trait DummyClass
8+
{
9+
10+
/**
11+
* Bootstrap the trait.
12+
*
13+
* The following functions can be called statically:
14+
* retrieved, creating, created, updating, updated,
15+
* saving, saved, deleting, deleted, restoring, restored
16+
*
17+
* @return void
18+
*/
19+
public static function bootDummyClass(): void
20+
{
21+
22+
static::creating(function ($model) {
23+
24+
$model->$uuidField = Str::uuid();
25+
});
26+
}
27+
}

0 commit comments

Comments
 (0)