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 ;
2017-02-02 16:28:38 -08:00
use Symfony\Component\Console\Helper\Helper ;
2015-08-17 17:00:26 -07:00
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputDefinition ;
use Symfony\Component\Console\Input\InputOption ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\Console\Output\OutputInterface ;
2015-08-17 17:00:26 -07:00
/**
* Markdown descriptor .
*
* @ author Jean - François Simon < contact @ jfsimon . fr >
*
* @ internal
*/
class MarkdownDescriptor extends Descriptor
{
2018-11-23 12:29:20 +00:00
/**
* { @ inheritdoc }
*/
public function describe ( OutputInterface $output , $object , array $options = array ())
{
$decorated = $output -> isDecorated ();
$output -> setDecorated ( false );
parent :: describe ( $output , $object , $options );
$output -> setDecorated ( $decorated );
}
/**
* { @ inheritdoc }
*/
protected function write ( $content , $decorated = true )
{
parent :: write ( $content , $decorated );
}
2015-08-17 17:00:26 -07:00
/**
* { @ inheritdoc }
*/
protected function describeInputArgument ( InputArgument $argument , array $options = array ())
{
$this -> write (
2018-11-23 12:29:20 +00:00
'#### `' . ( $argument -> getName () ? : '<none>' ) . " ` \n \n "
. ( $argument -> getDescription () ? preg_replace ( '/\s*[\r\n]\s*/' , " \n " , $argument -> getDescription ()) . " \n \n " : '' )
2015-08-17 17:00:26 -07:00
. '* Is required: ' . ( $argument -> isRequired () ? 'yes' : 'no' ) . " \n "
. '* Is array: ' . ( $argument -> isArray () ? 'yes' : 'no' ) . " \n "
. '* Default: `' . str_replace ( " \n " , '' , var_export ( $argument -> getDefault (), true )) . '`'
);
}
/**
* { @ inheritdoc }
*/
protected function describeInputOption ( InputOption $option , array $options = array ())
{
2018-11-23 12:29:20 +00:00
$name = '--' . $option -> getName ();
if ( $option -> getShortcut ()) {
$name .= '|-' . str_replace ( '|' , '|-' , $option -> getShortcut ()) . '' ;
}
2015-08-17 17:00:26 -07:00
$this -> write (
2018-11-23 12:29:20 +00:00
'#### `' . $name . '`' . " \n \n "
. ( $option -> getDescription () ? preg_replace ( '/\s*[\r\n]\s*/' , " \n " , $option -> getDescription ()) . " \n \n " : '' )
2015-08-17 17:00:26 -07:00
. '* Accept value: ' . ( $option -> acceptValue () ? 'yes' : 'no' ) . " \n "
. '* Is value required: ' . ( $option -> isValueRequired () ? 'yes' : 'no' ) . " \n "
. '* Is multiple: ' . ( $option -> isArray () ? 'yes' : 'no' ) . " \n "
. '* Default: `' . str_replace ( " \n " , '' , var_export ( $option -> getDefault (), true )) . '`'
);
}
/**
* { @ inheritdoc }
*/
protected function describeInputDefinition ( InputDefinition $definition , array $options = array ())
{
2018-11-23 12:29:20 +00:00
if ( $showArguments = \count ( $definition -> getArguments ()) > 0 ) {
$this -> write ( '### Arguments' );
2015-08-17 17:00:26 -07:00
foreach ( $definition -> getArguments () as $argument ) {
$this -> write ( " \n \n " );
$this -> write ( $this -> describeInputArgument ( $argument ));
}
}
2018-11-23 12:29:20 +00:00
if ( \count ( $definition -> getOptions ()) > 0 ) {
2015-08-17 17:00:26 -07:00
if ( $showArguments ) {
$this -> write ( " \n \n " );
}
2018-11-23 12:29:20 +00:00
$this -> write ( '### Options' );
2015-08-17 17:00:26 -07:00
foreach ( $definition -> getOptions () as $option ) {
$this -> write ( " \n \n " );
$this -> write ( $this -> describeInputOption ( $option ));
}
}
}
/**
* { @ inheritdoc }
*/
protected function describeCommand ( Command $command , array $options = array ())
{
$command -> getSynopsis ();
$command -> mergeApplicationDefinition ( false );
$this -> write (
2018-11-23 12:29:20 +00:00
'`' . $command -> getName () . " ` \n "
. str_repeat ( '-' , Helper :: strlen ( $command -> getName ()) + 2 ) . " \n \n "
. ( $command -> getDescription () ? $command -> getDescription () . " \n \n " : '' )
. '### Usage' . " \n \n "
2015-08-17 17:00:26 -07:00
. array_reduce ( array_merge ( array ( $command -> getSynopsis ()), $command -> getAliases (), $command -> getUsages ()), function ( $carry , $usage ) {
2018-11-23 12:29:20 +00:00
return $carry . '* `' . $usage . '`' . " \n " ;
2015-08-17 17:00:26 -07:00
})
);
if ( $help = $command -> getProcessedHelp ()) {
$this -> write ( " \n " );
$this -> write ( $help );
}
if ( $command -> getNativeDefinition ()) {
$this -> write ( " \n \n " );
$this -> describeInputDefinition ( $command -> getNativeDefinition ());
}
}
/**
* { @ inheritdoc }
*/
protected function describeApplication ( Application $application , array $options = array ())
{
$describedNamespace = isset ( $options [ 'namespace' ]) ? $options [ 'namespace' ] : null ;
$description = new ApplicationDescription ( $application , $describedNamespace );
2018-11-23 12:29:20 +00:00
$title = $this -> getApplicationTitle ( $application );
2015-08-17 17:00:26 -07:00
2018-11-23 12:29:20 +00:00
$this -> write ( $title . " \n " . str_repeat ( '=' , Helper :: strlen ( $title )));
2015-08-17 17:00:26 -07:00
foreach ( $description -> getNamespaces () as $namespace ) {
if ( ApplicationDescription :: GLOBAL_NAMESPACE !== $namespace [ 'id' ]) {
$this -> write ( " \n \n " );
$this -> write ( '**' . $namespace [ 'id' ] . ':**' );
}
$this -> write ( " \n \n " );
2018-11-23 12:29:20 +00:00
$this -> write ( implode ( " \n " , array_map ( function ( $commandName ) use ( $description ) {
return sprintf ( '* [`%s`](#%s)' , $commandName , str_replace ( ':' , '' , $description -> getCommand ( $commandName ) -> getName ()));
2015-08-17 17:00:26 -07:00
}, $namespace [ 'commands' ])));
}
foreach ( $description -> getCommands () as $command ) {
$this -> write ( " \n \n " );
$this -> write ( $this -> describeCommand ( $command ));
}
}
2018-11-23 12:29:20 +00:00
private function getApplicationTitle ( Application $application )
{
if ( 'UNKNOWN' !== $application -> getName ()) {
if ( 'UNKNOWN' !== $application -> getVersion ()) {
return sprintf ( '%s %s' , $application -> getName (), $application -> getVersion ());
}
return $application -> getName ();
}
return 'Console Tool' ;
}
2015-08-17 17:00:26 -07:00
}