This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Console/Shell.php

160 lines
4.3 KiB
PHP
Raw Normal View History

2010-01-14 09:45:37 +00:00
<?php
/*
* This file is part of the Symfony package.
2010-01-14 09:45:37 +00:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-01-14 09:45:37 +00:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2010-01-14 09:45:37 +00:00
*/
namespace Symfony\Component\Console;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
2010-01-14 09:45:37 +00:00
/**
* A Shell wraps an Application to add shell capabilities to it.
*
2011-09-29 13:51:30 +01:00
* Support for history and completion only works with a PHP compiled
* with readline support (either --with-readline or --with-libedit)
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Martin Hasoň <martin.hason@gmail.com>
2010-01-14 09:45:37 +00:00
*/
class Shell
{
private $application;
private $history;
private $output;
2011-07-01 09:40:43 +01:00
private $hasReadline;
private $prompt;
/**
* Constructor.
*
* If there is no readline support for the current PHP executable
* a \RuntimeException exception is thrown.
*
* @param Application $application An application instance
*
* @throws \RuntimeException When Readline extension is not enabled
*/
public function __construct(Application $application)
2010-01-14 09:45:37 +00:00
{
2011-09-29 13:51:30 +01:00
$this->hasReadline = function_exists('readline');
$this->application = $application;
$this->history = getenv('HOME').'/.history_'.$application->getName();
$this->output = new ConsoleOutput();
$this->prompt = $application->getName().' > ';
2010-01-14 09:45:37 +00:00
}
/**
* Runs the shell.
*/
public function run()
2010-01-14 09:45:37 +00:00
{
$this->application->setAutoExit(false);
$this->application->setCatchExceptions(true);
2010-01-14 09:45:37 +00:00
2011-07-01 09:40:43 +01:00
if ($this->hasReadline) {
readline_read_history($this->history);
readline_completion_function(array($this, 'autocompleter'));
}
2010-01-14 09:45:37 +00:00
$this->output->writeln($this->getHeader());
while (true) {
$command = $this->readline();
2010-01-14 09:45:37 +00:00
if (false === $command) {
$this->output->writeln("\n");
2010-01-14 09:45:37 +00:00
break;
}
2010-01-14 09:45:37 +00:00
2011-07-01 09:40:43 +01:00
if ($this->hasReadline) {
readline_add_history($command);
readline_write_history($this->history);
}
2010-01-14 09:45:37 +00:00
if (0 !== $ret = $this->application->run(new StringInput($command), $this->output)) {
$this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
}
}
2010-01-14 09:45:37 +00:00
}
/**
* Returns the shell header.
*
* @return string The header string
*/
protected function getHeader()
{
return <<<EOF
Welcome to the <info>{$this->application->getName()}</info> shell (<comment>{$this->application->getVersion()}</comment>).
At the prompt, type <comment>help</comment> for some help,
2011-10-29 11:01:39 +01:00
or <comment>list</comment> to get a list of available commands.
To exit the shell, type <comment>^D</comment>.
EOF;
}
/**
* Tries to return autocompletion for the current entered text.
*
* @param string $text The last segment of the entered text
* @return Boolean|array A list of guessed strings or true
*/
private function autocompleter($text)
2010-01-14 09:45:37 +00:00
{
$info = readline_info();
$text = substr($info['line_buffer'], 0, $info['end']);
if ($info['point'] !== $info['end']) {
return true;
}
// task name?
if (false === strpos($text, ' ') || !$text) {
made some method name changes to have a better coherence throughout the framework When an object has a "main" many relation with related "things" (objects, parameters, ...), the method names are normalized: * get() * set() * all() * replace() * remove() * clear() * isEmpty() * add() * register() * count() * keys() The classes below follow this method naming convention: * BrowserKit\CookieJar -> Cookie * BrowserKit\History -> Request * Console\Application -> Command * Console\Application\Helper\HelperSet -> HelperInterface * DependencyInjection\Container -> services * DependencyInjection\ContainerBuilder -> services * DependencyInjection\ParameterBag\ParameterBag -> parameters * DependencyInjection\ParameterBag\FrozenParameterBag -> parameters * DomCrawler\Form -> FormField * EventDispatcher\Event -> parameters * Form\FieldGroup -> Field * HttpFoundation\HeaderBag -> headers * HttpFoundation\ParameterBag -> parameters * HttpFoundation\Session -> attributes * HttpKernel\Profiler\Profiler -> DataCollectorInterface * Routing\RouteCollection -> Route * Security\Authentication\AuthenticationProviderManager -> AuthenticationProviderInterface * Templating\Engine -> HelperInterface * Translation\MessageCatalogue -> messages The usage of these methods are only allowed when it is clear that there is a main relation: * a CookieJar has many Cookies; * a Container has many services and many parameters (as services is the main relation, we use the naming convention for this relation); * a Console Input has many arguments and many options. There is no "main" relation, and so the naming convention does not apply. For many relations where the convention does not apply, the following methods must be used instead (where XXX is the name of the related thing): * get() -> getXXX() * set() -> setXXX() * all() -> getXXXs() * replace() -> setXXXs() * remove() -> removeXXX() * clear() -> clearXXX() * isEmpty() -> isEmptyXXX() * add() -> addXXX() * register() -> registerXXX() * count() -> countXXX() * keys()
2010-11-23 08:42:19 +00:00
return array_keys($this->application->all());
}
// options and arguments?
try {
$command = $this->application->find(substr($text, 0, strpos($text, ' ')));
} catch (\Exception $e) {
return true;
}
$list = array('--help');
foreach ($command->getDefinition()->getOptions() as $option) {
$list[] = '--'.$option->getName();
}
return $list;
2010-01-14 09:45:37 +00:00
}
/**
* Reads a single line from standard input.
*
* @return string The single line from standard input
*/
private function readline()
{
2011-07-01 09:40:43 +01:00
if ($this->hasReadline) {
$line = readline($this->prompt);
} else {
$this->output->write($this->prompt);
2011-07-01 09:40:43 +01:00
$line = fgets(STDIN, 1024);
$line = (!$line && strlen($line) == 0) ? false : rtrim($line);
}
2010-01-14 09:45:37 +00:00
2011-07-01 09:40:43 +01:00
return $line;
}
2010-01-14 09:45:37 +00:00
}