Skip to content

Commit

Permalink
Merge pull request #9 from SevanNerse/master
Browse files Browse the repository at this point in the history
added observer support
  • Loading branch information
denisdulici authored Apr 1, 2021
2 parents 7baae6c + 5c0f7e1 commit 5205b70
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 0 deletions.
124 changes: 124 additions & 0 deletions src/Commands/ObserverMakeCommand.php
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;
}
}
8 changes: 8 additions & 0 deletions src/Commands/stubs/observer-plain.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace $NAMESPACE$;

class $CLASS$
{
//
}
63 changes: 63 additions & 0 deletions src/Commands/stubs/observer.stub
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$)
{
//
}
}
1 change: 1 addition & 0 deletions src/Config/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
'route' => ['path' => 'Routes', 'generate' => true],
'component' => ['path' => 'View/Components', 'generate' => false],
'cast' => ['path' => 'Casts', 'generate' => false],
'observer' => ['path' => 'Observers', 'generate' => false],
],
],

Expand Down
1 change: 1 addition & 0 deletions src/Providers/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Console extends ServiceProvider
Commands\UseCommand::class,
Commands\ResourceMakeCommand::class,
Commands\TestMakeCommand::class,
Commands\ObserverMakeCommand::class,
];

/**
Expand Down

0 comments on commit 5205b70

Please sign in to comment.