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

Commit 8b90a42

Browse files
committed
Merge branch 'feature/95' into develop
Close #96 Close #95 Close #86
2 parents 23dbe80 + 47233a7 commit 8b90a42

10 files changed

+442
-149
lines changed

.travis.yml

+17-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,23 @@ 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
24-
- php: hhvm
30+
- php: 7
31+
env:
32+
- SERVICE_MANAGER_VERSION="^2.7.5"
33+
- php: hhvm
34+
- php: hhvm
35+
env:
36+
- SERVICE_MANAGER_VERSION="^2.7.5"
2537
allow_failures:
2638
- php: 7
2739

@@ -33,9 +45,12 @@ before_install:
3345
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
3446
- composer self-update
3547
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls ; fi
48+
- if [[ $SERVICE_MANAGER_VERSION != '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:$SERVICE_MANAGER_VERSION" ; fi
49+
- if [[ $SERVICE_MANAGER_VERSION == '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:^3.0.3" ; fi
3650

3751
install:
38-
- travis_retry composer install --no-interaction --ignore-platform-reqs
52+
- if [[ $SERVICE_MANAGER_V2 != 'true' ]]; then travis_retry composer install --no-interaction --ignore-platform-reqs ; fi
53+
- if [[ $SERVICE_MANAGER_V2 == 'true' ]]; then travis_retry composer update --no-interaction --ignore-platform-reqs --prefer-lowest ; fi
3954

4055
script:
4156
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi

CHANGELOG.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.6.0 - TBD
5+
## 2.6.0 - 2016-02-18
66

77
### Added
88

@@ -18,7 +18,10 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#86](https://github.com/zendframework/zend-inputfilter/pull/86),
22+
[#95](https://github.com/zendframework/zend-inputfilter/pull/95), and
23+
[#96](https://github.com/zendframework/zend-inputfilter/pull/96) update the
24+
component to be forwards-compatible with zend-servicemanager v3.
2225

2326
## 2.5.6 - TBD
2427

composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
}
1414
},
1515
"require": {
16-
"php": ">=5.5",
17-
"zendframework/zend-filter": "~2.5",
18-
"zendframework/zend-validator": "^2.5.3",
19-
"zendframework/zend-stdlib": "~2.5"
16+
"php": "^5.5 || ^7.0",
17+
"zendframework/zend-filter": "^2.6",
18+
"zendframework/zend-validator": "^2.6",
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

+51-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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;
@@ -22,21 +23,34 @@ class InputFilterAbstractServiceFactory implements AbstractFactoryInterface
2223
protected $factory;
2324

2425
/**
25-
* @param ServiceLocatorInterface $inputFilters
26+
* @param ContainerInterface $services
27+
* @param string $rName
28+
* @param array $options
29+
* @return InputFilterInterface
30+
*/
31+
public function __invoke(ContainerInterface $services, $rName, array $options = null)
32+
{
33+
$allConfig = $services->get('config');
34+
$config = $allConfig['input_filter_specs'][$rName];
35+
$factory = $this->getInputFilterFactory($services);
36+
37+
return $factory->createInputFilter($config);
38+
}
39+
40+
/**
41+
*
42+
* @param ContainerInterface $services
2643
* @param string $cName
2744
* @param string $rName
2845
* @return bool
2946
*/
30-
public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
47+
public function canCreate(ContainerInterface $services, $rName)
3148
{
32-
$services = $inputFilters->getServiceLocator();
33-
if (! $services instanceof ServiceLocatorInterface
34-
|| ! $services->has('Config')
35-
) {
49+
if (! $services->has('config')) {
3650
return false;
3751
}
3852

39-
$config = $services->get('Config');
53+
$config = $services->get('config');
4054
if (!isset($config['input_filter_specs'][$rName])
4155
|| !is_array($config['input_filter_specs'][$rName])
4256
) {
@@ -46,6 +60,27 @@ public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters,
4660
return true;
4761
}
4862

63+
/**
64+
* Determine if we can create a service with name (v2)
65+
*
66+
* @param ServiceLocatorInterface $serviceLocator
67+
* @param $name
68+
* @param $requestedName
69+
* @return bool
70+
*/
71+
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
72+
{
73+
// v2 => need to get parent service locator
74+
$services = $serviceLocator->getServiceLocator();
75+
76+
// No parent locator => cannot create service.
77+
if (! $services) {
78+
return false;
79+
}
80+
81+
return $this->canCreate($services, $requestedName);
82+
}
83+
4984
/**
5085
* @param ServiceLocatorInterface $inputFilters
5186
* @param string $cName
@@ -54,13 +89,15 @@ public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters,
5489
*/
5590
public function createServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
5691
{
57-
$services = $inputFilters->getServiceLocator();
58-
$allConfig = $services->get('Config');
59-
$config = $allConfig['input_filter_specs'][$rName];
92+
// v2 => need to get parent service locator
93+
$services = $inputFilters->getServiceLocator();
6094

61-
$factory = $this->getInputFilterFactory($services);
95+
// No parent locator => cannot create service.
96+
if (! $services) {
97+
return false;
98+
}
6299

63-
return $factory->createInputFilter($config);
100+
return $this($services, $rName);
64101
}
65102

66103
/**
@@ -94,7 +131,7 @@ protected function getFilterPluginManager(ServiceLocatorInterface $services)
94131
return $services->get('FilterManager');
95132
}
96133

97-
return new FilterPluginManager();
134+
return new FilterPluginManager($services);
98135
}
99136

100137
/**
@@ -107,6 +144,6 @@ protected function getValidatorPluginManager(ServiceLocatorInterface $services)
107144
return $services->get('ValidatorManager');
108145
}
109146

110-
return new ValidatorPluginManager();
147+
return new ValidatorPluginManager($services);
111148
}
112149
}

src/InputFilterPluginManager.php

+88-19
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,104 @@
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

4165
/**
42-
* @param ConfigInterface $configuration
66+
* @param null|\Zend\ServiceManager\ConfigInterface|ContainerInterface $configOrContainer
67+
* For zend-servicemanager v2, null or a ConfigInterface instance are
68+
* allowed; for v3, a ContainerInterface is expected.
69+
* @param array $v3config Optional configuration array (zend-servicemanager v3 only)
4370
*/
44-
public function __construct(ConfigInterface $configuration = null)
71+
public function __construct($configOrContainer = null, array $v3config = [])
4572
{
46-
parent::__construct($configuration);
47-
48-
$this->addInitializer([$this, 'populateFactory']);
73+
$this->initializers[] = [$this, 'populateFactory'];
74+
parent::__construct($configOrContainer, $v3config);
4975
}
5076

5177
/**
5278
* Inject this and populate the factory with filter chain and validator chain
5379
*
54-
* @param $inputFilter
80+
* @param mixed $first
81+
* @param mixed $second
5582
*/
56-
public function populateFactory($inputFilter)
83+
public function populateFactory($first, $second)
5784
{
85+
if ($first instanceof ContainerInterface) {
86+
$container = $first;
87+
$inputFilter = $second;
88+
} else {
89+
$container = $second;
90+
$inputFilter = $first;
91+
}
5892
if ($inputFilter instanceof InputFilter) {
5993
$factory = $inputFilter->getFactory();
6094

6195
$factory->setInputFilterManager($this);
96+
}
97+
}
6298

63-
if ($this->serviceLocator instanceof ServiceLocatorInterface) {
64-
$factory->getDefaultFilterChain()->setPluginManager($this->serviceLocator->get('FilterManager'));
65-
$factory->getDefaultValidatorChain()->setPluginManager($this->serviceLocator->get('ValidatorManager'));
66-
}
99+
/**
100+
* Populate the filter and validator managers for the default filter/validator chains.
101+
*
102+
* @param Factory $factory
103+
* @return void
104+
*/
105+
public function populateFactoryPluginManagers(Factory $factory)
106+
{
107+
$container = property_exists($this, 'creationContext')
108+
? $this->creationContext // v3
109+
: $this->serviceLocator; // v2
110+
111+
if ($container && $container->has('FilterManager')) {
112+
$factory->getDefaultFilterChain()->setPluginManager($container->get('FilterManager'));
113+
}
114+
115+
if ($container && $container->has('ValidatorManager')) {
116+
$factory->getDefaultValidatorChain()->setPluginManager($container->get('ValidatorManager'));
67117
}
68118
}
69119

70120
/**
71-
* {@inheritDoc}
121+
* {@inheritDoc} (v3)
72122
*/
73-
public function validatePlugin($plugin)
123+
public function validate($plugin)
74124
{
75125
if ($plugin instanceof InputFilterInterface || $plugin instanceof InputInterface) {
76126
// Hook to perform various initialization, when the inputFilter is not created through the factory
@@ -82,11 +132,30 @@ public function validatePlugin($plugin)
82132
return;
83133
}
84134

85-
throw new Exception\RuntimeException(sprintf(
135+
throw new InvalidServiceException(sprintf(
86136
'Plugin of type %s is invalid; must implement %s or %s',
87137
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
88138
InputFilterInterface::class,
89139
InputInterface::class
90140
));
91141
}
142+
143+
/**
144+
* Validate the plugin (v2)
145+
*
146+
* Checks that the filter loaded is either a valid callback or an instance
147+
* of FilterInterface.
148+
*
149+
* @param mixed $plugin
150+
* @return void
151+
* @throws Exception\RuntimeException if invalid
152+
*/
153+
public function validatePlugin($plugin)
154+
{
155+
try {
156+
$this->validate($plugin);
157+
} catch (InvalidServiceException $e) {
158+
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
159+
}
160+
}
92161
}

0 commit comments

Comments
 (0)