add Twig for template rendering
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Lexi / Zoe 2020-07-01 00:12:23 +02:00
parent 7cc2d40660
commit 92a4d2fcf8
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
7 changed files with 333 additions and 81 deletions

View File

@ -12,7 +12,8 @@
"php": "^7.4",
"slim/slim": "^4.5",
"slim/psr7": "^1.1",
"php-di/php-di": "^6.2"
"php-di/php-di": "^6.2",
"slim/twig-view": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^9"

352
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c9b48ebdf86b04852bf99bd1932d57e8",
"content-hash": "95a06692c9bf9b1b14a5a3a4c6b4d22f",
"packages": [
{
"name": "fig/http-message-util",
@ -837,6 +837,280 @@
}
],
"time": "2020-04-14T20:49:48+00:00"
},
{
"name": "slim/twig-view",
"version": "3.1.0",
"source": {
"type": "git",
"url": "https://github.com/slimphp/Twig-View.git",
"reference": "36f7a04ca9e58ef926dc25cd0110f629668cb3f0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/slimphp/Twig-View/zipball/36f7a04ca9e58ef926dc25cd0110f629668cb3f0",
"reference": "36f7a04ca9e58ef926dc25cd0110f629668cb3f0",
"shasum": ""
},
"require": {
"php": "^7.2",
"psr/http-message": "^1.0",
"slim/slim": "^4.0",
"twig/twig": "^3.0"
},
"require-dev": {
"phpstan/phpstan": "^0.11.15",
"phpunit/phpunit": "^8.5",
"psr/http-factory": "^1.0",
"squizlabs/php_codesniffer": "^3.5"
},
"type": "library",
"autoload": {
"psr-4": {
"Slim\\Views\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Josh Lockhart",
"email": "hello@joshlockhart.com",
"homepage": "http://joshlockhart.com"
},
{
"name": "Pierre Berube",
"email": "pierre@lgse.com",
"homepage": "http://www.lgse.com"
}
],
"description": "Slim Framework 4 view helper built on top of the Twig 3 templating component",
"homepage": "https://www.slimframework.com",
"keywords": [
"framework",
"slim",
"template",
"twig",
"view"
],
"time": "2020-03-13T18:08:36+00:00"
},
{
"name": "symfony/polyfill-ctype",
"version": "v1.17.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-ctype.git",
"reference": "2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d",
"reference": "2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-ctype": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.17-dev"
},
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Ctype\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Gert de Pagter",
"email": "BackEndTea@gmail.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for ctype functions",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"ctype",
"polyfill",
"portable"
],
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-06-06T08:46:27+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.17.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "7110338d81ce1cbc3e273136e4574663627037a7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/7110338d81ce1cbc3e273136e4574663627037a7",
"reference": "7110338d81ce1cbc3e273136e4574663627037a7",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.17-dev"
},
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-06-06T08:46:27+00:00"
},
{
"name": "twig/twig",
"version": "v3.0.3",
"source": {
"type": "git",
"url": "https://github.com/twigphp/Twig.git",
"reference": "3b88ccd180a6b61ebb517aea3b1a8906762a1dc2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/3b88ccd180a6b61ebb517aea3b1a8906762a1dc2",
"reference": "3b88ccd180a6b61ebb517aea3b1a8906762a1dc2",
"shasum": ""
},
"require": {
"php": "^7.2.5",
"symfony/polyfill-ctype": "^1.8",
"symfony/polyfill-mbstring": "^1.3"
},
"require-dev": {
"psr/container": "^1.0",
"symfony/phpunit-bridge": "^4.4|^5.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"autoload": {
"psr-4": {
"Twig\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com",
"homepage": "http://fabien.potencier.org",
"role": "Lead Developer"
},
{
"name": "Twig Team",
"role": "Contributors"
},
{
"name": "Armin Ronacher",
"email": "armin.ronacher@active-4.com",
"role": "Project Founder"
}
],
"description": "Twig, the flexible, fast, and secure template language for PHP",
"homepage": "https://twig.symfony.com",
"keywords": [
"templating"
],
"time": "2020-02-11T15:33:47+00:00"
}
],
"packages-dev": [
@ -2460,82 +2734,6 @@
],
"time": "2020-06-26T12:18:43+00:00"
},
{
"name": "symfony/polyfill-ctype",
"version": "v1.17.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-ctype.git",
"reference": "2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d",
"reference": "2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-ctype": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.17-dev"
},
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Ctype\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Gert de Pagter",
"email": "BackEndTea@gmail.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for ctype functions",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"ctype",
"polyfill",
"portable"
],
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-06-06T08:46:27+00:00"
},
{
"name": "theseer/tokenizer",
"version": "1.1.3",

View File

@ -4,12 +4,14 @@ declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use NoteCat\Dependencies;
use NoteCat\Middlewares;
use NoteCat\Routes;
use Slim\Factory\AppFactory;
$container = Dependencies::createContainer();
$app = AppFactory::createFromContainer($container);
Middlewares::setMiddlewares($app);
Routes::setRoutes($app);
$app->run();

View File

@ -5,9 +5,13 @@ namespace NoteCat;
use DI\Container;
use Psr\Container\ContainerInterface;
use Slim\Views\Twig;
class Dependencies
{
private const TWIG = 'view';
private const TWIG_TEMPLATE_DIR = __DIR__ . '/../templates';
public static function createContainer(): Container
{
$container = new Container();
@ -15,6 +19,7 @@ class Dependencies
// Controllers
$container->set(HelloWorldController::class, function (ContainerInterface $c) {
return new HelloWorldController(
$c->get(self::TWIG),
$c->get(HelloWorld::class)
);
});
@ -24,6 +29,12 @@ class Dependencies
return new HelloWorld();
});
$container->set(self::TWIG, function (ContainerInterface $c) {
// TODO cache
$twigSettings = [];
return Twig::create(self::TWIG_TEMPLATE_DIR, $twigSettings);
});
return $container;
}
}

View File

@ -5,19 +5,25 @@ namespace NoteCat;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
class HelloWorldController
{
private Twig $view;
private HelloWorld $hello;
public function __construct(HelloWorld $hello)
public function __construct(Twig $view, HelloWorld $hello)
{
$this->view = $view;
$this->hello = $hello;
}
public function hello(Request $request, Response $response): Response
{
$response->getBody()->write('<b>' . $this->hello->getHello() . "</b>\n");
return $response;
$renderData = [
'hello' => $this->hello->getHello(),
];
return $this->view->render($response, 'helloworld.html', $renderData);
}
}

15
src/Middlewares.php Normal file
View File

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace NoteCat;
use Slim\App;
use Slim\Views\TwigMiddleware;
class Middlewares
{
public static function setMiddlewares(App $app): void
{
$app->add(TwigMiddleware::createFromContainer($app));
}
}

19
templates/helloworld.html Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- <link rel="stylesheet" href=""> -->
</head>
<body>
<h1>Well hi there :)</h1>
<b>{{ hello }}</b>
<!-- <script src="" async defer></script> -->
</body>
</html>