Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring extension console #7

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
3 changes: 2 additions & 1 deletion Engine/Auth/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Bootstrap extends AbstractBootstrap {
/**
* Bootstrap constructor.
*/
public function __construct() {
protected function __construct() {
parent::__construct();
$this->models = [
User::class,
];
Expand Down
8 changes: 6 additions & 2 deletions Engine/Console/.meta/.phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
namespace PHPSTORM_META {

if (function_exists('override')) {
override(\Oforge\Engine\Core\Manager\Services\ServiceManager::get(0), map([
'console' => \Oforge\Engine\Console\Services\ConsoleService::class,
// AbstractBootstrap configuration
override(\Oforge\Engine\Core\Abstracts\AbstractBootstrap::getConfiguration(0), map([
'commands' => '',
]));
override(\Oforge\Engine\Core\Abstracts\AbstractBootstrap::setConfiguration(0), map([
'commands' => '',
]));
}

Expand Down
111 changes: 111 additions & 0 deletions Engine/Console/Abstracts/AbstractBatchCallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Oforge\Engine\Console\Abstracts;

use Exception;
use RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class AbstractBatchCallCommand
*
* @package Oforge\Engine\Console\Abstracts
*/
abstract class AbstractBatchCallCommand extends AbstractCommand {
const OPTION_STOP_ON_ERROR = 'stop-on-error';
/**
* Array with other commands to be called with optional input.
* $commands = [
* 'command_1', // Command name only, no input
* 'command_1' => '<methodname>', // Command name with method name(for dynamic inout at runtime), must be in static class.
* 'command_1' => 'arg_1 --option_1', // Command name with string input
* 'command_1' => [ // Command name with array input
* // 'command' => 'command_1', // Command key, optional. If not present, key is prepended.
* 'arg_1' => value,
* '--option_1' => value,
* ],
* ]
*
* @var array $commands
*/
protected $commands = null;

/**
* AbstractBatchCallCommand constructor.
*
* @param string|null $name
* @param array $commands
* @throws RuntimeException
*/
public function __construct(string $name = null, array $commands = []) {
$this->commands = $this->commands ?? $commands;
$this->checkBatchCallConfig();
parent::__construct($name);
}

/** @inheritdoc */
protected function configure() {
$this->addOption(self::OPTION_STOP_ON_ERROR, null, InputOption::VALUE_NONE, 'Quit when a subcommand fails.');
parent::configure();
}

/**
* @inheritDoc
* @throws RuntimeException
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output) : int {
$this->checkBatchCallConfig();
$stopOnError = $input->getOption(self::OPTION_STOP_ON_ERROR);
foreach ($this->commands as $name => $args) {
if (!is_string($name)) {
$name = $args;
$args = [];
}
if ($this->getName() === $name) {
continue;
}
if (is_string($args) && method_exists($this, $args)) {
$args = $this->$args();
}
$errorCode = $this->callOtherCommand($output, $name, $args);
if ($errorCode === self::FAILURE && $stopOnError) {
return self::FAILURE;
}
}

return self::SUCCESS;
}

/**
* Validate command config
*
* @throws RuntimeException
*/
private function checkBatchCallConfig() {
if (!isset($this->commands)) {
throw new RuntimeException("Property 'commands' not defined.");
}
if (empty($this->commands)) {
throw new RuntimeException("Property 'commands' is empty.");
}
foreach ($this->commands as $name => $args) {
if (!is_string($name)) {
$name = $args;
$args = [];
}
if (!is_string($name)) {
throw new RuntimeException("Command name '$name' is not a string.");
}
if ($args === null) {
throw new RuntimeException("Arguments of command '$name' is null.");
}
if (!(is_string($args) || is_array($args))) {
throw new RuntimeException("Arguments of command '$name' is not a string or array.");
}
}
}

}
67 changes: 0 additions & 67 deletions Engine/Console/Abstracts/AbstractBatchCommand.php

This file was deleted.

Loading