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' of git://github.com/zendframework/zf2 into string
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 40 changed files with 1,250 additions and 423 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "zendframework/zend-servicemanager",
"description": " ",
"license": "BSD-3-Clause",
"keywords": [
"zf2",
"servicemanager"
],
"autoload": {
"psr-4": {
"Zend\\ServiceManager": "src/"
"Zend\\ServiceManager\\": "src/"
}
},
"require": {
Expand Down
29 changes: 29 additions & 0 deletions src/AbstractFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 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
{
/**
* Determine if we can create a service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName);

/**
* Create service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return mixed
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName);
}
98 changes: 50 additions & 48 deletions src/AbstractPluginManager.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
<?php
/**
* Zend Framework
* Zend Framework (http://framework.zend.com/)
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_ServiceManager
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 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\Code\Reflection\ClassReflection;

/**
* ServiceManager implementation for managing plugins
*
Expand All @@ -34,8 +22,6 @@
*
* @category Zend
* @package Zend_ServiceManager
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class AbstractPluginManager extends ServiceManager implements ServiceLocatorAwareInterface
{
Expand All @@ -44,10 +30,19 @@ abstract class AbstractPluginManager extends ServiceManager implements ServiceLo
*
* @var bool
*/
protected $allowOverride = true;
protected $allowOverride = true;

/**
* @var mixed Options to use when creating an instance
* Whether or not to auto-add a class as an invokable class if it exists
*
* @var bool
*/
protected $autoAddInvokableClass = true;

/**
* Options to use when creating an instance
*
* @var mixed
*/
protected $creationOptions = null;

Expand All @@ -64,16 +59,15 @@ abstract class AbstractPluginManager extends ServiceManager implements ServiceLo
* Add a default initializer to ensure the plugin is valid after instance
* creation.
*
* @param null|ConfigurationInterface $configuration
* @return void
* @param null|ConfigInterface $configuration
*/
public function __construct(ConfigurationInterface $configuration = null)
public function __construct(ConfigInterface $configuration = null)
{
parent::__construct($configuration);
$self = $this;
$this->addInitializer(function ($instance) use ($self) {
if ($instance instanceof ServiceManagerAwareInterface) {
$instance->setServiceManager($self);
if ($instance instanceof ServiceLocatorAwareInterface) {
$instance->setServiceLocator($self);
}
});
}
Expand Down Expand Up @@ -105,7 +99,7 @@ abstract public function validatePlugin($plugin);
public function get($name, $options = array(), $usePeeringServiceManagers = true)
{
// Allow specifying a class name directly; registers as an invokable class
if (!$this->has($name) && class_exists($name)) {
if (!$this->has($name) && $this->autoAddInvokableClass && class_exists($name)) {
$this->setInvokableClass($name, $name);
}

Expand Down Expand Up @@ -173,15 +167,6 @@ public function getServiceLocator()
protected function createFromInvokable($canonicalName, $requestedName)
{
$invokable = $this->invokableClasses[$canonicalName];
if (!class_exists($invokable)) {
throw new Exception\ServiceNotCreatedException(sprintf(
'%s: failed retrieving "%s%s" via invokable class "%s"; class does not exist',
__METHOD__,
$canonicalName,
($requestedName ? '(alias: ' . $requestedName . ')' : ''),
$canonicalName
));
}

if (null === $this->creationOptions
|| (is_array($this->creationOptions) && empty($this->creationOptions))
Expand All @@ -195,23 +180,40 @@ protected function createFromInvokable($canonicalName, $requestedName)
}

/**
* Determine if a class implements a given interface
* Attempt to create an instance via a factory class
*
* For PHP versions >= 5.3.7, uses is_subclass_of; otherwise, uses
* reflection to determine the interfaces implemented.
* Overrides parent implementation by passing $creationOptions to the
* constructor, if non-null.
*
* @param string $class
* @param string $type
* @return bool
* @param string $canonicalName
* @param string $requestedName
* @return mixed
* @throws Exception\ServiceNotCreatedException If factory is not callable
*/
protected function isSubclassOf($class, $type)
protected function createFromFactory($canonicalName, $requestedName)
{
if (version_compare(PHP_VERSION, '5.3.7', 'gte')) {
return is_subclass_of($class, $type);
$factory = $this->factories[$canonicalName];
if (is_string($factory) && class_exists($factory, true)) {
if (null === $this->creationOptions || (is_array($this->creationOptions) && empty($this->creationOptions))) {
$factory = new $factory();
} else {
$factory = new $factory($this->creationOptions);
}

$this->factories[$canonicalName] = $factory;
}

$r = new ClassReflection($class);
$interfaces = $r->getInterfaceNames();
return (in_array($type, $interfaces));
if ($factory instanceof FactoryInterface) {
$instance = $this->createServiceViaCallback(array($factory, 'createService'), $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 . ')' : '')
));
}

return $instance;
}

}
155 changes: 155 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 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
{
/**
* @var array
*/
protected $config = array();

/**
* Constructor
*
* @param array $config
*/
public function __construct($config = array())
{
$this->config = $config;
}

/**
* Get allow override
*
* @return null|bool
*/
public function getAllowOverride()
{
return (isset($this->config['allow_override'])) ? $this->config['allow_override'] : null;
}

/**
* Get factories
*
* @return array
*/
public function getFactories()
{
return (isset($this->config['factories'])) ? $this->config['factories'] : array();
}

/**
* Get abstract factories
*
* @return array
*/
public function getAbstractFactories()
{
return (isset($this->config['abstract_factories'])) ? $this->config['abstract_factories'] : array();
}

/**
* Get invokables
*
* @return array
*/
public function getInvokables()
{
return (isset($this->config['invokables'])) ? $this->config['invokables'] : array();
}

/**
* Get services
*
* @return array
*/
public function getServices()
{
return (isset($this->config['services'])) ? $this->config['services'] : array();
}

/**
* Get aliases
*
* @return array
*/
public function getAliases()
{
return (isset($this->config['aliases'])) ? $this->config['aliases'] : array();
}

/**
* Get initializers
*
* @return array
*/
public function getInitializers()
{
return (isset($this->config['initializers'])) ? $this->config['initializers'] : array();
}

/**
* Get shared
*
* @return array
*/
public function getShared()
{
return (isset($this->config['shared'])) ? $this->config['shared'] : array();
}

/**
* Configure service manager
*
* @param ServiceManager $serviceManager
* @return void
*/
public function configureServiceManager(ServiceManager $serviceManager)
{
if (($allowOverride = $this->getAllowOverride()) !== null) {
$serviceManager->setAllowOverride($allowOverride);
}

foreach ($this->getFactories() as $name => $factory) {
$serviceManager->setFactory($name, $factory);
}

foreach ($this->getAbstractFactories() as $factory) {
$serviceManager->addAbstractFactory($factory);
}

foreach ($this->getInvokables() as $name => $invokable) {
$serviceManager->setInvokableClass($name, $invokable);
}

foreach ($this->getServices() as $name => $service) {
$serviceManager->setService($name, $service);
}

foreach ($this->getAliases() as $alias => $nameOrAlias) {
$serviceManager->setAlias($alias, $nameOrAlias);
}

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

foreach ($this->getShared() as $name => $isShared) {
$serviceManager->setShared($name, $isShared);
}
}

}
Loading

0 comments on commit 7981849

Please sign in to comment.