public function createServiceManager($config) { $serviceManager = new ServiceManager(); $serviceManager->setService('Configuration', $config); $serviceManager->setAlias('Config', 'Configuration'); return $serviceManager; }
public function setup() { $this->config = new ArrayObject(array('diagnostics' => array())); $this->sm = new ServiceManager(); $this->sm->setService('console', new ConsoleAdapter()); $this->sm->setService('config', $this->config); $this->sm->setAlias('configuration', 'config'); $this->mm = new InjectableModuleManager(); $this->sm->setService('modulemanager', $this->mm); $event = new MvcEvent(); $this->routeMatch = new RouteMatch(array('controller' => 'ZFTools\\Controller\\Diagnostics', 'action' => 'run')); $event->setRouteMatch($this->routeMatch); $this->controller = new DiagnosticsController(); $this->controller->setServiceLocator($this->sm); $this->controller->setEvent($event); }
/** * @covers Zend\ServiceManager\ServiceManager::hasAlias */ public function testHasAlias() { $this->assertFalse($this->serviceManager->hasAlias('foo')); $this->serviceManager->setService('bar', 'baz'); $this->serviceManager->setAlias('foo', 'bar'); $this->assertTrue($this->serviceManager->hasAlias('foo')); }
/** * Instantiates and configures the "route not found", or 404, strategy * * @return RouteNotFoundStrategy */ public function getRouteNotFoundStrategy() { if ($this->routeNotFoundStrategy) { return $this->routeNotFoundStrategy; } $this->routeNotFoundStrategy = new RouteNotFoundStrategy(); $displayExceptions = false; $displayNotFoundReason = false; $notFoundTemplate = '404'; if (isset($this->config['display_exceptions'])) { $displayExceptions = $this->config['display_exceptions']; } if (isset($this->config['display_not_found_reason'])) { $displayNotFoundReason = $this->config['display_not_found_reason']; } if (isset($this->config['not_found_template'])) { $notFoundTemplate = $this->config['not_found_template']; } $this->routeNotFoundStrategy->setDisplayExceptions($displayExceptions); $this->routeNotFoundStrategy->setDisplayNotFoundReason($displayNotFoundReason); $this->routeNotFoundStrategy->setNotFoundTemplate($notFoundTemplate); $this->services->setService('RouteNotFoundStrategy', $this->routeNotFoundStrategy); $this->services->setAlias('Zend\\Mvc\\View\\RouteNotFoundStrategy', 'RouteNotFoundStrategy'); $this->services->setAlias('Zend\\Mvc\\View\\Http\\RouteNotFoundStrategy', 'RouteNotFoundStrategy'); $this->services->setAlias('404Strategy', 'RouteNotFoundStrategy'); return $this->routeNotFoundStrategy; }
public function testGet() { // Via container $this->container->foo = []; $this->assertEquals([], $this->container->get('foo')); // Via service manager $this->sm->setService('foo', new \stdClass()); $this->sm->setAlias('bar', 'foo'); $this->assertInstanceOf('stdClass', $this->container->get('bar')); $this->sm->setFactory('factory', function (ServiceManager $sm) { return $sm->get('bar'); }); $this->assertInstanceOf('stdClass', $this->container->get('factory')); $this->sm->setInvokableClass('invokable', 'stdClass'); $this->assertInstanceOf('stdClass', $this->container->get('invokable')); }
protected function createServiceManager(array $config) { $serviceManager = new ServiceManager(new Config($config)); $serviceManager->setService('Configuration', $config); $serviceManager->setAlias('Config', 'Configuration'); return $serviceManager; }
/** * Test get SessionManager * * @expectedException PHPUnit_Framework_Error_Warning */ public function testCreateService() { $serviceManager = new ServiceManager(); $serviceFactory = new SessionManagerFactory(); $serviceManager->setAlias('Configuration', 'Config')->setService('Config', array())->setService('Zend\\Session\\Storage\\StorageInterface', new ArrayStorage()); $serviceFactory->createService($serviceManager); }
/** * @param array $applicationConfig * @return ServiceManager */ protected function initServiceManager(array $applicationConfig) { $smConfig = new Config(isset($applicationConfig['service_manager']) ? $applicationConfig['service_manager'] : []); $serviceManager = new ServiceManager($smConfig); $serviceManager->setService('Config', $applicationConfig); $serviceManager->setAlias('Configuration', 'Config'); return $serviceManager; }
/** * @covers Zend\ServiceManager\ServiceManager::setAlias * @covers Zend\ServiceManager\ServiceManager::get * @covers Zend\ServiceManager\ServiceManager::retrieveFromPeeringManager */ public function testCanGetAliasedServicesFromPeeringServiceManagers() { $service = new \stdClass(); $peeringSm = new ServiceManager(); $peeringSm->setService('actual-service-name', $service); $this->serviceManager->addPeeringServiceManager($peeringSm); $this->serviceManager->setAlias('alias-name', 'actual-service-name'); $this->assertSame($service, $this->serviceManager->get('alias-name')); }
/** * Instantiates and configures the Zend Monitor Exception strategy * @return \Zend\Mvc\View\Http\ZendMonitorExceptionStrategy */ public function getZendMonitorExceptionStrategy() { if ($this->zendMonitorExceptionStrategy) { return $this->zendMonitorExceptionStrategy; } $this->zendMonitorExceptionStrategy = new ZendMonitorExceptionStrategy(); $this->services->setService('zendMonitorExceptionStrategy', $this->zendMonitorExceptionStrategy); $this->services->setAlias('Zend\\Mvc\\View\\Http\\ZendMonitorExceptionStrategy', 'ZendMonitorExceptionStrategy'); return $this->zendMonitorExceptionStrategy; }
/** * @covers \DoctrineModule\Service\CacheFactory::createService * @group 547 */ public function testCreateZendCache() { $factory = new CacheFactory('phpunit'); $serviceManager = new ServiceManager(); $serviceManager->setAlias('Config', 'Configuration'); $serviceManager->setService('Configuration', ['doctrine' => ['cache' => ['phpunit' => ['class' => 'DoctrineModule\\Cache\\ZendStorageCache', 'instance' => 'my-zend-cache', 'namespace' => 'DoctrineModule']]], 'caches' => ['my-zend-cache' => ['adapter' => ['name' => 'blackhole']]]]); $serviceManager->addAbstractFactory('Zend\\Cache\\Service\\StorageCacheAbstractServiceFactory'); $cache = $factory->createService($serviceManager); $this->assertInstanceOf('DoctrineModule\\Cache\\ZendStorageCache', $cache); }
public function createServiceManager($config) { $serviceManager = new ServiceManager(); $serviceManager->setService('AsseticAssetManager', $this->assetManager); $serviceManager->setService('router', $this->router); $serviceManager->setService('Configuration', $config); $serviceManager->setAlias('Config', 'Configuration'); $serviceManager->setService('AssetManifest', $this->createAssetManifest($config)); return $serviceManager; }
/** * @expectedException Zend\ServiceManager\Exception\InvalidServiceNameException */ public function testAssignAliasWithExistingServiceName() { $this->serviceManager->setFactory('foo', 'ZendTest\\ServiceManager\\TestAsset\\FooFactory'); $this->serviceManager->setFactory('bar', function ($sm) { return new Bar(array('a')); }); $this->serviceManager->setAllowOverride(false); // should throw an exception because 'foo' already exists in the service manager $this->serviceManager->setAlias('foo', 'bar'); }
/** * Configure the provided service manager instance with the configuration * in this class. * * @param ServiceManager $serviceManager * @return ServiceManager */ public function configureServiceManager(ServiceManager $serviceManager) { foreach ($this->factories as $name => $factory) { $serviceManager->setFactory($name, $factory); } foreach ($this->aliases as $alias => $target) { $serviceManager->setAlias($alias, $target); } return $serviceManager; }
/** * Test get SessionManager */ public function testCreateService() { $serviceManager = new ServiceManager(); $serviceFactory = new SessionConfigFactory(); $serviceManager->setAlias('Zork\\Db\\SiteInfo', 'SiteInfo')->setAlias('Configuration', 'Config')->setService('Config', array('session_config' => array()))->setService('SiteInfo', new SiteInfo(array('domain' => 'example.com'))); /* @var $service \Zend\Session\Config\ConfigInterface */ $service = $serviceFactory->createService($serviceManager); $this->assertInstanceOf('Zend\\Session\\Config\\ConfigInterface', $service); $this->assertEquals('.example.com', $service->getCookieDomain()); }
/** * @covers Zend\ServiceManager\ServiceManager::resolveAlias */ public function testSetCircularAliasReferenceThrowsException() { $this->setExpectedException('Zend\\ServiceManager\\Exception\\CircularReferenceException'); // Only affects service managers that allow overwriting definitions $this->serviceManager->setAllowOverride(true); $this->serviceManager->setInvokableClass('foo-service', 'stdClass'); $this->serviceManager->setAlias('foo-alias', 'foo-service'); $this->serviceManager->setAlias('bar-alias', 'foo-alias'); $this->serviceManager->setAlias('baz-alias', 'bar-alias'); // This will now cause a cyclic reference and should throw an exception $this->serviceManager->setAlias('foo-alias', 'bar-alias'); }
/** * @dataProvider duplicateService */ public function testWithAllowOverrideOnRegisteringAServiceDuplicatingAnExistingAliasShouldInvalidateTheAlias($method, $service, $expected, $assertion = 'assertSame') { $this->serviceManager->setAllowOverride(true); $sm = $this->serviceManager; $this->serviceManager->setFactory('http.response', function ($services) use($sm) { return $sm; }); $this->serviceManager->setAlias('response', 'http.response'); $this->assertSame($sm, $this->serviceManager->get('response')); $self = $this; $this->serviceManager->{$method}('response', $service); $this->{$assertion}($expected, $this->serviceManager->get('response')); }
/** * Test create servive */ public function testCreateService() { $ns = __NAMESPACE__ . '\\BuilderTest\\'; $serviceFactory = new BuilderServiceFactory(); $serviceManager = new ServiceManager(); $serviceManager->setAlias('Configuration', 'Config'); $serviceManager->setService('Config', array('factory' => array($ns . 'Factory' => array('dependency' => array($ns . 'Dependecy', 'Countable'), 'adapter' => array('adapter1' => $ns . 'Adapter1', 'adapter2' => $ns . 'Adapter2'))))); $builder = $serviceFactory->createService($serviceManager); $this->assertFalse($builder->isFactoryRegistered('NonExistentFactory')); $this->assertTrue($builder->isFactoryRegistered($ns . 'Factory')); $this->assertTrue($builder->isAdapterRegistered($ns . 'Factory', $ns . 'Adapter1')); $this->assertTrue($builder->isAdapterRegistered($ns . 'Factory', $ns . 'Adapter2')); $this->assertFalse($builder->isAdapterRegistered($ns . 'Factory', $ns . 'Adapter3')); }
public static function init() { $zf2ModulePaths = array(dirname(dirname(__DIR__))); if ($path = static::findParentPath('vendor')) { $zf2ModulePaths[] = $path; } if (($path = static::findParentPath('module')) !== $zf2ModulePaths[0]) { $zf2ModulePaths[] = $path; } static::initAutoloader(); $cfg = (include __DIR__ . '/../config/module.config.php'); $serviceManager = new ServiceManager(new Config($cfg['service_manager'])); $serviceManager->setService('Configuration', $cfg); $serviceManager->setAlias('Config', 'Configuration'); static::$serviceManager = $serviceManager; }
/** * Bootstraps the config for the service locator for a single domain. * @param mixed $domain */ protected function bootstrapServiceLocatorConfig($domain) { if ($domain instanceof ServiceLocatorConfigProviderInterface) { $serviceLocatorConfig = $domain->getServiceLocatorConfig(); $this->config = array_merge_recursive($this->config, ['serviceLocator' => $serviceLocatorConfig]); if (isset($serviceLocatorConfig['aliases'])) { foreach ($serviceLocatorConfig['aliases'] as $alias => $target) { $this->serviceLocator->setAlias($alias, $target); } } if (isset($serviceLocatorConfig['factories'])) { foreach ($serviceLocatorConfig['factories'] as $alias => $factory) { $this->serviceLocator->setFactory($alias, $factory); } } if (isset($serviceLocatorConfig['services'])) { foreach ($serviceLocatorConfig['services'] as $alias => $service) { $this->serviceLocator->setService($alias, $service); } } } }
/** * Templating engines currently supported: * - PHP * - Twig * - Smarty * - Mustache. * * @param ServiceManager $serviceManager * * @throws \RuntimeException */ public function configureServiceManager(ServiceManager $serviceManager) { $config = $serviceManager->get('Config'); $appRootDir = $config['parameters']['app.root_dir']; $appCacheDir = $config['parameters']['app.cache_dir']; $appCharset = $config['parameters']['app.charset']; // The "framework.templating" option is deprecated. Please replace it with "framework.view" $config = $this->processConfiguration($config); // these are the templating engines currently supported // @todo - this needs to come from the app config. $knownEngineIds = array('php', 'smarty', 'twig', 'mustache', 'plates', 'latte'); // these are the engines selected by the user $engineIds = isset($config['engines']) ? $config['engines'] : array('php'); // filter templating engines $engineIds = array_intersect($engineIds, $knownEngineIds); if (empty($engineIds)) { throw new \RuntimeException(sprintf('At least one templating engine should be defined in your app config (in $config[\'view.engines\']). These are the available ones: "%s". Example: "$config[\'templating.engines\'] = array(\'%s\');"', implode('", ', $knownEngineIds), implode("', ", $knownEngineIds))); } /* * Templating Locator. */ $serviceManager->setFactory('templating.locator', function ($serviceManager) use($appCacheDir) { return new TemplateLocator($serviceManager->get('file_locator'), $appCacheDir); }); /* * Templating Name Parser. */ $serviceManager->setFactory('templating.name_parser', function ($serviceManager) { return new TemplateNameParser($serviceManager->get('modulemanager')); }); /* * Filesystem Loader. */ $serviceManager->setFactory('templating.loader.filesystem', function ($serviceManager) { return new FileSystemLoader($serviceManager->get('templating.locator')); }); /* * Templating assets helper. */ $serviceManager->setFactory('templating.helper.assets', function ($serviceManager) { return new AssetsHelper($serviceManager->get('request')->getBasePath()); }); /* * Templating globals. */ $serviceManager->setFactory('templating.globals', function ($serviceManager) { return new GlobalVariables($serviceManager->get('servicemanager')); }); /* * PHP Engine. * * TODO: Migrate to Symfony\Bundle\FrameworkBundle\Templating\PhpEngine */ $serviceManager->setFactory('templating.engine.php', function ($serviceManager) use($appCharset) { $engine = new PhpEngine($serviceManager->get('templating.name_parser'), $serviceManager->get('templating.loader'), array(new SlotsHelper(), $serviceManager->get('templating.helper.assets'), new RouterHelper($serviceManager->get('router')), new SessionHelper($serviceManager->get('session')))); $engine->addGlobal('app', $serviceManager->get('templating.globals')); $engine->setCharset($appCharset); return $engine; }); /* * Twig Engine */ $serviceManager->setFactory('templating.engine.twig', function ($serviceManager) { if (!class_exists('Twig_Environment')) { throw new \Exception('PPI\\Framework\\TwigModule not found. Composer require: ppi/twig-module'); } $twigEnvironment = new \Twig_Environment(new \PPI\Framework\View\Twig\Loader\FileSystemLoader($serviceManager->get('templating.locator'), $serviceManager->get('templating.name_parser'))); // Add some twig extension $twigEnvironment->addExtension(new \PPI\Framework\View\Twig\Extension\AssetsExtension($serviceManager->get('templating.helper.assets'))); $twigEnvironment->addExtension(new \PPI\Framework\View\Twig\Extension\RouterExtension($serviceManager->get('router'))); return new \PPI\Framework\View\Twig\TwigEngine($twigEnvironment, $serviceManager->get('templating.name_parser'), $serviceManager->get('templating.locator'), $serviceManager->get('templating.globals')); }); /* * Smarty Engine. */ $serviceManager->setFactory('templating.engine.smarty', function ($serviceManager) use($appCacheDir) { if (!class_exists('NoiseLabs\\Bundle\\SmartyBundle\\SmartyEngine')) { throw new \Exception('PPI\\Framework\\SmartyModule not found. Composer require: ppi/smarty-module'); } $cacheDir = $appCacheDir . DIRECTORY_SEPARATOR . 'smarty'; $smartyEngine = new \PPI\Framework\View\Smarty\SmartyEngine(new \Smarty(), $serviceManager->get('templating.locator'), $serviceManager->get('templating.name_parser'), $serviceManager->get('templating.loader'), array('cache_dir' => $cacheDir . DIRECTORY_SEPARATOR . 'cache', 'compile_dir' => $cacheDir . DIRECTORY_SEPARATOR . 'templates_c'), $serviceManager->get('templating.globals'), $serviceManager->get('logger')); // Add some SmartyBundle extensions $smartyEngine->addExtension(new SmartyAssetsExtension($serviceManager->get('templating.helper.assets'))); $smartyEngine->addExtension(new SmartyRouterExtension($serviceManager->get('router'))); return $smartyEngine; }); // Mustache Engine $serviceManager->setFactory('templating.engine.mustache', function ($serviceManager, $appCacheDir) { if (!class_exists('Mustache_Engine')) { throw new \Exception('PPI\\Framework\\MustacheModule not found. Composer require: ppi/mustache-module'); } $rawMustacheEngine = new \Mustache_Engine(array('loader' => new MustacheFileSystemLoader($serviceManager->get('templating.locator'), $serviceManager->get('templating.name_parser')), 'cache' => $appCacheDir . DIRECTORY_SEPARATOR . 'mustache')); return new MustacheEngine($rawMustacheEngine, $serviceManager->get('templating.name_parser')); }); /* * Delegating Engine. */ $serviceManager->setFactory('templating.engine.delegating', function ($serviceManager) use($engineIds) { $delegatingEngine = new DelegatingEngine(); // @todo - lazy load this foreach ($engineIds as $id) { $delegatingEngine->addEngine($serviceManager->get('templating.engine.' . $id)); } return $delegatingEngine; }); $serviceManager->setAlias('templating', 'templating.engine.delegating'); }
/** * Configure the provided service manager instance with the configuration * in this class. * * In addition to using each of the internal properties to configure the * service manager, also adds an initializer to inject ServiceManagerAware * and ServiceLocatorAware classes with the service manager. * * @param ServiceManager $serviceManager */ public function configureServiceManager(ServiceManager $serviceManager) { foreach ($this->invokables as $name => $class) { $serviceManager->setInvokableClass($name, $class); } foreach ($this->factories as $name => $factoryClass) { $serviceManager->setFactory($name, $factoryClass); } foreach ($this->abstractFactories as $factoryClass) { $serviceManager->addAbstractFactory($factoryClass); } foreach ($this->aliases as $name => $service) { $serviceManager->setAlias($name, $service); } foreach ($this->shared as $name => $value) { $serviceManager->setShared($name, $value); } $serviceManager->addInitializer(function ($instance) use($serviceManager) { if ($instance instanceof EventManagerAwareInterface) { if ($instance->getEventManager() instanceof EventManagerInterface) { $instance->getEventManager()->setSharedManager($serviceManager->get('SharedEventManager')); } else { $instance->setEventManager($serviceManager->get('EventManager')); } } }); $serviceManager->addInitializer(function ($instance) use($serviceManager) { if ($instance instanceof ServiceManagerAwareInterface) { $instance->setServiceManager($serviceManager); } }); $serviceManager->addInitializer(function ($instance) use($serviceManager) { if ($instance instanceof ServiceLocatorAwareInterface) { $instance->setServiceLocator($serviceManager); } }); $serviceManager->setService('ServiceManager', $serviceManager); }
protected function createServiceManager(array $config) { $serviceManager = new ServiceManager(new Config($config)); $serviceManager->setService('Configuration', $config); $serviceManager->setAlias('Config', 'Configuration'); $serviceManager->setService('AsseticAssetFactory', new AssetFactory(__DIR__)); $serviceManager->setService('AsseticCssRewriteFilter', new CssRewriteFilter()); $serviceManager->setService('AsseticCacheBuster', new CacheBustingWorker()); return $serviceManager; }