Skip to content

Commit

Permalink
Add event generator
Browse files Browse the repository at this point in the history
  • Loading branch information
hariadi committed May 16, 2019
1 parent 8fb6412 commit e5b0e48
Show file tree
Hide file tree
Showing 19 changed files with 542 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
app
composer.lock
vendor
*.php_cs.cache
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
}
],
"require": {
"illuminate/console": "^5.4",
"illuminate/support": "^5.4",
"illuminate/filesystem": "^5.4"
"illuminate/console": "^5.7",
"illuminate/support": "^5.7",
"illuminate/filesystem": "^5.7"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/AppAttributeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function getClassName()
*/
protected function getDefaultNamespace($rootNamespace)
{
$namespace = $this->option('namespace') ?? $this->argument('name');
$namespace = $this->option('namespace') ?? $this->argument('name');
return $rootNamespace . '\Models' . '\\' . $namespace . '\Traits' . '\\' . $this->type;
}
}
114 changes: 114 additions & 0 deletions src/Commands/AppEventCreatedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Hariadi\Boilerplate\Commands;

class AppEventCreatedCommand extends AppGeneratorCommand
{
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Created';

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'event:created
{name : The name of the class}
{--E|event=* : Default created event}';

/**
* Default events
*
* @var array
*/
protected $events = ['created', 'updated', 'deleted'];

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new created event for model';

/**
* The methods available.
*
* @var array
*/
protected function getMethods()
{
return [];
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/event.stub';
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$events = $this->option('event');

if (empty($events)) {
$events = $this->events;
}

if (parent::handle() !== false) {
if (in_array('updated', $events)) {
$this->call('event:updated', [
'name' => $this->argument('name'),
]);
}

if (in_array('deleted', $events)) {
$this->call('event:deleted', [
'name' => $this->argument('name'),
]);
}


$this->call('app:listener', [
'name' => $this->argument('name')
]);

$this->info('You need to subscribe the listener to your EventServiceProvider manually');
}
}

/**
* Get the intended name for class.
*
* @return string
*/
protected function getClassName()
{
return basename($this->getNameInput()) . $this->type;
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
$namespace = $this->argument('name');

return $rootNamespace . '\Events\Backend' . '\\' . $namespace;
}
}
80 changes: 80 additions & 0 deletions src/Commands/AppEventDeletedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Hariadi\Boilerplate\Commands;

class AppEventDeletedCommand extends AppGeneratorCommand
{
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Deleted';

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'event:deleted {name : The name of the class}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new deleted event for model';

/**
* The methods available.
*
* @var array
*/
protected function getMethods()
{
return [];
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/event.stub';
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
parent::handle();
}

/**
* Get the intended name for class.
*
* @return string
*/
protected function getClassName()
{
return basename($this->getNameInput()) . $this->type;
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
$namespace = $this->argument('name');

return $rootNamespace . '\Events\Backend' . '\\' . $namespace;
}
}
88 changes: 88 additions & 0 deletions src/Commands/AppEventListenerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Hariadi\Boilerplate\Commands;

class AppEventListenerCommand extends AppGeneratorCommand
{
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'EventListener';

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:listener {name : The name of the class}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new event listener for model';

/**
* The methods available.
*
* @var array
*/
protected function getMethods()
{
return ['created', 'updated', 'deleted'];
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/listener.stub';
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// $event = $this->option('event');

// if ($event != 'created') {
// $this->methods = [$event];
// }

// dd($this->methods);

parent::handle();
}

/**
* Get the intended name for class.
*
* @return string
*/
protected function getClassName()
{
return basename($this->getNameInput()) . $this->type;
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
$namespace = $this->argument('name');

return $rootNamespace . '\Listeners\Backend' . '\\' . $namespace;
}
}
80 changes: 80 additions & 0 deletions src/Commands/AppEventUpdatedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Hariadi\Boilerplate\Commands;

class AppEventUpdatedCommand extends AppGeneratorCommand
{
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Updated';

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'event:updated {name : The name of the class}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new updated event for model';

/**
* The methods available.
*
* @var array
*/
protected function getMethods()
{
return [];
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/event.stub';
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
parent::handle();
}

/**
* Get the intended name for class.
*
* @return string
*/
protected function getClassName()
{
return basename($this->getNameInput()) . $this->type;
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
$namespace = $this->argument('name');

return $rootNamespace . '\Events\Backend' . '\\' . $namespace;
}
}
Loading

0 comments on commit e5b0e48

Please sign in to comment.