Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DependencyInjection\Loader; Chris@0: Chris@0: use Symfony\Component\Config\Util\XmlUtils; Chris@0: use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; Chris@0: Chris@0: /** Chris@0: * IniFileLoader loads parameters from INI files. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class IniFileLoader extends FileLoader Chris@0: { Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function load($resource, $type = null) Chris@0: { Chris@0: $path = $this->locator->locate($resource); Chris@0: Chris@14: $this->container->fileExists($path); Chris@0: Chris@0: // first pass to catch parsing errors Chris@0: $result = parse_ini_file($path, true); Chris@17: if (false === $result || [] === $result) { Chris@0: throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $resource)); Chris@0: } Chris@0: Chris@0: // real raw parsing Chris@0: $result = parse_ini_file($path, true, INI_SCANNER_RAW); Chris@0: Chris@17: if (isset($result['parameters']) && \is_array($result['parameters'])) { Chris@0: foreach ($result['parameters'] as $key => $value) { Chris@0: $this->container->setParameter($key, $this->phpize($value)); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supports($resource, $type = null) Chris@0: { Chris@17: if (!\is_string($resource)) { Chris@14: return false; Chris@14: } Chris@14: Chris@14: if (null === $type && 'ini' === pathinfo($resource, PATHINFO_EXTENSION)) { Chris@14: return true; Chris@14: } Chris@14: Chris@14: return 'ini' === $type; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Note that the following features are not supported: Chris@0: * * strings with escaped quotes are not supported "foo\"bar"; Chris@0: * * string concatenation ("foo" "bar"). Chris@0: */ Chris@0: private function phpize($value) Chris@0: { Chris@0: // trim on the right as comments removal keep whitespaces Chris@17: if ($value !== $v = rtrim($value)) { Chris@17: $value = '""' === substr_replace($v, '', 1, -1) ? substr($v, 1, -1) : $v; Chris@17: } Chris@0: $lowercaseValue = strtolower($value); Chris@0: Chris@0: switch (true) { Chris@17: case \defined($value): Chris@17: return \constant($value); Chris@0: case 'yes' === $lowercaseValue || 'on' === $lowercaseValue: Chris@0: return true; Chris@0: case 'no' === $lowercaseValue || 'off' === $lowercaseValue || 'none' === $lowercaseValue: Chris@0: return false; Chris@0: case isset($value[1]) && ( Chris@17: ("'" === $value[0] && "'" === $value[\strlen($value) - 1]) || Chris@17: ('"' === $value[0] && '"' === $value[\strlen($value) - 1]) Chris@0: ): Chris@0: // quoted string Chris@0: return substr($value, 1, -1); Chris@0: default: Chris@0: return XmlUtils::phpize($value); Chris@0: } Chris@0: } Chris@0: }