/** * Returns extension configuration. * @param array default values. * @param bool perform %parameters% expansion? * @return array */ public function getConfig(array $defaults = NULL, $expand = TRUE) { $config = $this->compiler->getConfig(); $config = isset($config[$this->name]) ? $config[$this->name] : array(); unset($config['services'], $config['factories']); $config = ConfigHelpers::merge($config, $defaults); return $expand ? $this->compiler->getContainerBuilder()->expand($config) : $config; }
/** * Reads configuration from INI file. * @param string file name * @return array * @throws InvalidStateException */ public function load($file) { Debugger::tryError(); $ini = parse_ini_file($file, TRUE); if (Debugger::catchError($e)) { throw new InvalidStateException('parse_ini_file(): ' . $e->getMessage(), 0, $e); } $data = array(); foreach ($ini as $secName => $secData) { if (is_array($secData)) { // is section? if (substr($secName, -1) === self::RAW_SECTION) { $secName = substr($secName, 0, -1); } else { // process key nesting separator (key1.key2.key3) $tmp = array(); foreach ($secData as $key => $val) { $cursor =& $tmp; $key = str_replace(self::ESCAPED_KEY_SEPARATOR, "ÿ", $key); foreach (explode(self::KEY_SEPARATOR, $key) as $part) { $part = str_replace("ÿ", self::KEY_SEPARATOR, $part); if (!isset($cursor[$part]) || is_array($cursor[$part])) { $cursor =& $cursor[$part]; } else { throw new InvalidStateException("Invalid key '{$key}' in section [{$secName}] in file '{$file}'."); } } $cursor = $val; } $secData = $tmp; } $parts = explode(self::INHERITING_SEPARATOR, $secName); if (count($parts) > 1) { $secName = trim($parts[0]); $secData[ConfigHelpers::EXTENDS_KEY] = trim($parts[1]); } } $cursor =& $data; // nesting separator in section name foreach (explode(self::KEY_SEPARATOR, $secName) as $part) { if (!isset($cursor[$part]) || is_array($cursor[$part])) { $cursor =& $cursor[$part]; } else { throw new InvalidStateException("Invalid section [{$secName}] in file '{$file}'."); } } if (is_array($secData) && is_array($cursor)) { $secData = ConfigHelpers::merge($secData, $cursor); } $cursor = $secData; } return $data; }
/** * Build system container class. * @return string */ protected function buildContainer(&$dependencies = NULL) { $loader = $this->createLoader(); $config = array(); $code = "<?php\n"; foreach ($this->files as $tmp) { list($file, $section) = $tmp; $config = ConfigHelpers::merge($loader->load($file, $section), $config); $code .= "// source: {$file} {$section}\n"; } $code .= "\n"; $this->checkCompatibility($config); if (!isset($config['parameters'])) { $config['parameters'] = array(); } $config['parameters'] = ConfigHelpers::merge($config['parameters'], $this->parameters); $compiler = $this->createCompiler(); $this->onCompile($this, $compiler); $code .= $compiler->compile($config, $this->parameters['container']['class'], $config['parameters']['container']['parent']); $dependencies = array_merge($loader->getDependencies(), $this->isDebugMode() ? $compiler->getContainerBuilder()->getDependencies() : array()); return $code; }
private function getSection(array $data, $key, $file) { Validators::assertField($data, $key, 'array|null', "section '%' in file '{$file}'"); $item = $data[$key]; if ($parent = ConfigHelpers::takeParent($item)) { $item = ConfigHelpers::merge($item, $this->getSection($data, $parent, $file)); } return $item; }