/** * 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); } }
/** * Resolves parameters in an array using a bag of parameters recursively * * @param array $values * @param BagInterface $parameters * * @return array * * @throws ConfigurationException */ private function resolveParametersRecursively(array $values, BagInterface $parameters) { foreach ($values as $key => &$value) { $placeholder = $this->getPlaceholder($value); // Resolve directly. if (is_string($placeholder)) { if (!$parameters->has($placeholder)) { throw ConfigurationException::configurationParameterNotFoundException($placeholder); } // Set parameter. $values[$key] = $parameters->get($placeholder); continue; } // Resolve recursively as array. if (is_array($value)) { $values[$key] = $this->resolveParametersRecursively($value, $parameters); continue; } } return $values; }