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

Commit 7756cc1

Browse files
Gianluca Arbezzanokynx
Gianluca Arbezzano
authored andcommitted
ServiceManager v2-v3 compatibility
1 parent 23dbe80 commit 7756cc1

9 files changed

+349
-141
lines changed

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ matrix:
1717
- php: 5.5
1818
env:
1919
- EXECUTE_CS_CHECK=true
20+
- php: 5.5
21+
env:
22+
- SERVICE_MANAGER_VERSION="^2.7.5"
2023
- php: 5.6
2124
env:
2225
- EXECUTE_TEST_COVERALLS=true
26+
- php: 5.6
27+
env:
28+
- SERVICE_MANAGER_VERSION="^2.7.5"
2329
- php: 7
2430
- php: hhvm
2531
allow_failures:
@@ -41,6 +47,8 @@ script:
4147
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi
4248
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then ./vendor/bin/phpunit ; fi
4349
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/php-cs-fixer fix -v --diff --dry-run ; fi
50+
- if [[ $SERVICE_MANAGER_VERSION != '' ]]; then composer require --no-update "zendframework/zend-servicemanager:$SERVICE_MANAGER_VERSION" ; fi
51+
- if [[ $SERVICE_MANAGER_VERSION == '' ]]; then composer require --no-update "zendframework/zend-servicemanager:^3.0.3" ; fi
4452

4553
after_script:
4654
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls ; fi

composer.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
},
1515
"require": {
1616
"php": ">=5.5",
17-
"zendframework/zend-filter": "~2.5",
18-
"zendframework/zend-validator": "^2.5.3",
19-
"zendframework/zend-stdlib": "~2.5"
17+
"zendframework/zend-filter": "^2.6.0",
18+
"zendframework/zend-validator": "^2.6.0",
19+
"zendframework/zend-stdlib": "^2.7 || ^3.0"
2020
},
2121
"require-dev": {
22-
"zendframework/zend-servicemanager": "~2.5",
22+
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",
2323
"fabpot/php-cs-fixer": "1.7.*",
2424
"phpunit/PHPUnit": "^4.5"
2525
},

src/Factory.php

+3-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use Traversable;
1313
use Zend\Filter\FilterChain;
14-
use Zend\ServiceManager\ServiceLocatorInterface;
14+
use Zend\ServiceManager\ServiceManager;
1515
use Zend\Stdlib\ArrayUtils;
1616
use Zend\Validator\ValidatorChain;
1717
use Zend\Validator\ValidatorInterface;
@@ -117,15 +117,7 @@ public function clearDefaultValidatorChain()
117117
public function setInputFilterManager(InputFilterPluginManager $inputFilterManager)
118118
{
119119
$this->inputFilterManager = $inputFilterManager;
120-
$serviceLocator = $this->inputFilterManager->getServiceLocator();
121-
if ($serviceLocator && $serviceLocator instanceof ServiceLocatorInterface) {
122-
if ($serviceLocator->has('ValidatorManager')) {
123-
$this->getDefaultValidatorChain()->setPluginManager($serviceLocator->get('ValidatorManager'));
124-
}
125-
if ($serviceLocator->has('FilterManager')) {
126-
$this->getDefaultFilterChain()->setPluginManager($serviceLocator->get('FilterManager'));
127-
}
128-
}
120+
$inputFilterManager->populateFactoryPluginManagers($this);
129121
return $this;
130122
}
131123

@@ -135,7 +127,7 @@ public function setInputFilterManager(InputFilterPluginManager $inputFilterManag
135127
public function getInputFilterManager()
136128
{
137129
if (null === $this->inputFilterManager) {
138-
$this->inputFilterManager = new InputFilterPluginManager;
130+
$this->inputFilterManager = new InputFilterPluginManager(new ServiceManager());
139131
}
140132

141133
return $this->inputFilterManager;

src/InputFilterAbstractServiceFactory.php

+50-16
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
namespace Zend\InputFilter;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\Filter\FilterPluginManager;
1314
use Zend\ServiceManager\AbstractFactoryInterface;
1415
use Zend\ServiceManager\ServiceLocatorInterface;
16+
use Zend\ServiceManager\ServiceManager;
1517
use Zend\Validator\ValidatorPluginManager;
1618

1719
class InputFilterAbstractServiceFactory implements AbstractFactoryInterface
@@ -21,22 +23,47 @@ class InputFilterAbstractServiceFactory implements AbstractFactoryInterface
2123
*/
2224
protected $factory;
2325

26+
2427
/**
25-
* @param ServiceLocatorInterface $inputFilters
28+
* @param ContainerInterface $services
29+
* @param string $rName
30+
* @param array $options
31+
* @return InputFilterInterface
32+
*/
33+
public function __invoke(ContainerInterface $services, $rName, array $options = null)
34+
{
35+
// v2 - get parent service manager
36+
if (! method_exists($services, 'configure')) {
37+
$services = $services->getServiceLocator();
38+
}
39+
40+
$allConfig = $services->get('config');
41+
$config = $allConfig['input_filter_specs'][$rName];
42+
43+
$factory = $this->getInputFilterFactory($services);
44+
45+
return $factory->createInputFilter($config);
46+
}
47+
48+
/**
49+
*
50+
* @param ContainerInterface $services
2651
* @param string $cName
2752
* @param string $rName
2853
* @return bool
2954
*/
30-
public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
55+
public function canCreate(ContainerInterface $services, $rName)
3156
{
32-
$services = $inputFilters->getServiceLocator();
33-
if (! $services instanceof ServiceLocatorInterface
34-
|| ! $services->has('Config')
35-
) {
57+
// v2 - get parent service manager
58+
if (! method_exists($services, 'configure')) {
59+
$services = $services->getServiceLocator();
60+
}
61+
62+
if (! $services->has('config')) {
3663
return false;
3764
}
3865

39-
$config = $services->get('Config');
66+
$config = $services->get('config');
4067
if (!isset($config['input_filter_specs'][$rName])
4168
|| !is_array($config['input_filter_specs'][$rName])
4269
) {
@@ -46,6 +73,19 @@ public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters,
4673
return true;
4774
}
4875

76+
/**
77+
* Determine if we can create a service with name (v2)
78+
*
79+
* @param ServiceLocatorInterface $serviceLocator
80+
* @param $name
81+
* @param $requestedName
82+
* @return bool
83+
*/
84+
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
85+
{
86+
return $this->canCreate($serviceLocator, $requestedName);
87+
}
88+
4989
/**
5090
* @param ServiceLocatorInterface $inputFilters
5191
* @param string $cName
@@ -54,13 +94,7 @@ public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters,
5494
*/
5595
public function createServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
5696
{
57-
$services = $inputFilters->getServiceLocator();
58-
$allConfig = $services->get('Config');
59-
$config = $allConfig['input_filter_specs'][$rName];
60-
61-
$factory = $this->getInputFilterFactory($services);
62-
63-
return $factory->createInputFilter($config);
97+
return $this($inputFilters, $rName);
6498
}
6599

66100
/**
@@ -94,7 +128,7 @@ protected function getFilterPluginManager(ServiceLocatorInterface $services)
94128
return $services->get('FilterManager');
95129
}
96130

97-
return new FilterPluginManager();
131+
return new FilterPluginManager(new ServiceManager());
98132
}
99133

100134
/**
@@ -107,6 +141,6 @@ protected function getValidatorPluginManager(ServiceLocatorInterface $services)
107141
return $services->get('ValidatorManager');
108142
}
109143

110-
return new ValidatorPluginManager();
144+
return new ValidatorPluginManager(new ServiceManager());
111145
}
112146
}

src/InputFilterPluginManager.php

+88-18
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
namespace Zend\InputFilter;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\ServiceManager\AbstractPluginManager;
13-
use Zend\ServiceManager\ConfigInterface;
14-
use Zend\ServiceManager\ServiceLocatorInterface;
14+
use Zend\ServiceManager\Exception\InvalidServiceException;
15+
use Zend\ServiceManager\Factory\InvokableFactory;
1516
use Zend\Stdlib\InitializableInterface;
1617

1718
/**
@@ -22,55 +23,100 @@
2223
class InputFilterPluginManager extends AbstractPluginManager
2324
{
2425
/**
25-
* Default set of plugins
26+
* Default alias of plugins
2627
*
2728
* @var string[]
2829
*/
29-
protected $invokableClasses = [
30+
protected $aliases = [
3031
'inputfilter' => InputFilter::class,
32+
'inputFilter' => InputFilter::class,
33+
'InputFilter' => InputFilter::class,
3134
'collection' => CollectionInputFilter::class,
35+
'Collection' => CollectionInputFilter::class,
3236
];
3337

3438
/**
35-
* Whether or not to share by default
39+
* Default set of plugins
40+
*
41+
* @var string[]
42+
*/
43+
protected $factories = [
44+
InputFilter::class => InvokableFactory::class,
45+
CollectionInputFilter::class => InvokableFactory::class,
46+
// v2 canonical FQCN
47+
'zendinputfilterinputfilter' => InvokableFactory::class,
48+
'zendinputfiltercollectioninputfilter' => InvokableFactory::class,
49+
];
50+
51+
/**
52+
* Whether or not to share by default (v3)
53+
*
54+
* @var bool
55+
*/
56+
protected $sharedByDefault = false;
57+
58+
/**
59+
* Whether or not to share by default (v2)
3660
*
3761
* @var bool
3862
*/
3963
protected $shareByDefault = false;
4064

65+
4166
/**
42-
* @param ConfigInterface $configuration
67+
* @param ContainerInterface $parentLocator
68+
* @param array $config
4369
*/
44-
public function __construct(ConfigInterface $configuration = null)
70+
public function __construct(ContainerInterface $parentLocator, array $config = [])
4571
{
46-
parent::__construct($configuration);
47-
72+
parent::__construct($parentLocator, $config);
4873
$this->addInitializer([$this, 'populateFactory']);
4974
}
5075

5176
/**
5277
* Inject this and populate the factory with filter chain and validator chain
5378
*
54-
* @param $inputFilter
79+
* @param mixed $first
80+
* @param mixed $second
5581
*/
56-
public function populateFactory($inputFilter)
82+
public function populateFactory($first, $second)
5783
{
84+
if ($first instanceof ContainerInterface) {
85+
$container = $first;
86+
$inputFilter = $second;
87+
} else {
88+
$container = $second;
89+
$inputFilter = $first;
90+
}
5891
if ($inputFilter instanceof InputFilter) {
5992
$factory = $inputFilter->getFactory();
6093

6194
$factory->setInputFilterManager($this);
95+
}
96+
}
6297

63-
if ($this->serviceLocator instanceof ServiceLocatorInterface) {
64-
$factory->getDefaultFilterChain()->setPluginManager($this->serviceLocator->get('FilterManager'));
65-
$factory->getDefaultValidatorChain()->setPluginManager($this->serviceLocator->get('ValidatorManager'));
66-
}
98+
public function populateFactoryPluginManagers(Factory $factory)
99+
{
100+
if (property_exists($this, 'creationContext')) {
101+
// v3
102+
$container = $this->creationContext;
103+
} else {
104+
// v2
105+
$container = $this->serviceLocator;
106+
}
107+
108+
if ($container && $container->has('FilterManager')) {
109+
$factory->getDefaultFilterChain()->setPluginManager($container->get('FilterManager'));
110+
}
111+
if ($container && $container->has('ValidatorManager')) {
112+
$factory->getDefaultValidatorChain()->setPluginManager($container->get('ValidatorManager'));
67113
}
68114
}
69115

70116
/**
71-
* {@inheritDoc}
117+
* {@inheritDoc} (v3)
72118
*/
73-
public function validatePlugin($plugin)
119+
public function validate($plugin)
74120
{
75121
if ($plugin instanceof InputFilterInterface || $plugin instanceof InputInterface) {
76122
// Hook to perform various initialization, when the inputFilter is not created through the factory
@@ -82,11 +128,35 @@ public function validatePlugin($plugin)
82128
return;
83129
}
84130

85-
throw new Exception\RuntimeException(sprintf(
131+
throw new InvalidServiceException(sprintf(
86132
'Plugin of type %s is invalid; must implement %s or %s',
87133
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
88134
InputFilterInterface::class,
89135
InputInterface::class
90136
));
91137
}
138+
139+
/**
140+
* Validate the plugin (v2)
141+
*
142+
* Checks that the filter loaded is either a valid callback or an instance
143+
* of FilterInterface.
144+
*
145+
* @param mixed $plugin
146+
* @return void
147+
* @throws Exception\RuntimeException if invalid
148+
*/
149+
public function validatePlugin($plugin)
150+
{
151+
try {
152+
$this->validate($plugin);
153+
} catch (InvalidServiceException $e) {
154+
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
155+
}
156+
}
157+
158+
public function shareByDefault()
159+
{
160+
return $this->sharedByDefault;
161+
}
92162
}

0 commit comments

Comments
 (0)