From 4f79ce529976a90f7795708f913a55cc40f23b28 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Wed, 23 Mar 2016 13:21:31 +0200 Subject: [PATCH] Added ServiceManager v3 and EventManager v3 support --- .travis.yml | 10 +++++++--- composer.json | 19 ++++++++++++------- src/ZfrCors/Factory/CorsOptionsFactory.php | 17 +++++++++++++++-- .../Factory/CorsRequestListenerFactory.php | 15 +++++++++++++-- src/ZfrCors/Factory/CorsServiceFactory.php | 14 ++++++++++++-- src/ZfrCors/Module.php | 4 +++- src/ZfrCors/Mvc/CorsRequestListener.php | 2 +- tests/ZfrCorsTest/ModuleTest.php | 5 +---- .../Util/ServiceManagerFactory.php | 9 +++++---- 9 files changed, 69 insertions(+), 26 deletions(-) diff --git a/.travis.yml b/.travis.yml index d714d29..3d887f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,14 @@ language: php php: - - 5.3.3 - - 5.3 - - 5.4 - 5.5 + - 5.6 + - 7 + - hhvm + +matrix: + allow_failures: + - php: hhvm before_script: - composer self-update diff --git a/composer.json b/composer.json index acd1b80..c14f866 100644 --- a/composer.json +++ b/composer.json @@ -23,15 +23,20 @@ } ], "require": { - "php": ">=5.3.3", - "zendframework/zend-eventmanager": "~2.2", - "zendframework/zend-http": "~2.2", - "zendframework/zend-mvc": "~2.2", - "zendframework/zend-servicemanager": "~2.2" + "php": "^5.5 || ^7.0", + "zendframework/zend-eventmanager": "^2.6 || ^3.0", + "zendframework/zend-http": "^2.5.2", + "zendframework/zend-mvc": "^2.5.2", + "zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3" }, "require-dev": { - "zendframework/zendframework": "~2.2", - "phpunit/phpunit": "~3.7", + "zendframework/zend-modulemanager": "^2.6.1", + "zendframework/zend-config": "^2.6", + "zendframework/zend-view": "^2.5.2", + "zendframework/zend-serializer": "^2.6", + "zendframework/zend-log": "^2.5.2", + "zendframework/zend-i18n": "^2.6", + "phpunit/phpunit": "^4.8", "squizlabs/php_codesniffer": "1.4.*", "satooshi/php-coveralls": "~0.6" }, diff --git a/src/ZfrCors/Factory/CorsOptionsFactory.php b/src/ZfrCors/Factory/CorsOptionsFactory.php index 7caecef..96a2d9e 100644 --- a/src/ZfrCors/Factory/CorsOptionsFactory.php +++ b/src/ZfrCors/Factory/CorsOptionsFactory.php @@ -18,6 +18,7 @@ namespace ZfrCors\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfrCors\Options\CorsOptions; @@ -32,12 +33,24 @@ class CorsOptionsFactory implements FactoryInterface { /** * {@inheritDoc} + * + * @return CorsOptions */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var $config array */ - $config = $serviceLocator->get('Config'); + $config = $container->get('Config'); return new CorsOptions($config['zfr_cors']); } + + /** + * {@inheritDoc} + * + * @return CorsOptions + */ + public function createService(ServiceLocatorInterface $container) + { + return $this($container, CorsOptions::class); + } } diff --git a/src/ZfrCors/Factory/CorsRequestListenerFactory.php b/src/ZfrCors/Factory/CorsRequestListenerFactory.php index 5407643..d7b8a0a 100644 --- a/src/ZfrCors/Factory/CorsRequestListenerFactory.php +++ b/src/ZfrCors/Factory/CorsRequestListenerFactory.php @@ -18,6 +18,7 @@ namespace ZfrCors\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfrCors\Mvc\CorsRequestListener; @@ -33,13 +34,23 @@ class CorsRequestListenerFactory implements FactoryInterface { /** * {@inheritDoc} + * * @return CorsRequestListener */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var $corsService CorsService */ - $corsService = $serviceLocator->get('ZfrCors\Service\CorsService'); + $corsService = $container->get('ZfrCors\Service\CorsService'); return new CorsRequestListener($corsService); } + /** + * {@inheritDoc} + * + * @return CorsRequestListener + */ + public function createService(ServiceLocatorInterface $container) + { + return $this($container, CorsRequestListener::class); + } } diff --git a/src/ZfrCors/Factory/CorsServiceFactory.php b/src/ZfrCors/Factory/CorsServiceFactory.php index a72588f..1b2d542 100644 --- a/src/ZfrCors/Factory/CorsServiceFactory.php +++ b/src/ZfrCors/Factory/CorsServiceFactory.php @@ -18,6 +18,7 @@ namespace ZfrCors\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfrCors\Options\CorsOptions; @@ -35,11 +36,20 @@ class CorsServiceFactory implements FactoryInterface * {@inheritDoc} * @return CorsService */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var $corsOptions CorsOptions */ - $corsOptions = $serviceLocator->get('ZfrCors\Options\CorsOptions'); + $corsOptions = $container->get('ZfrCors\Options\CorsOptions'); return new CorsService($corsOptions); } + /** + * {@inheritDoc} + * + * @return CorsService + */ + public function createService(ServiceLocatorInterface $container) + { + return $this($container, CorsOptions::class); + } } diff --git a/src/ZfrCors/Module.php b/src/ZfrCors/Module.php index ba2a0d0..4a6e2ac 100644 --- a/src/ZfrCors/Module.php +++ b/src/ZfrCors/Module.php @@ -39,7 +39,9 @@ public function onBootstrap(EventInterface $event) $serviceManager = $application->getServiceManager(); $eventManager = $application->getEventManager(); - $eventManager->attach($serviceManager->get('ZfrCors\Mvc\CorsRequestListener')); + /** @var \ZfrCors\Mvc\CorsRequestListener $listener */ + $listener = $serviceManager->get('ZfrCors\Mvc\CorsRequestListener'); + $listener->attach($eventManager); } /** diff --git a/src/ZfrCors/Mvc/CorsRequestListener.php b/src/ZfrCors/Mvc/CorsRequestListener.php index cc3052b..95ecd01 100644 --- a/src/ZfrCors/Mvc/CorsRequestListener.php +++ b/src/ZfrCors/Mvc/CorsRequestListener.php @@ -57,7 +57,7 @@ public function __construct(CorsService $corsService) /** * {@inheritDoc} */ - public function attach(EventManagerInterface $events) + public function attach(EventManagerInterface $events, $priority = 1) { // Preflight can be handled during the route event, and should return early $this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, array($this, 'onCorsPreflight'), -1); diff --git a/tests/ZfrCorsTest/ModuleTest.php b/tests/ZfrCorsTest/ModuleTest.php index baff2af..20cd11c 100644 --- a/tests/ZfrCorsTest/ModuleTest.php +++ b/tests/ZfrCorsTest/ModuleTest.php @@ -19,9 +19,6 @@ namespace ZfrCorsTest; use PHPUnit_Framework_TestCase; -use Zend\Mvc\Application; -use Zend\Mvc\MvcEvent; -use Zend\ServiceManager\ServiceManager; use ZfrCors\Module; /** @@ -66,7 +63,7 @@ public function testAssertListenerIsCorrectlyRegistered() ->with('ZfrCors\Mvc\CorsRequestListener') ->will($this->returnValue($corsListener)); - $eventManager->expects($this->once())->method('attach')->with($corsListener); + $corsListener->expects($this->once())->method('attach')->with($eventManager); $module->onBootstrap($mvcEvent); } diff --git a/tests/ZfrCorsTest/Util/ServiceManagerFactory.php b/tests/ZfrCorsTest/Util/ServiceManagerFactory.php index 818a1c7..6fa7e05 100644 --- a/tests/ZfrCorsTest/Util/ServiceManagerFactory.php +++ b/tests/ZfrCorsTest/Util/ServiceManagerFactory.php @@ -62,11 +62,12 @@ public static function getApplicationConfig() public static function getServiceManager(array $config = null) { $config = $config ?: static::getApplicationConfig(); - $serviceManager = new ServiceManager( - new ServiceManagerConfig( - isset($config['service_manager']) ? $config['service_manager'] : array() - ) + $serviceManager = new ServiceManager(); + $serviceManagerConfig = new ServiceManagerConfig( + isset($config['service_manager']) ? $config['service_manager'] : array() ); + $serviceManagerConfig->configureServiceManager($serviceManager); + $serviceManager->setService('ApplicationConfig', $config); /* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */