-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from SevanNerse/master
added observer support
- Loading branch information
Showing
5 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace Akaunting\Module\Commands; | ||
|
||
use Akaunting\Module\Module; | ||
use Akaunting\Module\Support\Config\GenerateConfigReader; | ||
use Akaunting\Module\Support\Stub; | ||
use Akaunting\Module\Traits\ModuleCommandTrait; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
class ObserverMakeCommand extends GeneratorCommand | ||
{ | ||
use ModuleCommandTrait; | ||
|
||
protected $argumentName = 'name'; | ||
|
||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'module:make-observer'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create a new observer class for the specified module'; | ||
|
||
public function getTemplateContents() | ||
{ | ||
$module = $this->getModule(); | ||
|
||
return (new Stub($this->getStubName(), [ | ||
'NAMESPACE' => $this->getClassNamespace($module), | ||
'CLASS' => $this->getClass(), | ||
'MODELNAME' => $this->getModelName($module), | ||
'SHORTMODELNAME' => $this->getShortModelName(), | ||
'MODELVARIABLE' => $this->getModelVariable(), | ||
]))->render(); | ||
} | ||
|
||
public function getDestinationFilePath() | ||
{ | ||
$path = $this->laravel['module']->getModulePath($this->getModuleAlias()); | ||
|
||
$observerPath = GenerateConfigReader::read('observer'); | ||
|
||
return $path . $observerPath->getPath() . '/' . $this->getFileName() . '.php'; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getFileName() | ||
{ | ||
return Str::studly($this->argument('name')); | ||
} | ||
|
||
public function getDefaultNamespace(): string | ||
{ | ||
return $this->laravel['module']->config('paths.generator.observer.path', 'Observers'); | ||
} | ||
|
||
protected function getModelName(Module $module) | ||
{ | ||
$config = GenerateConfigReader::read('model'); | ||
|
||
$name = $this->laravel['module']->config('namespace') . "\\" . $module->getStudlyName() . "\\" . $config->getPath() . "\\" . $this->option('model'); | ||
|
||
return str_replace('/', '\\', $name); | ||
} | ||
|
||
protected function getShortModelName() | ||
{ | ||
return class_basename($this->option('model')); | ||
} | ||
|
||
protected function getModelVariable() | ||
{ | ||
return strtolower(class_basename($this->option('model'))); | ||
} | ||
|
||
/** | ||
* Get the console command arguments. | ||
* | ||
* @return array | ||
*/ | ||
protected function getArguments() | ||
{ | ||
return [ | ||
['name', InputArgument::REQUIRED, 'The name of the observer.'], | ||
['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], | ||
]; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function getOptions() | ||
{ | ||
return [ | ||
['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the observer applies to.'], | ||
]; | ||
} | ||
|
||
/** | ||
* Get the stub file name based on the options | ||
* @return string | ||
*/ | ||
protected function getStubName() | ||
{ | ||
$stub = '/observer-plain.stub'; | ||
|
||
if ($this->option('model')) { | ||
$stub = '/observer.stub'; | ||
} | ||
|
||
return $stub; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace $NAMESPACE$; | ||
|
||
class $CLASS$ | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace $NAMESPACE$; | ||
|
||
use $MODELNAME$ as Model; | ||
|
||
class $CLASS$ | ||
{ | ||
/** | ||
* Handle the $SHORTMODELNAME$ "created" event. | ||
* | ||
* @param \$MODELNAME$ $$MODELVARIABLE$ | ||
* @return void | ||
*/ | ||
public function created(Model $$MODELVARIABLE$) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Handle the $SHORTMODELNAME$ "updated" event. | ||
* | ||
* @param \$MODELNAME$ $$MODELVARIABLE$ | ||
* @return void | ||
*/ | ||
public function updated(Model $$MODELVARIABLE$) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Handle the $SHORTMODELNAME$ "deleted" event. | ||
* | ||
* @param \$MODELNAME$ $$MODELVARIABLE$ | ||
* @return void | ||
*/ | ||
public function deleted(Model $$MODELVARIABLE$) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Handle the $SHORTMODELNAME$ "restored" event. | ||
* | ||
* @param \$MODELNAME$ $$MODELVARIABLE$ | ||
* @return void | ||
*/ | ||
public function restored(Model $$MODELVARIABLE$) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Handle the $SHORTMODELNAME$ "force deleted" event. | ||
* | ||
* @param \$MODELNAME$ $$MODELVARIABLE$ | ||
* @return void | ||
*/ | ||
public function forceDeleted(Model $$MODELVARIABLE$) | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters