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 ;
2016-04-20 09:56:34 -07:00
use Symfony\Component\Console\Exception\CommandNotFoundException ;
2015-08-17 17:00:26 -07:00
/**
* @ author Jean - François Simon < jeanfrancois . simon @ sensiolabs . com >
*
* @ internal
*/
class ApplicationDescription
{
const GLOBAL_NAMESPACE = '_global' ;
private $application ;
private $namespace ;
2018-11-23 12:29:20 +00:00
private $showHidden ;
2015-08-17 17:00:26 -07:00
/**
* @ var array
*/
private $namespaces ;
/**
* @ var Command []
*/
private $commands ;
/**
* @ var Command []
*/
private $aliases ;
/**
* @ param Application $application
* @ param string | null $namespace
2018-11-23 12:29:20 +00:00
* @ param bool $showHidden
2015-08-17 17:00:26 -07:00
*/
2018-11-23 12:29:20 +00:00
public function __construct ( Application $application , $namespace = null , $showHidden = false )
2015-08-17 17:00:26 -07:00
{
$this -> application = $application ;
$this -> namespace = $namespace ;
2018-11-23 12:29:20 +00:00
$this -> showHidden = $showHidden ;
2015-08-17 17:00:26 -07:00
}
/**
* @ return array
*/
public function getNamespaces ()
{
if ( null === $this -> namespaces ) {
$this -> inspectApplication ();
}
return $this -> namespaces ;
}
/**
* @ return Command []
*/
public function getCommands ()
{
if ( null === $this -> commands ) {
$this -> inspectApplication ();
}
return $this -> commands ;
}
/**
* @ param string $name
*
* @ return Command
*
2016-04-20 09:56:34 -07:00
* @ throws CommandNotFoundException
2015-08-17 17:00:26 -07:00
*/
public function getCommand ( $name )
{
if ( ! isset ( $this -> commands [ $name ]) && ! isset ( $this -> aliases [ $name ])) {
2016-04-20 09:56:34 -07:00
throw new CommandNotFoundException ( sprintf ( 'Command %s does not exist.' , $name ));
2015-08-17 17:00:26 -07:00
}
return isset ( $this -> commands [ $name ]) ? $this -> commands [ $name ] : $this -> aliases [ $name ];
}
private function inspectApplication ()
{
$this -> commands = array ();
$this -> namespaces = array ();
$all = $this -> application -> all ( $this -> namespace ? $this -> application -> findNamespace ( $this -> namespace ) : null );
foreach ( $this -> sortCommands ( $all ) as $namespace => $commands ) {
$names = array ();
/** @var Command $command */
foreach ( $commands as $name => $command ) {
2018-11-23 12:29:20 +00:00
if ( ! $command -> getName () || ( ! $this -> showHidden && $command -> isHidden ())) {
2015-08-17 17:00:26 -07:00
continue ;
}
if ( $command -> getName () === $name ) {
$this -> commands [ $name ] = $command ;
} else {
$this -> aliases [ $name ] = $command ;
}
$names [] = $name ;
}
$this -> namespaces [ $namespace ] = array ( 'id' => $namespace , 'commands' => $names );
}
}
/**
* @ return array
*/
private function sortCommands ( array $commands )
{
$namespacedCommands = array ();
2015-11-17 13:42:33 -08:00
$globalCommands = array ();
2015-08-17 17:00:26 -07:00
foreach ( $commands as $name => $command ) {
$key = $this -> application -> extractNamespace ( $name , 1 );
if ( ! $key ) {
2015-11-17 13:42:33 -08:00
$globalCommands [ '_global' ][ $name ] = $command ;
} else {
$namespacedCommands [ $key ][ $name ] = $command ;
2015-08-17 17:00:26 -07:00
}
}
ksort ( $namespacedCommands );
2015-11-17 13:42:33 -08:00
$namespacedCommands = array_merge ( $globalCommands , $namespacedCommands );
2015-08-17 17:00:26 -07:00
foreach ( $namespacedCommands as & $commandsSet ) {
ksort ( $commandsSet );
}
// unset reference to keep scope clear
unset ( $commandsSet );
return $namespacedCommands ;
}
}