Skip to content

Commit

Permalink
Added ServiceManager v3 and EventManager v3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
svycka committed Mar 23, 2016
1 parent 37ddd30 commit 4f79ce5
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 26 deletions.
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 12 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
17 changes: 15 additions & 2 deletions src/ZfrCors/Factory/CorsOptionsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace ZfrCors\Factory;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfrCors\Options\CorsOptions;
Expand All @@ -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);
}
}
15 changes: 13 additions & 2 deletions src/ZfrCors/Factory/CorsRequestListenerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace ZfrCors\Factory;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfrCors\Mvc\CorsRequestListener;
Expand All @@ -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);
}
}
14 changes: 12 additions & 2 deletions src/ZfrCors/Factory/CorsServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace ZfrCors\Factory;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfrCors\Options\CorsOptions;
Expand All @@ -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);
}
}
4 changes: 3 additions & 1 deletion src/ZfrCors/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ZfrCors/Mvc/CorsRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions tests/ZfrCorsTest/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 5 additions & 4 deletions tests/ZfrCorsTest/Util/ServiceManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 4f79ce5

Please sign in to comment.