| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Yii\Console; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Command\LazyCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Exception\CommandNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | final class CommandLoader implements CommandLoaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private ContainerInterface $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var array<string, string> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private array $commandMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param array<string, string> $commandMap An array with command names as keys and service ids as values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 33 |  |     public function __construct(ContainerInterface $container, array $commandMap) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 33 |  |         $this->container = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 33 |  |         $this->commandMap = $commandMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 33 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 9 |  |     public function get(string $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 9 |  |         if (!$this->has($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         /** @var Command $commandClass */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 9 |  |         $commandClass = $this->commandMap[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 9 |  |         $description = $commandClass::getDefaultDescription(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 9 |  |         if ($description === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |             return $this->getCommandInstance($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 6 |  |         $aliases = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 6 |  |         $hidden = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 6 |  |         $commandName = $commandClass::getDefaultName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 6 |  |         if ($commandName !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 6 |  |             $aliases = explode('|', $commandName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 6 |  |             $primaryName = array_shift($aliases); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 6 |  |             if ($primaryName === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $hidden = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 6 |  |         return new LazyCommand( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 6 |  |             $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $aliases, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $description, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $hidden, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 6 |  |             function () use ($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 5 |  |                 return $this->getCommandInstance($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 6 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 8 |  |     private function getCommandInstance(string $name): Command | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         /** @var Command $command */ | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 8 |  |         $command = $this->container->get($this->commandMap[$name]); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 8 |  |         if ($command->getName() !== $name) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 1 |  |             $command->setName($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 8 |  |         return $command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 9 |  |     public function has(string $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 9 |  |         return isset($this->commandMap[$name]) && $this->container->has($this->commandMap[$name]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function getNames() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return array_keys($this->commandMap); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  |