2021-05-10 00:23:30 +00: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 .
*/
2021-09-04 07:42:32 +00:00
namespace RectorPrefix20210904\Symfony\Component\Console\Input ;
2021-05-10 00:23:30 +00:00
2021-09-04 07:42:32 +00:00
use RectorPrefix20210904\Symfony\Component\Console\Exception\InvalidArgumentException ;
use RectorPrefix20210904\Symfony\Component\Console\Exception\LogicException ;
2021-05-10 00:23:30 +00:00
/**
* A InputDefinition represents a set of valid command line arguments and options .
*
* Usage :
*
* $definition = new InputDefinition ([
* new InputArgument ( 'name' , InputArgument :: REQUIRED ),
* new InputOption ( 'foo' , 'f' , InputOption :: VALUE_REQUIRED ),
* ]);
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
class InputDefinition
{
private $arguments ;
private $requiredCount ;
2021-06-01 12:39:02 +00:00
private $lastArrayArgument ;
private $lastOptionalArgument ;
2021-05-10 00:23:30 +00:00
private $options ;
2021-06-01 12:39:02 +00:00
private $negations ;
2021-05-10 00:23:30 +00:00
private $shortcuts ;
/**
* @ param array $definition An array of InputArgument and InputOption instance
*/
public function __construct ( array $definition = [])
{
$this -> setDefinition ( $definition );
}
/**
* Sets the definition of the input .
2021-07-05 22:50:18 +00:00
* @ param mixed [] $definition
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public function setDefinition ( $definition )
2021-05-10 00:23:30 +00:00
{
$arguments = [];
$options = [];
foreach ( $definition as $item ) {
2021-09-04 07:42:32 +00:00
if ( $item instanceof \RectorPrefix20210904\Symfony\Component\Console\Input\InputOption ) {
2021-05-10 00:23:30 +00:00
$options [] = $item ;
} else {
$arguments [] = $item ;
}
}
$this -> setArguments ( $arguments );
$this -> setOptions ( $options );
}
/**
* Sets the InputArgument objects .
*
* @ param InputArgument [] $arguments An array of InputArgument objects
*/
2021-07-05 22:50:18 +00:00
public function setArguments ( $arguments = [])
2021-05-10 00:23:30 +00:00
{
$this -> arguments = [];
$this -> requiredCount = 0 ;
2021-06-01 12:39:02 +00:00
$this -> lastOptionalArgument = null ;
$this -> lastArrayArgument = null ;
2021-05-10 00:23:30 +00:00
$this -> addArguments ( $arguments );
}
/**
* Adds an array of InputArgument objects .
*
* @ param InputArgument [] $arguments An array of InputArgument objects
*/
2021-07-05 22:50:18 +00:00
public function addArguments ( $arguments = [])
2021-05-10 00:23:30 +00:00
{
if ( null !== $arguments ) {
foreach ( $arguments as $argument ) {
$this -> addArgument ( $argument );
}
}
}
/**
* @ throws LogicException When incorrect argument is given
2021-08-23 00:20:32 +00:00
* @ param \Symfony\Component\Console\Input\InputArgument $argument
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public function addArgument ( $argument )
2021-05-10 00:23:30 +00:00
{
if ( isset ( $this -> arguments [ $argument -> getName ()])) {
2021-09-04 07:42:32 +00:00
throw new \RectorPrefix20210904\Symfony\Component\Console\Exception\LogicException ( \sprintf ( 'An argument with name "%s" already exists.' , $argument -> getName ()));
2021-05-10 00:23:30 +00:00
}
2021-06-01 12:39:02 +00:00
if ( null !== $this -> lastArrayArgument ) {
2021-09-04 07:42:32 +00:00
throw new \RectorPrefix20210904\Symfony\Component\Console\Exception\LogicException ( \sprintf ( 'Cannot add a required argument "%s" after an array argument "%s".' , $argument -> getName (), $this -> lastArrayArgument -> getName ()));
2021-05-10 00:23:30 +00:00
}
2021-06-01 12:39:02 +00:00
if ( $argument -> isRequired () && null !== $this -> lastOptionalArgument ) {
2021-09-04 07:42:32 +00:00
throw new \RectorPrefix20210904\Symfony\Component\Console\Exception\LogicException ( \sprintf ( 'Cannot add a required argument "%s" after an optional one "%s".' , $argument -> getName (), $this -> lastOptionalArgument -> getName ()));
2021-05-10 00:23:30 +00:00
}
if ( $argument -> isArray ()) {
2021-06-01 12:39:02 +00:00
$this -> lastArrayArgument = $argument ;
2021-05-10 00:23:30 +00:00
}
if ( $argument -> isRequired ()) {
++ $this -> requiredCount ;
} else {
2021-06-01 12:39:02 +00:00
$this -> lastOptionalArgument = $argument ;
2021-05-10 00:23:30 +00:00
}
$this -> arguments [ $argument -> getName ()] = $argument ;
}
/**
* Returns an InputArgument by name or by position .
*
* @ param string | int $name The InputArgument name or position
*
* @ return InputArgument An InputArgument object
*
* @ throws InvalidArgumentException When argument given doesn ' t exist
*/
public function getArgument ( $name )
{
if ( ! $this -> hasArgument ( $name )) {
2021-09-04 07:42:32 +00:00
throw new \RectorPrefix20210904\Symfony\Component\Console\Exception\InvalidArgumentException ( \sprintf ( 'The "%s" argument does not exist.' , $name ));
2021-05-10 00:23:30 +00:00
}
$arguments = \is_int ( $name ) ? \array_values ( $this -> arguments ) : $this -> arguments ;
return $arguments [ $name ];
}
/**
* Returns true if an InputArgument object exists by name or position .
*
* @ param string | int $name The InputArgument name or position
*
* @ return bool true if the InputArgument object exists , false otherwise
*/
public function hasArgument ( $name )
{
$arguments = \is_int ( $name ) ? \array_values ( $this -> arguments ) : $this -> arguments ;
return isset ( $arguments [ $name ]);
}
/**
* Gets the array of InputArgument objects .
*
* @ return InputArgument [] An array of InputArgument objects
*/
public function getArguments ()
{
return $this -> arguments ;
}
/**
* Returns the number of InputArguments .
*
* @ return int The number of InputArguments
*/
public function getArgumentCount ()
{
2021-06-01 12:39:02 +00:00
return null !== $this -> lastArrayArgument ? \PHP_INT_MAX : \count ( $this -> arguments );
2021-05-10 00:23:30 +00:00
}
/**
* Returns the number of required InputArguments .
*
* @ return int The number of required InputArguments
*/
public function getArgumentRequiredCount ()
{
return $this -> requiredCount ;
}
/**
2021-07-05 15:06:41 +00:00
* @ return array < string | bool | int | float | array | null >
2021-05-10 00:23:30 +00:00
*/
public function getArgumentDefaults ()
{
$values = [];
foreach ( $this -> arguments as $argument ) {
$values [ $argument -> getName ()] = $argument -> getDefault ();
}
return $values ;
}
/**
* Sets the InputOption objects .
*
* @ param InputOption [] $options An array of InputOption objects
*/
2021-07-05 22:50:18 +00:00
public function setOptions ( $options = [])
2021-05-10 00:23:30 +00:00
{
$this -> options = [];
$this -> shortcuts = [];
2021-06-01 12:39:02 +00:00
$this -> negations = [];
2021-05-10 00:23:30 +00:00
$this -> addOptions ( $options );
}
/**
* Adds an array of InputOption objects .
*
* @ param InputOption [] $options An array of InputOption objects
*/
2021-07-05 22:50:18 +00:00
public function addOptions ( $options = [])
2021-05-10 00:23:30 +00:00
{
foreach ( $options as $option ) {
$this -> addOption ( $option );
}
}
/**
* @ throws LogicException When option given already exist
2021-08-23 00:20:32 +00:00
* @ param \Symfony\Component\Console\Input\InputOption $option
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public function addOption ( $option )
2021-05-10 00:23:30 +00:00
{
if ( isset ( $this -> options [ $option -> getName ()]) && ! $option -> equals ( $this -> options [ $option -> getName ()])) {
2021-09-04 07:42:32 +00:00
throw new \RectorPrefix20210904\Symfony\Component\Console\Exception\LogicException ( \sprintf ( 'An option named "%s" already exists.' , $option -> getName ()));
2021-06-01 12:39:02 +00:00
}
if ( isset ( $this -> negations [ $option -> getName ()])) {
2021-09-04 07:42:32 +00:00
throw new \RectorPrefix20210904\Symfony\Component\Console\Exception\LogicException ( \sprintf ( 'An option named "%s" already exists.' , $option -> getName ()));
2021-05-10 00:23:30 +00:00
}
if ( $option -> getShortcut ()) {
foreach ( \explode ( '|' , $option -> getShortcut ()) as $shortcut ) {
if ( isset ( $this -> shortcuts [ $shortcut ]) && ! $option -> equals ( $this -> options [ $this -> shortcuts [ $shortcut ]])) {
2021-09-04 07:42:32 +00:00
throw new \RectorPrefix20210904\Symfony\Component\Console\Exception\LogicException ( \sprintf ( 'An option with shortcut "%s" already exists.' , $shortcut ));
2021-05-10 00:23:30 +00:00
}
}
}
$this -> options [ $option -> getName ()] = $option ;
if ( $option -> getShortcut ()) {
foreach ( \explode ( '|' , $option -> getShortcut ()) as $shortcut ) {
$this -> shortcuts [ $shortcut ] = $option -> getName ();
}
}
2021-06-01 12:39:02 +00:00
if ( $option -> isNegatable ()) {
$negatedName = 'no-' . $option -> getName ();
if ( isset ( $this -> options [ $negatedName ])) {
2021-09-04 07:42:32 +00:00
throw new \RectorPrefix20210904\Symfony\Component\Console\Exception\LogicException ( \sprintf ( 'An option named "%s" already exists.' , $negatedName ));
2021-06-01 12:39:02 +00:00
}
$this -> negations [ $negatedName ] = $option -> getName ();
}
2021-05-10 00:23:30 +00:00
}
/**
* Returns an InputOption by name .
*
* @ return InputOption A InputOption object
*
* @ throws InvalidArgumentException When option given doesn ' t exist
2021-07-05 22:50:18 +00:00
* @ param string $name
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public function getOption ( $name )
2021-05-10 00:23:30 +00:00
{
if ( ! $this -> hasOption ( $name )) {
2021-09-04 07:42:32 +00:00
throw new \RectorPrefix20210904\Symfony\Component\Console\Exception\InvalidArgumentException ( \sprintf ( 'The "--%s" option does not exist.' , $name ));
2021-05-10 00:23:30 +00:00
}
return $this -> options [ $name ];
}
/**
* Returns true if an InputOption object exists by name .
*
* This method can ' t be used to check if the user included the option when
* executing the command ( use getOption () instead ) .
*
* @ return bool true if the InputOption object exists , false otherwise
2021-07-05 22:50:18 +00:00
* @ param string $name
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public function hasOption ( $name )
2021-05-10 00:23:30 +00:00
{
return isset ( $this -> options [ $name ]);
}
/**
* Gets the array of InputOption objects .
*
* @ return InputOption [] An array of InputOption objects
*/
public function getOptions ()
{
return $this -> options ;
}
/**
* Returns true if an InputOption object exists by shortcut .
*
* @ return bool true if the InputOption object exists , false otherwise
2021-07-05 22:50:18 +00:00
* @ param string $name
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public function hasShortcut ( $name )
2021-05-10 00:23:30 +00:00
{
return isset ( $this -> shortcuts [ $name ]);
}
2021-06-01 12:39:02 +00:00
/**
* Returns true if an InputOption object exists by negated name .
2021-07-05 22:50:18 +00:00
* @ param string $name
2021-06-01 12:39:02 +00:00
*/
2021-07-05 22:50:18 +00:00
public function hasNegation ( $name ) : bool
2021-06-01 12:39:02 +00:00
{
return isset ( $this -> negations [ $name ]);
}
2021-05-10 00:23:30 +00:00
/**
* Gets an InputOption by shortcut .
*
* @ return InputOption An InputOption object
2021-07-05 22:50:18 +00:00
* @ param string $shortcut
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public function getOptionForShortcut ( $shortcut )
2021-05-10 00:23:30 +00:00
{
return $this -> getOption ( $this -> shortcutToName ( $shortcut ));
}
/**
2021-07-05 15:06:41 +00:00
* @ return array < string | bool | int | float | array | null >
2021-05-10 00:23:30 +00:00
*/
public function getOptionDefaults ()
{
$values = [];
foreach ( $this -> options as $option ) {
$values [ $option -> getName ()] = $option -> getDefault ();
}
return $values ;
}
/**
* Returns the InputOption name given a shortcut .
*
* @ throws InvalidArgumentException When option given does not exist
*
* @ internal
2021-07-05 22:50:18 +00:00
* @ param string $shortcut
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public function shortcutToName ( $shortcut ) : string
2021-05-10 00:23:30 +00:00
{
if ( ! isset ( $this -> shortcuts [ $shortcut ])) {
2021-09-04 07:42:32 +00:00
throw new \RectorPrefix20210904\Symfony\Component\Console\Exception\InvalidArgumentException ( \sprintf ( 'The "-%s" option does not exist.' , $shortcut ));
2021-05-10 00:23:30 +00:00
}
return $this -> shortcuts [ $shortcut ];
}
2021-06-01 12:39:02 +00:00
/**
* Returns the InputOption name given a negation .
*
* @ throws InvalidArgumentException When option given does not exist
*
* @ internal
2021-07-05 22:50:18 +00:00
* @ param string $negation
2021-06-01 12:39:02 +00:00
*/
2021-07-05 22:50:18 +00:00
public function negationToName ( $negation ) : string
2021-06-01 12:39:02 +00:00
{
if ( ! isset ( $this -> negations [ $negation ])) {
2021-09-04 07:42:32 +00:00
throw new \RectorPrefix20210904\Symfony\Component\Console\Exception\InvalidArgumentException ( \sprintf ( 'The "--%s" option does not exist.' , $negation ));
2021-06-01 12:39:02 +00:00
}
return $this -> negations [ $negation ];
}
2021-05-10 00:23:30 +00:00
/**
* Gets the synopsis .
*
* @ return string The synopsis
2021-07-05 22:50:18 +00:00
* @ param bool $short
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public function getSynopsis ( $short = \false )
2021-05-10 00:23:30 +00:00
{
$elements = [];
if ( $short && $this -> getOptions ()) {
$elements [] = '[options]' ;
} elseif ( ! $short ) {
foreach ( $this -> getOptions () as $option ) {
$value = '' ;
if ( $option -> acceptValue ()) {
$value = \sprintf ( ' %s%s%s' , $option -> isValueOptional () ? '[' : '' , \strtoupper ( $option -> getName ()), $option -> isValueOptional () ? ']' : '' );
}
$shortcut = $option -> getShortcut () ? \sprintf ( '-%s|' , $option -> getShortcut ()) : '' ;
2021-06-01 12:39:02 +00:00
$negation = $option -> isNegatable () ? \sprintf ( '|--no-%s' , $option -> getName ()) : '' ;
$elements [] = \sprintf ( '[%s--%s%s%s]' , $shortcut , $option -> getName (), $value , $negation );
2021-05-10 00:23:30 +00:00
}
}
if ( \count ( $elements ) && $this -> getArguments ()) {
$elements [] = '[--]' ;
}
$tail = '' ;
foreach ( $this -> getArguments () as $argument ) {
$element = '<' . $argument -> getName () . '>' ;
if ( $argument -> isArray ()) {
$element .= '...' ;
}
if ( ! $argument -> isRequired ()) {
$element = '[' . $element ;
$tail .= ']' ;
}
$elements [] = $element ;
}
return \implode ( ' ' , $elements ) . $tail ;
}
}