CodeIgniter4/system/CLI/BaseCommand.php

234 lines
6.1 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)
*
* 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-19 09:28:29 +02:00
* Class BaseCommand
*
* @property $group
* @property $name
* @property description
*
* @package CodeIgniter\CLI
*/
abstract class BaseCommand
{
2017-03-14 01:04:54 +02:00
/**
2017-03-19 09:28:29 +02:00
* The group the command is lumped under
* when listing commands.
*
* @var string
*/
2017-03-14 01:04:54 +02:00
protected $group;
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
/**
2017-03-19 09:28:29 +02:00
* The Command's name
*
* @var string
*/
2017-03-14 01:04:54 +02:00
protected $name;
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
/**
2017-03-19 09:28:29 +02:00
* the Command's usage description
*
* @var string
*/
2017-03-14 01:04:54 +02:00
protected $usage;
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
/**
2017-03-19 09:28:29 +02:00
* the Command's short description
*
* @var string
*/
2017-03-14 01:04:54 +02:00
protected $description;
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
/**
2017-03-19 09:28:29 +02:00
* the Command's options description
*
* @var string
*/
2017-03-14 01:04:54 +02:00
protected $options = array();
2017-03-19 09:28:29 +02:00
/**
* the Command's Arguments description
*
* @var string
*/
2017-03-14 01:04:54 +02:00
protected $arguments = array();
/**
2017-03-19 09:28:29 +02:00
* @var \Psr\Log\LoggerInterface
*/
2017-03-14 01:04:54 +02:00
protected $logger;
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
/**
2017-03-19 09:28:29 +02:00
* Instance of the CommandRunner controller
* so commands can call other commands.
*
* @var \CodeIgniter\CLI\CommandRunner
*/
2017-03-14 01:04:54 +02:00
protected $commands;
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
//--------------------------------------------------------------------
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
public function __construct(LoggerInterface $logger, CommandRunner $commands)
{
$this->logger = $logger;
$this->commands = $commands;
}
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
//--------------------------------------------------------------------
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
abstract public function run(array $params);
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
//--------------------------------------------------------------------
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
/**
2017-03-19 09:28:29 +02:00
* Can be used by a command to run other commands.
*
* @param string $command
* @param array $params
*/
protected function call(string $command, array $params = [])
2017-03-14 01:04:54 +02:00
{
// The CommandRunner will grab the first element
// for the command name.
array_unshift($params, $command);
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
return $this->commands->index($params);
}
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
//--------------------------------------------------------------------
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
/**
2017-03-19 09:28:29 +02:00
* A simple method to display an error with line/file,
* in child commands.
*
* @param \Exception $e
*/
2017-03-14 01:04:54 +02:00
protected function showError(\Exception $e)
{
CLI::newLine();
CLI::error($e->getMessage());
2017-03-19 09:28:29 +02:00
CLI::write($e->getFile() . ' - ' . $e->getLine());
2017-03-14 01:04:54 +02:00
CLI::newLine();
}
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
//--------------------------------------------------------------------
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
/**
2017-03-19 09:28:29 +02:00
* Makes it simple to access our protected properties.
*
* @param string $key
*
* @return mixed
*/
2017-03-14 01:04:54 +02:00
public function __get(string $key)
{
2017-03-19 09:28:29 +02:00
if (isset($this->$key)) {
2017-03-14 01:04:54 +02:00
return $this->$key;
}
}
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
//--------------------------------------------------------------------
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
/**
2017-03-19 09:28:29 +02:00
* show Help include (usage,arguments,description,options)
*
*
* @return mixed
*/
2017-03-14 01:04:54 +02:00
public function showHelp()
{
2017-03-19 09:28:29 +02:00
// 4 spaces insted of tab
$tab = " ";
2017-03-14 01:04:54 +02:00
CLI::write(lang('CLI.helpDescription'), 'yellow');
2017-03-19 09:28:29 +02:00
CLI::write($tab . $this->description);
2017-03-14 01:04:54 +02:00
CLI::newLine();
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
CLI::write(lang('CLI.helpUsage'), 'yellow');
$usage = empty($this->usage) ? $this->name . " [arguments]" : $this->usage;
2017-03-19 09:28:29 +02:00
CLI::write($tab . $usage);
2017-03-14 01:04:54 +02:00
CLI::newLine();
2017-03-19 09:28:29 +02:00
$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($tab . CLI::color(str_pad($argument, $pad), 'green') . $description, 'yellow');
2017-03-14 01:04:54 +02:00
}
2017-03-19 09:28:29 +02:00
CLI::newLine();
2017-03-14 01:04:54 +02:00
}
2017-03-19 09:28:29 +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');
2017-03-14 01:04:54 +02:00
}
CLI::newLine();
2017-03-19 09:28:29 +02:00
}
}
2017-03-14 01:04:54 +02:00
//--------------------------------------------------------------------
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
/**
2017-03-19 09:28:29 +02:00
* Get pad for $key => $value array output
*
* @param array $array
* @param int $pad
*
* @return int
*/
2017-03-14 01:04:54 +02:00
public function getPad($array, string $pad)
{
$max = 0;
2017-03-19 09:28:29 +02:00
foreach ($array as $key => $value) {
2017-03-14 01:04:54 +02:00
$max = max($max, strlen($key));
}
return $max + $pad;
}
2017-03-19 09:28:29 +02:00
2017-03-14 01:04:54 +02:00
//--------------------------------------------------------------------
}