Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ZF3 support
  • Loading branch information
Adam Grabek committed Jun 7, 2016
commit da39c3c7cbca55f14533598eb331edcd7c5c0375
37 changes: 25 additions & 12 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@

namespace ZF\ApiProblem;

use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\ResponseSender\SendResponseEvent;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\SendResponseListener;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZF\ApiProblem\Listener\ApiProblemListener;
use ZF\ApiProblem\Listener\SendApiProblemResponseListener;

/**
* ZF2 module
Expand All @@ -21,11 +26,11 @@ class Module
*/
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array('namespaces' => array(
return [
'Zend\Loader\StandardAutoloader' => ['namespaces' => [
__NAMESPACE__ => __DIR__ . '/src/',
))
);
]],
];
}

/**
Expand All @@ -47,17 +52,25 @@ public function getConfig()
*/
public function onBootstrap($e)
{
$app = $e->getTarget();
$app = $e->getTarget();
/** @var ServiceLocatorInterface $serviceManager */
$serviceManager = $app->getServiceManager();
$eventManager = $app->getEventManager();

$eventManager->attach($serviceManager->get('ZF\ApiProblem\ApiProblemListener'));
$eventManager->attach(MvcEvent::EVENT_RENDER, array($this, 'onRender'), 100);
/** @var EventManagerInterface $eventManager */
$eventManager = $app->getEventManager();

/** @var ApiProblemListener $apiProblemListener */
$apiProblemListener = $serviceManager->get(ApiProblemListener::class);
/** @var SendResponseListener $sendResponseListener */
$sendResponseListener = $serviceManager->get('SendResponseListener');



$apiProblemListener->attach($eventManager);
$eventManager->attach(MvcEvent::EVENT_RENDER, [$this, 'onRender'], 100);

$sendResponseListener->getEventManager()->attach(
SendResponseEvent::EVENT_SEND_RESPONSE,
$serviceManager->get('ZF\ApiProblem\Listener\SendApiProblemResponseListener'),
$serviceManager->get(SendApiProblemResponseListener::class),
-500
);
}
Expand All @@ -71,11 +84,11 @@ public function onBootstrap($e)
*/
public function onRender($e)
{
$app = $e->getTarget();
$app = $e->getTarget();
$services = $app->getServiceManager();

if ($services->has('View')) {
$view = $services->get('View');
$view = $services->get('View');
$events = $view->getEventManager();

// register at high priority, to "beat" normal json strategy registered
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
}
},
"require": {
"php": ">=5.5",
"zendframework/zend-eventmanager": "~2.3",
"zendframework/zend-http": "~2.3",
"zendframework/zend-json": "~2.3",
"zendframework/zend-mvc": "~2.3",
"zendframework/zend-view": "~2.3"
"php": ">=5.6",
"zendframework/zend-eventmanager": "3.0.*",
"zendframework/zend-http": "2.5.*",
"zendframework/zend-json": "2.6.*",
"zendframework/zend-mvc": "3.0.*",
"zendframework/zend-view": "2.7.*"
},
"require-dev": {
"phpunit/phpunit": "~4.7",
"zendframework/zend-console": "~2.3",
"zendframework/zend-loader": "~2.3",
"zendframework/zend-console": "2.6.*",
"zendframework/zend-loader": "2.5.*",
"squizlabs/php_codesniffer": "^2.3.1"
},
"autoload": {
Expand Down
20 changes: 14 additions & 6 deletions src/Factory/ApiProblemListenerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@

namespace ZF\ApiProblem\Factory;

use Zend\ServiceManager\FactoryInterface;
use Interop\Container\ContainerInterface;

use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZF\ApiProblem\Listener\ApiProblemListener;

class ApiProblemListenerFactory implements FactoryInterface
{

/**
* {@inheritDoc}
* @return ApiProblemListener
* @param \Interop\Container\ContainerInterface $container
* @param string $requestedName
* @param array|NULL $options
*
* @return \ZF\ApiProblem\Listener\ApiProblemListener
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
{
$filters = null;
$config = [];

if ($serviceLocator->has('Config')) {
$config = $serviceLocator->get('Config');
if ($container->has('Config')) {
$config = $container->get('Config');
}

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

return new ApiProblemListener($filters);
}


}
22 changes: 16 additions & 6 deletions src/Factory/ApiProblemRendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@

namespace ZF\ApiProblem\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use ZF\ApiProblem\View\ApiProblemRenderer;

/**
* Class ApiProblemRendererFactory
*
* @package ZF\ApiProblem\Factory
*/
class ApiProblemRendererFactory implements FactoryInterface
{
/**
* {@inheritDoc}
* @return ApiProblemRenderer
* @param \Interop\Container\ContainerInterface $container
* @param string $requestedName
* @param array|NULL $options
*
* @return \ZF\ApiProblem\View\ApiProblemRenderer
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
{
$config = $serviceLocator->get('Config');
$config = $container->get('Config');
$displayExceptions = isset($config['view_manager'])
&& isset($config['view_manager']['display_exceptions'])
&& $config['view_manager']['display_exceptions'];
Expand All @@ -28,4 +36,6 @@ public function createService(ServiceLocatorInterface $serviceLocator)

return $renderer;
}


}
22 changes: 16 additions & 6 deletions src/Factory/ApiProblemStrategyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@

namespace ZF\ApiProblem\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use ZF\ApiProblem\View\ApiProblemRenderer;
use ZF\ApiProblem\View\ApiProblemStrategy;

/**
* Class ApiProblemStrategyFactory
*
* @package ZF\ApiProblem\Factory
*/
class ApiProblemStrategyFactory implements FactoryInterface
{
/**
* {@inheritDoc}
* @return ApiProblemStrategy
* @param \Interop\Container\ContainerInterface $container
* @param string $requestedName
* @param array|NULL $options
*
* @return \ZF\ApiProblem\View\ApiProblemStrategy
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
{
return new ApiProblemStrategy($serviceLocator->get('ZF\ApiProblem\ApiProblemRenderer'));
return new ApiProblemStrategy($container->get(ApiProblemRenderer::class));
}

}
21 changes: 15 additions & 6 deletions src/Factory/RenderErrorListenerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@

namespace ZF\ApiProblem\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use ZF\ApiProblem\Listener\RenderErrorListener;

/**
* Class RenderErrorListenerFactory
*
* @package ZF\ApiProblem\Factory
*/
class RenderErrorListenerFactory implements FactoryInterface
{
/**
* {@inheritDoc}
* @return RenderErrorListener
* @param \Interop\Container\ContainerInterface $container
* @param string $requestedName
* @param array|NULL $options
*
* @return \ZF\ApiProblem\Listener\RenderErrorListener
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
{
$config = $serviceLocator->get('Config');
$config = $container->get('Config');
$displayExceptions = false;

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

return $listener;
}

}
17 changes: 7 additions & 10 deletions src/Factory/SendApiProblemResponseListenerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,31 @@

namespace ZF\ApiProblem\Factory;

use Interop\Container\ContainerInterface;
use Zend\Http\Response as HttpResponse;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use ZF\ApiProblem\Listener\SendApiProblemResponseListener;

class SendApiProblemResponseListenerFactory implements FactoryInterface
{
/**
* {@inheritDoc}
* @return SendApiProblemResponseListener
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
{
$config = $serviceLocator->get('Config');
$config = $container->get('Config');
$displayExceptions = isset($config['view_manager'])
&& isset($config['view_manager']['display_exceptions'])
&& $config['view_manager']['display_exceptions'];

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

if ($serviceLocator->has('Response')) {
$response = $serviceLocator->get('Response');
if ($container->has('Response')) {
$response = $container->get('Response');
if ($response instanceof HttpResponse) {
$listener->setApplicationResponse($response);
}
}

return $listener;
}

}
5 changes: 3 additions & 2 deletions src/Listener/ApiProblemListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ public function __construct($filters = null)
}

/**
* @param EventManagerInterface $events
* @param EventManagerInterface $events
* @param int $priority
*/
public function attach(EventManagerInterface $events)
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER, [$this, 'onRender'], 1000);
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'onDispatchError'], 100);
Expand Down
3 changes: 2 additions & 1 deletion src/Listener/RenderErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class RenderErrorListener extends AbstractListenerAggregate

/**
* @param EventManagerInterface $events
* @param int $priority
*/
public function attach(EventManagerInterface $events)
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER_ERROR, [$this, 'onRenderError'], 100);
}
Expand Down