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

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 36 changed files with 852 additions and 380 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

21 changes: 12 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
{
"name": "zendframework/zend-service-manager",
"description": "Zend\\ServiceManager component",
"name": "zendframework/zend-servicemanager",
"description": " ",
"license": "BSD-3-Clause",
"keywords": [
"zf2",
"service-manager"
"servicemanager"
],
"autoload": {
"psr-4": {
"Zend\\ServiceManager\\": "src/"
"Zend\\ServiceManager": "src/"
}
},
"require": {
"php": ">=5.3.23"
"php": ">=5.3.3"
},
"require-dev": {
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master",
"phpunit/PHPUnit": "~4.0"
"suggest": {
"zendframework/zend-di": "Zend\\Di component"
},
"homepage": "https://github.com/zendframework/zend-service-manager",
"autoload-dev": {
"psr-4": {
"ZendTest\\ServiceManager\\": "test/"
}
},
"require-dev": {
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master",
"phpunit/PHPUnit": "~4.0"
}
}
12 changes: 10 additions & 2 deletions src/AbstractFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<?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;

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);
}
124 changes: 62 additions & 62 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 license@zend.com 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,52 +22,62 @@
*
* @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
abstract class AbstractPluginManager extends ServiceManager implements ServiceLocatorAwareInterface
{
/**
* Allow overriding by default
*
*
* @var bool
*/
protected $allowOverride = true;

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

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

/**
* Enable this by default to allow overriding the default plugins
* The main service locator
*
* @var bool
* @var ServiceLocatorInterface
*/
protected $retrieveFromPeeringManagerFirst = true;
protected $serviceLocator;

/**
* Constructor
*
* Add a default initializer to ensure the plugin is valid after instance
* creation.
*
* @param null|ConfigurationInterface $configuration
*
* @param null|ConfigInterface $configuration
* @return void
*/
public function __construct(ConfigurationInterface $configuration = null)
public function __construct(ConfigInterface $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,22 +89,23 @@ 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)
{
// 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);
}

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

Expand All @@ -115,7 +114,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 @@ -131,14 +130,36 @@ public function setService($name, $service, $shared = true)
return $this;
}

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

/**
* Get the main plugin manager. Useful for fetching dependencies from within factories.
*
* @return mixed
*/
public function getServiceLocator()
{
return $this->serviceLocator;
}

/**
* 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 +176,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 @@ -165,25 +186,4 @@ protected function createFromInvokable($canonicalName, $requestedName)

return $instance;
}

/**
* Determine if a class implements a given interface
*
* For PHP versions >= 5.3.7, uses is_subclass_of; otherwise, uses
* reflection to determine the interfaces implemented.
*
* @param string $class
* @param string $type
* @return bool
*/
protected function isSubclassOf($class, $type)
{
if (version_compare(PHP_VERSION, '5.3.7', 'gte')) {
return is_subclass_of($class, $type);
}

$r = new ClassReflection($class);
$interfaces = $r->getInterfaceNames();
return (in_array($type, $interfaces));
}
}
43 changes: 30 additions & 13 deletions src/Configuration.php → src/Config.php
Original file line number Diff line number Diff line change
@@ -1,56 +1,69 @@
<?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;

class Configuration implements ConfigurationInterface
class Config implements ConfigInterface
{
protected $configuration = array();
protected $config = array();

public function __construct($configuration = array())
public function __construct($config = array())
{
$this->configuration = $configuration;
$this->config = $config;
}

public function getAllowOverride()
{
return (isset($this->configuration['allow_override'])) ? $this->configuration['allow_override'] : null;
return (isset($this->config['allow_override'])) ? $this->config['allow_override'] : null;
}

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

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

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

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

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

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

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

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 +84,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
16 changes: 16 additions & 0 deletions src/ConfigInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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;

interface ConfigInterface
{
public function configureServiceManager(ServiceManager $serviceManager);
}
Loading

0 comments on commit e2db3b8

Please sign in to comment.