CodeIgniter4/system/CLI/BaseCommand.php

262 lines
4.9 KiB
PHP
Raw Normal View History

2019-03-27 03:26:42 -07:00
<?php
2020-07-12 21:30:19 +08:00
2017-01-20 00:57:53 -08:00
/**
2020-10-24 16:38:41 +08:00
* This file is part of the CodeIgniter 4 framework.
2017-03-19 09:28:29 +02:00
*
2020-10-24 16:38:41 +08:00
* (c) CodeIgniter Foundation <admin@codeigniter.com>
2017-03-19 09:28:29 +02:00
*
2020-10-24 16:38:41 +08:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2017-03-19 09:28:29 +02:00
*/
2018-11-10 02:57:39 -02:00
2019-03-27 03:26:42 -07:00
namespace CodeIgniter\CLI;
use Psr\Log\LoggerInterface;
2020-10-04 00:27:56 +07:00
use ReflectionException;
2020-08-14 20:44:19 +08:00
use Throwable;
/**
2021-05-14 01:29:18 +08:00
* BaseCommand is the base class used in creating CLI commands.
*
* @property string $group
* @property string $name
* @property string $usage
* @property string $description
* @property array $options
* @property array $arguments
* @property LoggerInterface $logger
* @property Commands $commands
2017-03-19 09:28:29 +02:00
*/
abstract class BaseCommand
{
/**
* The group the command is lumped under
* when listing commands.
*
* @var string
*/
protected $group;
/**
* The Command's name
*
* @var string
*/
protected $name;
/**
* the Command's usage description
*
* @var string
*/
protected $usage;
/**
* the Command's short description
*
* @var string
*/
protected $description;
/**
* the Command's options description
*
2017-07-23 11:34:40 -03:00
* @var array
*/
protected $options = [];
/**
* the Command's Arguments description
*
2017-07-23 11:34:40 -03:00
* @var array
*/
protected $arguments = [];
/**
2019-03-27 03:26:42 -07:00
* The Logger to use for a command
*
2020-10-04 00:27:56 +07:00
* @var LoggerInterface
*/
protected $logger;
/**
2020-08-12 00:10:17 +08:00
* Instance of Commands so
* commands can call other commands.
*
2020-10-04 00:27:56 +07:00
* @var Commands
*/
protected $commands;
2017-07-23 11:34:40 -03:00
/**
* BaseCommand constructor.
*
2020-10-04 00:27:56 +07:00
* @param LoggerInterface $logger
* @param Commands $commands
2017-07-23 11:34:40 -03:00
*/
public function __construct(LoggerInterface $logger, Commands $commands)
{
2018-11-10 02:57:39 -02:00
$this->logger = $logger;
$this->commands = $commands;
}
2019-03-27 03:26:42 -07:00
/**
* Actually execute a command.
* This has to be over-ridden in any concrete implementation.
*
* @param array $params
*/
abstract public function run(array $params);
/**
* Can be used by a command to run other commands.
*
* @param string $command
2018-11-10 02:57:39 -02:00
* @param array $params
2017-07-24 23:25:31 -03:00
*
* @return mixed
2020-10-04 00:27:56 +07:00
* @throws ReflectionException
*/
protected function call(string $command, array $params = [])
{
return $this->commands->run($command, $params);
}
/**
2020-07-31 21:34:22 +02:00
* A simple method to display an error with line/file, in child commands.
*
2020-08-14 20:44:19 +08:00
* @param Throwable $e
*/
2020-08-14 20:44:19 +08:00
protected function showError(Throwable $e)
{
2020-08-14 20:44:19 +08:00
$exception = $e;
$message = $e->getMessage();
require APPPATH . 'Views/errors/cli/error_exception.php';
}
/**
2020-07-31 21:34:22 +02:00
* Show Help includes (Usage, Arguments, Description, Options).
*/
2020-07-31 21:34:22 +02:00
public function showHelp()
{
2020-07-31 21:34:22 +02:00
CLI::write(lang('CLI.helpUsage'), 'yellow');
2020-07-31 21:34:22 +02:00
if (! empty($this->usage))
{
2020-07-31 21:34:22 +02:00
$usage = $this->usage;
}
2020-07-31 21:34:22 +02:00
else
{
$usage = $this->name;
2019-04-17 23:23:12 +05:30
2020-07-31 21:34:22 +02:00
if (! empty($this->arguments))
{
$usage .= ' [arguments]';
}
}
2020-07-31 21:34:22 +02:00
CLI::write($this->setPad($usage, 0, 0, 2));
2020-07-31 21:34:22 +02:00
if (! empty($this->description))
{
CLI::newLine();
CLI::write(lang('CLI.helpDescription'), 'yellow');
CLI::write($this->setPad($this->description, 0, 0, 2));
}
2018-11-10 02:57:39 -02:00
if (! empty($this->arguments))
{
2020-07-31 21:34:22 +02:00
CLI::newLine();
CLI::write(lang('CLI.helpArguments'), 'yellow');
2020-07-31 21:34:22 +02:00
$length = max(array_map('strlen', array_keys($this->arguments)));
foreach ($this->arguments as $argument => $description)
{
2020-07-31 21:34:22 +02:00
CLI::write(CLI::color($this->setPad($argument, $length, 2, 2), 'green') . $description);
}
}
2018-11-10 02:57:39 -02:00
if (! empty($this->options))
{
2020-07-31 21:34:22 +02:00
CLI::newLine();
CLI::write(lang('CLI.helpOptions'), 'yellow');
2020-07-31 21:34:22 +02:00
$length = max(array_map('strlen', array_keys($this->options)));
foreach ($this->options as $option => $description)
{
2020-07-31 21:34:22 +02:00
CLI::write(CLI::color($this->setPad($option, $length, 2, 2), 'green') . $description);
}
}
}
2020-07-31 21:34:22 +02:00
/**
* Pads our string out so that all titles are the same length to nicely line up descriptions.
*
* @param string $item
* @param integer $max
* @param integer $extra How many extra spaces to add at the end
2020-07-31 21:34:22 +02:00
* @param integer $indent
*
* @return string
*/
public function setPad(string $item, int $max, int $extra = 2, int $indent = 0): string
2020-07-31 21:34:22 +02:00
{
$max += $extra + $indent;
return str_pad(str_repeat(' ', $indent) . $item, $max);
}
/**
* Get pad for $key => $value array output
*
2018-11-10 02:57:39 -02:00
* @param array $array
* @param integer $pad
*
2018-11-10 02:57:39 -02:00
* @return integer
*
* @deprecated Use setPad() instead.
*
* @codeCoverageIgnore
*/
2019-04-17 23:23:12 +05:30
public function getPad(array $array, int $pad): int
{
$max = 0;
2021-05-14 01:29:18 +08:00
foreach (array_keys($array) as $key)
{
$max = max($max, strlen($key));
}
2021-05-14 01:29:18 +08:00
return $max + $pad;
}
2020-07-31 21:34:22 +02:00
/**
* Makes it simple to access our protected properties.
*
* @param string $key
*
* @return mixed
*/
public function __get(string $key)
{
if (isset($this->$key))
{
return $this->$key;
}
return null;
}
/**
* Makes it simple to check our protected properties.
*
* @param string $key
*
* @return boolean
*/
public function __isset(string $key): bool
{
return isset($this->$key);
}
}