Esempio n. 1
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) {
     }
 }
Esempio n. 2
0
 public function testAddService()
 {
     $container = (include self::$fixturesPath . '/containers/container9.php');
     $dumper = new PhpDumper($container);
     $this->assertEquals(str_replace('%path%', str_replace('\\', '\\\\', self::$fixturesPath . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath . '/php/services9.php')), $dumper->dump(), '->dump() dumps services');
     $dumper = new PhpDumper($container = new ContainerBuilder());
     $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() returns a LogicException if the dump() method has not been overriden by a children class');
         $this->assertEquals('Unable to dump a service container if a parameter is an object or a resource.', $e->getMessage(), '->dump() returns a LogicException if the dump() method has not been overriden by a children class');
     }
 }
Esempio n. 3
0
 protected function buildContainer($class, $file)
 {
     $container = new Builder(new ParameterBag($this->getKernelParameters()));
     $configuration = new BuilderConfiguration();
     foreach ($this->bundles as $bundle) {
         $configuration->merge($bundle->buildContainer($container));
         if ($this->debug) {
             $configuration->addObjectResource($bundle);
         }
     }
     $configuration->merge($this->registerContainerConfiguration());
     $container->merge($configuration);
     $container->freeze();
     foreach (array('cache', 'logs') as $name) {
         $dir = $container->getParameter(sprintf('kernel.%s_dir', $name));
         if (!is_dir($dir)) {
             if (false === @mkdir($dir, 0777, true)) {
                 die(sprintf('Unable to create the %s directory (%s)', $name, dirname($dir)));
             }
         } elseif (!is_writable($dir)) {
             die(sprintf('Unable to write in the %s directory (%s)', $name, $dir));
         }
     }
     // cache the container
     $dumper = new PhpDumper($container);
     $content = $dumper->dump(array('class' => $class));
     if (!$this->debug) {
         $content = self::stripComments($content);
     }
     $this->writeCacheFile($file, $content);
     if ($this->debug) {
         $configuration->addObjectResource($this);
         // save the resources
         $this->writeCacheFile($this->getCacheDir() . '/' . $class . '.meta', serialize($configuration->getResources()));
     }
 }
Esempio n. 4
0
 protected function buildContainer($class, $file, $parameters)
 {
     $container = new Builder($parameters);
     $configuration = new BuilderConfiguration();
     foreach ($this->bundles as $bundle) {
         $configuration->merge($bundle->buildContainer($container));
     }
     $configuration->merge($this->registerContainerConfiguration());
     $container->merge($configuration);
     $this->optimizeContainer($container);
     // cache dir
     if (!is_dir($parameters['kernel.cache_dir'])) {
         if (false === @mkdir($parameters['kernel.cache_dir'], 0777, true)) {
             die(sprintf('Unable to write in the cache directory (%s)', dirname($parameters['kernel.cache_dir'])));
         }
     } elseif (!is_writable($parameters['kernel.cache_dir'])) {
         die(sprintf('Unable to write in the cache directory (%s)', $parameters['kernel.cache_dir']));
     }
     // logs dir
     if (!is_dir($parameters['kernel.logs_dir'])) {
         if (false === @mkdir($parameters['kernel.logs_dir'], 0777, true)) {
             die(sprintf('Failed to write in the logs directory (%s)', dirname($parameters['kernel.logs_dir'])));
         }
     } elseif (!is_writable($parameters['kernel.logs_dir'])) {
         die(sprintf('Failed to write in the logs directory (%s)', $parameters['kernel.logs_dir']));
     }
     // cache the container
     $dumper = new PhpDumper($container);
     $content = $dumper->dump(array('class' => $class));
     if (!$this->debug) {
         $content = self::stripComments($content);
     }
     $this->writeCacheFile($file, $content);
     if ($this->debug) {
         // save the resources
         $this->writeCacheFile($parameters['kernel.cache_dir'] . '/' . $class . '.meta', serialize($configuration->getResources()));
     }
 }
 */
require_once __DIR__ . '/../../../../bootstrap.php';
use Symfony\Components\DependencyInjection\Builder;
use Symfony\Components\DependencyInjection\Dumper\PhpDumper;
$t = new LimeTest(5);
$fixturesPath = realpath(__DIR__ . '/../../../../../fixtures/Symfony/Components/DependencyInjection/');
// ->dump()
$t->diag('->dump()');
$dumper = new PhpDumper($container = new Builder());
$t->is($dumper->dump(), file_get_contents($fixturesPath . '/php/services1.php'), '->dump() dumps an empty container as an empty PHP class');
$t->is($dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer')), file_get_contents($fixturesPath . '/php/services1-1.php'), '->dump() takes a class and a base_class options');
$container = new Builder();
$dumper = new PhpDumper($container);
// ->addParameters()
$t->diag('->addParameters()');
$container = (include $fixturesPath . '/containers/container8.php');
$dumper = new PhpDumper($container);
$t->is($dumper->dump(), file_get_contents($fixturesPath . '/php/services8.php'), '->dump() dumps parameters');
// ->addService()
$t->diag('->addService()');
$container = (include $fixturesPath . '/containers/container9.php');
$dumper = new PhpDumper($container);
$t->is($dumper->dump(), str_replace('%path%', $fixturesPath . '/includes', file_get_contents($fixturesPath . '/php/services9.php')), '->dump() dumps services');
$dumper = new PhpDumper($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');
}