コード例 #1
0
 /**
  * Loads commands from configuration
  */
 private function loadCommandsFile()
 {
     // Load file.
     $bag = new ContainerLessBag();
     $commandBag = $this->getCommandBag();
     $fileLoader = $this->getServiceBag()->getFileLoader();
     $filename = $this->generateConfigFilePath('commands.yml');
     $fileLoader->loadFileToBag($filename, 'commands', $bag);
     // Resolve parameters.
     $fileLoader->resolveParameters($bag, $this->getParameterBag());
     $commands = array();
     // Instantiate services and load then to the container.
     foreach ($bag->values() as $name => $values) {
         // Override command name if defined.
         $name = isset($values['name']) ? $values['name'] : $name;
         // Multiple definitions for the same command, abort.
         if (array_key_exists($name, $commands)) {
             throw ConfigurationException::multipleCommandDefinitionsException($name);
         }
         // Check if class is set (null counts as unset).
         if (!isset($values['class'])) {
             throw ConfigurationException::configurationAttributeNotFoundException($filename, $name, 'class');
         }
         $class = $values['class'];
         $command = $this->instantiateClass($class, array($name));
         // Check if correct class.
         if (!$command instanceof BaseCommand) {
             throw LogicException::mustExtendClassException('BaseCommand', $class);
         }
         // Add description.
         if (isset($values['description'])) {
             $command->setDescription($values['description']);
         }
         $commands[$name] = $command;
     }
     // Register the command.
     foreach ($commands as $name => $command) {
         $commandBag->set($name, $command);
     }
 }