From 49ea5cc78bf2a52d59737ac0edb9016b055e7e4d Mon Sep 17 00:00:00 2001
From: Alejandro Celaya <alejandro@alejandrocelaya.com>
Date: Sun, 22 Nov 2020 18:03:27 +0100
Subject: [PATCH] #912 Removed dependency on league/plates

---
 composer.json                                 |  1 -
 config/autoload/templates.global.php          | 17 -----------
 config/config.php                             |  1 -
 module/Core/config/dependencies.config.php    |  5 ++--
 module/Core/config/mezzio.config.php          | 14 ---------
 module/Core/config/templates.config.php       | 13 ---------
 .../ErrorHandler/NotFoundTemplateHandler.php  | 29 +++++++++----------
 module/Core/templates/404.html                | 27 +++++++++++++++++
 module/Core/templates/error/404.phtml         | 19 ------------
 module/Core/templates/error/error.phtml       | 25 ----------------
 module/Core/templates/invalid-short-code.html | 27 +++++++++++++++++
 .../Core/templates/invalid-short-code.phtml   | 19 ------------
 module/Core/templates/layout/default.phtml    | 23 ---------------
 module/Core/test/ConfigProviderTest.php       |  4 +--
 14 files changed, 70 insertions(+), 154 deletions(-)
 delete mode 100644 config/autoload/templates.global.php
 delete mode 100644 module/Core/config/mezzio.config.php
 delete mode 100644 module/Core/config/templates.config.php
 create mode 100644 module/Core/templates/404.html
 delete mode 100644 module/Core/templates/error/404.phtml
 delete mode 100644 module/Core/templates/error/error.phtml
 create mode 100644 module/Core/templates/invalid-short-code.html
 delete mode 100644 module/Core/templates/invalid-short-code.phtml
 delete mode 100644 module/Core/templates/layout/default.phtml

diff --git a/composer.json b/composer.json
index 679fdbe9..caf5e5dd 100644
--- a/composer.json
+++ b/composer.json
@@ -38,7 +38,6 @@
         "mezzio/mezzio": "^3.2",
         "mezzio/mezzio-fastroute": "^3.0",
         "mezzio/mezzio-helpers": "^5.3",
-        "mezzio/mezzio-platesrenderer": "^2.1",
         "mezzio/mezzio-problem-details": "^1.1",
         "mezzio/mezzio-swoole": "^2.6.4",
         "monolog/monolog": "^2.0",
diff --git a/config/autoload/templates.global.php b/config/autoload/templates.global.php
deleted file mode 100644
index e1b457fa..00000000
--- a/config/autoload/templates.global.php
+++ /dev/null
@@ -1,17 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-return [
-
-    'templates' => [
-        'extension' => 'phtml',
-    ],
-
-    'plates' => [
-        'extensions' => [
-            // extension service names or instances
-        ],
-    ],
-
-];
diff --git a/config/config.php b/config/config.php
index ba0657fc..cf9eb86b 100644
--- a/config/config.php
+++ b/config/config.php
@@ -15,7 +15,6 @@ return (new ConfigAggregator\ConfigAggregator([
     Mezzio\ConfigProvider::class,
     Mezzio\Router\ConfigProvider::class,
     Mezzio\Router\FastRouteRouter\ConfigProvider::class,
-    Mezzio\Plates\ConfigProvider::class,
     Mezzio\Swoole\ConfigProvider::class,
     ProblemDetails\ConfigProvider::class,
     Diactoros\ConfigProvider::class,
diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php
index b6beb1ac..94b5858a 100644
--- a/module/Core/config/dependencies.config.php
+++ b/module/Core/config/dependencies.config.php
@@ -5,7 +5,7 @@ declare(strict_types=1);
 namespace Shlinkio\Shlink\Core;
 
 use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
-use Mezzio\Template\TemplateRendererInterface;
+use Laminas\ServiceManager\Factory\InvokableFactory;
 use Psr\EventDispatcher\EventDispatcherInterface;
 use Shlinkio\Shlink\Core\ErrorHandler;
 use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
@@ -16,7 +16,7 @@ return [
     'dependencies' => [
         'factories' => [
             ErrorHandler\NotFoundRedirectHandler::class => ConfigAbstractFactory::class,
-            ErrorHandler\NotFoundTemplateHandler::class => ConfigAbstractFactory::class,
+            ErrorHandler\NotFoundTemplateHandler::class => InvokableFactory::class,
 
             Options\AppOptions::class => ConfigAbstractFactory::class,
             Options\DeleteShortUrlsOptions::class => ConfigAbstractFactory::class,
@@ -60,7 +60,6 @@ return [
             Util\RedirectResponseHelper::class,
             'config.router.base_path',
         ],
-        ErrorHandler\NotFoundTemplateHandler::class => [TemplateRendererInterface::class],
 
         Options\AppOptions::class => ['config.app_options'],
         Options\DeleteShortUrlsOptions::class => ['config.delete_short_urls'],
diff --git a/module/Core/config/mezzio.config.php b/module/Core/config/mezzio.config.php
deleted file mode 100644
index 5e4acb22..00000000
--- a/module/Core/config/mezzio.config.php
+++ /dev/null
@@ -1,14 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-return [
-
-    'mezzio' => [
-        'error_handler' => [
-            'template_404'   => 'ShlinkCore::error/404',
-            'template_error' => 'ShlinkCore::error/error',
-        ],
-    ],
-
-];
diff --git a/module/Core/config/templates.config.php b/module/Core/config/templates.config.php
deleted file mode 100644
index 784f731e..00000000
--- a/module/Core/config/templates.config.php
+++ /dev/null
@@ -1,13 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-return [
-
-    'templates' => [
-        'paths' => [
-            'ShlinkCore' => __DIR__ . '/../templates',
-        ],
-    ],
-
-];
diff --git a/module/Core/src/ErrorHandler/NotFoundTemplateHandler.php b/module/Core/src/ErrorHandler/NotFoundTemplateHandler.php
index e5968a68..62b78973 100644
--- a/module/Core/src/ErrorHandler/NotFoundTemplateHandler.php
+++ b/module/Core/src/ErrorHandler/NotFoundTemplateHandler.php
@@ -4,40 +4,37 @@ declare(strict_types=1);
 
 namespace Shlinkio\Shlink\Core\ErrorHandler;
 
+use Closure;
 use Fig\Http\Message\StatusCodeInterface;
-use InvalidArgumentException;
 use Laminas\Diactoros\Response;
 use Mezzio\Router\RouteResult;
-use Mezzio\Template\TemplateRendererInterface;
 use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
 use Psr\Http\Server\RequestHandlerInterface;
 
+use function file_get_contents;
+use function sprintf;
+
 class NotFoundTemplateHandler implements RequestHandlerInterface
 {
-    public const NOT_FOUND_TEMPLATE = 'ShlinkCore::error/404';
-    public const INVALID_SHORT_CODE_TEMPLATE = 'ShlinkCore::invalid-short-code';
+    private const TEMPLATES_BASE_DIR = __DIR__ . '/../../templates';
+    public const NOT_FOUND_TEMPLATE = '404.html';
+    public const INVALID_SHORT_CODE_TEMPLATE = 'invalid-short-code.html';
+    private Closure $readFile;
 
-    private TemplateRendererInterface $renderer;
-
-    public function __construct(TemplateRendererInterface $renderer)
+    public function __construct(?callable $readFile = null)
     {
-        $this->renderer = $renderer;
+        $this->readFile = $readFile ? Closure::fromCallable($readFile) : fn (string $file) => file_get_contents($file);
     }
 
-    /**
-     * Dispatch the next available middleware and return the response.
-     *
-     *
-     * @throws InvalidArgumentException
-     */
     public function handle(ServerRequestInterface $request): ResponseInterface
     {
         /** @var RouteResult $routeResult */
-        $routeResult = $request->getAttribute(RouteResult::class, RouteResult::fromRouteFailure(null));
+        $routeResult = $request->getAttribute(RouteResult::class) ?? RouteResult::fromRouteFailure(null);
         $status = StatusCodeInterface::STATUS_NOT_FOUND;
 
         $template = $routeResult->isFailure() ? self::NOT_FOUND_TEMPLATE : self::INVALID_SHORT_CODE_TEMPLATE;
-        return new Response\HtmlResponse($this->renderer->render($template), $status);
+        $templateContent = ($this->readFile)(sprintf('%s/%s', self::TEMPLATES_BASE_DIR, $template));
+        return new Response\HtmlResponse($templateContent, $status);
     }
 }
diff --git a/module/Core/templates/404.html b/module/Core/templates/404.html
new file mode 100644
index 00000000..93e6fb64
--- /dev/null
+++ b/module/Core/templates/404.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <title>Not Found | Shlink</title>
+        <meta charset="utf-8">
+        <meta http-equiv="X-UA-Compatible" content="IE=edge">
+        <meta name="viewport" content="width=device-width, initial-scale=1">
+        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
+        <link rel="shortcut icon" href="/favicon.ico">
+        <style>
+            html, body {height: 100%}
+            .app {height: 100vh; display: flex; align-items: center; justify-content: center; flex-flow: column;}
+            p {margin-bottom: 20px;}
+            body {text-align: center;}
+        </style>
+    </head>
+    <body>
+        <div class="app">
+            <main class="container">
+                <h1>404</h1>
+                <hr>
+                <h3>Page not found.</h3>
+                <p>The page you requested could not be found.</p>
+            </main>
+        </div>
+    </body>
+</html>
diff --git a/module/Core/templates/error/404.phtml b/module/Core/templates/error/404.phtml
deleted file mode 100644
index 20ac4ff8..00000000
--- a/module/Core/templates/error/404.phtml
+++ /dev/null
@@ -1,19 +0,0 @@
-<?php $this->layout('ShlinkCore::layout/default') ?>
-
-<?php $this->start('title') ?>
-  Not Found
-<?php $this->end() ?>
-
-<?php $this->start('stylesheets') ?>
-  <style>
-    p {margin-bottom: 20px;}
-    body {text-align: center;}
-  </style>
-<?php $this->end() ?>
-
-<?php $this->start('main') ?>
-  <h1>404</h1>
-  <hr>
-  <h3>Page not found.</h3>
-  <p>The page you requested could not be found.</p>
-<?php $this->end() ?>
diff --git a/module/Core/templates/error/error.phtml b/module/Core/templates/error/error.phtml
deleted file mode 100644
index 77108f26..00000000
--- a/module/Core/templates/error/error.phtml
+++ /dev/null
@@ -1,25 +0,0 @@
-<?php $this->layout('ShlinkCore::layout/default') ?>
-
-<?php $this->start('title') ?>
-  <?= $this->e($status . ' ' . $reason) ?>
-<?php $this->end() ?>
-
-<?php $this->start('stylesheets') ?>
-  <style>
-    p {margin-bottom: 20px;}
-    body {text-align: center;}
-  </style>
-<?php $this->end() ?>
-
-<?php $this->start('main') ?>
-  <h1>Oops!</h1>
-  <hr>
-
-  <?php if ($status !== 404): ?>
-    <p><?= sprintf('We encountered a %s %s error.', $status, $reason) ?></p>
-  <?php else: ?>
-    <p>'This short URL doesn't seem to be valid.</p>
-    <p>'Make sure you included all the characters, with no extra punctuation.</p>
-  <?php endif; ?>
-<?php $this->end() ?>
-
diff --git a/module/Core/templates/invalid-short-code.html b/module/Core/templates/invalid-short-code.html
new file mode 100644
index 00000000..61c5a804
--- /dev/null
+++ b/module/Core/templates/invalid-short-code.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <title>Invalid Short URL | Shlink</title>
+        <meta charset="utf-8">
+        <meta http-equiv="X-UA-Compatible" content="IE=edge">
+        <meta name="viewport" content="width=device-width, initial-scale=1">
+        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
+        <link rel="shortcut icon" href="/favicon.ico">
+        <style>
+            html, body {height: 100%}
+            .app {height: 100vh; display: flex; align-items: center; justify-content: center; flex-flow: column;}
+            p {margin-bottom: 20px;}
+            body {text-align: center;}
+        </style>
+    </head>
+    <body>
+        <div class="app">
+            <main class="container">
+                <h1>Oops!</h1>
+                <hr>
+                <p>This short URL doesn't seem to be valid.</p>
+                <p>Make sure you included all the characters, with no extra punctuation.</p>
+            </main>
+        </div>
+    </body>
+</html>
diff --git a/module/Core/templates/invalid-short-code.phtml b/module/Core/templates/invalid-short-code.phtml
deleted file mode 100644
index 47be4a16..00000000
--- a/module/Core/templates/invalid-short-code.phtml
+++ /dev/null
@@ -1,19 +0,0 @@
-<?php $this->layout('ShlinkCore::layout/default') ?>
-
-<?php $this->start('title') ?>
-    Invalid Short URL
-<?php $this->end() ?>
-
-<?php $this->start('stylesheets') ?>
-    <style>
-        p {margin-bottom: 20px;}
-        body {text-align: center;}
-    </style>
-<?php $this->end() ?>
-
-<?php $this->start('main') ?>
-    <h1>Oops!</h1>
-    <hr>
-    <p>This short URL doesn't seem to be valid.</p>
-    <p>Make sure you included all the characters, with no extra punctuation.</p>
-<?php $this->end() ?>
diff --git a/module/Core/templates/layout/default.phtml b/module/Core/templates/layout/default.phtml
deleted file mode 100644
index fbb78b26..00000000
--- a/module/Core/templates/layout/default.phtml
+++ /dev/null
@@ -1,23 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-  <head>
-    <title><?= $this->section('title', '') ?> | Shlink</title>
-    <meta charset="utf-8">
-    <meta http-equiv="X-UA-Compatible" content="IE=edge">
-    <meta name="viewport" content="width=device-width, initial-scale=1">
-    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
-    <link rel="shortcut icon" href="/favicon.ico">
-    <style>
-      html, body {height: 100%}
-      .app {height: 100vh; display: flex; align-items: center; justify-content: center; flex-flow: column;}
-    </style>
-    <?= $this->section('stylesheets', '') ?>
-  </head>
-  <body>
-    <div class="app">
-      <main class="container">
-        <?= $this->section('main', '') ?>
-      </main>
-    </div>
-  </body>
-</html>
diff --git a/module/Core/test/ConfigProviderTest.php b/module/Core/test/ConfigProviderTest.php
index 2660803b..7f4eb8dd 100644
--- a/module/Core/test/ConfigProviderTest.php
+++ b/module/Core/test/ConfigProviderTest.php
@@ -19,11 +19,9 @@ class ConfigProviderTest extends TestCase
     /** @test */
     public function properConfigIsReturned(): void
     {
-        $config = $this->configProvider->__invoke();
+        $config = ($this->configProvider)();
 
         self::assertArrayHasKey('routes', $config);
         self::assertArrayHasKey('dependencies', $config);
-        self::assertArrayHasKey('templates', $config);
-        self::assertArrayHasKey('mezzio', $config);
     }
 }