Skip to content

Commit

Permalink
Make facade, more trait templates, signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
EsdertCO committed Jul 24, 2020
1 parent 0bb184f commit b72d8dc
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/Config/laravel-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
],
/**
* make:scope
*
* Allows you to change the namespace (and so the folder) where the scope will be stored
*/
'scope' => [
'namespace' => '\App\Scopes'
Expand All @@ -63,9 +65,17 @@
],
/**
* make:view:composer
*
* Allows you to change the namespace (and so the folder) where the view composer will be stored
*/
'view:composer' => [
'namespace' => '\App\Http\View\Composers'
],
/**
* make:view:composer
*/
'facade' => [
'namespace' => '\App\Facades'
]
]
];
8 changes: 5 additions & 3 deletions src/Console/Create/CreateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CreateUser extends Command
*
* @var string
*/
protected $signature = 'create:user';
protected $name = 'create:user';

/**
* The console command description.
Expand All @@ -21,6 +21,8 @@ class CreateUser extends Command
*/
protected $description = 'Create an user';

protected $signature = 'create:user';

/**
* Create a new command instance.
*
Expand All @@ -43,7 +45,7 @@ public function handle()

if (!\class_exists($model)) {

return $this->error('Model does not exist.');
return $this->error('Model does not exist');
}

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

$this->error('This user already exists.');
$this->error('This user already exists');
}
else {

Expand Down
91 changes: 91 additions & 0 deletions src/Console/Make/MakeFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Creativeorange\LaravelStubs\Console\Make;

use Creativeorange\LaravelStubs\Console\CustomGeneratorCommand;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class MakeFacade extends CustomGeneratorCommand
{

/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'make:facade';

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

protected $signature = 'make:facade
{name : The name of the facade}
{accessor : The accessor for the facade}';

protected $type = 'Facade';

public function handle()
{
parent::handle();
}

protected function getStub()
{

return $this->resolveStubPath('/../stubs/facade.stub');
}

protected function replaceNamespace(&$stub, $name)
{
$this->parseAccessor($stub);

return parent::replaceNamespace($stub, $name);
}

protected function parseAccessor(&$stub)
{

$accessor = $this->argument('accessor');
$accessor = Str::endsWith($accessor, '/')
? $accessor
: '/' . $accessor;

$accessor = \str_replace('/', '\\', $accessor);

$stub = \str_replace('DummyAccessor', $accessor, $stub);
}

protected function getDefaultNamespace($rootNamespace)
{

return (empty(config('laravel-stubs.make.facade.namespace')))
? $rootNamespace
: config('laravel-stubs.make.facade.namespace');
}

protected function getNameInput()
{

$name = trim($this->argument('name'));

$name = Str::endsWith($name, 'Facade')
? $name
: $name . 'Facade';

return $name;
}

protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the facade'],
['accessor', InputArgument::REQUIRED, 'The accessor for the facade']
];
}
}
3 changes: 3 additions & 0 deletions src/Console/Make/MakeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class MakeInterface extends CustomGeneratorCommand
*/
protected $description = 'Create a new interface';

protected $signature = 'make:interface
{name : The name of the interface}';

protected $type = 'Interface';

public function handle()
Expand Down
3 changes: 3 additions & 0 deletions src/Console/Make/MakeScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class MakeScope extends CustomGeneratorCommand
*/
protected $description = 'Create a new scope';

protected $signature = 'make:scope
{name : The name of the scope}';

protected $type = 'Scope';

public function handle()
Expand Down
26 changes: 22 additions & 4 deletions src/Console/Make/MakeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class MakeTrait extends CustomGeneratorCommand
*/
protected $description = 'Create a new trait';

protected $signature = 'make:trait
{name : The name of the trait}
{--b|boot : Create a boot trait}
{--a|anonymous : Create a trait to anonymous data}
{--u|uuid : Create a trait to generate an uuid field}';

protected $type = 'Trait';

public function handle()
Expand All @@ -34,9 +40,19 @@ public function handle()
protected function getStub()
{

return $this->option('boot')
? $this->resolveStubPath('/../stubs/trait-boot.stub')
: $this->resolveStubPath('/../stubs/trait.stub');
if ($this->option('boot')) {
return $this->resolveStubPath('/../stubs/trait-boot.stub');
}

if ($this->option('anonymous')) {
return $this->resolveStubPath('/../stubs/trait-anonymous.stub');
}

if ($this->option('uuid')) {
return $this->resolveStubPath('/../stubs/trait-uuid.stub');
}

return $this->resolveStubPath('/../stubs/trait.stub');
}

protected function getDefaultNamespace($rootNamespace)
Expand Down Expand Up @@ -69,7 +85,9 @@ protected function getArguments()
protected function getOptions()
{
return [
['boot', 'b', InputOption::VALUE_NONE, 'Create a boot trait.'],
['boot', 'b', InputOption::VALUE_NONE, 'Create a boot trait'],
['anonymous', 'a', InputOption::VALUE_NONE, 'Create a trait to anonymous data'],
['uuid', 'u', InputOption::VALUE_NONE, 'Create a trait to generate an uuid field'],
];
}
}
3 changes: 3 additions & 0 deletions src/Console/Make/MakeViewComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class MakeViewComposer extends CustomGeneratorCommand
*/
protected $description = 'Create a new view composer';

protected $signature = 'make:view:composer
{name : The name of the view composer}';

protected $type = 'View composer';

public function handle()
Expand Down
5 changes: 4 additions & 1 deletion src/Console/Publish/PublishStubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class PublishStubs extends Command
*/
protected $description = 'Publish the stubs of this package';

protected $signature = 'publish:stubs
{--f|force : Overwrite any existing files}';

/**
* Create a new command instance.
*
Expand All @@ -47,7 +50,7 @@ public function handle()

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

return $this->info('Stubs published successfully.');
return $this->info('Stubs published successfully');
}

protected function getOptions()
Expand Down
8 changes: 6 additions & 2 deletions src/Console/Run/RunFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class RunFactory extends Command
*/
protected $description = 'Run a factory';

protected $signature = 'run:factory
{model= : The model to run the factory for}
{amount= : The amount of records generated by the factory}';

/**
* Create a new command instance.
*
Expand Down Expand Up @@ -100,8 +104,8 @@ protected function getAmountOption()
protected function getArguments()
{
return [
['model', InputArgument::OPTIONAL, 'The model to run the factory for.'],
['amount', InputArgument::OPTIONAL, 'The amount of records generated by the factory.'],
['model', InputArgument::OPTIONAL, 'The model to run the factory for'],
['amount', InputArgument::OPTIONAL, 'The amount of records generated by the factory'],
];
}
}
1 change: 1 addition & 0 deletions src/LaravelStubsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function boot() {
Console\Make\MakeScope::class,
Console\Make\MakeInterface::class,
Console\Make\MakeViewComposer::class,
Console\Make\MakeFacade::class,
/** Publish */
Console\Publish\PublishStubs::class,
/** Run */
Expand Down
21 changes: 21 additions & 0 deletions src/stubs/facade.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace DummyNamespace;

use Illuminate\Support\Facades\Facade;

/**
* Class DummyClass
* @package DummyNamespace
*/
class DummyClass extends Facade
{

/**
* @return string
*/
protected static function getFacadeAccessor()
{
return DummyAccessor::class;
}
}
29 changes: 29 additions & 0 deletions src/stubs/trait-anonymous.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace DummyNamespace;

trait DummyClass
{

/**
* Bootstrap the trait.
*
* The following functions can be called statically:
* retrieved, creating, created, updating, updated,
* saving, saved, deleting, deleted, restoring, restored
*
* @return void
*/
public static function bootDummyClass(): void
{

static::deleted(function ($model) {

foreach (self::$anonymousFields as $field) {
$model->$field = null;
}

$model->save();
});
}
}
27 changes: 27 additions & 0 deletions src/stubs/trait-uuid.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace DummyNamespace;

use Illuminate\Support\Str;

trait DummyClass
{

/**
* Bootstrap the trait.
*
* The following functions can be called statically:
* retrieved, creating, created, updating, updated,
* saving, saved, deleting, deleted, restoring, restored
*
* @return void
*/
public static function bootDummyClass(): void
{

static::creating(function ($model) {

$model->$uuidField = Str::uuid();
});
}
}

0 comments on commit b72d8dc

Please sign in to comment.