Author: Fabien Potencier (fabien@symfony.com)
Author: Martin Hasoň (martin.hason@gmail.com)
Inheritance: extends Symfony\Component\DependencyInjection\Dumper\Dumper
Exemple #1
0
 /**
  * Loads a container and returns it.
  *
  * If the cache file for the service container exists and is current, it
  * will be loaded and returned. Otherwise, a new container will be built
  * using the configuration file and the provided optional builder. The
  * builder will be used to make changes to the service container before
  * it is compiled and cached.
  *
  * It may be important to note that debug mode for the `ConfigCache` class
  * is enabled by default. This will ensure that cached configuration files
  * are updated whenever they are changed.
  *
  * @param string   $containerCacheFilePath     The container cache file path.
  * @param callable $containerBuilderCallable   The new container builder callable.
  * @param string   $compiledContainerClassName The compiled container class name.
  * @param boolean  $debug                      Is debugging mode enabled?
  *
  * @return Jarvis The loaded application.
  */
 public static function create($containerCacheFilePath, callable $containerBuilderCallable = null, $compiledContainerClassName = 'AppCachedContainer', $debug = true)
 {
     $cacheManager = new ConfigCache($containerCacheFilePath, $debug);
     if (!$cacheManager->isFresh()) {
         $container = static::createContainer();
         if (null !== $containerBuilderCallable) {
             $containerBuilderCallable($container);
         }
         if ($debug) {
             $filename = pathinfo($containerCacheFilePath, PATHINFO_DIRNAME) . '/' . pathinfo($containerCacheFilePath, PATHINFO_FILENAME) . '.xml';
             $container->setParameter('debug.container.dump', $filename);
         }
         $container->compile();
         $dumper = new PhpDumper($container);
         $cacheManager->write($dumper->dump(array('class' => $compiledContainerClassName)), $container->getResources());
         if ($debug) {
             $filename = $container->getParameter('debug.container.dump');
             $dumper = new XmlDumper($container);
             $filesystem = new Filesystem();
             $filesystem->dumpFile($filename, $dumper->dump(), null);
             try {
                 $filesystem->chmod($filename, 0666, umask());
             } catch (IOException $e) {
                 // discard chmod failure (some filesystem may not support it)
             }
         }
     }
     if (!class_exists($compiledContainerClassName)) {
         /** @noinspection PhpIncludeInspection */
         require $containerCacheFilePath;
     }
     return new Jarvis(new $compiledContainerClassName());
 }
 public function process(ContainerBuilder $container)
 {
     $dumper = new XmlDumper($container);
     $filename = $container->getParameter('debug.container.dump');
     $filesystem = new Filesystem();
     $filesystem->dumpFile($filename, $dumper->dump(), null);
     // discard chmod failure (some filesystem may not support it)
     @chmod($filename, 0666 & ~umask());
 }
Exemple #3
0
 public function testInterfaceInjectors()
 {
     $interfaceInjector = new InterfaceInjector('FooClass');
     $interfaceInjector->addMethodCall('setBar', array('someValue'));
     $container = (include self::$fixturesPath . '/containers/interfaces1.php');
     $container->addInterfaceInjector($interfaceInjector);
     $dumper = new XmlDumper($container);
     $classBody = $dumper->dump();
     //TODO: find a better way to test dumper
     //var_dump($classBody);
     $this->assertEquals("<?xml version=\"1.0\" ?>\n\n<container xmlns=\"http://www.symfony-project.org/schema/dic/services\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xsi:schemaLocation=\"http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd\">\n  <parameters>\n    <parameter key=\"cla\">Fo</parameter>\n    <parameter key=\"ss\">Class</parameter>\n  </parameters>\n  <interfaces>\n    <interface class=\"FooClass\">\n      <call method=\"setBar\">\n        <argument>someValue</argument>\n      </call>\n    </interface>\n  </interfaces>\n  <services>\n    <service id=\"foo\" class=\"%cla%o%ss%\">\n    </service>\n  </services>\n</container>\n", $classBody);
 }
Exemple #4
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 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() 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');
     }
 }
Exemple #5
0
 public function testDumpEntities()
 {
     include self::$fixturesPath . '/containers/container12.php';
     $dumper = new XmlDumper($container);
     $this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<container xmlns=\"http://symfony.com/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd\">\n  <services>\n    <service id=\"foo\" class=\"FooClass\\Foo\">\n      <tag name=\"foo&quot;bar\\bar\" foo=\"foo&quot;barřž€\"/>\n      <argument>foo&lt;&gt;&amp;bar</argument>\n    </service>\n  </services>\n</container>\n", $dumper->dump());
 }
 /**
  * Dumps the container configuration as an XML file for rebuilding.
  *
  * @param ContainerBuilder $container The container builder.
  * @param string           $file      The path to the cache file.
  */
 private function dumpXmlConfig(ContainerBuilder $container, $file)
 {
     $file = sprintf('%s%s%s.xml', dirname($file), DIRECTORY_SEPARATOR, pathinfo($file, PATHINFO_FILENAME));
     $container->getDefinition(self::getId('helper.container'))->addMethodCall('setFile', array($file));
     $dumper = new XmlDumper($container);
     $writer = new File($file, 'w');
     $writer->fwrite($dumper->dump());
 }
 public function testDumpAutowireData()
 {
     $container = (include self::$fixturesPath . '/containers/container24.php');
     $dumper = new XmlDumper($container);
     $this->assertEquals(file_get_contents(self::$fixturesPath . '/xml/services24.xml'), $dumper->dump());
 }
 /**
  * @dataProvider provideCompiledContainerData
  */
 public function testCompiledContainerCanBeDumped($containerFile)
 {
     $fixturesPath = __DIR__ . '/../Fixtures';
     $container = (require $fixturesPath . '/containers/' . $containerFile . '.php');
     $container->compile();
     $dumper = new XmlDumper($container);
     $dumper->dump();
 }
 private function dump(string $configFile, string $servicesFile)
 {
     $container = $this->loadContainer($configFile, $servicesFile);
     $dumper = null;
     $dumper = new XmlDumper($container);
     self::assertNotEmpty($dumper->dump());
     $dumper = new YamlDumper($container);
     self::assertNotEmpty($dumper->dump());
 }
 private static function dumpForDebug($filename, ContainerBuilder $container)
 {
     $dumper = new XmlDumper($container);
     $filesystem = new Filesystem();
     $filesystem->dumpFile($filename, $dumper->dump());
 }
 public function process(ContainerBuilder $container)
 {
     $dumper = new XmlDumper($container);
     $filesystem = new Filesystem();
     $filesystem->dumpFile($container->getParameter('debug.container.dump'), $dumper->dump(), 0666 & ~umask());
 }
Exemple #12
0
 /**
  * @return Runnable
  */
 public function boot()
 {
     $cacheDir = $this->getCacheDir();
     if (!file_exists($cacheDir)) {
         mkdir($cacheDir, 0755, true);
     }
     $containerFile = sprintf('%s/%s', $cacheDir, 'CachedContainer.php');
     $containerConfigCache = new ConfigCache($containerFile, $this->isDebug());
     if ($containerConfigCache->isFresh()) {
         require_once $containerFile;
         return new Runnable(new \CachedContainer());
     }
     $container = new ContainerBuilder();
     $passConfig = $container->getCompilerPassConfig();
     $this->configureContainerPass($passConfig);
     $this->configureContainer($container);
     foreach ($this->registerBundles() as $bundle) {
         $bundle->build($container);
     }
     if (null !== ($cont = $this->registerContainerConfiguration($this->getContainerLoader($container)))) {
         $container->merge($cont);
     }
     $container->compile();
     $phpDumper = new PhpDumper($container);
     file_put_contents($containerFile, $phpDumper->dump(['class' => 'CachedContainer']));
     $xmlDumper = new XmlDumper($container);
     file_put_contents(dirname($containerFile) . '/' . basename($containerFile, '.php') . '.xml', $xmlDumper->dump());
     return new Runnable($container);
 }
Exemple #13
0
 /**
  * @dataProvider provideDecoratedServicesData
  */
 public function testDumpDecoratedServices($expectedXmlDump, $container)
 {
     $dumper = new XmlDumper($container);
     $this->assertEquals($expectedXmlDump, $dumper->dump());
 }
Exemple #14
0
 /**
  * Build and return the DIC 
  * Will set a parameter 'baseDir' into the DI container
  * Stores cached version of DIC and if environment == 'development', will
  * also write out an xml version in the same location which can be helpful
  * for debugging
  * 
  * @param StringType $baseDir
  * @param StringType $environment
  * 
  * @return Symfony\Component\DependencyInjection\ContainerBuilder
  * 
  * @throws \Exception
  */
 public static function buildDic(StringType $baseDir, StringType $environment)
 {
     $diName = sprintf(self::DIC_SOURCE_TPL_NAME, $baseDir, $environment);
     if (!file_exists($diName)) {
         throw new \Exception(self::ERR_NO_DIC);
     }
     //create dic and cache it
     $dic = new ContainerBuilder();
     $loader = new XmlFileLoader($dic, new FileLocator(dirname($diName)));
     $loader->load($diName);
     self::setSpoolDir($dic, $baseDir);
     $dic->compile();
     $dumper = new PhpDumper($dic);
     $diCacheName = $baseDir . self::CACHE_PHP_NAME;
     file_put_contents($diCacheName, $dumper->dump());
     if ($environment() == Environment::ENVSTATE_DEV) {
         $xmlCacheName = $baseDir . self::CACHE_XML_NAME;
         $xmlDumper = new XmlDumper($dic);
         file_put_contents($xmlCacheName, $xmlDumper->dump());
     }
     return $dic;
 }
 public function process(ContainerBuilder $container)
 {
     $dumper = new XmlDumper($container);
     $cache = new ConfigCache($container->getParameter('debug.container.dump'), false);
     $cache->write($dumper->dump());
 }
Exemple #16
0
    public function testDumpInlinedServices()
    {
        $container = include self::$fixturesPath.'/containers/container21.php';
        $dumper = new XmlDumper($container);

        $this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services21.xml'), $dumper->dump());
    }