Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
Merging develop to master in preparation for 2.4.0rc1.
  • Loading branch information
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 40 deletions.
104 changes: 80 additions & 24 deletions src/AbstractPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Zend\ServiceManager;

use Exception as BaseException;

/**
* ServiceManager implementation for managing plugins
*
Expand Down Expand Up @@ -55,7 +57,7 @@ abstract class AbstractPluginManager extends ServiceManager implements ServiceLo
* Add a default initializer to ensure the plugin is valid after instance
* creation.
*
* @param null|ConfigInterface $configuration
* @param null|ConfigInterface $configuration
*/
public function __construct(ConfigInterface $configuration = null)
{
Expand All @@ -74,7 +76,7 @@ public function __construct(ConfigInterface $configuration = null)
* Checks that the filter loaded is either a valid callback or an instance
* of FilterInterface.
*
* @param mixed $plugin
* @param mixed $plugin
* @return void
* @throws Exception\RuntimeException if invalid
*/
Expand All @@ -88,21 +90,44 @@ abstract public function validatePlugin($plugin);
* constructor if not null and a non-empty array.
*
* @param string $name
* @param array $options
* @param bool $usePeeringServiceManagers
* @param array $options
* @param bool $usePeeringServiceManagers
*
* @return object
*
* @throws Exception\ServiceNotFoundException
* @throws Exception\ServiceNotCreatedException
* @throws Exception\RuntimeException
*/
public function get($name, $options = array(), $usePeeringServiceManagers = true)
{
$isAutoInvokable = false;

// Allow specifying a class name directly; registers as an invokable class
if (!$this->has($name) && $this->autoAddInvokableClass && class_exists($name)) {
$isAutoInvokable = true;

$this->setInvokableClass($name, $name);
}

$this->creationOptions = $options;
$instance = parent::get($name, $usePeeringServiceManagers);

try {
$instance = parent::get($name, $usePeeringServiceManagers);
} catch (Exception\ServiceNotFoundException $exception) {
$this->tryThrowingServiceLocatorUsageException($name, $isAutoInvokable, $exception);
} catch (Exception\ServiceNotCreatedException $exception) {
$this->tryThrowingServiceLocatorUsageException($name, $isAutoInvokable, $exception);
}

$this->creationOptions = null;
$this->validatePlugin($instance);

try {
$this->validatePlugin($instance);
} catch (Exception\RuntimeException $exception) {
$this->tryThrowingServiceLocatorUsageException($name, $isAutoInvokable, $exception);
}

return $instance;
}

Expand All @@ -112,9 +137,9 @@ public function get($name, $options = array(), $usePeeringServiceManagers = true
* Validates that the service object via validatePlugin() prior to
* attempting to register it.
*
* @param string $name
* @param mixed $service
* @param bool $shared
* @param string $name
* @param mixed $service
* @param bool $shared
* @return AbstractPluginManager
* @throws Exception\InvalidServiceNameException
*/
Expand All @@ -124,18 +149,20 @@ public function setService($name, $service, $shared = true)
$this->validatePlugin($service);
}
parent::setService($name, $service, $shared);

return $this;
}

/**
* Set the main service locator so factories can have access to it to pull deps
*
* @param ServiceLocatorInterface $serviceLocator
* @param ServiceLocatorInterface $serviceLocator
* @return AbstractPluginManager
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;

return $this;
}

Expand All @@ -155,8 +182,8 @@ public function getServiceLocator()
* Overrides parent implementation by passing $creationOptions to the
* constructor, if non-null.
*
* @param string $canonicalName
* @param string $requestedName
* @param string $canonicalName
* @param string $requestedName
* @return null|\stdClass
* @throws Exception\ServiceNotCreatedException If resolved class does not exist
*/
Expand Down Expand Up @@ -191,8 +218,8 @@ protected function createFromInvokable($canonicalName, $requestedName)
* Overrides parent implementation by passing $creationOptions to the
* constructor, if non-null.
*
* @param string $canonicalName
* @param string $requestedName
* @param string $canonicalName
* @param string $requestedName
* @return mixed
* @throws Exception\ServiceNotCreatedException If factory is not callable
*/
Expand All @@ -216,13 +243,11 @@ protected function createFromFactory($canonicalName, $requestedName)
} elseif (is_callable($factory)) {
$instance = $this->createServiceViaCallback($factory, $canonicalName, $requestedName);
} else {
throw new Exception\ServiceNotCreatedException(
sprintf(
'While attempting to create %s%s an invalid factory was registered for this instance type.',
$canonicalName,
($requestedName ? '(alias: ' . $requestedName . ')' : '')
)
);
throw new Exception\ServiceNotCreatedException(sprintf(
'While attempting to create %s%s an invalid factory was registered for this instance type.',
$canonicalName,
($requestedName ? '(alias: ' . $requestedName . ')' : '')
));
}

return $instance;
Expand All @@ -231,9 +256,9 @@ protected function createFromFactory($canonicalName, $requestedName)
/**
* Create service via callback
*
* @param callable $callable
* @param string $cName
* @param string $rName
* @param callable $callable
* @param string $cName
* @param string $rName
* @throws Exception\ServiceNotCreatedException
* @throws Exception\ServiceNotFoundException
* @throws Exception\CircularDependencyFoundException
Expand All @@ -258,4 +283,35 @@ protected function createServiceViaCallback($callable, $cName, $rName)

return parent::createServiceViaCallback($callable, $cName, $rName);
}

/**
* @param string $serviceName
* @param bool $isAutoInvokable
* @param BaseException $exception
*
* @throws BaseException
* @throws Exception\ServiceLocatorUsageException
*/
private function tryThrowingServiceLocatorUsageException(
$serviceName,
$isAutoInvokable,
BaseException $exception
) {
if ($isAutoInvokable) {
$this->unregisterService($this->canonicalizeName($serviceName));
}

$serviceLocator = $this->getServiceLocator();

if ($serviceLocator && $serviceLocator->has($serviceName)) {
throw Exception\ServiceLocatorUsageException::fromInvalidPluginManagerRequestedServiceName(
$this,
$serviceLocator,
$serviceName,
$exception
);
}

throw $exception;
}
}
50 changes: 50 additions & 0 deletions src/Exception/ServiceLocatorUsageException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\ServiceManager\Exception;

use Exception as BaseException;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ServiceLocatorInterface;

class ServiceLocatorUsageException extends ServiceNotFoundException
{
/**
* Static constructor
*
* @param AbstractPluginManager $pluginManager
* @param ServiceLocatorInterface $parentLocator
* @param string $serviceName
* @param BaseException $previousException
*
* @return self
*/
public static function fromInvalidPluginManagerRequestedServiceName(
AbstractPluginManager $pluginManager,
ServiceLocatorInterface $parentLocator,
$serviceName,
BaseException $previousException
) {
return new self(
sprintf(
"Service \"%s\" has been requested to plugin manager of type \"%s\", but couldn't be retrieved.\n"
. "A previous exception of type \"%s\" has been raised in the process.\n"
. "By the way, a service with the name \"%s\" has been found in the parent service locator \"%s\": "
. 'did you forget to use $parentLocator = $serviceLocator->getServiceLocator() in your factory code?',
$serviceName,
get_class($pluginManager),
get_class($previousException),
$serviceName,
get_class($parentLocator)
),
0,
$previousException
);
}
}
42 changes: 42 additions & 0 deletions src/MutableCreationOptionsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\ServiceManager;

/**
* Trait for MutableCreationOptions Factories
*/
trait MutableCreationOptionsTrait
{
/**
* @var array
*/
protected $creationOptions = array();

/**
* Set creation options
*
* @param array $creationOptions
* @return void
*/
public function setCreationOptions(array $creationOptions)
{
$this->creationOptions = $creationOptions;
}

/**
* Get creation options
*
* @return array
*/
public function getCreationOptions()
{
return $this->creationOptions;
}
}
1 change: 0 additions & 1 deletion src/Proxy/LazyServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Zend\ServiceManager\Proxy;

use ProxyManager\Factory\LazyLoadingValueHolderFactory;

use ProxyManager\Proxy\LazyLoadingInterface;
use Zend\ServiceManager\DelegatorFactoryInterface;
use Zend\ServiceManager\Exception;
Expand Down
Loading

0 comments on commit 7a49640

Please sign in to comment.