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

Commit

Permalink
Merge branch 'feature/95' into develop
Browse files Browse the repository at this point in the history
Close #96
Close #95
Close #86
  • Loading branch information
weierophinney committed Feb 18, 2016
2 parents 23dbe80 + 47233a7 commit 8b90a42
Show file tree
Hide file tree
Showing 10 changed files with 442 additions and 149 deletions.
19 changes: 17 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ matrix:
- php: 5.5
env:
- EXECUTE_CS_CHECK=true
- php: 5.5
env:
- SERVICE_MANAGER_VERSION="^2.7.5"
- php: 5.6
env:
- EXECUTE_TEST_COVERALLS=true
- php: 5.6
env:
- SERVICE_MANAGER_VERSION="^2.7.5"
- php: 7
- php: hhvm
- php: 7
env:
- SERVICE_MANAGER_VERSION="^2.7.5"
- php: hhvm
- php: hhvm
env:
- SERVICE_MANAGER_VERSION="^2.7.5"
allow_failures:
- php: 7

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

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

script:
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi
Expand Down
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

## 2.6.0 - TBD
## 2.6.0 - 2016-02-18

### Added

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

### Fixed

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

## 2.5.6 - TBD

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
}
},
"require": {
"php": ">=5.5",
"zendframework/zend-filter": "~2.5",
"zendframework/zend-validator": "^2.5.3",
"zendframework/zend-stdlib": "~2.5"
"php": "^5.5 || ^7.0",
"zendframework/zend-filter": "^2.6",
"zendframework/zend-validator": "^2.6",
"zendframework/zend-stdlib": "^2.7 || ^3.0"
},
"require-dev": {
"zendframework/zend-servicemanager": "~2.5",
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "^4.5"
},
Expand Down
14 changes: 3 additions & 11 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use Traversable;
use Zend\Filter\FilterChain;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\ValidatorChain;
use Zend\Validator\ValidatorInterface;
Expand Down Expand Up @@ -117,15 +117,7 @@ public function clearDefaultValidatorChain()
public function setInputFilterManager(InputFilterPluginManager $inputFilterManager)
{
$this->inputFilterManager = $inputFilterManager;
$serviceLocator = $this->inputFilterManager->getServiceLocator();
if ($serviceLocator && $serviceLocator instanceof ServiceLocatorInterface) {
if ($serviceLocator->has('ValidatorManager')) {
$this->getDefaultValidatorChain()->setPluginManager($serviceLocator->get('ValidatorManager'));
}
if ($serviceLocator->has('FilterManager')) {
$this->getDefaultFilterChain()->setPluginManager($serviceLocator->get('FilterManager'));
}
}
$inputFilterManager->populateFactoryPluginManagers($this);
return $this;
}

Expand All @@ -135,7 +127,7 @@ public function setInputFilterManager(InputFilterPluginManager $inputFilterManag
public function getInputFilterManager()
{
if (null === $this->inputFilterManager) {
$this->inputFilterManager = new InputFilterPluginManager;
$this->inputFilterManager = new InputFilterPluginManager(new ServiceManager());
}

return $this->inputFilterManager;
Expand Down
65 changes: 51 additions & 14 deletions src/InputFilterAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\InputFilter;

use Interop\Container\ContainerInterface;
use Zend\Filter\FilterPluginManager;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand All @@ -22,21 +23,34 @@ class InputFilterAbstractServiceFactory implements AbstractFactoryInterface
protected $factory;

/**
* @param ServiceLocatorInterface $inputFilters
* @param ContainerInterface $services
* @param string $rName
* @param array $options
* @return InputFilterInterface
*/
public function __invoke(ContainerInterface $services, $rName, array $options = null)
{
$allConfig = $services->get('config');
$config = $allConfig['input_filter_specs'][$rName];
$factory = $this->getInputFilterFactory($services);

return $factory->createInputFilter($config);
}

/**
*
* @param ContainerInterface $services
* @param string $cName
* @param string $rName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
public function canCreate(ContainerInterface $services, $rName)
{
$services = $inputFilters->getServiceLocator();
if (! $services instanceof ServiceLocatorInterface
|| ! $services->has('Config')
) {
if (! $services->has('config')) {
return false;
}

$config = $services->get('Config');
$config = $services->get('config');
if (!isset($config['input_filter_specs'][$rName])
|| !is_array($config['input_filter_specs'][$rName])
) {
Expand All @@ -46,6 +60,27 @@ public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters,
return true;
}

/**
* Determine if we can create a service with name (v2)
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
// v2 => need to get parent service locator
$services = $serviceLocator->getServiceLocator();

// No parent locator => cannot create service.
if (! $services) {
return false;
}

return $this->canCreate($services, $requestedName);
}

/**
* @param ServiceLocatorInterface $inputFilters
* @param string $cName
Expand All @@ -54,13 +89,15 @@ public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters,
*/
public function createServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
{
$services = $inputFilters->getServiceLocator();
$allConfig = $services->get('Config');
$config = $allConfig['input_filter_specs'][$rName];
// v2 => need to get parent service locator
$services = $inputFilters->getServiceLocator();

$factory = $this->getInputFilterFactory($services);
// No parent locator => cannot create service.
if (! $services) {
return false;
}

return $factory->createInputFilter($config);
return $this($services, $rName);
}

/**
Expand Down Expand Up @@ -94,7 +131,7 @@ protected function getFilterPluginManager(ServiceLocatorInterface $services)
return $services->get('FilterManager');
}

return new FilterPluginManager();
return new FilterPluginManager($services);
}

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

return new ValidatorPluginManager();
return new ValidatorPluginManager($services);
}
}
107 changes: 88 additions & 19 deletions src/InputFilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

namespace Zend\InputFilter;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\Exception\InvalidServiceException;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\Stdlib\InitializableInterface;

/**
Expand All @@ -22,55 +23,104 @@
class InputFilterPluginManager extends AbstractPluginManager
{
/**
* Default set of plugins
* Default alias of plugins
*
* @var string[]
*/
protected $invokableClasses = [
protected $aliases = [
'inputfilter' => InputFilter::class,
'inputFilter' => InputFilter::class,
'InputFilter' => InputFilter::class,
'collection' => CollectionInputFilter::class,
'Collection' => CollectionInputFilter::class,
];

/**
* Whether or not to share by default
* Default set of plugins
*
* @var string[]
*/
protected $factories = [
InputFilter::class => InvokableFactory::class,
CollectionInputFilter::class => InvokableFactory::class,
// v2 canonical FQCN
'zendinputfilterinputfilter' => InvokableFactory::class,
'zendinputfiltercollectioninputfilter' => InvokableFactory::class,
];

/**
* Whether or not to share by default (v3)
*
* @var bool
*/
protected $sharedByDefault = false;

/**
* Whether or not to share by default (v2)
*
* @var bool
*/
protected $shareByDefault = false;

/**
* @param ConfigInterface $configuration
* @param null|\Zend\ServiceManager\ConfigInterface|ContainerInterface $configOrContainer
* For zend-servicemanager v2, null or a ConfigInterface instance are
* allowed; for v3, a ContainerInterface is expected.
* @param array $v3config Optional configuration array (zend-servicemanager v3 only)
*/
public function __construct(ConfigInterface $configuration = null)
public function __construct($configOrContainer = null, array $v3config = [])
{
parent::__construct($configuration);

$this->addInitializer([$this, 'populateFactory']);
$this->initializers[] = [$this, 'populateFactory'];
parent::__construct($configOrContainer, $v3config);
}

/**
* Inject this and populate the factory with filter chain and validator chain
*
* @param $inputFilter
* @param mixed $first
* @param mixed $second
*/
public function populateFactory($inputFilter)
public function populateFactory($first, $second)
{
if ($first instanceof ContainerInterface) {
$container = $first;
$inputFilter = $second;
} else {
$container = $second;
$inputFilter = $first;
}
if ($inputFilter instanceof InputFilter) {
$factory = $inputFilter->getFactory();

$factory->setInputFilterManager($this);
}
}

if ($this->serviceLocator instanceof ServiceLocatorInterface) {
$factory->getDefaultFilterChain()->setPluginManager($this->serviceLocator->get('FilterManager'));
$factory->getDefaultValidatorChain()->setPluginManager($this->serviceLocator->get('ValidatorManager'));
}
/**
* Populate the filter and validator managers for the default filter/validator chains.
*
* @param Factory $factory
* @return void
*/
public function populateFactoryPluginManagers(Factory $factory)
{
$container = property_exists($this, 'creationContext')
? $this->creationContext // v3
: $this->serviceLocator; // v2

if ($container && $container->has('FilterManager')) {
$factory->getDefaultFilterChain()->setPluginManager($container->get('FilterManager'));
}

if ($container && $container->has('ValidatorManager')) {
$factory->getDefaultValidatorChain()->setPluginManager($container->get('ValidatorManager'));
}
}

/**
* {@inheritDoc}
* {@inheritDoc} (v3)
*/
public function validatePlugin($plugin)
public function validate($plugin)
{
if ($plugin instanceof InputFilterInterface || $plugin instanceof InputInterface) {
// Hook to perform various initialization, when the inputFilter is not created through the factory
Expand All @@ -82,11 +132,30 @@ public function validatePlugin($plugin)
return;
}

throw new Exception\RuntimeException(sprintf(
throw new InvalidServiceException(sprintf(
'Plugin of type %s is invalid; must implement %s or %s',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
InputFilterInterface::class,
InputInterface::class
));
}

/**
* Validate the plugin (v2)
*
* Checks that the filter loaded is either a valid callback or an instance
* of FilterInterface.
*
* @param mixed $plugin
* @return void
* @throws Exception\RuntimeException if invalid
*/
public function validatePlugin($plugin)
{
try {
$this->validate($plugin);
} catch (InvalidServiceException $e) {
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
}
}
}
Loading

0 comments on commit 8b90a42

Please sign in to comment.