|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Hariadi\Boilerplate\Commands; |
| 4 | + |
| 5 | +class AppEventCreatedCommand extends AppGeneratorCommand |
| 6 | +{ |
| 7 | + /** |
| 8 | + * The type of class being generated. |
| 9 | + * |
| 10 | + * @var string |
| 11 | + */ |
| 12 | + protected $type = 'Created'; |
| 13 | + |
| 14 | + /** |
| 15 | + * The name and signature of the console command. |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $signature = 'event:created |
| 20 | + {name : The name of the class} |
| 21 | + {--E|event=* : Default created event}'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Default events |
| 25 | + * |
| 26 | + * @var array |
| 27 | + */ |
| 28 | + protected $events = ['created', 'updated', 'deleted']; |
| 29 | + |
| 30 | + /** |
| 31 | + * The console command description. |
| 32 | + * |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + protected $description = 'Create a new created event for model'; |
| 36 | + |
| 37 | + /** |
| 38 | + * The methods available. |
| 39 | + * |
| 40 | + * @var array |
| 41 | + */ |
| 42 | + protected function getMethods() |
| 43 | + { |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the stub file for the generator. |
| 49 | + * |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + protected function getStub() |
| 53 | + { |
| 54 | + return __DIR__.'/stubs/event.stub'; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Execute the console command. |
| 59 | + * |
| 60 | + * @return mixed |
| 61 | + */ |
| 62 | + public function handle() |
| 63 | + { |
| 64 | + $events = $this->option('event'); |
| 65 | + |
| 66 | + if (empty($events)) { |
| 67 | + $events = $this->events; |
| 68 | + } |
| 69 | + |
| 70 | + if (parent::handle() !== false) { |
| 71 | + if (in_array('updated', $events)) { |
| 72 | + $this->call('event:updated', [ |
| 73 | + 'name' => $this->argument('name'), |
| 74 | + ]); |
| 75 | + } |
| 76 | + |
| 77 | + if (in_array('deleted', $events)) { |
| 78 | + $this->call('event:deleted', [ |
| 79 | + 'name' => $this->argument('name'), |
| 80 | + ]); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + $this->call('app:listener', [ |
| 85 | + 'name' => $this->argument('name') |
| 86 | + ]); |
| 87 | + |
| 88 | + $this->info('You need to subscribe the listener to your EventServiceProvider manually'); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get the intended name for class. |
| 94 | + * |
| 95 | + * @return string |
| 96 | + */ |
| 97 | + protected function getClassName() |
| 98 | + { |
| 99 | + return basename($this->getNameInput()) . $this->type; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Get the default namespace for the class. |
| 104 | + * |
| 105 | + * @param string $rootNamespace |
| 106 | + * @return string |
| 107 | + */ |
| 108 | + protected function getDefaultNamespace($rootNamespace) |
| 109 | + { |
| 110 | + $namespace = $this->argument('name'); |
| 111 | + |
| 112 | + return $rootNamespace . '\Events\Backend' . '\\' . $namespace; |
| 113 | + } |
| 114 | +} |
0 commit comments