CodeIgniter4/system/CLI/BaseCommand.php

242 lines
5.6 KiB
PHP
Raw Normal View History

<?php namespace CodeIgniter\CLI;
2017-01-20 00:57:53 -08:00
/**
2017-03-19 09:28:29 +02:00
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
2018-12-26 21:49:51 -08:00
* Copyright (c) 2014-2019 British Columbia Institute of Technology
2017-03-19 09:28:29 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
2018-11-10 02:57:39 -02:00
* @package CodeIgniter
* @author CodeIgniter Dev Team
2018-12-26 21:49:51 -08:00
* @copyright 2014-2019 British Columbia Institute of Technology (https://bcit.ca/)
2018-11-10 02:57:39 -02:00
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 3.0.0
2017-03-19 09:28:29 +02:00
* @filesource
*/
2018-11-10 02:57:39 -02:00
use Psr\Log\LoggerInterface;
/**
2017-03-19 09:28:29 +02:00
* Class BaseCommand
*
* @property $group
* @property $name
2017-07-23 11:34:40 -03:00
* @property $description
2017-03-19 09:28:29 +02:00
*
* @package CodeIgniter\CLI
*/
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 = [];
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Instance of the CommandRunner controller
* so commands can call other commands.
*
* @var \CodeIgniter\CLI\CommandRunner
*/
protected $commands;
//--------------------------------------------------------------------
2017-07-23 11:34:40 -03:00
/**
* BaseCommand constructor.
*
* @param \Psr\Log\LoggerInterface $logger
* @param \CodeIgniter\CLI\CommandRunner $commands
*/
public function __construct(LoggerInterface $logger, CommandRunner $commands)
{
2018-11-10 02:57:39 -02:00
$this->logger = $logger;
$this->commands = $commands;
}
//--------------------------------------------------------------------
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
*/
protected function call(string $command, array $params = [])
{
// The CommandRunner will grab the first element
// for the command name.
array_unshift($params, $command);
return $this->commands->index($params);
}
//--------------------------------------------------------------------
/**
* A simple method to display an error with line/file,
* in child commands.
*
* @param \Exception $e
*/
protected function showError(\Exception $e)
{
CLI::newLine();
CLI::error($e->getMessage());
CLI::write($e->getFile() . ' - ' . $e->getLine());
CLI::newLine();
}
//--------------------------------------------------------------------
/**
* 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;
}
}
//--------------------------------------------------------------------
/**
* show Help include (usage,arguments,description,options)
*/
public function showHelp()
{
// 4 spaces insted of tab
2018-11-10 02:57:39 -02:00
$tab = ' ';
CLI::write(lang('CLI.helpDescription'), 'yellow');
CLI::write($tab . $this->description);
CLI::newLine();
CLI::write(lang('CLI.helpUsage'), 'yellow');
2018-11-10 02:57:39 -02:00
$usage = empty($this->usage) ? $this->name . ' [arguments]' : $this->usage;
CLI::write($tab . $usage);
CLI::newLine();
$pad = max($this->getPad($this->options, 6), $this->getPad($this->arguments, 6));
2018-11-10 02:57:39 -02:00
if (! empty($this->arguments))
{
CLI::write(lang('CLI.helpArguments'), 'yellow');
foreach ($this->arguments as $argument => $description)
{
CLI::write($tab . CLI::color(str_pad($argument, $pad), 'green') . $description, 'yellow');
}
CLI::newLine();
}
2018-11-10 02:57:39 -02:00
if (! empty($this->options))
{
CLI::write(lang('CLI.helpOptions'), 'yellow');
foreach ($this->options as $option => $description)
{
CLI::write($tab . CLI::color(str_pad($option, $pad), 'green') . $description, 'yellow');
}
CLI::newLine();
}
}
//--------------------------------------------------------------------
/**
* 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
*/
2017-07-23 11:34:40 -03:00
public function getPad($array, int $pad)
{
$max = 0;
foreach ($array as $key => $value)
{
$max = max($max, strlen($key));
}
return $max + $pad;
}
//--------------------------------------------------------------------
}