/** * Occurs on event ``bbapplication.init`` to dump application container if debug mode is false. * * @param Event $event */ public static function onApplicationInit(Event $event) { $application = $event->getTarget(); $container = $application->getContainer(); if (false === $application->isDebugMode() && false === $container->isRestored()) { $containerFilename = $container->getParameter('container.filename'); $containerDir = $container->getParameter('container.dump_directory'); if (false === is_dir($containerDir) && false === @mkdir($containerDir, 0755)) { throw new CannotCreateContainerDirectoryException($containerDir); } if (false === is_writable($containerDir)) { throw new ContainerDirectoryNotWritableException($containerDir); } $dumper = new PhpArrayDumper($container); $dump = $dumper->dump(array('do_compile' => true)); $container_proxy = new ContainerProxy(); $dump = unserialize($dump); $container_proxy->init($dump); $container_proxy->setParameter('services_dump', serialize($dump['services'])); $container_proxy->setParameter('is_compiled', $dump['is_compiled']); file_put_contents($containerDir . DIRECTORY_SEPARATOR . $containerFilename . '.php', (new PhpDumper($container_proxy))->dump(array('class' => $containerFilename, 'base_class' => 'BackBee\\DependencyInjection\\ContainerProxy'))); } elseif (true === $application->isDebugMode() && false === $container->isRestored()) { $container->compile(); } }
/** * @depends testGetContainerWithContextAndEnvironmentAndDebugFalse * * @covers ::tryParseContainerDump */ public function testDumpAndRestoreContainer(Container $container) { $dump_directory = $container->getParameter('container.dump_directory'); $dump_filename = $container->getParameter('container.filename'); $dumper = new PhpArrayDumper($container); $dump = $dumper->dump(array('do_compile' => true)); $container_proxy = new ContainerProxy(); $dump = unserialize($dump); $container_proxy->init($dump); $container_proxy->setParameter('services_dump', serialize($dump['services'])); $container_proxy->setParameter('is_compiled', $dump['is_compiled']); file_put_contents($dump_directory . DIRECTORY_SEPARATOR . $dump_filename . '.php', (new PhpDumper($container_proxy))->dump(array('class' => $dump_filename, 'base_class' => 'BackBee\\DependencyInjection\\ContainerProxy'))); $this->assertFileExists($dump_directory . DIRECTORY_SEPARATOR . $dump_filename . '.php'); $this->assertTrue(is_readable($dump_directory . DIRECTORY_SEPARATOR . $dump_filename . '.php')); $this->application->setContext('test'); $this->application->setEnvironment('test'); $this->application->setRepository($this->application->getBaseRepository() . DIRECTORY_SEPARATOR . $this->application->getContext()); $container_builder = new ContainerBuilder($this->application); $container_proxy = $container_builder->getContainer(); $this->assertInstanceOf($dump_filename, $container_proxy); $this->assertInstanceOf('BackBee\\DependencyInjection\\ContainerProxy', $container_proxy); }
public function testIsCompiled() { // test that ContainerProxy::isCompiled return false value $dumper = new PhpArrayDumper($this->container); $container = new ContainerProxy(); $container->init(unserialize($dumper->dump(array('do_compile' => false)))); $this->assertInstanceOf('BackBee\\DependencyInjection\\ContainerProxy', $container); $this->assertFalse($container->isCompiled()); // test that ContainerProxy::isCompiled return false value $dumper = new PhpArrayDumper($this->container); $container = new ContainerProxy(); $container->init(unserialize($dumper->dump(array('do_compile' => true)))); $this->assertTrue($container->isCompiled()); }