From 30922f4063edc697ad772e8e5f8627ae883c3f1e Mon Sep 17 00:00:00 2001 From: Tomoaki Kosugi Date: Thu, 13 Mar 2014 15:09:04 +0900 Subject: [PATCH 1/7] Can get Translator with minimum bootstrap and di configuration --- .../Service/TranslatorServiceFactoryTest.php | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/ZendTest/Mvc/Service/TranslatorServiceFactoryTest.php b/tests/ZendTest/Mvc/Service/TranslatorServiceFactoryTest.php index 33cfda945a5..1b729fec3d1 100644 --- a/tests/ZendTest/Mvc/Service/TranslatorServiceFactoryTest.php +++ b/tests/ZendTest/Mvc/Service/TranslatorServiceFactoryTest.php @@ -10,6 +10,8 @@ namespace ZendTest\Mvc\Service; use PHPUnit_Framework_TestCase as TestCase; +use Zend\Mvc\Service\RoutePluginManagerFactory; +use Zend\Mvc\Service\ServiceManagerConfig; use Zend\Mvc\Service\TranslatorServiceFactory; use Zend\ServiceManager\ServiceManager; @@ -18,7 +20,7 @@ class TranslatorServiceFactoryTest extends TestCase public function setUp() { $this->factory = new TranslatorServiceFactory(); - $this->services = new ServiceManager(); + $this->services = new ServiceManager(new ServiceManagerConfig()); } public function testReturnsMvcTranslatorWithTranslatorInterfaceServiceComposedWhenPresent() @@ -74,6 +76,46 @@ public function testReturnsTranslatorBasedOnConfigurationWhenNoTranslatorInterfa ); } + public function testReturnsTranslatorBasedOnConfigurationWhenNoTranslatorInterfaceServicePresentWithMinimumBootstrap() + { + if (!extension_loaded('intl')) { + $this->markTestSkipped('This test will only run if ext/intl is present'); + } + + //minimum bootstrap + $applicationConfig = array( + 'module_listener_options' => array(), + 'modules' => array(), + ); + $this->services->setService('ApplicationConfig', $applicationConfig); + $this->services->get('ModuleManager')->loadModules(); + $this->services->get('Application')->bootstrap(); + + //enable to re-write Config + $ref = new \ReflectionObject($this->services); + $prop = $ref->getProperty('allowOverride'); + $prop->setAccessible(true); + $prop->setValue($this->services, true); + + $config = array( + 'di' => array(), + 'translator' => array( + 'locale' => 'en_US', + ), + ); + + $this->services->setService('Config', $config); + + //get any plugins with AbstractPluginManagerFactory + $routePluginManagerFactory = new RoutePluginManagerFactory; + $routePluginManager = $routePluginManagerFactory->createService($this->services); + + $translator = $this->factory->createService($this->services); + $this->assertInstanceOf('Zend\Mvc\I18n\Translator', $translator); + $this->assertInstanceOf('Zend\I18n\Translator\Translator', $translator->getTranslator()); + + } + /** * @depends testReturnsTranslatorBasedOnConfigurationWhenNoTranslatorInterfaceServicePresent */ From 3fe0c9f7f7f3206a7386b8acbfb2475ebab6e7bb Mon Sep 17 00:00:00 2001 From: Tomoaki Kosugi Date: Thu, 13 Mar 2014 16:35:36 +0900 Subject: [PATCH 2/7] Fix #5959 MvcTranslator Problem same as: https://github.com/zendframework/zf2/blob/5d1189c6bbfa68f1fb041959e9241bd80fefdbfb/library/Zend/Mvc/Service/ServiceListenerFactory.php#L77 --- library/Zend/Mvc/Service/ServiceListenerFactory.php | 1 + 1 file changed, 1 insertion(+) diff --git a/library/Zend/Mvc/Service/ServiceListenerFactory.php b/library/Zend/Mvc/Service/ServiceListenerFactory.php index 6686a4dcaad..c1110d6fb45 100644 --- a/library/Zend/Mvc/Service/ServiceListenerFactory.php +++ b/library/Zend/Mvc/Service/ServiceListenerFactory.php @@ -76,6 +76,7 @@ class ServiceListenerFactory implements FactoryInterface 'ViewResolver' => 'Zend\Mvc\Service\ViewResolverFactory', 'ViewTemplateMapResolver' => 'Zend\Mvc\Service\ViewTemplateMapResolverFactory', 'ViewTemplatePathStack' => 'Zend\Mvc\Service\ViewTemplatePathStackFactory', + 'Zend\I18n\Translator\TranslatorInterface' => 'Zend\I18n\Translator\TranslatorServiceFactory', ), 'aliases' => array( 'Configuration' => 'Config', From d93fbe14925571c43e6f386aa7f44e5ae98a5e83 Mon Sep 17 00:00:00 2001 From: Tomoaki Kosugi Date: Fri, 14 Mar 2014 08:01:35 +0900 Subject: [PATCH 3/7] Revert "Fix #5959 MvcTranslator Problem" #issuecomment-37542937 This reverts commit 3fe0c9f7f7f3206a7386b8acbfb2475ebab6e7bb. --- library/Zend/Mvc/Service/ServiceListenerFactory.php | 1 - 1 file changed, 1 deletion(-) diff --git a/library/Zend/Mvc/Service/ServiceListenerFactory.php b/library/Zend/Mvc/Service/ServiceListenerFactory.php index c1110d6fb45..6686a4dcaad 100644 --- a/library/Zend/Mvc/Service/ServiceListenerFactory.php +++ b/library/Zend/Mvc/Service/ServiceListenerFactory.php @@ -76,7 +76,6 @@ class ServiceListenerFactory implements FactoryInterface 'ViewResolver' => 'Zend\Mvc\Service\ViewResolverFactory', 'ViewTemplateMapResolver' => 'Zend\Mvc\Service\ViewTemplateMapResolverFactory', 'ViewTemplatePathStack' => 'Zend\Mvc\Service\ViewTemplatePathStackFactory', - 'Zend\I18n\Translator\TranslatorInterface' => 'Zend\I18n\Translator\TranslatorServiceFactory', ), 'aliases' => array( 'Configuration' => 'Config', From 708261bfb388a2cd0cedf606000df4f72466b02a Mon Sep 17 00:00:00 2001 From: Tomoaki Kosugi Date: Fri, 14 Mar 2014 08:13:48 +0900 Subject: [PATCH 4/7] Update test #5959 https://github.com/zendframework/zf2/pull/5959#discussion-diff-10564458 --- .../Service/TranslatorServiceFactoryTest.php | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/ZendTest/Mvc/Service/TranslatorServiceFactoryTest.php b/tests/ZendTest/Mvc/Service/TranslatorServiceFactoryTest.php index 1b729fec3d1..329862d3108 100644 --- a/tests/ZendTest/Mvc/Service/TranslatorServiceFactoryTest.php +++ b/tests/ZendTest/Mvc/Service/TranslatorServiceFactoryTest.php @@ -20,7 +20,7 @@ class TranslatorServiceFactoryTest extends TestCase public function setUp() { $this->factory = new TranslatorServiceFactory(); - $this->services = new ServiceManager(new ServiceManagerConfig()); + $this->services = new ServiceManager(); } public function testReturnsMvcTranslatorWithTranslatorInterfaceServiceComposedWhenPresent() @@ -87,15 +87,16 @@ public function testReturnsTranslatorBasedOnConfigurationWhenNoTranslatorInterfa 'module_listener_options' => array(), 'modules' => array(), ); - $this->services->setService('ApplicationConfig', $applicationConfig); - $this->services->get('ModuleManager')->loadModules(); - $this->services->get('Application')->bootstrap(); + $serviceLocator = new ServiceManager(new ServiceManagerConfig()); + $serviceLocator->setService('ApplicationConfig', $applicationConfig); + $serviceLocator->get('ModuleManager')->loadModules(); + $serviceLocator->get('Application')->bootstrap(); //enable to re-write Config - $ref = new \ReflectionObject($this->services); + $ref = new \ReflectionObject($serviceLocator); $prop = $ref->getProperty('allowOverride'); $prop->setAccessible(true); - $prop->setValue($this->services, true); + $prop->setValue($serviceLocator, true); $config = array( 'di' => array(), @@ -104,13 +105,14 @@ public function testReturnsTranslatorBasedOnConfigurationWhenNoTranslatorInterfa ), ); - $this->services->setService('Config', $config); + $serviceLocator->setService('Config', $config); + //#5959 //get any plugins with AbstractPluginManagerFactory $routePluginManagerFactory = new RoutePluginManagerFactory; - $routePluginManager = $routePluginManagerFactory->createService($this->services); + $routePluginManager = $routePluginManagerFactory->createService($serviceLocator); - $translator = $this->factory->createService($this->services); + $translator = $this->factory->createService($serviceLocator); $this->assertInstanceOf('Zend\Mvc\I18n\Translator', $translator); $this->assertInstanceOf('Zend\I18n\Translator\Translator', $translator->getTranslator()); From 7c7d684003e1f63b453103f7947ef12492750d52 Mon Sep 17 00:00:00 2001 From: Tomoaki Kosugi Date: Fri, 14 Mar 2014 08:16:50 +0900 Subject: [PATCH 5/7] Fix #5959 update DiAbstractFactory canCreateServiceWithName check instantiable strictly --- .../Di/DiAbstractServiceFactory.php | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php b/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php index 616ed25670c..cbc50a0082b 100644 --- a/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php +++ b/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php @@ -10,6 +10,7 @@ namespace Zend\ServiceManager\Di; use Zend\Di\Di; +use Zend\Di\Exception\RuntimeException as DiException; use Zend\ServiceManager\AbstractFactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -51,10 +52,25 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $ */ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { - return $this->instanceManager->hasSharedInstance($requestedName) + if ($this->instanceManager->hasSharedInstance($requestedName) || $this->instanceManager->hasAlias($requestedName) || $this->instanceManager->hasConfig($requestedName) || $this->instanceManager->hasTypePreferences($requestedName) - || $this->definitions->hasClass($requestedName); + ) { + return true; + } + + if (! $this->definitions->hasClass($requestedName)) { + return false; + } + + try { + $this->serviceLocator = $serviceLocator; + $this->get($requestedName); + } catch (DiException $e) { + return false; + } + + return true; } } From 4c93ee93f4858efaddaa3df9c5d1fee5a3b56630 Mon Sep 17 00:00:00 2001 From: Tomoaki Kosugi Date: Wed, 26 Mar 2014 10:13:07 +0900 Subject: [PATCH 6/7] tmp: Revert "Fix #5959 update DiAbstractFactory canCreateServiceWithName" This reverts commit 7c7d684003e1f63b453103f7947ef12492750d52. --- .../Di/DiAbstractServiceFactory.php | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php b/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php index cbc50a0082b..616ed25670c 100644 --- a/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php +++ b/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php @@ -10,7 +10,6 @@ namespace Zend\ServiceManager\Di; use Zend\Di\Di; -use Zend\Di\Exception\RuntimeException as DiException; use Zend\ServiceManager\AbstractFactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -52,25 +51,10 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $ */ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { - if ($this->instanceManager->hasSharedInstance($requestedName) + return $this->instanceManager->hasSharedInstance($requestedName) || $this->instanceManager->hasAlias($requestedName) || $this->instanceManager->hasConfig($requestedName) || $this->instanceManager->hasTypePreferences($requestedName) - ) { - return true; - } - - if (! $this->definitions->hasClass($requestedName)) { - return false; - } - - try { - $this->serviceLocator = $serviceLocator; - $this->get($requestedName); - } catch (DiException $e) { - return false; - } - - return true; + || $this->definitions->hasClass($requestedName); } } From a522d51dc7d52013914b9a32de95a0016fc46794 Mon Sep 17 00:00:00 2001 From: Tomoaki Kosugi Date: Wed, 26 Mar 2014 10:18:57 +0900 Subject: [PATCH 7/7] Fix #5959 update DiAbstractFactory canCreateServiceWithName --- .../ServiceManager/Di/DiAbstractServiceFactory.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php b/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php index 616ed25670c..50b57077b6d 100644 --- a/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php +++ b/library/Zend/ServiceManager/Di/DiAbstractServiceFactory.php @@ -51,10 +51,18 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $ */ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { - return $this->instanceManager->hasSharedInstance($requestedName) + if ($this->instanceManager->hasSharedInstance($requestedName) || $this->instanceManager->hasAlias($requestedName) || $this->instanceManager->hasConfig($requestedName) || $this->instanceManager->hasTypePreferences($requestedName) - || $this->definitions->hasClass($requestedName); + ) { + return true; + } + + if (! $this->definitions->hasClass($requestedName) || interface_exists($requestedName)) { + return false; + } + + return true; } }