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

Commit

Permalink
Merge remote-tracking branch 'ralphschindler/feature/service-manager-…
Browse files Browse the repository at this point in the history
…component'
  • Loading branch information
EvanDotPro committed May 7, 2012
11 parents 467f8b2 + 30bb6f0 + f8cc896 + 2ee0afd + 656f0b0 + 7092cb4 + 5e1b259 + abb3ff8 + 39a873f + b6e6c92 + ee23691 commit 41fab24
Show file tree
Hide file tree
Showing 27 changed files with 1,528 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/AbstractFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Zend\ServiceManager;

interface AbstractFactoryInterface
{
public function canCreateServiceWithName($name /*, $requestedName */);
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name /*, $requestedName */);
}
71 changes: 71 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Zend\ServiceManager;

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

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

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

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

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

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

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

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

public function configureServiceManager(ServiceManager $serviceManager)
{
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->setInvokable($name, $invokable);
}

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

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

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

}
8 changes: 8 additions & 0 deletions src/ConfigurationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Zend\ServiceManager;

interface ConfigurationInterface
{
public function configureServiceManager(ServiceManager $serviceManager);
}
53 changes: 53 additions & 0 deletions src/Di/DiAbstractServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Zend\ServiceManager\Di;

use Zend\ServiceManager\AbstractFactoryInterface,
Zend\ServiceManager\ServiceLocatorInterface,
Zend\Di\Di;

class DiAbstractServiceFactory extends DiServiceFactory implements AbstractFactoryInterface
{

/**
* @param \Zend\Di\Di $di
* @param null|string|\Zend\Di\InstanceManager $useServiceLocator
*/
public function __construct(Di $di, $useServiceLocator = self::USE_SL_NONE)
{
$this->di = $di;
if (in_array($useServiceLocator, array(self::USE_SL_BEFORE_DI, self::USE_SL_AFTER_DI, self::USE_SL_NONE))) {
$this->useServiceManager = $useServiceLocator;
}

// since we are using this in a proxy-fashion, localize state
$this->definitions = $this->di->definitions;
$this->instanceManager = $this->di->instanceManager;
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param $serviceName
* @param null $requestedName
* @return object
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $serviceName, $requestedName = null)
{
$this->serviceLocator = $serviceLocator;
if ($requestedName) {
return $this->get($requestedName, array(), true);
} else {
return $this->get($serviceName, array(), true);
}

}

/**
* @param $name
* @return null
*/
public function canCreateServiceWithName($name)
{
return null; // not sure
}
}
51 changes: 51 additions & 0 deletions src/Di/DiInstanceManagerProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Zend\ServiceManager\Di;

use Zend\Di\InstanceManager as DiInstanceManager,
Zend\ServiceManager\ServiceLocatorInterface;

class DiInstanceManagerProxy extends DiInstanceManager
{
/**
* @var DiInstanceManager
*/
protected $diInstanceManager = null;

/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator = null;

/**
* @param DiInstanceManager $diInstanceManager
* @param ServiceLocatorInterface $serviceLocator
*/
public function __construct(DiInstanceManager $diInstanceManager, ServiceLocatorInterface $serviceLocator)
{
$this->diInstanceManager = $diInstanceManager;
$this->serviceLocator = $serviceLocator;
}

/**
* @param $classOrAlias
* @return bool
*/
public function hasSharedInstance($classOrAlias)
{
return ($this->serviceLocator->has($classOrAlias) || $this->diInstanceManager->hasSharedInstance($classOrAlias));
}

/**
* @param $classOrAlias
* @return mixed
*/
public function getSharedInstance($classOrAlias)
{
if ($this->serviceLocator->has($classOrAlias)) {
return $this->serviceLocator->get($classOrAlias);
} else {
return $this->diInstanceManager->getSharedInstance($classOrAlias);
}
}
}
112 changes: 112 additions & 0 deletions src/Di/DiServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Zend\ServiceManager\Di;

use Zend\ServiceManager\FactoryInterface,
Zend\ServiceManager\ServiceLocatorInterface,
Zend\ServiceManager\Exception,
Zend\Di\Di,
Zend\Di\Exception\ClassNotFoundException as DiClassNotFoundException;

class DiServiceFactory extends Di implements FactoryInterface
{
/**@#+
* constants
*/
const USE_SL_BEFORE_DI = 'before';
const USE_SL_AFTER_DI = 'after';
const USE_SL_NONE = 'none';
/**@#-*/

/**
* @var \Zend\Di\Di
*/
protected $di = null;

/**
* @var \Zend\Di\InstanceManager
*/
protected $name = null;

/**
* @var array
*/
protected $parameters = array();

/**
* @var string
*/
protected $useServiceLocator = self::USE_SL_AFTER_DI;

/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator = null;

/**
* @param \Zend\Di\Di $di
* @param null|\Zend\Di\InstanceManager $name
* @param array $parameters
* @param string $useServiceLocator
*/
public function __construct(Di $di, $name, array $parameters = array(), $useServiceLocator = self::USE_SL_NONE)
{
$this->di = $di;
$this->name = $name;
$this->parameters = $parameters;
if (in_array($useServiceLocator, array(self::USE_SL_BEFORE_DI, self::USE_SL_AFTER_DI, self::USE_SL_NONE))) {
$this->useServiceLocator = $useServiceLocator;
}

// since we are using this in a proxy-fashion, localize state
$this->definitions = $this->di->definitions;
$this->instanceManager = $this->di->instanceManager;
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @return object
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
return $this->get($this->name, $this->parameters, true);
}

/**
* Override, as we want it to use the functionality defined in the proxy
*
* @param string $name
* @param array $params
* @return object
* @throws Exception\InvalidServiceNameException
*/
public function get($name, array $params = array())
{
// allow this di service to get dependencies from the service locator BEFORE trying di
if ($this->useServiceLocator == self::USE_SL_BEFORE_DI && $this->serviceLocator->has($name)) {
return $this->serviceLocator->get($name);
}

try {

$service = parent::get($name, $params);
return $service;

} catch (DiClassNotFoundException $e) {

// allow this di service to get dependencies from the service locator AFTER trying di
if ($this->useServiceLocator == self::USE_SL_AFTER_DI && $this->serviceLocator->has($name)) {
return $this->serviceLocator->get($name);
} else {
throw new Exception\InvalidServiceNameException(
sprintf('Service %s was not found in this DI instance', $name),
null,
$e
);
}
}

}

}
56 changes: 56 additions & 0 deletions src/Di/DiServiceInitializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Zend\ServiceManager\Di;

use Zend\ServiceManager\InitializerInterface,
Zend\ServiceManager\ServiceLocatorInterface,
Zend\ServiceManager\Exception,
Zend\Di\Di,
Zend\Di\Exception\ClassNotFoundException as DiClassNotFoundException;

class DiServiceInitializer extends Di implements InitializerInterface
{
/**
* @var Di
*/
protected $di = null;

/**
* @var DiInstanceManagerProxy
*/
protected $diInstanceManagerProxy = null;

/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator = null;

/**
* @param \Zend\Di\Di $di
* @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator
* @param null|DiInstanceManagerProxy $diImProxy
*/
public function __construct(Di $di, ServiceLocatorInterface $serviceLocator, DiInstanceManagerProxy $diImProxy = null)
{
$this->di = $di;
$this->serviceLocator = $serviceLocator;
$this->diInstanceManagerProxy = ($diImProxy) ?: new DiInstanceManagerProxy($di->instanceManager(), $serviceLocator);
}

/**
* @param $instance
*/
public function initialize($instance)
{
$instanceManager = $this->di->instanceManager;
$this->di->instanceManager = $this->diInstanceManagerProxy;
try {
$this->di->injectDependencies($instance);
$this->di->instanceManager = $instanceManager;
} catch (\Exception $e) {
$this->di->instanceManager = $instanceManager;
throw $e;
}
}

}
7 changes: 7 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Zend\ServiceManager;

interface Exception
{
}
9 changes: 9 additions & 0 deletions src/Exception/CircularDependencyFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Zend\ServiceManager\Exception;

use Zend\ServiceManager\Exception;

class CircularDependencyFoundException extends \RuntimeException implements Exception
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Zend\ServiceManager\Exception;

use Zend\ServiceManager\Exception;

class InvalidArgumentException extends \InvalidArgumentException implements Exception
{
}
Loading

0 comments on commit 41fab24

Please sign in to comment.