Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 751f5b4

Browse files
committed
Re-added functionality removed in previous patches
This patch re-introduces the following classes from the `Zend\Mvc\Service` namespace, along with related tests: - `ConfigFactory` - `ControllerLoaderFactory` - `DiAbstractServiceFactoryFactory` - `DiFactory` - `DiServiceInitializerFactory` - `DiStrictAbstractServiceFactory` - `DiStrictAbstractServiceFactoryFactory` It also re-instates the `ServiceManagerAware` and `ServiceLocatorAware` initializers in the `ServiceManagerConfig` class (though they will now emit deprecation notices), and ensures that class properly defines services such that they'll be accessible identically between v2 and v3 of zend-servicemanager.
1 parent 27f184f commit 751f5b4

11 files changed

+569
-29
lines changed

CHANGELOG.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,20 @@ All notable changes to this project will be documented in this file, in reverse
4040

4141
### Deprecated
4242

43-
- Nothing.
43+
- Two initializers registered by `Zend\Mvc\Service\ServiceManagerConfig` are now
44+
deprecated, and will be removed starting in version 3.0:
45+
- `ServiceManagerAwareInitializer`, which injects classes implementing
46+
`Zend\ServiceManager\ServiceManagerAwareInterface` with the service manager
47+
instance. Users should create factories for such classes that directly
48+
inject their dependencies instead.
49+
- `ServiceLocatorAwareInitializer`, which injects classes implementing
50+
`Zend\ServiceManager\ServiceLocatorAwareInterface` with the service manager
51+
instance. Users should create factories for such classes that directly
52+
inject their dependencies instead.
4453

4554
### Removed
4655

47-
- [#36](https://github.com/zendframework/zend-mvc/pull/36) removes
48-
`Zend\Mvc\Service\ConfigFactory`, as the functionality is now incorporated
49-
into `Zend\ModuleManager\Listener\ServiceListener`.
50-
- [#36](https://github.com/zendframework/zend-mvc/pull/36) removes
51-
the `ServiceLocatorAware` intializer, as zend-servicemanager v3 no longer
52-
defines the interface.
53-
- [#36](https://github.com/zendframework/zend-mvc/pull/36) removes
54-
`Zend\Mvc\Service\ControllerLoaderFactory` and replaces it with
55-
`Zend\Mvc\Service\ControllerManagerFactory`.
56-
- [#36](https://github.com/zendframework/zend-mvc/pull/36) removes
57-
`Zend\Mvc\Service\DiFactory`, `Zend\Mvc\Service\DiAbstractServiceFactory`,
58-
`Zend\Mvc\Service\DiStrictAbstractServiceFactory`,
59-
`Zend\Mvc\Service\DiStrictAbstractServiceFactoryFactory`,
60-
and `Zend\Mvc\Service\DiServiceInitializerFactory`, as zend-servicemanager v3
61-
removes `Zend\Di` integration.
56+
- Nothing.
6257

6358
### Fixed
6459

src/Service/ConfigFactory.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Mvc\Service;
11+
12+
use Zend\ServiceManager\FactoryInterface;
13+
use Zend\ServiceManager\ServiceLocatorInterface;
14+
15+
class ConfigFactory implements FactoryInterface
16+
{
17+
/**
18+
* Create the application configuration service
19+
*
20+
* Retrieves the Module Manager from the service locator, and executes
21+
* {@link Zend\ModuleManager\ModuleManager::loadModules()}.
22+
*
23+
* It then retrieves the config listener from the module manager, and from
24+
* that the merged configuration.
25+
*
26+
* @param ServiceLocatorInterface $serviceLocator
27+
* @return array|\Traversable
28+
*/
29+
public function createService(ServiceLocatorInterface $serviceLocator)
30+
{
31+
$mm = $serviceLocator->get('ModuleManager');
32+
$mm->loadModules();
33+
$moduleParams = $mm->getEvent()->getParams();
34+
$config = $moduleParams['configListener']->getMergedConfig(false);
35+
return $config;
36+
}
37+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Mvc\Service;
11+
12+
use Zend\Mvc\Controller\ControllerManager;
13+
use Zend\ServiceManager\FactoryInterface;
14+
use Zend\ServiceManager\ServiceLocatorInterface;
15+
16+
class ControllerLoaderFactory implements FactoryInterface
17+
{
18+
/**
19+
* Create the controller loader service
20+
*
21+
* Creates and returns an instance of ControllerManager. The
22+
* only controllers this manager will allow are those defined in the
23+
* application configuration's "controllers" array. If a controller is
24+
* matched, the scoped manager will attempt to load the controller.
25+
* Finally, it will attempt to inject the controller plugin manager
26+
* if the controller implements a setPluginManager() method.
27+
*
28+
* This plugin manager is _not_ peered against DI, and as such, will
29+
* not load unknown classes.
30+
*
31+
* @param ServiceLocatorInterface $serviceLocator
32+
* @return ControllerManager
33+
*/
34+
public function createService(ServiceLocatorInterface $serviceLocator)
35+
{
36+
$controllerLoader = new ControllerManager();
37+
$controllerLoader->setServiceLocator($serviceLocator);
38+
$controllerLoader->addPeeringServiceManager($serviceLocator);
39+
40+
$config = $serviceLocator->get('Config');
41+
42+
if (isset($config['di']) && isset($config['di']['allowed_controllers']) && $serviceLocator->has('Di')) {
43+
$controllerLoader->addAbstractFactory($serviceLocator->get('DiStrictAbstractServiceFactory'));
44+
}
45+
46+
return $controllerLoader;
47+
}
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Mvc\Service;
11+
12+
use Zend\ServiceManager\Di\DiAbstractServiceFactory;
13+
use Zend\ServiceManager\FactoryInterface;
14+
use Zend\ServiceManager\ServiceLocatorInterface;
15+
use Zend\ServiceManager\ServiceManager;
16+
17+
class DiAbstractServiceFactoryFactory implements FactoryInterface
18+
{
19+
/**
20+
* Class responsible for instantiating a DiAbstractServiceFactory
21+
*
22+
* @param ServiceLocatorInterface $serviceLocator
23+
* @return DiAbstractServiceFactory
24+
*/
25+
public function createService(ServiceLocatorInterface $serviceLocator)
26+
{
27+
$factory = new DiAbstractServiceFactory($serviceLocator->get('Di'), DiAbstractServiceFactory::USE_SL_BEFORE_DI);
28+
29+
if ($serviceLocator instanceof ServiceManager) {
30+
/* @var $serviceLocator ServiceManager */
31+
$serviceLocator->addAbstractFactory($factory, false);
32+
}
33+
34+
return $factory;
35+
}
36+
}

src/Service/DiFactory.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Mvc\Service;
11+
12+
use Zend\Di\Config;
13+
use Zend\Di\Di;
14+
use Zend\ServiceManager\FactoryInterface;
15+
use Zend\ServiceManager\ServiceLocatorInterface;
16+
17+
class DiFactory implements FactoryInterface
18+
{
19+
/**
20+
* Create and return abstract factory seeded by dependency injector
21+
*
22+
* Creates and returns an abstract factory seeded by the dependency
23+
* injector. If the "di" key of the configuration service is set, that
24+
* sub-array is passed to a DiConfig object and used to configure
25+
* the DI instance. The DI instance is then used to seed the
26+
* DiAbstractServiceFactory, which is then registered with the service
27+
* manager.
28+
*
29+
* @param ServiceLocatorInterface $serviceLocator
30+
* @return Di
31+
*/
32+
public function createService(ServiceLocatorInterface $serviceLocator)
33+
{
34+
$di = new Di();
35+
$config = $serviceLocator->get('Config');
36+
37+
if (isset($config['di'])) {
38+
$config = new Config($config['di']);
39+
$config->configure($di);
40+
}
41+
42+
return $di;
43+
}
44+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Mvc\Service;
11+
12+
use Zend\ServiceManager\Di\DiServiceInitializer;
13+
use Zend\ServiceManager\FactoryInterface;
14+
use Zend\ServiceManager\ServiceLocatorInterface;
15+
16+
class DiServiceInitializerFactory implements FactoryInterface
17+
{
18+
/**
19+
* Class responsible for instantiating a DiServiceInitializer
20+
*
21+
* @param ServiceLocatorInterface $serviceLocator
22+
* @return DiServiceInitializer
23+
*/
24+
public function createService(ServiceLocatorInterface $serviceLocator)
25+
{
26+
return new DiServiceInitializer($serviceLocator->get('Di'), $serviceLocator);
27+
}
28+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Mvc\Service;
11+
12+
use Zend\Di\Di;
13+
use Zend\Di\Exception\ClassNotFoundException;
14+
use Zend\Mvc\Exception\DomainException;
15+
use Zend\ServiceManager\AbstractFactoryInterface;
16+
use Zend\ServiceManager\AbstractPluginManager;
17+
use Zend\ServiceManager\Exception;
18+
use Zend\ServiceManager\ServiceLocatorInterface;
19+
20+
class DiStrictAbstractServiceFactory extends Di implements AbstractFactoryInterface
21+
{
22+
/**@#+
23+
* constants
24+
*/
25+
const USE_SL_BEFORE_DI = 'before';
26+
const USE_SL_AFTER_DI = 'after';
27+
const USE_SL_NONE = 'none';
28+
/**@#-*/
29+
30+
/**
31+
* @var Di
32+
*/
33+
protected $di = null;
34+
35+
/**
36+
* @var string
37+
*/
38+
protected $useServiceLocator = self::USE_SL_AFTER_DI;
39+
40+
/**
41+
* @var ServiceLocatorInterface
42+
*/
43+
protected $serviceLocator = null;
44+
45+
/**
46+
* @var array an array of whitelisted service names (keys are the service names)
47+
*/
48+
protected $allowedServiceNames = [];
49+
50+
/**
51+
* @param Di $di
52+
* @param string $useServiceLocator
53+
*/
54+
public function __construct(Di $di, $useServiceLocator = self::USE_SL_NONE)
55+
{
56+
$this->useServiceLocator = $useServiceLocator;
57+
// since we are using this in a proxy-fashion, localize state
58+
$this->di = $di;
59+
$this->definitions = $this->di->definitions;
60+
$this->instanceManager = $this->di->instanceManager;
61+
}
62+
63+
/**
64+
* @param array $allowedServiceNames
65+
*/
66+
public function setAllowedServiceNames(array $allowedServiceNames)
67+
{
68+
$this->allowedServiceNames = array_flip(array_values($allowedServiceNames));
69+
}
70+
71+
/**
72+
* @return array
73+
*/
74+
public function getAllowedServiceNames()
75+
{
76+
return array_keys($this->allowedServiceNames);
77+
}
78+
79+
/**
80+
* {@inheritDoc}
81+
*
82+
* Allows creation of services only when in a whitelist
83+
*/
84+
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $serviceName, $requestedName)
85+
{
86+
if (!isset($this->allowedServiceNames[$requestedName])) {
87+
throw new Exception\InvalidServiceNameException('Service "' . $requestedName . '" is not whitelisted');
88+
}
89+
90+
if ($serviceLocator instanceof AbstractPluginManager) {
91+
/* @var $serviceLocator AbstractPluginManager */
92+
$this->serviceLocator = $serviceLocator->getServiceLocator();
93+
} else {
94+
$this->serviceLocator = $serviceLocator;
95+
}
96+
97+
return parent::get($requestedName);
98+
}
99+
100+
/**
101+
* Overrides Zend\Di to allow the given serviceLocator's services to be reused by Di itself
102+
*
103+
* {@inheritDoc}
104+
*
105+
* @throws Exception\InvalidServiceNameException
106+
*/
107+
public function get($name, array $params = [])
108+
{
109+
if (null === $this->serviceLocator) {
110+
throw new DomainException('No ServiceLocator defined, use `createServiceWithName` instead of `get`');
111+
}
112+
113+
if (self::USE_SL_BEFORE_DI === $this->useServiceLocator && $this->serviceLocator->has($name)) {
114+
return $this->serviceLocator->get($name);
115+
}
116+
117+
try {
118+
return parent::get($name, $params);
119+
} catch (ClassNotFoundException $e) {
120+
if (self::USE_SL_AFTER_DI === $this->useServiceLocator && $this->serviceLocator->has($name)) {
121+
return $this->serviceLocator->get($name);
122+
}
123+
124+
throw new Exception\ServiceNotFoundException(
125+
sprintf('Service %s was not found in this DI instance', $name),
126+
null,
127+
$e
128+
);
129+
}
130+
}
131+
132+
/**
133+
* {@inheritDoc}
134+
*
135+
* Allows creation of services only when in a whitelist
136+
*/
137+
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
138+
{
139+
// won't check if the service exists, we are trusting the user's whitelist
140+
return isset($this->allowedServiceNames[$requestedName]);
141+
}
142+
}

0 commit comments

Comments
 (0)