/**
   * Loads an array of XML files.
   *
   * @param  string $file An XML file path
   *
   * @return BuilderConfiguration A BuilderConfiguration instance
   */
  public function load($file)
  {
    $path = $this->findFile($file);

    $xml = $this->parseFile($path);

    $configuration = new BuilderConfiguration();

    $configuration->addResource(new FileResource($path));

    // anonymous services
    $xml = $this->processAnonymousServices($configuration, $xml, $file);

    // imports
    $this->parseImports($configuration, $xml, $file);

    // parameters
    $this->parseParameters($configuration, $xml, $file);

    // services
    $this->parseDefinitions($configuration, $xml, $file);

    // extensions
    $this->loadFromExtensions($configuration, $xml);

    return $configuration;
  }
Exemple #2
0
 /**
  * Loads an array of Yaml files.
  *
  * @param mixed                $resource       The resource
  * @param Boolean              $main           Whether this is the main load() call
  * @param BuilderConfiguration $configuration  A BuilderConfiguration instance to use for the configuration
  *
  * @return BuilderConfiguration A BuilderConfiguration instance
  */
 public function load($file, $main = true, BuilderConfiguration $configuration = null)
 {
     $path = $this->findFile($file);
     $content = $this->loadFile($path);
     if (null === $configuration) {
         $configuration = new BuilderConfiguration();
     }
     $configuration->addResource(new FileResource($path));
     if (!$content) {
         return $configuration;
     }
     // imports
     $this->parseImports($configuration, $content, $file);
     // extensions
     $this->loadFromExtensions($configuration, $content);
     if ($main) {
         $configuration->mergeExtensionsConfiguration();
     }
     // parameters
     if (isset($content['parameters'])) {
         foreach ($content['parameters'] as $key => $value) {
             $configuration->setParameter(strtolower($key), $this->resolveServices($value));
         }
     }
     // services
     $this->parseDefinitions($configuration, $content, $file);
     return $configuration;
 }
 /**
  * Loads a resource.
  *
  * @param  string $file An INI file path
  *
  * @return BuilderConfiguration A BuilderConfiguration instance
  *
  * @throws \InvalidArgumentException When ini file is not valid
  */
 public function load($file)
 {
     $path = $this->findFile($file);
     $configuration = new BuilderConfiguration();
     $configuration->addResource(new FileResource($path));
     $result = parse_ini_file($path, true);
     if (false === $result || array() === $result) {
         throw new \InvalidArgumentException(sprintf('The %s file is not valid.', $file));
     }
     if (isset($result['parameters']) && is_array($result['parameters'])) {
         foreach ($result['parameters'] as $key => $value) {
             $configuration->setParameter(strtolower($key), $value);
         }
     }
     return $configuration;
 }
$configuration->setDefinition('foobar', $foo = new Definition('FooBarClass'));
$t->is($configuration->getDefinition('foobar'), $foo, '->getDefinition() returns a service definition if defined');
$t->ok($configuration->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fuild interface by returning the service reference');

$configuration->addDefinitions($defs = array('foobar' => new Definition('FooBarClass')));
$t->is($configuration->getDefinitions(), array_merge($definitions, $defs), '->addDefinitions() adds the service definitions');

try
{
  $configuration->getDefinition('baz');
  $t->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
}
catch (InvalidArgumentException $e)
{
  $t->pass('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
}

// ->findDefinition()
$t->diag('->findDefinition()');
$configuration = new BuilderConfiguration(array('foo' => $definition = new Definition('FooClass')));
$configuration->setAlias('bar', 'foo');
$configuration->setAlias('foobar', 'bar');
$t->is($configuration->findDefinition('foobar'), $definition, '->findDefinition() returns a Definition');

// ->addResource() ->getResources()
$t->diag('->addResource() ->getResources()');
$configuration = new BuilderConfiguration();
$configuration->addResource($a = new FileResource('foo.xml'));
$configuration->addResource($b = new FileResource('foo.yml'));
$t->is($configuration->getResources(), array($a, $b), '->getResources() returns an array of resources read for the current configuration');
 public function testResources()
 {
     $configuration = new BuilderConfiguration();
     $configuration->addResource($a = new FileResource('foo.xml'));
     $configuration->addResource($b = new FileResource('foo.yml'));
     $this->assertEquals(array($a, $b), $configuration->getResources(), '->getResources() returns an array of resources read for the current configuration');
 }
 /**
  * @covers Symfony\Components\DependencyInjection\BuilderConfiguration::getResources
  * @covers Symfony\Components\DependencyInjection\BuilderConfiguration::addResource
  */
 public function testResources()
 {
     $configuration = new BuilderConfiguration();
     $configuration->addResource($a = new FileResource(self::$fixturesPath . '/xml/services1.xml'));
     $configuration->addResource($b = new FileResource(self::$fixturesPath . '/xml/services2.xml'));
     $this->assertEquals(array($a, $b), $configuration->getResources(), '->getResources() returns an array of resources read for the current configuration');
 }
Exemple #7
0
 /**
  * Loads the web configuration.
  *
  * @param array                $config        A configuration array
  * @param BuilderConfiguration $configuration A BuilderConfiguration instance
  *
  * @return BuilderConfiguration A BuilderConfiguration instance
  */
 public function configLoad($config, BuilderConfiguration $configuration)
 {
     if (!$configuration->hasDefinition('controller_manager')) {
         $loader = new XmlFileLoader(__DIR__ . '/../Resources/config');
         $configuration->merge($loader->load($this->resources['web']));
     }
     if (isset($config['ide']) && 'textmate' === $config['ide']) {
         $configuration->setParameter('debug.file_link_format', 'txmt://open?url=file://%%f&line=%%l');
     }
     if (isset($config['toolbar']) && $config['toolbar']) {
         $config['profiler'] = true;
     }
     if (isset($config['profiler'])) {
         if ($config['profiler']) {
             if (!$configuration->hasDefinition('profiler')) {
                 $loader = new XmlFileLoader(__DIR__ . '/../Resources/config');
                 $configuration->merge($loader->load('profiling.xml'));
             }
         } elseif ($configuration->hasDefinition('profiler')) {
             $configuration->getDefinition('profiling')->clearAnnotations();
         }
     }
     // toolbar need to be registered after the profiler
     if (isset($config['toolbar'])) {
         if ($config['toolbar']) {
             if (!$configuration->hasDefinition('debug.toolbar')) {
                 $loader = new XmlFileLoader(__DIR__ . '/../Resources/config');
                 $configuration->merge($loader->load('toolbar.xml'));
             }
         } elseif ($configuration->hasDefinition('debug.toolbar')) {
             $configuration->getDefinition('debug.toolbar')->clearAnnotations();
         }
     }
     if (isset($config['validation'])) {
         if ($config['validation']) {
             if (!$configuration->hasDefinition('validator')) {
                 $loader = new XmlFileLoader(__DIR__ . '/../Resources/config');
                 $configuration->merge($loader->load($this->resources['validation']));
             }
             $xmlMappingFiles = array();
             $yamlMappingFiles = array();
             $messageFiles = array();
             // default entries by the framework
             $xmlMappingFiles[] = __DIR__ . '/../../../Components/Form/Resources/config/validation.xml';
             $messageFiles[] = __DIR__ . '/../../../Components/Validator/Resources/i18n/messages.en.xml';
             $messageFiles[] = __DIR__ . '/../../../Components/Form/Resources/i18n/messages.en.xml';
             foreach ($this->bundles as $className) {
                 $tmp = dirname(str_replace('\\', '/', $className));
                 $namespace = str_replace('/', '\\', dirname($tmp));
                 $bundle = basename($tmp);
                 foreach ($this->bundleDirs as $dir) {
                     if (file_exists($file = $dir . '/' . $bundle . '/Resources/config/validation.xml')) {
                         $xmlMappingFiles[] = realpath($file);
                     }
                     if (file_exists($file = $dir . '/' . $bundle . '/Resources/config/validation.yml')) {
                         $yamlMappingFiles[] = realpath($file);
                     }
                     // TODO do we really want the message files of all cultures?
                     foreach (glob($dir . '/' . $bundle . '/Resources/i18n/messages.*.xml') as $file) {
                         $messageFiles[] = realpath($file);
                     }
                 }
             }
             $xmlFilesLoader = new Definition($configuration->getParameter('validator.mapping.loader.xml_files_loader.class'), array($xmlMappingFiles));
             $yamlFilesLoader = new Definition($configuration->getParameter('validator.mapping.loader.yaml_files_loader.class'), array($yamlMappingFiles));
             $configuration->setDefinition('validator.mapping.loader.xml_files_loader', $xmlFilesLoader);
             $configuration->setDefinition('validator.mapping.loader.yaml_files_loader', $yamlFilesLoader);
             $configuration->setParameter('validator.message_interpolator.files', $messageFiles);
             foreach ($xmlMappingFiles as $file) {
                 $configuration->addResource(new FileResource($file));
             }
             foreach ($yamlMappingFiles as $file) {
                 $configuration->addResource(new FileResource($file));
             }
             foreach ($messageFiles as $file) {
                 $configuration->addResource(new FileResource($file));
             }
             if (isset($config['validation']['annotations']) && $config['validation']['annotations'] === true) {
                 $annotationLoader = new Definition($configuration->getParameter('validator.mapping.loader.annotation_loader.class'));
                 $configuration->setDefinition('validator.mapping.loader.annotation_loader', $annotationLoader);
                 $loader = $configuration->getDefinition('validator.mapping.loader.loader_chain');
                 $arguments = $loader->getArguments();
                 array_unshift($arguments, new Reference('validator.mapping.loader.annotation_loader'));
                 $loader->setArguments($arguments);
             }
         } elseif ($configuration->hasDefinition('validator')) {
             $configuration->getDefinition('validator')->clearAnnotations();
         }
     }
     return $configuration;
 }
 /**
  * Detects what metadata driver to use for the supplied directory.
  *
  * @param string $dir A directory path
  * @param Symfony\Components\DependencyInjection\BuilderConfiguration $configuration A builder configuration
  *
  * @return string|null A metadata driver short name, if one can be detected
  */
 protected static function detectMetadataDriver($dir, BuilderConfiguration $configuration)
 {
     // add the closest existing directory as a resource
     $resource = $dir . '/Resources/config/doctrine/metadata';
     while (!is_dir($resource)) {
         $resource = dirname($resource);
     }
     $configuration->addResource(new FileResource($resource));
     if (count(glob($dir . '/Resources/config/doctrine/metadata/*.xml'))) {
         return 'xml';
     } elseif (count(glob($dir . '/Resources/config/doctrine/metadata/*.yml'))) {
         return 'yml';
     }
     // add the directory itself as a resource
     $configuration->addResource(new FileResource($dir));
     if (is_dir($dir . '/Document')) {
         return 'annotation';
     }
 }