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\Descriptor ;
use Symfony\Component\Console\Application ;
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputDefinition ;
use Symfony\Component\Console\Input\InputOption ;
/**
* JSON descriptor .
*
* @ author Jean - François Simon < contact @ jfsimon . fr >
*
* @ internal
*/
class JsonDescriptor extends Descriptor
{
/**
* { @ inheritdoc }
*/
protected function describeInputArgument ( InputArgument $argument , array $options = array ())
{
$this -> writeData ( $this -> getInputArgumentData ( $argument ), $options );
}
/**
* { @ inheritdoc }
*/
protected function describeInputOption ( InputOption $option , array $options = array ())
{
$this -> writeData ( $this -> getInputOptionData ( $option ), $options );
}
/**
* { @ inheritdoc }
*/
protected function describeInputDefinition ( InputDefinition $definition , array $options = array ())
{
$this -> writeData ( $this -> getInputDefinitionData ( $definition ), $options );
}
/**
* { @ inheritdoc }
*/
protected function describeCommand ( Command $command , array $options = array ())
{
$this -> writeData ( $this -> getCommandData ( $command ), $options );
}
/**
* { @ inheritdoc }
*/
protected function describeApplication ( Application $application , array $options = array ())
{
$describedNamespace = isset ( $options [ 'namespace' ]) ? $options [ 'namespace' ] : null ;
2018-11-23 12:29:20 +00:00
$description = new ApplicationDescription ( $application , $describedNamespace , true );
2015-08-17 17:00:26 -07:00
$commands = array ();
foreach ( $description -> getCommands () as $command ) {
$commands [] = $this -> getCommandData ( $command );
}
2018-11-23 12:29:20 +00:00
$data = array ();
if ( 'UNKNOWN' !== $application -> getName ()) {
$data [ 'application' ][ 'name' ] = $application -> getName ();
if ( 'UNKNOWN' !== $application -> getVersion ()) {
$data [ 'application' ][ 'version' ] = $application -> getVersion ();
}
}
$data [ 'commands' ] = $commands ;
if ( $describedNamespace ) {
$data [ 'namespace' ] = $describedNamespace ;
} else {
$data [ 'namespaces' ] = array_values ( $description -> getNamespaces ());
}
2015-08-17 17:00:26 -07:00
$this -> writeData ( $data , $options );
}
/**
* Writes data as json .
*
* @ return array | string
*/
private function writeData ( array $data , array $options )
{
$this -> write ( json_encode ( $data , isset ( $options [ 'json_encoding' ]) ? $options [ 'json_encoding' ] : 0 ));
}
/**
* @ return array
*/
private function getInputArgumentData ( InputArgument $argument )
{
return array (
'name' => $argument -> getName (),
'is_required' => $argument -> isRequired (),
'is_array' => $argument -> isArray (),
2015-10-08 11:40:12 -07:00
'description' => preg_replace ( '/\s*[\r\n]\s*/' , ' ' , $argument -> getDescription ()),
2018-11-23 12:29:20 +00:00
'default' => INF === $argument -> getDefault () ? 'INF' : $argument -> getDefault (),
2015-08-17 17:00:26 -07:00
);
}
/**
* @ return array
*/
private function getInputOptionData ( InputOption $option )
{
return array (
'name' => '--' . $option -> getName (),
2018-11-23 12:29:20 +00:00
'shortcut' => $option -> getShortcut () ? '-' . str_replace ( '|' , '|-' , $option -> getShortcut ()) : '' ,
2015-08-17 17:00:26 -07:00
'accept_value' => $option -> acceptValue (),
'is_value_required' => $option -> isValueRequired (),
'is_multiple' => $option -> isArray (),
2015-10-08 11:40:12 -07:00
'description' => preg_replace ( '/\s*[\r\n]\s*/' , ' ' , $option -> getDescription ()),
2018-11-23 12:29:20 +00:00
'default' => INF === $option -> getDefault () ? 'INF' : $option -> getDefault (),
2015-08-17 17:00:26 -07:00
);
}
/**
* @ return array
*/
private function getInputDefinitionData ( InputDefinition $definition )
{
$inputArguments = array ();
foreach ( $definition -> getArguments () as $name => $argument ) {
$inputArguments [ $name ] = $this -> getInputArgumentData ( $argument );
}
$inputOptions = array ();
foreach ( $definition -> getOptions () as $name => $option ) {
$inputOptions [ $name ] = $this -> getInputOptionData ( $option );
}
return array ( 'arguments' => $inputArguments , 'options' => $inputOptions );
}
/**
* @ return array
*/
private function getCommandData ( Command $command )
{
$command -> getSynopsis ();
$command -> mergeApplicationDefinition ( false );
return array (
'name' => $command -> getName (),
'usage' => array_merge ( array ( $command -> getSynopsis ()), $command -> getUsages (), $command -> getAliases ()),
'description' => $command -> getDescription (),
'help' => $command -> getProcessedHelp (),
'definition' => $this -> getInputDefinitionData ( $command -> getNativeDefinition ()),
2018-11-23 12:29:20 +00:00
'hidden' => $command -> isHidden (),
2015-08-17 17:00:26 -07:00
);
}
}