Вход Регистрация
Файл: system/vendor/symfony/console/Descriptor/MarkdownDescriptor.php
Строк: 323
<?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 SymfonyComponentConsoleDescriptor;

use 
SymfonyComponentConsoleApplication;
use 
SymfonyComponentConsoleCommandCommand;
use 
SymfonyComponentConsoleHelperHelper;
use 
SymfonyComponentConsoleInputInputArgument;
use 
SymfonyComponentConsoleInputInputDefinition;
use 
SymfonyComponentConsoleInputInputOption;
use 
SymfonyComponentConsoleOutputOutputInterface;

/**
 * Markdown descriptor.
 *
 * @author Jean-François Simon <contact@jfsimon.fr>
 *
 * @internal
 */
class MarkdownDescriptor extends Descriptor
{
    
/**
     * {@inheritdoc}
     */
    
public function describe(OutputInterface $output$object, array $options = [])
    {
        
$decorated $output->isDecorated();
        
$output->setDecorated(false);

        
parent::describe($output$object$options);

        
$output->setDecorated($decorated);
    }

    
/**
     * {@inheritdoc}
     */
    
protected function write(string $contentbool $decorated true)
    {
        
parent::write($content$decorated);
    }

    
/**
     * {@inheritdoc}
     */
    
protected function describeInputArgument(InputArgument $argument, array $options = [])
    {
        
$this->write(
            
'#### `'.($argument->getName() ?: '<none>')."`nn"
            
.($argument->getDescription() ? preg_replace('/s*[rn]s*/'"n"$argument->getDescription())."nn" '')
            .
'* 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 = [])
    {
        
$name '--'.$option->getName();
        if (
$option->getShortcut()) {
            
$name .= '|-'.str_replace('|''|-'$option->getShortcut()).'';
        }

        
$this->write(
            
'#### `'.$name.'`'."nn"
            
.($option->getDescription() ? preg_replace('/s*[rn]s*/'"n"$option->getDescription())."nn" '')
            .
'* 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 = [])
    {
        if (
$showArguments count($definition->getArguments()) > 0) {
            
$this->write('### Arguments');
            foreach (
$definition->getArguments() as $argument) {
                
$this->write("nn");
                if (
null !== $describeInputArgument $this->describeInputArgument($argument)) {
                    
$this->write($describeInputArgument);
                }
            }
        }

        if (
count($definition->getOptions()) > 0) {
            if (
$showArguments) {
                
$this->write("nn");
            }

            
$this->write('### Options');
            foreach (
$definition->getOptions() as $option) {
                
$this->write("nn");
                if (
null !== $describeInputOption $this->describeInputOption($option)) {
                    
$this->write($describeInputOption);
                }
            }
        }
    }

    
/**
     * {@inheritdoc}
     */
    
protected function describeCommand(Command $command, array $options = [])
    {
        
$command->getSynopsis();
        
$command->mergeApplicationDefinition(false);

        
$this->write(
            
'`'.$command->getName()."`n"
            
.str_repeat('-'Helper::strlen($command->getName()) + 2)."nn"
            
.($command->getDescription() ? $command->getDescription()."nn" '')
            .
'### Usage'."nn"
            
.array_reduce(array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()), function ($carry$usage) {
                return 
$carry.'* `'.$usage.'`'."n";
            })
        );

        if (
$help $command->getProcessedHelp()) {
            
$this->write("n");
            
$this->write($help);
        }

        if (
$command->getNativeDefinition()) {
            
$this->write("nn");
            
$this->describeInputDefinition($command->getNativeDefinition());
        }
    }

    
/**
     * {@inheritdoc}
     */
    
protected function describeApplication(Application $application, array $options = [])
    {
        
$describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
        
$description = new ApplicationDescription($application$describedNamespace);
        
$title $this->getApplicationTitle($application);

        
$this->write($title."n".str_repeat('='Helper::strlen($title)));

        foreach (
$description->getNamespaces() as $namespace) {
            if (
ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
                
$this->write("nn");
                
$this->write('**'.$namespace['id'].':**');
            }

            
$this->write("nn");
            
$this->write(implode("n"array_map(function ($commandName) use ($description) {
                return 
sprintf('* [`%s`](#%s)'$commandNamestr_replace(':'''$description->getCommand($commandName)->getName()));
            }, 
$namespace['commands'])));
        }

        foreach (
$description->getCommands() as $command) {
            
$this->write("nn");
            if (
null !== $describeCommand $this->describeCommand($command)) {
                
$this->write($describeCommand);
            }
        }
    }

    private function 
getApplicationTitle(Application $application): string
    
{
        if (
'UNKNOWN' !== $application->getName()) {
            if (
'UNKNOWN' !== $application->getVersion()) {
                return 
sprintf('%s %s'$application->getName(), $application->getVersion());
            }

            return 
$application->getName();
        }

        return 
'Console Tool';
    }
}
Онлайн: 0
Реклама