2015-08-17 17:00:26 -07:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Console\Input ;
2016-04-20 09:56:34 -07:00
use Symfony\Component\Console\Exception\InvalidArgumentException ;
use Symfony\Component\Console\Exception\RuntimeException ;
2015-08-17 17:00:26 -07:00
/**
* Input is the base class for all concrete Input classes .
*
* Three concrete classes are provided by default :
*
* * `ArgvInput` : The input comes from the CLI arguments ( argv )
* * `StringInput` : The input is provided as a string
* * `ArrayInput` : The input is provided as an array
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
abstract class Input implements InputInterface
{
/**
* @ var InputDefinition
*/
protected $definition ;
protected $options = array ();
protected $arguments = array ();
protected $interactive = true ;
/**
* Constructor .
*
2017-02-02 16:28:38 -08:00
* @ param InputDefinition | null $definition A InputDefinition instance
2015-08-17 17:00:26 -07:00
*/
public function __construct ( InputDefinition $definition = null )
{
if ( null === $definition ) {
$this -> definition = new InputDefinition ();
} else {
$this -> bind ( $definition );
$this -> validate ();
}
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function bind ( InputDefinition $definition )
{
$this -> arguments = array ();
$this -> options = array ();
$this -> definition = $definition ;
$this -> parse ();
}
/**
* Processes command line arguments .
*/
abstract protected function parse ();
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function validate ()
{
2015-11-17 13:42:33 -08:00
$definition = $this -> definition ;
$givenArguments = $this -> arguments ;
$missingArguments = array_filter ( array_keys ( $definition -> getArguments ()), function ( $argument ) use ( $definition , $givenArguments ) {
return ! array_key_exists ( $argument , $givenArguments ) && $definition -> getArgument ( $argument ) -> isRequired ();
});
if ( count ( $missingArguments ) > 0 ) {
2016-04-20 09:56:34 -07:00
throw new RuntimeException ( sprintf ( 'Not enough arguments (missing: "%s").' , implode ( ', ' , $missingArguments )));
2015-08-17 17:00:26 -07:00
}
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function isInteractive ()
{
return $this -> interactive ;
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function setInteractive ( $interactive )
{
$this -> interactive = ( bool ) $interactive ;
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function getArguments ()
{
return array_merge ( $this -> definition -> getArgumentDefaults (), $this -> arguments );
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function getArgument ( $name )
{
if ( ! $this -> definition -> hasArgument ( $name )) {
2016-04-20 09:56:34 -07:00
throw new InvalidArgumentException ( sprintf ( 'The "%s" argument does not exist.' , $name ));
2015-08-17 17:00:26 -07:00
}
return isset ( $this -> arguments [ $name ]) ? $this -> arguments [ $name ] : $this -> definition -> getArgument ( $name ) -> getDefault ();
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function setArgument ( $name , $value )
{
if ( ! $this -> definition -> hasArgument ( $name )) {
2016-04-20 09:56:34 -07:00
throw new InvalidArgumentException ( sprintf ( 'The "%s" argument does not exist.' , $name ));
2015-08-17 17:00:26 -07:00
}
$this -> arguments [ $name ] = $value ;
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function hasArgument ( $name )
{
return $this -> definition -> hasArgument ( $name );
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function getOptions ()
{
return array_merge ( $this -> definition -> getOptionDefaults (), $this -> options );
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function getOption ( $name )
{
if ( ! $this -> definition -> hasOption ( $name )) {
2016-04-20 09:56:34 -07:00
throw new InvalidArgumentException ( sprintf ( 'The "%s" option does not exist.' , $name ));
2015-08-17 17:00:26 -07:00
}
return isset ( $this -> options [ $name ]) ? $this -> options [ $name ] : $this -> definition -> getOption ( $name ) -> getDefault ();
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function setOption ( $name , $value )
{
if ( ! $this -> definition -> hasOption ( $name )) {
2016-04-20 09:56:34 -07:00
throw new InvalidArgumentException ( sprintf ( 'The "%s" option does not exist.' , $name ));
2015-08-17 17:00:26 -07:00
}
$this -> options [ $name ] = $value ;
}
/**
2017-02-02 16:28:38 -08:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function hasOption ( $name )
{
return $this -> definition -> hasOption ( $name );
}
/**
* Escapes a token through escapeshellarg if it contains unsafe chars .
*
* @ param string $token
*
* @ return string
*/
public function escapeToken ( $token )
{
return preg_match ( '{^[\w-]+$}' , $token ) ? $token : escapeshellarg ( $token );
}
}