From 92a4d2fcf82bd6436a609cd63986c566520e016e Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Wed, 1 Jul 2020 00:12:23 +0200 Subject: [PATCH] add Twig for template rendering --- composer.json | 3 +- composer.lock | 352 +++++++++++++++++++++++++++-------- public/index.php | 2 + src/Dependencies.php | 11 ++ src/HelloWorldController.php | 12 +- src/Middlewares.php | 15 ++ templates/helloworld.html | 19 ++ 7 files changed, 333 insertions(+), 81 deletions(-) create mode 100644 src/Middlewares.php create mode 100644 templates/helloworld.html diff --git a/composer.json b/composer.json index 34cd9b3..7d97942 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/composer.lock b/composer.lock index 8ded0f0..799f266 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/public/index.php b/public/index.php index e5121cd..7349ab5 100644 --- a/public/index.php +++ b/public/index.php @@ -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(); diff --git a/src/Dependencies.php b/src/Dependencies.php index b0a6172..a6eecf9 100644 --- a/src/Dependencies.php +++ b/src/Dependencies.php @@ -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; } } diff --git a/src/HelloWorldController.php b/src/HelloWorldController.php index b98ba33..0a9e2d8 100644 --- a/src/HelloWorldController.php +++ b/src/HelloWorldController.php @@ -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('' . $this->hello->getHello() . "\n"); - return $response; + $renderData = [ + 'hello' => $this->hello->getHello(), + ]; + + return $this->view->render($response, 'helloworld.html', $renderData); } } diff --git a/src/Middlewares.php b/src/Middlewares.php new file mode 100644 index 0000000..c742928 --- /dev/null +++ b/src/Middlewares.php @@ -0,0 +1,15 @@ +add(TwigMiddleware::createFromContainer($app)); + } +} diff --git a/templates/helloworld.html b/templates/helloworld.html new file mode 100644 index 0000000..c3bb31e --- /dev/null +++ b/templates/helloworld.html @@ -0,0 +1,19 @@ + + + + + + + + + + + +

Well hi there :)

+ +{{ hello }} + + + + +