| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the Cubiche package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) Cubiche | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Cubiche\Core\Console; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Cubiche\Core\EventBus\Event\EventBus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Cubiche\Core\Bus\Exception\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Cubiche\Core\Console\Api\Config\CommandConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Cubiche\Core\Console\Command\ConsoleCommandInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Cubiche\Core\Console\Converter\ConsoleArgsToCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Cubiche\Core\Console\Handler\DefaultEventHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Cubiche\Core\Cqrs\Command\CommandBus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Webmozart\Assert\Assert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Webmozart\Console\Api\Config\ApplicationConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Webmozart\Console\Api\Event\ConsoleEvents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Webmozart\Console\Api\Event\PreHandleEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Webmozart\Console\ConsoleApplication as BaseConsoleApplication; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * ConsoleApplication class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @author Ivannis Suárez Jerez <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class ConsoleApplication extends BaseConsoleApplication | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var CommandBus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $commandBus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var EventBus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     protected $eventBus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var DefaultEventHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected $defaultHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var ConsoleArgsToCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     protected $commandConverter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * ConsoleApplication constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param callable|ApplicationConfig $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param CommandBus                 $commandBus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param EventBus                   $eventBus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function __construct($config, CommandBus $commandBus, EventBus $eventBus) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->commandBus = $commandBus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->eventBus = $eventBus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->defaultHandler = new DefaultEventHandler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->commandConverter = new ConsoleArgsToCommand(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         parent::__construct($this->normalizeConfig($config)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param ApplicationConfig|callable $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return ApplicationConfig|callable | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     protected function normalizeConfig($config) | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         if (is_callable($config)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             /** @var ApplicationConfig $configuration */ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             $configuration = $config(); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             $configuration = $config; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         Assert::isInstanceOf( | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             $configuration, | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             'Webmozart\Console\Api\Config\ApplicationConfig', | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             'The $config argument must be an ApplicationConfig or a callable. Got: %s' | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         // add listener | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $configuration->addEventListener(ConsoleEvents::PRE_HANDLE, function (PreHandleEvent $event) { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             $this->onPreHandle($event); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         return $configuration; | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param PreHandleEvent $event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     protected function onPreHandle(PreHandleEvent $event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         // convert console args to command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->commandConverter->setArgs($event->getArgs()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->commandConverter->setFormat($event->getCommand()->getArgsFormat()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         /** @var CommandConfig $commandConfig */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $commandConfig = $event->getCommand()->getConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if ($event->getCommand()->getName() !== 'help') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             if ($commandConfig->className() === null && count($commandConfig->getSubCommandConfigs()) === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 throw new \RuntimeException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                     'The command %s definition must set the className using ->setClass() method', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                     $event->getCommand()->getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $command = $this->commandConverter->getCommandFrom($commandConfig->className()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $this->defaultHandler->setIo($event->getIO()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->defaultHandler->clearEventHandlers(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         if ($commandConfig->preDispatchEventHandler() !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $this->defaultHandler->addPreDispatchEventHandler($commandConfig->preDispatchEventHandler()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         if ($commandConfig->postDispatchEventHandler()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $this->defaultHandler->addPostDispatchEventHandler($commandConfig->postDispatchEventHandler()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $commandConfig->setEventBus($this->eventBus); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $commandConfig->addEventSubscriber($this->defaultHandler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         if ($command !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             if ($command instanceof ConsoleCommandInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 $command->setIo($event->getIO()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 $this->commandBus->dispatch($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 $event->setHandled(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 $event->setStatusCode(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             } catch (NotFoundException $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             if ($event->getCommand()->getName() !== 'help') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 $helpCommand = $this->getCommand('help'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 $args = $event->getArgs(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 $args = $helpCommand->parseArgs($args->getRawArgs()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 $args->setArgument('command', $event->getCommand()->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 $helpConfig = $this->getConfig()->getCommandConfig('help'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                 $commandHandler = $helpConfig->getHandler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                 $handlerMethod = $helpConfig->getHandlerMethod(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                 $commandHandler->$handlerMethod($args, $event->getIO(), $helpCommand); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 $event->setHandled(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                 $event->setStatusCode(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |  | 
            
                        
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: