CodeIgniter4/system/CLI/BaseCommand.php

232 lines
6.1 KiB
PHP
Raw Normal View History

<?php namespace CodeIgniter\CLI;
2017-01-20 00:57:53 -08:00
/**
2017-03-14 01:04:54 +02:00
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014 - 2017, British Columbia Institute of Technology
*
* 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.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 3.0.0
* @filesource
*/
2017-01-20 00:57:53 -08:00
use Psr\Log\LoggerInterface;
/**
2017-03-14 01:04:54 +02:00
* Class BaseCommand
*
* @property $group
* @property $name
* @property description
*
* @package CodeIgniter\CLI
*/
abstract class BaseCommand
{
2017-03-14 01:04:54 +02:00
/**
* 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
*
* @var string
*/
protected $options = array();
/**
2017-03-14 01:04:54 +02:00
* the Command's Arguments description
*
* @var string
*/
protected $arguments = array();
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Instance of the CommandRunner controller
* so commands can call other commands.
*
* @var \CodeIgniter\CLI\CommandRunner
*/
protected $commands;
//--------------------------------------------------------------------
public function __construct(LoggerInterface $logger, CommandRunner $commands)
{
$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
* @param array $params
*/
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)
*
*
* @return mixed
*/
public function showHelp()
{
$spaces= " ";
CLI::write(lang('CLI.helpDescription'), 'yellow');
CLI::write($spaces . $this->description);
2017-03-14 01:04:54 +02:00
CLI::newLine();
CLI::write(lang('CLI.helpUsage'), 'yellow');
$usage = empty($this->usage) ? $this->name . " [arguments]" : $this->usage;
CLI::write($spaces . $usage );
2017-03-14 01:04:54 +02:00
CLI::newLine();
$pad = max($this->getPad($this->options,6), $this->getPad($this->arguments,6));
if (!empty($this->arguments)){
CLI::write(lang('CLI.helpArguments'), 'yellow');
foreach ($this->arguments as $argument => $description) {
CLI::write( $spaces . CLI::color(str_pad($argument, $pad),'green') . $description,'yellow');
2017-03-14 01:04:54 +02:00
}
CLI::newLine();
2017-03-14 01:04:54 +02:00
}
if (!empty($this->options)){
CLI::write(lang('CLI.helpOptions'), 'yellow');
foreach ($this->options as $option => $description) {
CLI::write( $spaces . CLI::color(str_pad($option, $pad),'green') . $description,'yellow');
2017-03-14 01:04:54 +02:00
}
CLI::newLine();
2017-03-14 01:04:54 +02:00
}
}
//--------------------------------------------------------------------
/**
* Get pad for $key => $value array output
*
* @param array $array
* @param int $pad
*
* @return int
*/
public function getPad($array, string $pad)
{
$max = 0;
foreach ($array as $key => $value)
{
$max = max($max, strlen($key));
}
return $max + $pad;
}
//--------------------------------------------------------------------
}