diff --git a/src/AbstractFactoryInterface.php b/src/AbstractFactoryInterface.php index d678c2f1..64ab7598 100644 --- a/src/AbstractFactoryInterface.php +++ b/src/AbstractFactoryInterface.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager; -/** - * @category Zend - * @package Zend_ServiceManager - */ interface AbstractFactoryInterface { /** diff --git a/src/AbstractPluginManager.php b/src/AbstractPluginManager.php index 41ba046b..b0b26880 100644 --- a/src/AbstractPluginManager.php +++ b/src/AbstractPluginManager.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager; @@ -19,9 +18,6 @@ * the plugin when retrieved. Finally, enables the allowOverride property by * default to allow registering factories, aliases, and invokables to take * the place of those provided by the implementing class. - * - * @category Zend - * @package Zend_ServiceManager */ abstract class AbstractPluginManager extends ServiceManager implements ServiceLocatorAwareInterface { @@ -30,7 +26,7 @@ abstract class AbstractPluginManager extends ServiceManager implements ServiceLo * * @var bool */ - protected $allowOverride = true; + protected $allowOverride = true; /** * Whether or not to auto-add a class as an invokable class if it exists @@ -69,9 +65,6 @@ public function __construct(ConfigInterface $configuration = null) if ($instance instanceof ServiceLocatorAwareInterface) { $instance->setServiceLocator($self); } - if ($instance instanceof ServiceManagerAwareInterface) { - $instance->setServiceManager($self); - } }); } @@ -218,5 +211,4 @@ protected function createFromFactory($canonicalName, $requestedName) return $instance; } - } diff --git a/src/Config.php b/src/Config.php index 352072e7..94e1bdcc 100644 --- a/src/Config.php +++ b/src/Config.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager; -/** - * @category Zend - * @package Zend_ServiceManager - */ class Config implements ConfigInterface { /** @@ -151,5 +146,4 @@ public function configureServiceManager(ServiceManager $serviceManager) $serviceManager->setShared($name, $isShared); } } - } diff --git a/src/ConfigInterface.php b/src/ConfigInterface.php index 289c868a..68e7d275 100644 --- a/src/ConfigInterface.php +++ b/src/ConfigInterface.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager; -/** - * @category Zend - * @package Zend_ServiceManager - */ interface ConfigInterface { /** diff --git a/src/Di/DiAbstractServiceFactory.php b/src/Di/DiAbstractServiceFactory.php index 9831fe8e..a68a186d 100644 --- a/src/Di/DiAbstractServiceFactory.php +++ b/src/Di/DiAbstractServiceFactory.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager\Di; diff --git a/src/Di/DiInstanceManagerProxy.php b/src/Di/DiInstanceManagerProxy.php index 1f46bd06..4f1cdb1e 100644 --- a/src/Di/DiInstanceManagerProxy.php +++ b/src/Di/DiInstanceManagerProxy.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager\Di; diff --git a/src/Di/DiServiceFactory.php b/src/Di/DiServiceFactory.php index c3871924..550fe1b5 100644 --- a/src/Di/DiServiceFactory.php +++ b/src/Di/DiServiceFactory.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager\Di; @@ -120,5 +119,4 @@ public function get($name, array $params = array()) } } - } diff --git a/src/Di/DiServiceInitializer.php b/src/Di/DiServiceInitializer.php index 2b440c15..f3c0627c 100644 --- a/src/Di/DiServiceInitializer.php +++ b/src/Di/DiServiceInitializer.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager\Di; @@ -65,5 +64,4 @@ public function initialize($instance, ServiceLocatorInterface $serviceLocator) throw $e; } } - } diff --git a/src/Exception/CircularDependencyFoundException.php b/src/Exception/CircularDependencyFoundException.php index ff17b216..fa6d1f10 100644 --- a/src/Exception/CircularDependencyFoundException.php +++ b/src/Exception/CircularDependencyFoundException.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager\Exception; -/** - * @category Zend - * @package Zend_ServiceManager - * @subpackage Exception - */ class CircularDependencyFoundException extends RuntimeException { } diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index 776901d0..8c9869db 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager\Exception; -/** - * @category Zend - * @package Zend_ServiceManager - * @subpackage Exception - */ interface ExceptionInterface { } diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index f81323e4..64f59ed4 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager\Exception; -/** - * @category Zend - * @package Zend_ServiceManager - * @subpackage Exception - */ class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface { } diff --git a/src/Exception/InvalidServiceNameException.php b/src/Exception/InvalidServiceNameException.php index a922f98a..6e44f9c9 100644 --- a/src/Exception/InvalidServiceNameException.php +++ b/src/Exception/InvalidServiceNameException.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager\Exception; -/** - * @category Zend - * @package Zend_ServiceManager - * @subpackage Exception - */ class InvalidServiceNameException extends RuntimeException { } diff --git a/src/Exception/RuntimeException.php b/src/Exception/RuntimeException.php index 8ee6d641..822a895b 100644 --- a/src/Exception/RuntimeException.php +++ b/src/Exception/RuntimeException.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager\Exception; -/** - * @category Zend - * @package Zend_ServiceManager - * @subpackage Exception - */ class RuntimeException extends \RuntimeException implements ExceptionInterface { } diff --git a/src/Exception/ServiceNotCreatedException.php b/src/Exception/ServiceNotCreatedException.php index 45634289..4226e75b 100644 --- a/src/Exception/ServiceNotCreatedException.php +++ b/src/Exception/ServiceNotCreatedException.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager\Exception; -/** - * @category Zend - * @package Zend_ServiceManager - * @subpackage Exception - */ class ServiceNotCreatedException extends RuntimeException { } diff --git a/src/Exception/ServiceNotFoundException.php b/src/Exception/ServiceNotFoundException.php index d0f926f0..877aa629 100644 --- a/src/Exception/ServiceNotFoundException.php +++ b/src/Exception/ServiceNotFoundException.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager\Exception; -/** - * @category Zend - * @package Zend_ServiceManager - * @subpackage Exception - */ class ServiceNotFoundException extends InvalidArgumentException { } diff --git a/src/FactoryInterface.php b/src/FactoryInterface.php index aa1d1039..6b681225 100644 --- a/src/FactoryInterface.php +++ b/src/FactoryInterface.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager; -/** - * @category Zend - * @package Zend_ServiceManager - */ interface FactoryInterface { /** diff --git a/src/InitializerInterface.php b/src/InitializerInterface.php index 687bc8b8..0db23f34 100644 --- a/src/InitializerInterface.php +++ b/src/InitializerInterface.php @@ -5,17 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager; use Zend\ServiceManager\ServiceLocatorInterface; -/** - * @category Zend - * @package Zend_ServiceManager - */ interface InitializerInterface { /** diff --git a/src/ServiceLocatorAwareInterface.php b/src/ServiceLocatorAwareInterface.php index 93c36189..cd666803 100644 --- a/src/ServiceLocatorAwareInterface.php +++ b/src/ServiceLocatorAwareInterface.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager; -/** - * @category Zend - * @package Zend_ServiceManager - */ interface ServiceLocatorAwareInterface { /** diff --git a/src/ServiceLocatorAwareTrait.php b/src/ServiceLocatorAwareTrait.php new file mode 100644 index 00000000..a6c9669f --- /dev/null +++ b/src/ServiceLocatorAwareTrait.php @@ -0,0 +1,43 @@ +serviceLocator = $serviceLocator; + + return $this; + } + + /** + * Get service locator + * + * @return ServiceLocator + */ + public function getServiceLocator() + { + return $this->serviceLocator; + } +} diff --git a/src/ServiceLocatorInterface.php b/src/ServiceLocatorInterface.php index 1cd98e65..46064c84 100644 --- a/src/ServiceLocatorInterface.php +++ b/src/ServiceLocatorInterface.php @@ -5,16 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager; /** * Service locator interface - * - * @category Zend - * @package Zend_ServiceManager */ interface ServiceLocatorInterface { diff --git a/src/ServiceManager.php b/src/ServiceManager.php index c953a4f2..4a86ff5b 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -5,17 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager; use ReflectionClass; -/** - * @category Zend - * @package Zend_ServiceManager - */ class ServiceManager implements ServiceLocatorInterface { @@ -223,7 +218,7 @@ public function retrieveFromPeeringManagerFirst() * @return ServiceManager * @throws Exception\InvalidServiceNameException */ - public function setInvokableClass($name, $invokableClass, $shared = true) + public function setInvokableClass($name, $invokableClass, $shared = null) { $cName = $this->canonicalizeName($name); @@ -237,6 +232,10 @@ public function setInvokableClass($name, $invokableClass, $shared = true) $this->unregisterService($cName); } + if ($shared === null) { + $shared = $this->shareByDefault(); + } + $this->invokableClasses[$cName] = $invokableClass; $this->shared[$cName] = (bool) $shared; @@ -253,7 +252,7 @@ public function setInvokableClass($name, $invokableClass, $shared = true) * @throws Exception\InvalidArgumentException * @throws Exception\InvalidServiceNameException */ - public function setFactory($name, $factory, $shared = true) + public function setFactory($name, $factory, $shared = null) { $cName = $this->canonicalizeName($name); @@ -273,6 +272,10 @@ public function setFactory($name, $factory, $shared = true) $this->unregisterService($cName); } + if ($shared === null) { + $shared = $this->shareByDefault(); + } + $this->factories[$cName] = $factory; $this->shared[$cName] = (bool) $shared; @@ -352,7 +355,7 @@ public function addInitializer($initializer, $topOfStack = true) * @return ServiceManager * @throws Exception\InvalidServiceNameException */ - public function setService($name, $service, $shared = true) + public function setService($name, $service, $shared = null) { $cName = $this->canonicalizeName($name); @@ -367,6 +370,10 @@ public function setService($name, $service, $shared = true) $this->unregisterService($cName); } + if ($shared === null) { + $shared = $this->shareByDefault(); + } + $this->instances[$cName] = $service; $this->shared[$cName] = (bool) $shared; return $this; @@ -419,16 +426,21 @@ public function get($name, $usePeeringServiceManagers = true) } while ($this->hasAlias($cName)); } - if (isset($this->instances[$cName])) { - return $this->instances[$cName]; - } - $instance = null; $retrieveFromPeeringManagerFirst = $this->retrieveFromPeeringManagerFirst(); if ($usePeeringServiceManagers && $retrieveFromPeeringManagerFirst) { $instance = $this->retrieveFromPeeringManager($name); + + if(null !== $instance) { + return $instance; + } + } + + if (isset($this->instances[$cName])) { + return $this->instances[$cName]; } + if (!$instance) { if ($this->canCreate(array($cName, $name))) { $instance = $this->create(array($cName, $name)); @@ -453,7 +465,10 @@ public function get($name, $usePeeringServiceManagers = true) )); } - if ($this->shareByDefault() && (!isset($this->shared[$cName]) || $this->shared[$cName] === true)) { + if ( + ($this->shareByDefault() && !isset($this->shared[$cName])) + || (isset($this->shared[$cName]) && $this->shared[$cName] === true) + ) { $this->instances[$cName] = $instance; } diff --git a/src/ServiceManagerAwareInterface.php b/src/ServiceManagerAwareInterface.php index ac28bb83..4f6a0cc9 100644 --- a/src/ServiceManagerAwareInterface.php +++ b/src/ServiceManagerAwareInterface.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_ServiceManager */ namespace Zend\ServiceManager; -/** - * @category Zend - * @package Zend_ServiceManager - */ interface ServiceManagerAwareInterface { /** diff --git a/test/ServiceLocatorAwareTraitTest.php b/test/ServiceLocatorAwareTraitTest.php new file mode 100644 index 00000000..d33e136a --- /dev/null +++ b/test/ServiceLocatorAwareTraitTest.php @@ -0,0 +1,46 @@ +getObjectForTrait('\Zend\ServiceManager\ServiceLocatorAwareTrait'); + + $this->assertAttributeEquals(null, 'serviceLocator', $object); + + $serviceLocator = new ServiceManager; + + $object->setServiceLocator($serviceLocator); + + $this->assertAttributeEquals($serviceLocator, 'serviceLocator', $object); + } + + public function testGetServiceLocator() + { + $object = $this->getObjectForTrait('\Zend\ServiceManager\ServiceLocatorAwareTrait'); + + $this->assertNull($object->getServiceLocator()); + + $serviceLocator = new ServiceManager; + + $object->setServiceLocator($serviceLocator); + + $this->assertEquals($serviceLocator, $object->getServiceLocator()); + } +} diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index 2a9eb8e3..6fc287d0 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -208,6 +208,23 @@ public function testGetThrowsExceptionOnUnknownService() $this->assertEquals('bar', $this->serviceManager->get('foo')); } + /** + * @covers Zend\ServiceManager\ServiceManager::get + */ + public function testGetUsesIndivualSharedSettingWhenSetAndDeviatesFromShareByDefaultSetting() + { + $this->serviceManager->setAllowOverride(true); + $this->serviceManager->setShareByDefault(false); + $this->serviceManager->setInvokableClass('foo', 'ZendTest\ServiceManager\TestAsset\Foo'); + $this->serviceManager->setShared('foo', true); + $this->assertSame($this->serviceManager->get('foo'), $this->serviceManager->get('foo')); + + $this->serviceManager->setShareByDefault(true); + $this->serviceManager->setInvokableClass('foo', 'ZendTest\ServiceManager\TestAsset\Foo'); + $this->serviceManager->setShared('foo', false); + $this->assertNotSame($this->serviceManager->get('foo'), $this->serviceManager->get('foo')); + } + /** * @covers Zend\ServiceManager\ServiceManager::get */ @@ -667,4 +684,36 @@ public function testDuplicateNewInstanceMultipleAbstractFactories() $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\Bar', $this->serviceManager->get('bar')); $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\Bar', $this->serviceManager->get('bar')); } + + /** + * @covers Zend\ServiceManager\ServiceManager::setService + * @covers Zend\ServiceManager\ServiceManager::get + * @covers Zend\ServiceManager\ServiceManager::retrieveFromPeeringManagerFirst + * @covers Zend\ServiceManager\ServiceManager::setRetrieveFromPeeringManagerFirst + * @covers Zend\ServiceManager\ServiceManager::addPeeringServiceManager + */ + public function testRetrieveServiceFromPeeringServiceManagerIfretrieveFromPeeringManagerFirstSetToTrueAndServiceNamesAreSame() + { + $foo1 = "foo1"; + $boo1 = "boo1"; + $boo2 = "boo2"; + + $this->serviceManager->setService($foo1, $boo1); + $this->assertEquals($this->serviceManager->get($foo1), $boo1); + + $serviceManagerChild = new ServiceManager(); + $serviceManagerChild->setService($foo1, $boo2); + $this->assertEquals($serviceManagerChild->get($foo1), $boo2); + + $this->assertFalse($this->serviceManager->retrieveFromPeeringManagerFirst()); + $this->serviceManager->setRetrieveFromPeeringManagerFirst(true); + $this->assertTrue($this->serviceManager->retrieveFromPeeringManagerFirst()); + + $this->serviceManager->addPeeringServiceManager($serviceManagerChild); + + $this->assertContains($serviceManagerChild, $this->readAttribute($this->serviceManager, 'peeringServiceManagers')); + + $this->assertEquals($serviceManagerChild->get($foo1), $boo2); + $this->assertEquals($this->serviceManager->get($foo1), $boo2); + } }