Ejemplo n.º 1
0
 /**
  * @dataProvider crossCheckLoadersDumpers
  */
 public function testCrossCheck($fixture, $type)
 {
     $loaderClass = 'Symfony\\Components\\DependencyInjection\\Loader\\' . ucfirst($type) . 'FileLoader';
     $dumperClass = 'Symfony\\Components\\DependencyInjection\\Dumper\\' . ucfirst($type) . 'Dumper';
     $container1 = new Builder();
     $loader1 = new $loaderClass($container1);
     $loader1->load(self::$fixturesPath . '/' . $type . '/' . $fixture);
     $container1->setParameter('path', self::$fixturesPath . '/includes');
     $dumper = new $dumperClass($container1);
     $tmp = tempnam('sf_service_container', 'sf');
     file_put_contents($tmp, $dumper->dump());
     $container2 = new Builder();
     $loader2 = new $loaderClass($container2);
     $loader2->load($tmp);
     $container2->setParameter('path', self::$fixturesPath . '/includes');
     unlink($tmp);
     $this->assertEquals(serialize($container2), serialize($container1), 'loading a dump from a previously loaded container returns the same container');
     $this->assertEquals($container2->getParameterBag()->all(), $container1->getParameterBag()->all(), '->getParameterBag() returns the same value for both containers');
     $services1 = array();
     foreach ($container1 as $id => $service) {
         $services1[$id] = serialize($service);
     }
     $services2 = array();
     foreach ($container2 as $id => $service) {
         $services2[$id] = serialize($service);
     }
     unset($services1['service_container'], $services2['service_container']);
     $this->assertEquals($services2, $services1, 'Iterator on the containers returns the same services');
 }
Ejemplo n.º 2
0
 public function testAddService()
 {
     $container = (include self::$fixturesPath . '/containers/container9.php');
     $dumper = new PhpDumper($container);
     $this->assertEquals($dumper->dump(), str_replace('%path%', self::$fixturesPath . '/includes', file_get_contents(self::$fixturesPath . '/php/services9.php')), '->dump() dumps services');
     $dumper = new PhpDumper($container = new Builder());
     $container->register('foo', 'FooClass')->addArgument(new \stdClass());
     try {
         $dumper->dump();
         $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
     } catch (\RuntimeException $e) {
     }
 }
Ejemplo n.º 3
0
 public function testAddService()
 {
     $container = (include self::$fixturesPath . '/containers/container9.php');
     $dumper = new XmlDumper($container);
     $this->assertEquals(str_replace('%path%', self::$fixturesPath . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR, file_get_contents(self::$fixturesPath . '/xml/services9.xml')), $dumper->dump(), '->dump() dumps services');
     $dumper = new XmlDumper($container = new Builder());
     $container->register('foo', 'FooClass')->addArgument(new \stdClass());
     try {
         $dumper->dump();
         $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
         $this->assertEquals('Unable to dump a service container if a parameter is an object or a resource.', $e->getMessage(), '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
     }
 }
Ejemplo n.º 4
0
<?php

require_once __DIR__ . '/../includes/classes.php';
use Symfony\Components\DependencyInjection\Container;
use Symfony\Components\DependencyInjection\Builder;
use Symfony\Components\DependencyInjection\Reference;
use Symfony\Components\DependencyInjection\Parameter;
$container = new Builder();
$container->register('foo', 'FooClass')->setConstructor('getInstance')->setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, new Reference('service_container')))->setFile(realpath(__DIR__ . '/../includes/foo.php'))->setShared(false)->addMethodCall('setBar', array('bar'))->addMethodCall('initialize')->setConfigurator('sc_configure');
$container->register('bar', 'FooClass')->setArguments(array('foo', new Reference('foo.baz'), new Parameter('foo_bar')))->setShared(true)->setConfigurator(array(new Reference('foo.baz'), 'configure'));
$container->register('foo.baz', '%baz_class%')->setConstructor('getInstance')->setConfigurator(array('%baz_class%', 'configureStatic1'));
$container->register('foo_bar', '%foo_class%');
$container->setParameters(array('baz_class' => 'BazClass', 'foo_class' => 'FooClass', 'foo' => 'bar', 'foo_bar' => new Reference('foo_bar')));
$container->setAlias('alias_for_foo', 'foo');
$container->register('method_call1', 'FooClass')->addMethodCall('setBar', array(new Reference('foo')))->addMethodCall('setBar', array(new Reference('foo', Container::NULL_ON_INVALID_REFERENCE)))->addMethodCall('setBar', array(new Reference('foo', Container::IGNORE_ON_INVALID_REFERENCE)))->addMethodCall('setBar', array(new Reference('foobaz', Container::IGNORE_ON_INVALID_REFERENCE)));
return $container;
 protected function getValue($value, $default = '')
 {
     return Builder::resolveValue($value, $this->container->getParameters());
 }
Ejemplo n.º 6
0
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'baz'), '->merge() does not change the already defined parameters');
$container = new Builder(array('bar' => 'foo'));
$config = new BuilderConfiguration();
$config->setParameters(array('foo' => '%bar%'));
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
$container = new Builder(array('bar' => 'foo'));
$config = new BuilderConfiguration();
$config->setParameters(array('foo' => '%bar%', 'baz' => '%foo%'));
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
$container = new Builder();
$container->register('foo', 'FooClass');
$container->register('bar', 'BarClass');
$config = new BuilderConfiguration();
$config->setDefinition('baz', new Definition('BazClass'));
$config->setAlias('alias_for_foo', 'foo');
$container->merge($config);
$t->is(array_keys($container->getDefinitions()), array('foo', 'bar', 'baz'), '->merge() merges definitions already defined ones');
$t->is($container->getAliases(), array('alias_for_foo' => 'foo'), '->merge() registers defined aliases');
$container = new Builder();
$container->register('foo', 'FooClass');
$config->setDefinition('foo', new Definition('BazClass'));
$container->merge($config);
$t->is($container->getDefinition('foo')->getClass(), 'BazClass', '->merge() overrides already defined services');
// ->findAnnotatedServiceIds()
$t->diag('->findAnnotatedServiceIds()');
$builder = new Builder();
$builder->register('foo', 'FooClass')->addAnnotation('foo', array('foo' => 'foo'))->addAnnotation('bar', array('bar' => 'bar'))->addAnnotation('foo', array('foofoo' => 'foofoo'));
$t->is($builder->findAnnotatedServiceIds('foo'), array('foo' => array(array('foo' => 'foo'), array('foofoo' => 'foofoo'))), '->findAnnotatedServiceIds() returns an array of service ids and its annotation attributes');
$t->is($builder->findAnnotatedServiceIds('foobar'), array(), '->findAnnotatedServiceIds() returns an empty array if there is annotated services');
Ejemplo n.º 7
0
 /**
  * @covers Symfony\Components\DependencyInjection\Builder::findAnnotatedServiceIds
  */
 public function testFindAnnotatedServiceIds()
 {
     $builder = new Builder();
     $builder->register('foo', 'FooClass')->addAnnotation('foo', array('foo' => 'foo'))->addAnnotation('bar', array('bar' => 'bar'))->addAnnotation('foo', array('foofoo' => 'foofoo'));
     $this->assertEquals($builder->findAnnotatedServiceIds('foo'), array('foo' => array(array('foo' => 'foo'), array('foofoo' => 'foofoo'))), '->findAnnotatedServiceIds() returns an array of service ids and its annotation attributes');
     $this->assertEquals(array(), $builder->findAnnotatedServiceIds('foobar'), '->findAnnotatedServiceIds() returns an empty array if there is annotated services');
 }
require_once $fixturesPath . '/includes/foo.php';
$t = new LimeTest(30);
// cross-check loaders/dumpers
$t->diag('cross-check loaders/dumpers');
$fixtures = array('services1.xml' => 'xml', 'services2.xml' => 'xml', 'services6.xml' => 'xml', 'services8.xml' => 'xml', 'services9.xml' => 'xml', 'services1.yml' => 'yaml', 'services2.yml' => 'yaml', 'services6.yml' => 'yaml', 'services8.yml' => 'yaml', 'services9.yml' => 'yaml');
foreach ($fixtures as $fixture => $type) {
    $loaderClass = 'Symfony\\Components\\DependencyInjection\\Loader\\' . ucfirst($type) . 'FileLoader';
    $dumperClass = 'Symfony\\Components\\DependencyInjection\\Dumper\\' . ucfirst($type) . 'Dumper';
    $container1 = new Builder();
    $loader1 = new $loaderClass($container1);
    $loader1->load($fixturesPath . '/' . $type . '/' . $fixture);
    $container1->setParameter('path', $fixturesPath . '/includes');
    $dumper = new $dumperClass($container1);
    $tmp = tempnam('sf_service_container', 'sf');
    file_put_contents($tmp, $dumper->dump());
    $container2 = new Builder();
    $loader2 = new $loaderClass($container2);
    $loader2->load($tmp);
    $container2->setParameter('path', $fixturesPath . '/includes');
    unlink($tmp);
    $t->is(serialize($container1), serialize($container2), 'loading a dump from a previously loaded container returns the same container');
    $t->is($container1->getParameters(), $container2->getParameters(), '->getParameters() returns the same value for both containers');
    $services1 = array();
    foreach ($container1 as $id => $service) {
        $services1[$id] = serialize($service);
    }
    $services2 = array();
    foreach ($container2 as $id => $service) {
        $services2[$id] = serialize($service);
    }
    unset($services1['service_container'], $services2['service_container']);
Ejemplo n.º 9
0
<?php

use Symfony\Components\DependencyInjection\Builder;
$container = new Builder();
$container->setParameters(array('FOO' => 'bar', 'bar' => 'foo is %foo bar', 'values' => array(true, false, null, 0, 1000.3, 'true', 'false', 'null')));
return $container;
Ejemplo n.º 10
0
  protected function wrapServiceConditionals($value, $code)
  {
    if (!$services = Builder::getServiceConditionals($value))
    {
      return $code;
    }

    $conditions = array();
    foreach ($services as $service)
    {
      $conditions[] = sprintf("\$this->hasService('%s')", $service);
    }

    // re-indent the wrapped code
    $code = implode("\n", array_map(function ($line) { return $line ? '  '.$line : $line; }, explode("\n", $code)));

    return sprintf("    if (%s)\n    {\n%s    }\n", implode(' && ', $conditions), $code);
  }
Ejemplo n.º 11
0
 public function optimizeContainer(Builder $container)
 {
     // replace all classes with the real value
     foreach ($container->getDefinitions() as $definition) {
         if (false !== strpos($class = $definition->getClass(), '%')) {
             $definition->setClass(Builder::resolveValue($class, $container->getParameters()));
             unset($container[substr($class, 1, -1)]);
         }
     }
 }
 * file that was distributed with this source code.
 */
require_once __DIR__ . '/../../../../bootstrap.php';
use Symfony\Components\DependencyInjection\Builder;
use Symfony\Components\DependencyInjection\Dumper\XmlDumper;
$t = new LimeTest(4);
$fixturesPath = realpath(__DIR__ . '/../../../../../fixtures/Symfony/Components/DependencyInjection/');
// ->dump()
$t->diag('->dump()');
$dumper = new XmlDumper($container = new Builder());
$t->is($dumper->dump(), file_get_contents($fixturesPath . '/xml/services1.xml'), '->dump() dumps an empty container as an empty XML file');
$container = new Builder();
$dumper = new XmlDumper($container);
// ->addParameters()
$t->diag('->addParameters()');
$container = (include $fixturesPath . '//containers/container8.php');
$dumper = new XmlDumper($container);
$t->is($dumper->dump(), file_get_contents($fixturesPath . '/xml/services8.xml'), '->dump() dumps parameters');
// ->addService()
$t->diag('->addService()');
$container = (include $fixturesPath . '/containers/container9.php');
$dumper = new XmlDumper($container);
$t->is($dumper->dump(), str_replace('%path%', $fixturesPath . '/includes', file_get_contents($fixturesPath . '/xml/services9.xml')), '->dump() dumps services');
$dumper = new XmlDumper($container = new Builder());
$container->register('foo', 'FooClass')->addArgument(new stdClass());
try {
    $dumper->dump();
    $t->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
} catch (RuntimeException $e) {
    $t->pass('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
}
Ejemplo n.º 13
0
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->merge() merges current parameters with the loaded ones');
$container = new Builder(array('bar' => 'foo', 'foo' => 'baz'));
$config = new BuilderConfiguration();
$config->setParameters(array('foo' => 'bar'));
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'baz'), '->merge() does not change the already defined parameters');
$container = new Builder(array('bar' => 'foo'));
$config = new BuilderConfiguration();
$config->setParameters(array('foo' => '%bar%'));
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
$container = new Builder(array('bar' => 'foo'));
$config = new BuilderConfiguration();
$config->setParameters(array('foo' => '%bar%', 'baz' => '%foo%'));
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
$container = new Builder();
$container->register('foo', 'FooClass');
$container->register('bar', 'BarClass');
$config = new BuilderConfiguration();
$config->setDefinition('baz', new Definition('BazClass'));
$config->setAlias('alias_for_foo', 'foo');
$container->merge($config);
$t->is(array_keys($container->getDefinitions()), array('foo', 'bar', 'baz'), '->load() merges definitions already defined ones');
$t->is($container->getAliases(), array('alias_for_foo' => 'foo'), '->merge() registers defined aliases');
$container = new Builder();
$container->register('foo', 'FooClass');
$config->setDefinition('foo', new Definition('BazClass'));
$container->merge($config);
$t->is($container->getDefinition('foo')->getClass(), 'BazClass', '->merge() overrides already defined services');
Ejemplo n.º 14
0
<?php

require_once __DIR__.'/../includes/classes.php';

use Symfony\Components\DependencyInjection\Container;
use Symfony\Components\DependencyInjection\Builder;
use Symfony\Components\DependencyInjection\Reference;
use Symfony\Components\DependencyInjection\Parameter;

$container = new Builder();
$container->
  register('foo', 'FooClass')->
  setConstructor('getInstance')->
  setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, new Reference('service_container')))->
  setFile(realpath(__DIR__.'/../includes/foo.php'))->
  setShared(false)->
  addMethodCall('setBar', array('bar'))->
  addMethodCall('initialize')->
  setConfigurator('sc_configure')
;
$container->
  register('bar', 'FooClass')->
  setArguments(array('foo', new Reference('foo.baz'), new Parameter('foo_bar')))->
  setShared(true)->
  setConfigurator(array(new Reference('foo.baz'), 'configure'))
;
$container->
  register('foo.baz', '%baz_class%')->
  setConstructor('getInstance')->
  setConfigurator(array('%baz_class%', 'configureStatic1'))
;
Ejemplo n.º 15
0
<?php

require_once __DIR__ . '/../includes/classes.php';
use Symfony\Components\DependencyInjection\Builder;
use Symfony\Components\DependencyInjection\Reference;
$container = new Builder();
$container->register('foo', 'FooClass')->addArgument(new Reference('bar'));
return $container;