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\Resource\FileResource; 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@0: $this->container->addResource(new FileResource($path)); Chris@0: Chris@0: // first pass to catch parsing errors Chris@0: $result = parse_ini_file($path, true); Chris@0: if (false === $result || array() === $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@0: 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@0: return is_string($resource) && 'ini' === pathinfo($resource, PATHINFO_EXTENSION); 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@0: $value = rtrim($value); Chris@0: $lowercaseValue = strtolower($value); Chris@0: Chris@0: switch (true) { Chris@0: case defined($value): Chris@0: 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@0: ("'" === $value[0] && "'" === $value[strlen($value) - 1]) || Chris@0: ('"' === $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: }