Skip to content

Commit

Permalink
Merge branch 'non-zend-view-support'
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Apr 8, 2018
2 parents 3ca7a51 + fa7ba25 commit 072c3a7
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ return [

// if enable and display_errors = 0, the page will bring layout and view
'template' => [
// non zend-view (plates, twig) for expressive not need a layout definition
// as layout defined in the view
'layout' => 'layout/layout',
'view' => 'error-hero-module/error-default'
],
Expand Down
1 change: 1 addition & 0 deletions config/expressive-error-hero-module.local.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ return [

// if enable and display_errors = 0, the page will bring layout and view
'template' => [
// 'layout' config only when using Zend View Template Engine
'layout' => 'layout::default',
'view' => 'error-hero-module::error-default'
],
Expand Down
16 changes: 15 additions & 1 deletion spec/Handler/LoggingSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Zend\Db\Sql\Sql;
use Zend\Db\TableGateway\TableGateway;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;
use Zend\Http\PhpEnvironment\Request;
use Zend\Log\Logger;
use Zend\Log\Writer\Db as DbWriter;
Expand Down Expand Up @@ -120,7 +121,20 @@

it('set request and requestUri properties', function () {

$this->logging->setServerRequestandRequestUri(new ServerRequest([], [], $this->serverUrl . '/error-preview', 'GET'));
$request = new ServerRequest(
[],
[],
new Uri('http://example.com/error-preview'),
'GET',
'php://memory',
[],
[],
[],
'',
'1.2'
);

$this->logging->setServerRequestandRequestUri($request);

$r = new ReflectionProperty($this->logging, 'request');
$r->setAccessible(true);
Expand Down
4 changes: 3 additions & 1 deletion src/Handler/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public function __construct(
public function setServerRequestandRequestUri(ServerRequestInterface $request) : void
{
$this->request = $request;
$this->requestUri = \substr($request->getUri()->__toString(), \strlen($this->serverUrl));
$uri = $request->getUri();
$this->serverUrl = $uri->getScheme() . '://' . $uri->getHost();
$this->requestUri = \substr($uri->__toString(), \strlen($this->serverUrl));
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Handler/LoggingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ class LoggingFactory
public function __invoke(ContainerInterface $container) : Logging
{
if (! Console::isConsole()) {
$serverUrlHelper = $container->get('ViewHelperManager')->get('ServerUrl');
if ($container->has('Request')) {
$serverUrl = $serverUrlHelper->__invoke();
$serverUrl = $container->get('ViewHelperManager')->get('ServerUrl')();
$request = $container->get('Request');
$requestUri = $request->getRequestUri();
} else {
$serverUrl = $serverUrlHelper->__invoke(true);
$serverUrl = '';
$request = null;
$requestUri = '';
}
Expand Down
15 changes: 9 additions & 6 deletions src/Middleware/Expressive.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Expressive\Application;
use Zend\Expressive\Template\TemplateRendererInterface;
use Zend\Expressive\ZendView\ZendViewRenderer;
use Zend\View\Model\ViewModel;

class Expressive implements MiddlewareInterface
Expand Down Expand Up @@ -108,13 +109,15 @@ private function showDefaultViewWhenDisplayErrorSetttingIsDisabled() : ResponseI
return $response->withStatus(500);
}

$layout = new ViewModel();
$layout->setTemplate($this->errorHeroModuleConfig['display-settings']['template']['layout']);
if ($this->renderer instanceof ZendViewRenderer) {
$layout = new ViewModel();
$layout->setTemplate($this->errorHeroModuleConfig['display-settings']['template']['layout']);

$rendererLayout = & Closure::bind(function & ($renderer) {
return $renderer->layout;
}, null, $this->renderer)($this->renderer);
$rendererLayout = $layout;
$rendererLayout = & Closure::bind(function & ($renderer) {
return $renderer->layout;
}, null, $this->renderer)($this->renderer);
$rendererLayout = $layout;
}

return new HtmlResponse(
$this->renderer->render($this->errorHeroModuleConfig['display-settings']['template']['view']),
Expand Down
7 changes: 7 additions & 0 deletions view/error-hero-module/error-default.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends '@layout/default.html.twig' %}

{% block title %}Home{% endblock %}

{% block content %}
<p>We have encountered a problem and we can not fulfill your request. An error report has been generated and sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience.</p>
{% endblock %}
14 changes: 13 additions & 1 deletion view/error-hero-module/error-default.phtml
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
<p>We have encountered a problem and we can not fulfill your request. An error report has been generated and sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience.</p>
<?php

use Zend\Expressive\ZendView\ZendViewRenderer as ExpressiveZendViewRenderer;
use Zend\View\Renderer\PhpRenderer as MvcZendViewRenderer;

try {
if (! ($this instanceof ExpressiveZendViewRenderer || $this instanceof MvcZendViewRenderer)) {
$this->layout('layout::default', ['title' => 'Error']);
}
} catch (Throwable $e){}
?>

<p>We have encountered a problem and we can not fulfill your request. An error report has been generated and sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience.</p>

0 comments on commit 072c3a7

Please sign in to comment.