public function testLoad() { $this->prepareForLoad(); $this->_sut->load($this->_mockContainer); $realContainerBuilder = new tubepress_internal_ioc_ContainerBuilder(); $realContainerBuilder->registerExtension($this->_sut); foreach ($this->getExpectedExternalServicesMap() as $id => $type) { if (is_string($type)) { $realContainerBuilder->set($id, $this->mock($type)); } else { $realContainerBuilder->set($id, $type); } } foreach ($this->getExpectedParameterMap() as $key => $value) { $realContainerBuilder->setParameter($key, $value); } foreach ($this->_expectedDecoratedServices as $serviceId) { $decoratedDefinition = new tubepress_internal_ioc_Definition($serviceId); $decoratedDefinition->setFactoryClass('Mockery'); $decoratedDefinition->setFactoryMethod('mock'); $decoratedDefinition->addArgument($serviceId); $realContainerBuilder->setDefinition(strtolower($serviceId), $decoratedDefinition); } $this->preCompile($realContainerBuilder); $realContainerBuilder->compile(); $this->postCompile($realContainerBuilder); foreach ($this->_expectedServiceConstructions as $id => $type) { $this->assertTrue($realContainerBuilder->hasDefinition($id), "Expected that container has definition for {$id}"); $this->assertTrue($realContainerBuilder->has($id), "Expected that container has definition for {$id}"); $service = $realContainerBuilder->get($id); if (is_string($type)) { $this->assertInstanceOf($type, $service, "{$id} is not an instance of {$type} (actually is " . get_class($service) . ')'); } else { /** * @var $def tubepress_api_ioc_DefinitionInterface */ $def = $type; $this->assertInstanceOf($def->getClass(), $service); } } }
/** * @param tubepress_internal_ioc_ContainerBuilder $containerBuilder * * @return \Symfony\Component\DependencyInjection\ContainerInterface */ private function _convertToSymfonyContainer(tubepress_internal_ioc_ContainerBuilder $containerBuilder) { if ($this->_shouldLog) { $this->_logDebug('Preparing to store boot to cache.'); } $dumpedContainerText = $this->_getDumpedSymfonyContainerAsString($containerBuilder->getDelegateContainerBuilder()); if ($this->_bootSettings->isSystemCacheEnabled()) { $cachePath = $this->_bootSettings->getPathToSystemCacheDirectory(); $storagePath = sprintf('%s%sTubePressServiceContainer.php', $cachePath, DIRECTORY_SEPARATOR); } else { $storagePath = tempnam(sys_get_temp_dir(), 'TubePressServiceContainer'); } if (!is_dir(dirname($storagePath))) { if ($this->_shouldLog) { $this->_logDebug(sprintf('Attempting to create all the parent directories of <code>%s</code>', $storagePath)); } $success = @mkdir(dirname($storagePath), 0755, true); if ($this->_shouldLog) { if ($success === true) { $this->_logDebug(sprintf('Created all the parent directories of <code>%s</code>', $storagePath)); } else { $this->_logger->error(sprintf('Failed to create all the parent directories of <code>%s</code>', $storagePath)); } } if ($success !== true) { return $containerBuilder->getDelegateContainerBuilder(); } } if ($this->_shouldLog) { $this->_logDebug(sprintf('Now writing dumped container to <code>%s</code>', $storagePath)); } $success = @file_put_contents($storagePath, $dumpedContainerText) !== false; if ($success) { if ($this->_shouldLog) { $this->_logDebug(sprintf('Saved service container to <code>%s</code>. Now including it.', $storagePath)); } if (!class_exists('TubePressServiceContainer', false)) { /** @noinspection PhpIncludeInspection */ require $storagePath; } } else { if ($this->_shouldLog) { $this->_logger->error(sprintf('Could not write service container to <code>%s</code>.', $storagePath)); } return $containerBuilder->getDelegateContainerBuilder(); } /** @noinspection PhpUndefinedClassInspection */ return new TubePressServiceContainer(); }
public function testAddDefinitionsWhenFrozen() { $this->setExpectedException('BadMethodCallException', 'Adding definition to a frozen container is not allowed'); $this->_sut->compile(); $this->_sut->addDefinitions(array(new tubepress_internal_ioc_Definition('clazz'))); }
/** * @param tubepress_internal_ioc_ContainerBuilder $container * @param $extensionClassName * @param $index * @param $count * @param $addon */ private function _registerModernExtension(tubepress_internal_ioc_ContainerBuilder $container, $extensionClassName, $index, $count, tubepress_api_contrib_AddonInterface $addon) { try { $ref = new ReflectionClass($extensionClassName); /** @noinspection PhpParamsInspection */ $container->registerExtension($ref->newInstance()); if ($this->_shouldLog) { $this->_logDebug(sprintf('(Add-on <code>%d</code> of <code>%d</code>: <code>%s</code>) Successfully loaded <code>%s</code> as a container extension', $index, $count, $addon->getName(), $extensionClassName)); } } catch (Exception $e) { if ($this->_shouldLog) { $this->_logger->error(sprintf('(Add-on <code>%d</code> of <code>%d</code>: <code>%s</code>) Failed to load <code>%s</code> as a container extension: <code>%s</code>', $index, $count, $addon->getName(), $extensionClassName, $e->getMessage())); } } }
/** * @internal */ private function merge(tubepress_internal_ioc_ContainerBuilder $containerBuilder) { $this->addDefinitions($containerBuilder->getDefinitions()); $this->getParameterBag()->add($containerBuilder->getParameterBag()->all()); }