Skip to content

Commit

Permalink
- Migrate console commands to Lovata.Toolbox console
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaro-canepa committed Dec 21, 2020
1 parent c483a5a commit 2581b1d
Show file tree
Hide file tree
Showing 23 changed files with 354 additions and 317 deletions.
37 changes: 37 additions & 0 deletions classes/console/CommonConsole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php namespace PlanetaDelEste\ApiToolbox\Classes\Console;

use Lovata\Toolbox\Classes\Console\CommonCreateFile;

class CommonConsole extends CommonCreateFile
{
/**
* @var string[]
*/
protected $arLogoLovata = [
'▒█▀▀█ █░░ █▀▀█ █▀▀▄ █▀▀ ▀▀█▀▀ █▀▀█ ▒█▀▀▄ █▀▀ █░░ ▒█▀▀▀ █▀▀ ▀▀█▀▀ █▀▀',
'▒█▄▄█ █░░ █▄▄█ █░░█ █▀▀ ░░█░░ █▄▄█ ▒█░▒█ █▀▀ █░░ ▒█▀▀▀ ▀▀█ ░░█░░ █▀▀',
'▒█░░░ ▀▀▀ ▀░░▀ ▀░░▀ ▀▀▀ ░░▀░░ ▀░░▀ ▒█▄▄▀ ▀▀▀ ▀▀▀ ▒█▄▄▄ ▀▀▀ ░░▀░░ ▀▀▀',
];

protected function getModelCachedAttrs()
{
$sExpansionAuthor = array_get($this->arData, 'replace.studly_expansion_author');
$sExpansionPlugin = array_get($this->arData, 'replace.studly_expansion_plugin');
$sModel = array_get($this->arData, 'replace.studly_model');
if (!$sExpansionAuthor || !$sExpansionPlugin || !$sModel) {
return;
}

/** @var \Model|\Eloquent $obModel */
$obModel = app(join("\\", [$sExpansionAuthor, $sExpansionPlugin, 'Models', $sModel]));
if ($obModel) {
if (property_exists($obModel, 'cached')) {
$arColumns = $obModel->cached;
$sFileContent = \File::get(plugins_path('planetadeleste/apitoolbox/classes/parser/templates/cached_attributes.stub'));
$sContent = \Twig::parse($sFileContent, ['attributes' => $arColumns]);
array_set($this->arData, 'replace.cached', $sContent);
$this->setEnableList($arColumns);
}
}
}
}
97 changes: 16 additions & 81 deletions classes/console/CreateApiController.php
Original file line number Diff line number Diff line change
@@ -1,91 +1,26 @@
<?php namespace PlanetaDelEste\ApiToolbox\Classes\Console;

use October\Rain\Scaffold\GeneratorCommand;
use October\Rain\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use PlanetaDelEste\ApiToolbox\Classes\Parser\Create\ControllerCreateFile;

class CreateApiController extends GeneratorCommand
class CreateApiController extends CommonConsole
{
/**
* @var string The console command name.
*/
protected $name = 'apitoolbox:create:controller';

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

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Api Controller';

/**
* A mapping of stub to generated file.
*
* @var array
*/
protected $stubs = [
'controllers/Controller.stub' => 'controllers/api/{{studly_name}}.php',
];

/**
* @inheritDoc
*/
protected function prepareVars()
{
$pluginCode = $this->argument('plugin');

$parts = explode('.', $pluginCode);
$plugin = array_pop($parts);
$author = array_pop($parts);

$controller = $this->argument('controller');

/*
* Determine the model name to use,
* either supplied or singular from the controller name.
*/
$model = $this->option('model');
if (!$model) {
$model = Str::singular($controller);
}

return [
'name' => $controller,
'model' => $model,
'author' => $author,
'plugin' => $plugin
];
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['plugin', InputArgument::REQUIRED, 'The name of the plugin to create. Eg: RainLab.Blog'],
['controller', InputArgument::REQUIRED, 'The name of the controller to create. Eg: Posts'],
];
}
/** @var string The console command name. */
protected $name = 'toolbox:create.api.controller';
/** @var string The console command description. */
protected $description = 'Create a new api resource index collection.';

/**
* Get the console command options.
*
* @return array
* Execute the console command.
*/
protected function getOptions()
public function handle()
{
return [
['force', null, InputOption::VALUE_NONE, 'Overwrite existing files with generated ones.'],
['model', null, InputOption::VALUE_OPTIONAL, 'The name of the model. Eg: Post'],
];
parent::handle();

$this->setAuthor(true);
$this->setPlugin(true);
$this->setModel();
$this->setController();
$this->setAdditionList(self::CODE_COMMAND_PARENT);
$this->createFile(ControllerCreateFile::class);
}
}
25 changes: 25 additions & 0 deletions classes/console/CreateApiResourceIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php namespace PlanetaDelEste\ApiToolbox\Classes\Console;

use PlanetaDelEste\ApiToolbox\Classes\Parser\Create\ResourceIndexCreateFile;

class CreateApiResourceIndex extends CommonConsole
{
/** @var string The console command name. */
protected $name = 'toolbox:create.api.resourceindex';
/** @var string The console command description. */
protected $description = 'Create a new api resource index collection.';

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

$this->setAuthor(true);
$this->setPlugin(true);
$this->setModel();
$this->setAdditionList(self::CODE_COMMAND_PARENT);
$this->createFile(ResourceIndexCreateFile::class);
}
}
27 changes: 27 additions & 0 deletions classes/console/CreateApiResourceItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace PlanetaDelEste\ApiToolbox\Classes\Console;

use PlanetaDelEste\ApiToolbox\Classes\Parser\Create\ResourceItemCreateFile;

class CreateApiResourceItem extends CommonConsole
{
/** @var string The console command name. */
protected $name = 'toolbox:create.api.resourceitem';
/** @var string The console command description. */
protected $description = 'Create a new api resource index collection.';

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

$this->setAuthor(true);
$this->setPlugin(true);
$this->setModel();
// $this->setFieldList(['external_id', self::CODE_VIEW_COUNT]);
$this->getModelCachedAttrs();
$this->setAdditionList(self::CODE_COMMAND_PARENT);
$this->createFile(ResourceItemCreateFile::class);
}
}
25 changes: 25 additions & 0 deletions classes/console/CreateApiResourceList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php namespace PlanetaDelEste\ApiToolbox\Classes\Console;

use PlanetaDelEste\ApiToolbox\Classes\Parser\Create\ResourceListCreateFile;

class CreateApiResourceList extends CommonConsole
{
/** @var string The console command name. */
protected $name = 'toolbox:create.api.resourcelist';
/** @var string The console command description. */
protected $description = 'Create a new api resource list collection.';

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

$this->setAuthor(true);
$this->setPlugin(true);
$this->setModel();
$this->setAdditionList(self::CODE_COMMAND_PARENT);
$this->createFile(ResourceListCreateFile::class);
}
}
27 changes: 27 additions & 0 deletions classes/console/CreateApiResourceShow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace PlanetaDelEste\ApiToolbox\Classes\Console;

use PlanetaDelEste\ApiToolbox\Classes\Parser\Create\ResourceShowCreateFile;

class CreateApiResourceShow extends CommonConsole
{
/** @var string The console command name. */
protected $name = 'toolbox:create.api.resourceshow';
/** @var string The console command description. */
protected $description = 'Create a new api resource show item.';

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

$this->setAuthor(true);
$this->setPlugin(true);
$this->setModel();
// $this->setFieldList(['external_id', self::CODE_VIEW_COUNT]);
$this->getModelCachedAttrs();
$this->setAdditionList(self::CODE_COMMAND_PARENT);
$this->createFile(ResourceShowCreateFile::class);
}
}
35 changes: 35 additions & 0 deletions classes/console/CreateApiResources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php namespace PlanetaDelEste\ApiToolbox\Classes\Console;

use Lovata\Toolbox\Classes\Parser\Create\ModelCreateFile;

class CreateApiResources extends CommonConsole
{
/** @var string The console command name. */
protected $name = 'toolbox:create.api.resources';
/** @var string The console command description. */
protected $description = 'Create a new api resource items.';

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

$this->setAuthor(true);
$this->setPlugin(true);
$this->setModel();
// $this->setFieldList(['external_id', self::CODE_VIEW_COUNT]);
$this->getModelCachedAttrs();
$this->setAdditionList(self::CODE_COMMAND_PARENT);
$this->callCommandList();
}

protected function callCommandList()
{
$this->call('toolbox:create.api.resourceitem', ['data' => $this->arData]);
$this->call('toolbox:create.api.resourceshow', ['data' => $this->arData]);
$this->call('toolbox:create.api.resourceindex', ['data' => $this->arData]);
$this->call('toolbox:create.api.resourcelist', ['data' => $this->arData]);
}
}
Loading

0 comments on commit 2581b1d

Please sign in to comment.