Skip to content

Commit

Permalink
trully refactor to use e->stopPropagation(true) in Mvc listener
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed May 15, 2018
1 parent ee1be66 commit 9e02d19
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 44 deletions.
Expand Up @@ -34,8 +34,6 @@

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

$db = $serviceManager->get(Adapter::class);
$tableGateway = new TableGateway('log', $db, null, new ResultSet());
Expand Down
Expand Up @@ -33,8 +33,6 @@

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

$entityManager = $serviceManager->get(EntityManager::class);
$stmt = $entityManager->getConnection()->prepare('DELETE FROM log');
Expand Down
Expand Up @@ -27,11 +27,6 @@
],
]);

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

return $application;

});
Expand Down
Expand Up @@ -27,11 +27,6 @@
],
]);

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

return $application;

});
Expand Down
Expand Up @@ -29,11 +29,6 @@
],
]);

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

return $application;

});
Expand Down
Expand Up @@ -29,11 +29,6 @@
],
]);

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

return $application;

});
Expand Down
2 changes: 0 additions & 2 deletions spec/Integration/IntegrationViaErrorPreviewControllerSpec.php
Expand Up @@ -32,8 +32,6 @@

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

$db = $serviceManager->get(Adapter::class);
$tableGateway = new TableGateway('log', $db, null, new ResultSet());
Expand Down
Expand Up @@ -33,8 +33,6 @@

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

$db = $serviceManager->get(Adapter::class);
$tableGateway = new TableGateway('log', $db, null, new ResultSet());
Expand Down
Expand Up @@ -11,7 +11,7 @@
use Zend\Db\TableGateway\TableGateway;
use Zend\Mvc\Application;

describe('Integration via ErrorPreviewController with enable send mail with emtpy receiver', function () {
describe('Integration via ErrorPreviewController with enable send mail with empty receiver', function () {

given('application', function () {

Expand All @@ -33,8 +33,6 @@

$events = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$serviceManager->get('SendResponseListener')
->detach($events);

$db = $serviceManager->get(Adapter::class);
$tableGateway = new TableGateway('log', $db, null, new ResultSet());
Expand Down
26 changes: 13 additions & 13 deletions src/Listener/Mvc.php
Expand Up @@ -10,12 +10,12 @@
use Zend\Console\Console;
use Zend\Console\Response as ConsoleResponse;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\Event;
use Zend\EventManager\EventManagerInterface;
use Zend\Http\PhpEnvironment\Request;
use Zend\Http\PhpEnvironment\Response as HttpResponse;
use Zend\Http\PhpEnvironment\Response;
use Zend\Mvc\MvcEvent;
use Zend\Text\Table;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
use Zend\View\Renderer\PhpRenderer;

Expand Down Expand Up @@ -47,13 +47,13 @@ public function attach(EventManagerInterface $events, $priority = 1) : void
$this->listeners[] = $events->attach(MvcEvent::EVENT_BOOTSTRAP, [$this, 'phpError']);
}

public function phpError(Event $e)
public function phpError(MvcEvent $e)
{
\register_shutdown_function([$this, 'execOnShutdown']);
\set_error_handler([$this, 'phpErrorHandler']);
}

public function exceptionError(Event $e) : void
public function exceptionError(MvcEvent $e) : void
{
$exception = $e->getParam('exception');
if (! $exception) {
Expand Down Expand Up @@ -83,13 +83,16 @@ public function exceptionError(Event $e) : void
/**
* It show default view if display_errors setting = 0.
*/
private function showDefaultViewWhenDisplayErrorSetttingIsDisabled(Event $e) : void
private function showDefaultViewWhenDisplayErrorSetttingIsDisabled(MvcEvent $e) : void
{
if (! Console::isConsole()) {
$response = new HttpResponse();
$response = $e->getResponse();
Assertion::isInstanceOf($response, Response::class);
$response->setStatusCode(500);

$request = new Request();
$request = $e->getRequest();
Assertion::isInstanceOf($request, Request::class);

$isXmlHttpRequest = $request->isXmlHttpRequest();
if ($isXmlHttpRequest === true &&
isset($this->errorHeroModuleConfig['display-settings']['ajax']['message'])
Expand All @@ -98,9 +101,9 @@ private function showDefaultViewWhenDisplayErrorSetttingIsDisabled(Event $e) : v
$contentType = $this->detectAjaxMessageContentType($message);

$response->getHeaders()->addHeaderLine('Content-type', $contentType);
$response->sendHeaders();
echo $message;
$response->setContent($message);

$e->setViewModel($contentType === 'application/problem+json' ? new JsonModel() : new ViewModel());
$e->stopPropagation(true);
return;
}
Expand All @@ -114,10 +117,7 @@ private function showDefaultViewWhenDisplayErrorSetttingIsDisabled(Event $e) : v
$layout->setTemplate($this->errorHeroModuleConfig['display-settings']['template']['layout']);
$layout->setVariable('content', $this->renderer->render($view));

$response->getHeaders()->addHeaderLine('Content-type', 'text/html');
$response->sendHeaders();
echo $this->renderer->render($layout);

$e->setViewModel($layout);
$e->stopPropagation(true);
return;
}
Expand Down

0 comments on commit 9e02d19

Please sign in to comment.