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

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 111 deletions.
4 changes: 2 additions & 2 deletions src/AbstractFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

interface AbstractFactoryInterface
{
public function canCreateServiceWithName($name /*, $requestedName */);
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name /*, $requestedName */);
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName);
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName);
}
44 changes: 25 additions & 19 deletions src/AbstractPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class AbstractPluginManager extends ServiceManager
{
/**
* Allow overriding by default
*
*
* @var bool
*/
protected $allowOverride = true;
Expand All @@ -63,23 +63,29 @@ abstract class AbstractPluginManager extends ServiceManager
*
* Add a default initializer to ensure the plugin is valid after instance
* creation.
*
* @param null|ConfigurationInterface $configuration
*
* @param null|ConfigurationInterface $configuration
* @return void
*/
public function __construct(ConfigurationInterface $configuration = null)
{
parent::__construct($configuration);
$this->addInitializer(array($this, 'validatePlugin'), true);
$self = $this;
$this->addInitializer(function ($instance) use ($self) {
if ($instance instanceof ServiceManagerAwareInterface) {
$instance->setServiceManager($self);
}
});
}

/**
* Validate the plugin
*
* 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 @@ -91,10 +97,10 @@ abstract public function validatePlugin($plugin);
* Allows passing an array of options to use when creating the instance.
* createFromInvokable() will use these and pass them to the instance
* constructor if not null and a non-empty array.
*
* @param string $name
* @param array $options
* @param bool $usePeeringServiceManagers
*
* @param string $name
* @param array $options
* @param bool $usePeeringServiceManagers
* @return object
*/
public function get($name, $options = array(), $usePeeringServiceManagers = true)
Expand All @@ -115,7 +121,7 @@ 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
Expand All @@ -134,11 +140,11 @@ public function setService($name, $service, $shared = true)
/**
* Attempt to create an instance via an invokable class
*
* Overrides parent implementation by passing $creationOptions to the
* 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 All @@ -155,7 +161,7 @@ protected function createFromInvokable($canonicalName, $requestedName)
));
}

if (null === $this->creationOptions
if (null === $this->creationOptions
|| (is_array($this->creationOptions) && empty($this->creationOptions))
) {
$instance = new $invokable();
Expand All @@ -169,11 +175,11 @@ protected function createFromInvokable($canonicalName, $requestedName)
/**
* Determine if a class implements a given interface
*
* For PHP versions >= 5.3.7, uses is_subclass_of; otherwise, uses
* For PHP versions >= 5.3.7, uses is_subclass_of; otherwise, uses
* reflection to determine the interfaces implemented.
*
* @param string $class
* @param string $type
*
* @param string $class
* @param string $type
* @return bool
*/
protected function isSubclassOf($class, $type)
Expand Down
13 changes: 11 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function getAllowOverride()
{
return (isset($this->configuration['allow_override'])) ? $this->configuration['allow_override'] : null;
}

public function getFactories()
{
return (isset($this->configuration['factories'])) ? $this->configuration['factories'] : array();
Expand All @@ -41,6 +41,11 @@ public function getAliases()
return (isset($this->configuration['aliases'])) ? $this->configuration['aliases'] : array();
}

public function getInitializers()
{
return (isset($this->configuration['initializers'])) ? $this->configuration['initializers'] : array();
}

public function getShared()
{
return (isset($this->configuration['shared'])) ? $this->configuration['shared'] : array();
Expand All @@ -50,7 +55,7 @@ public function configureServiceManager(ServiceManager $serviceManager)
{
$allowOverride = $this->getAllowOverride();
isset($allowOverride) ? $serviceManager->setAllowOverride($allowOverride) : null;

foreach ($this->getFactories() as $name => $factory) {
$serviceManager->setFactory($name, $factory);
}
Expand All @@ -71,6 +76,10 @@ public function configureServiceManager(ServiceManager $serviceManager)
$serviceManager->setAlias($alias, $nameOrAlias);
}

foreach ($this->getInitializers() as $initializer) {
$serviceManager->addInitializer($initializer);
}

foreach ($this->getShared() as $name => $isShared) {
$serviceManager->setShared($name, $isShared);
}
Expand Down
19 changes: 9 additions & 10 deletions src/Di/DiAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class DiAbstractServiceFactory extends DiServiceFactory implements AbstractFactoryInterface
{

/**
* @param \Zend\Di\Di $di
* @param null|string|\Zend\Di\InstanceManager $useServiceLocator
Expand All @@ -26,12 +25,9 @@ public function __construct(Di $di, $useServiceLocator = self::USE_SL_NONE)
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param $serviceName
* @param null $requestedName
* @return object
* {@inheritDoc}
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $serviceName, $requestedName = null)
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $serviceName, $requestedName)
{
$this->serviceLocator = $serviceLocator;
if ($requestedName) {
Expand All @@ -43,11 +39,14 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $
}

/**
* @param $name
* @return null
* {@inheritDoc}
*/
public function canCreateServiceWithName($name)
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return null; // not sure
return $this->instanceManager->hasSharedInstance($requestedName)
|| $this->instanceManager->hasAlias($requestedName)
|| $this->instanceManager->hasConfiguration($requestedName)
|| $this->instanceManager->hasTypePreferences($requestedName)
|| $this->definitions->hasClass($requestedName);
}
}
Loading

0 comments on commit 327d366

Please sign in to comment.