Skip to content

Commit da39c3c

Browse files
author
Adam Grabek
committed
ZF3 support
1 parent 564b422 commit da39c3c

9 files changed

+106
-57
lines changed

Module.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66

77
namespace ZF\ApiProblem;
88

9+
use Zend\EventManager\EventManagerInterface;
910
use Zend\Mvc\ResponseSender\SendResponseEvent;
1011
use Zend\Mvc\MvcEvent;
12+
use Zend\Mvc\SendResponseListener;
13+
use Zend\ServiceManager\ServiceLocatorInterface;
14+
use ZF\ApiProblem\Listener\ApiProblemListener;
15+
use ZF\ApiProblem\Listener\SendApiProblemResponseListener;
1116

1217
/**
1318
* ZF2 module
@@ -21,11 +26,11 @@ class Module
2126
*/
2227
public function getAutoloaderConfig()
2328
{
24-
return array(
25-
'Zend\Loader\StandardAutoloader' => array('namespaces' => array(
29+
return [
30+
'Zend\Loader\StandardAutoloader' => ['namespaces' => [
2631
__NAMESPACE__ => __DIR__ . '/src/',
27-
))
28-
);
32+
]],
33+
];
2934
}
3035

3136
/**
@@ -47,17 +52,25 @@ public function getConfig()
4752
*/
4853
public function onBootstrap($e)
4954
{
50-
$app = $e->getTarget();
55+
$app = $e->getTarget();
56+
/** @var ServiceLocatorInterface $serviceManager */
5157
$serviceManager = $app->getServiceManager();
52-
$eventManager = $app->getEventManager();
53-
54-
$eventManager->attach($serviceManager->get('ZF\ApiProblem\ApiProblemListener'));
55-
$eventManager->attach(MvcEvent::EVENT_RENDER, array($this, 'onRender'), 100);
58+
/** @var EventManagerInterface $eventManager */
59+
$eventManager = $app->getEventManager();
5660

61+
/** @var ApiProblemListener $apiProblemListener */
62+
$apiProblemListener = $serviceManager->get(ApiProblemListener::class);
63+
/** @var SendResponseListener $sendResponseListener */
5764
$sendResponseListener = $serviceManager->get('SendResponseListener');
65+
66+
67+
68+
$apiProblemListener->attach($eventManager);
69+
$eventManager->attach(MvcEvent::EVENT_RENDER, [$this, 'onRender'], 100);
70+
5871
$sendResponseListener->getEventManager()->attach(
5972
SendResponseEvent::EVENT_SEND_RESPONSE,
60-
$serviceManager->get('ZF\ApiProblem\Listener\SendApiProblemResponseListener'),
73+
$serviceManager->get(SendApiProblemResponseListener::class),
6174
-500
6275
);
6376
}
@@ -71,11 +84,11 @@ public function onBootstrap($e)
7184
*/
7285
public function onRender($e)
7386
{
74-
$app = $e->getTarget();
87+
$app = $e->getTarget();
7588
$services = $app->getServiceManager();
7689

7790
if ($services->has('View')) {
78-
$view = $services->get('View');
91+
$view = $services->get('View');
7992
$events = $view->getEventManager();
8093

8194
// register at high priority, to "beat" normal json strategy registered

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727
}
2828
},
2929
"require": {
30-
"php": ">=5.5",
31-
"zendframework/zend-eventmanager": "~2.3",
32-
"zendframework/zend-http": "~2.3",
33-
"zendframework/zend-json": "~2.3",
34-
"zendframework/zend-mvc": "~2.3",
35-
"zendframework/zend-view": "~2.3"
30+
"php": ">=5.6",
31+
"zendframework/zend-eventmanager": "3.0.*",
32+
"zendframework/zend-http": "2.5.*",
33+
"zendframework/zend-json": "2.6.*",
34+
"zendframework/zend-mvc": "3.0.*",
35+
"zendframework/zend-view": "2.7.*"
3636
},
3737
"require-dev": {
3838
"phpunit/phpunit": "~4.7",
39-
"zendframework/zend-console": "~2.3",
40-
"zendframework/zend-loader": "~2.3",
39+
"zendframework/zend-console": "2.6.*",
40+
"zendframework/zend-loader": "2.5.*",
4141
"squizlabs/php_codesniffer": "^2.3.1"
4242
},
4343
"autoload": {

src/Factory/ApiProblemListenerFactory.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@
66

77
namespace ZF\ApiProblem\Factory;
88

9-
use Zend\ServiceManager\FactoryInterface;
9+
use Interop\Container\ContainerInterface;
10+
11+
use Zend\ServiceManager\Factory\FactoryInterface;
1012
use Zend\ServiceManager\ServiceLocatorInterface;
1113
use ZF\ApiProblem\Listener\ApiProblemListener;
1214

1315
class ApiProblemListenerFactory implements FactoryInterface
1416
{
17+
1518
/**
16-
* {@inheritDoc}
17-
* @return ApiProblemListener
19+
* @param \Interop\Container\ContainerInterface $container
20+
* @param string $requestedName
21+
* @param array|NULL $options
22+
*
23+
* @return \ZF\ApiProblem\Listener\ApiProblemListener
1824
*/
19-
public function createService(ServiceLocatorInterface $serviceLocator)
25+
public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
2026
{
2127
$filters = null;
2228
$config = [];
2329

24-
if ($serviceLocator->has('Config')) {
25-
$config = $serviceLocator->get('Config');
30+
if ($container->has('Config')) {
31+
$config = $container->get('Config');
2632
}
2733

2834
if (isset($config['zf-api-problem']['accept_filters'])) {
@@ -31,4 +37,6 @@ public function createService(ServiceLocatorInterface $serviceLocator)
3137

3238
return new ApiProblemListener($filters);
3339
}
40+
41+
3442
}

src/Factory/ApiProblemRendererFactory.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@
66

77
namespace ZF\ApiProblem\Factory;
88

9-
use Zend\ServiceManager\FactoryInterface;
10-
use Zend\ServiceManager\ServiceLocatorInterface;
9+
use Interop\Container\ContainerInterface;
10+
use Zend\ServiceManager\Factory\FactoryInterface;
1111
use ZF\ApiProblem\View\ApiProblemRenderer;
1212

13+
/**
14+
* Class ApiProblemRendererFactory
15+
*
16+
* @package ZF\ApiProblem\Factory
17+
*/
1318
class ApiProblemRendererFactory implements FactoryInterface
1419
{
1520
/**
16-
* {@inheritDoc}
17-
* @return ApiProblemRenderer
21+
* @param \Interop\Container\ContainerInterface $container
22+
* @param string $requestedName
23+
* @param array|NULL $options
24+
*
25+
* @return \ZF\ApiProblem\View\ApiProblemRenderer
1826
*/
19-
public function createService(ServiceLocatorInterface $serviceLocator)
27+
public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
2028
{
21-
$config = $serviceLocator->get('Config');
29+
$config = $container->get('Config');
2230
$displayExceptions = isset($config['view_manager'])
2331
&& isset($config['view_manager']['display_exceptions'])
2432
&& $config['view_manager']['display_exceptions'];
@@ -28,4 +36,6 @@ public function createService(ServiceLocatorInterface $serviceLocator)
2836

2937
return $renderer;
3038
}
39+
40+
3141
}

src/Factory/ApiProblemStrategyFactory.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,28 @@
66

77
namespace ZF\ApiProblem\Factory;
88

9-
use Zend\ServiceManager\FactoryInterface;
10-
use Zend\ServiceManager\ServiceLocatorInterface;
9+
use Interop\Container\ContainerInterface;
10+
use Zend\ServiceManager\Factory\FactoryInterface;
11+
use ZF\ApiProblem\View\ApiProblemRenderer;
1112
use ZF\ApiProblem\View\ApiProblemStrategy;
1213

14+
/**
15+
* Class ApiProblemStrategyFactory
16+
*
17+
* @package ZF\ApiProblem\Factory
18+
*/
1319
class ApiProblemStrategyFactory implements FactoryInterface
1420
{
1521
/**
16-
* {@inheritDoc}
17-
* @return ApiProblemStrategy
22+
* @param \Interop\Container\ContainerInterface $container
23+
* @param string $requestedName
24+
* @param array|NULL $options
25+
*
26+
* @return \ZF\ApiProblem\View\ApiProblemStrategy
1827
*/
19-
public function createService(ServiceLocatorInterface $serviceLocator)
28+
public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
2029
{
21-
return new ApiProblemStrategy($serviceLocator->get('ZF\ApiProblem\ApiProblemRenderer'));
30+
return new ApiProblemStrategy($container->get(ApiProblemRenderer::class));
2231
}
32+
2333
}

src/Factory/RenderErrorListenerFactory.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@
66

77
namespace ZF\ApiProblem\Factory;
88

9-
use Zend\ServiceManager\FactoryInterface;
10-
use Zend\ServiceManager\ServiceLocatorInterface;
9+
use Interop\Container\ContainerInterface;
10+
use Zend\ServiceManager\Factory\FactoryInterface;
1111
use ZF\ApiProblem\Listener\RenderErrorListener;
1212

13+
/**
14+
* Class RenderErrorListenerFactory
15+
*
16+
* @package ZF\ApiProblem\Factory
17+
*/
1318
class RenderErrorListenerFactory implements FactoryInterface
1419
{
1520
/**
16-
* {@inheritDoc}
17-
* @return RenderErrorListener
21+
* @param \Interop\Container\ContainerInterface $container
22+
* @param string $requestedName
23+
* @param array|NULL $options
24+
*
25+
* @return \ZF\ApiProblem\Listener\RenderErrorListener
1826
*/
19-
public function createService(ServiceLocatorInterface $serviceLocator)
27+
public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
2028
{
21-
$config = $serviceLocator->get('Config');
29+
$config = $container->get('Config');
2230
$displayExceptions = false;
2331

2432
if (isset($config['view_manager'])
@@ -32,4 +40,5 @@ public function createService(ServiceLocatorInterface $serviceLocator)
3240

3341
return $listener;
3442
}
43+
3544
}

src/Factory/SendApiProblemResponseListenerFactory.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,31 @@
66

77
namespace ZF\ApiProblem\Factory;
88

9+
use Interop\Container\ContainerInterface;
910
use Zend\Http\Response as HttpResponse;
10-
use Zend\ServiceManager\FactoryInterface;
11-
use Zend\ServiceManager\ServiceLocatorInterface;
11+
use Zend\ServiceManager\Factory\FactoryInterface;
1212
use ZF\ApiProblem\Listener\SendApiProblemResponseListener;
1313

1414
class SendApiProblemResponseListenerFactory implements FactoryInterface
1515
{
16-
/**
17-
* {@inheritDoc}
18-
* @return SendApiProblemResponseListener
19-
*/
20-
public function createService(ServiceLocatorInterface $serviceLocator)
16+
public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
2117
{
22-
$config = $serviceLocator->get('Config');
18+
$config = $container->get('Config');
2319
$displayExceptions = isset($config['view_manager'])
2420
&& isset($config['view_manager']['display_exceptions'])
2521
&& $config['view_manager']['display_exceptions'];
2622

2723
$listener = new SendApiProblemResponseListener();
2824
$listener->setDisplayExceptions($displayExceptions);
2925

30-
if ($serviceLocator->has('Response')) {
31-
$response = $serviceLocator->get('Response');
26+
if ($container->has('Response')) {
27+
$response = $container->get('Response');
3228
if ($response instanceof HttpResponse) {
3329
$listener->setApplicationResponse($response);
3430
}
3531
}
3632

3733
return $listener;
3834
}
35+
3936
}

src/Listener/ApiProblemListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ public function __construct($filters = null)
5858
}
5959

6060
/**
61-
* @param EventManagerInterface $events
61+
* @param EventManagerInterface $events
62+
* @param int $priority
6263
*/
63-
public function attach(EventManagerInterface $events)
64+
public function attach(EventManagerInterface $events, $priority = 1)
6465
{
6566
$this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER, [$this, 'onRender'], 1000);
6667
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'onDispatchError'], 100);

src/Listener/RenderErrorListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ class RenderErrorListener extends AbstractListenerAggregate
2525

2626
/**
2727
* @param EventManagerInterface $events
28+
* @param int $priority
2829
*/
29-
public function attach(EventManagerInterface $events)
30+
public function attach(EventManagerInterface $events, $priority = 1)
3031
{
3132
$this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER_ERROR, [$this, 'onRenderError'], 100);
3233
}

0 commit comments

Comments
 (0)