From d98ea86ac1b3b5dd214f39713eb2f7be09181da7 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Wed, 25 Oct 2023 20:31:49 +0200 Subject: [PATCH] Add a TwigManager --- composer.json | 11 +- composer.lock | 443 ++++++++++++++++++++++++++++++++++- src/Managers/TwigManager.php | 111 +++++++++ 3 files changed, 561 insertions(+), 4 deletions(-) create mode 100644 src/Managers/TwigManager.php diff --git a/composer.json b/composer.json index e5d392e..2cdda11 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,10 @@ "require": { "php": "^7.3|^8.0", "symfony/yaml": "^5.1", - "studiometa/webpack-config": "^4.0" + "studiometa/webpack-config": "^4.0", + "djboris88/twig-commented-include": "dev-master", + "twig/twig": "^2.0|^3.0", + "studiometa/ui": "^0.2.40" }, "require-dev": { "squizlabs/php_codesniffer": "^3.4", @@ -18,6 +21,12 @@ "phpunit/phpunit": "^9.0", "yoast/phpunit-polyfills": "^1.0" }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/EstudioNexos/twig-commented-include" + } + ], "autoload": { "psr-4": { "Studiometa\\WPToolkit\\": "src/" diff --git a/composer.lock b/composer.lock index 25b7749..0fc4a2f 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": "3673c457682ecd22e94f372b339a4700", + "content-hash": "5fb39b038194917a940121725dacc140", "packages": [ { "name": "anahkiasen/html-object", @@ -52,6 +52,370 @@ }, "time": "2017-05-31T07:52:45+00:00" }, + { + "name": "djboris88/twig-commented-include", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/EstudioNexos/twig-commented-include.git", + "reference": "14ab6d68247553565b88f558fdaccad3a589d849" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/EstudioNexos/twig-commented-include/zipball/14ab6d68247553565b88f558fdaccad3a589d849", + "reference": "14ab6d68247553565b88f558fdaccad3a589d849", + "shasum": "" + }, + "require": { + "php": ">=5.6", + "twig/twig": "^2.0|^3.0" + }, + "require-dev": { + "symfony/framework-bundle": "^2.7|^3.2", + "symfony/twig-bundle": "^2.7|^3.2" + }, + "suggest": { + "symfony/framework-bundle": "The framework bundle to integrate the extension into Symfony", + "symfony/twig-bundle": "The twig bundle to integrate the extension into Symfony" + }, + "default-branch": true, + "type": "library", + "autoload": { + "psr-4": { + "Djboris88\\Twig\\": "src/" + } + }, + "license": [ + "GPL-3.0" + ], + "authors": [ + { + "name": "Boris Đemrovski", + "email": "djboris88@gmail.com", + "role": "developer" + } + ], + "description": "Twig extension which outputs HTML comments before and after the `include` tag", + "homepage": "https://github.com/djboris88/twig-include-comments", + "keywords": [ + "comments", + "debug", + "html", + "twig" + ], + "support": { + "source": "https://github.com/EstudioNexos/twig-commented-include/tree/master" + }, + "time": "2023-10-06T04:55:14+00:00" + }, + { + "name": "jawira/case-converter", + "version": "v3.4.6", + "source": { + "type": "git", + "url": "https://github.com/jawira/case-converter.git", + "reference": "2f284c0760ee8085ab463f57275cf1f887fb78ae" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/jawira/case-converter/zipball/2f284c0760ee8085ab463f57275cf1f887fb78ae", + "reference": "2f284c0760ee8085ab463f57275cf1f887fb78ae", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "php": "^7.1 || ^8.0" + }, + "require-dev": { + "behat/behat": "^3.0", + "phpstan/phpstan": "^1.0", + "phpunit/phpunit": "^9.0", + "vimeo/psalm": "^4.0" + }, + "suggest": { + "pds/skeleton": "PHP Package Development Standards", + "phing/phing": "PHP Build Tool" + }, + "type": "library", + "autoload": { + "psr-4": { + "Jawira\\CaseConverter\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jawira Portugal", + "email": "dev@tugal.be" + } + ], + "description": "Convert strings between 13 naming conventions: Snake case, Camel case, Pascal case, Kebab case, Ada case, Train case, Cobol case, Macro case, Upper case, Lower case, Sentence case, Title case and Dot notation.", + "homepage": "https://jawira.github.io/case-converter/", + "keywords": [ + "Ada case", + "Cobol case", + "Macro case", + "Train case", + "camel case", + "dot notation", + "kebab case", + "lower case", + "pascal case", + "sentence case", + "snake case", + "title case", + "upper case" + ], + "support": { + "issues": "https://github.com/jawira/case-converter/issues", + "source": "https://github.com/jawira/case-converter/tree/v3.4.6" + }, + "time": "2021-11-21T19:52:51+00:00" + }, + { + "name": "psr/http-message", + "version": "1.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "cb6ce4845ce34a8ad9e68117c10ee90a29919eba" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/cb6ce4845ce34a8ad9e68117c10ee90a29919eba", + "reference": "cb6ce4845ce34a8ad9e68117c10ee90a29919eba", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "support": { + "source": "https://github.com/php-fig/http-message/tree/1.1" + }, + "time": "2023-04-04T09:50:52+00:00" + }, + { + "name": "spatie/macroable", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/spatie/macroable.git", + "reference": "7a99549fc001c925714b329220dea680c04bfa48" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/macroable/zipball/7a99549fc001c925714b329220dea680c04bfa48", + "reference": "7a99549fc001c925714b329220dea680c04bfa48", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "require-dev": { + "phpunit/phpunit": "^8.0|^9.3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\Macroable\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "A trait to dynamically add methods to a class", + "homepage": "https://github.com/spatie/macroable", + "keywords": [ + "macroable", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/macroable/issues", + "source": "https://github.com/spatie/macroable/tree/1.0.1" + }, + "time": "2020-11-03T10:15:05+00:00" + }, + { + "name": "spatie/url", + "version": "1.3.5", + "source": { + "type": "git", + "url": "https://github.com/spatie/url.git", + "reference": "3633de58e0709ea98cecceff61ee51caf1fde7e3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/url/zipball/3633de58e0709ea98cecceff61ee51caf1fde7e3", + "reference": "3633de58e0709ea98cecceff61ee51caf1fde7e3", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0", + "psr/http-message": "^1.0", + "spatie/macroable": "^1.0.1" + }, + "require-dev": { + "phpunit/phpunit": "^8.0|^9.3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\Url\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Sebastian De Deyne", + "email": "sebastian@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Parse, build and manipulate URL's", + "homepage": "https://github.com/spatie/url", + "keywords": [ + "spatie", + "url" + ], + "support": { + "issues": "https://github.com/spatie/url/issues", + "source": "https://github.com/spatie/url/tree/1.3.5" + }, + "time": "2020-11-03T10:36:20+00:00" + }, + { + "name": "studiometa/twig-toolkit", + "version": "1.3.5", + "source": { + "type": "git", + "url": "https://github.com/studiometa/twig-toolkit.git", + "reference": "c028d6934f52cb364af0b8196db9862d09c2e79d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/studiometa/twig-toolkit/zipball/c028d6934f52cb364af0b8196db9862d09c2e79d", + "reference": "c028d6934f52cb364af0b8196db9862d09c2e79d", + "shasum": "" + }, + "require": { + "jawira/case-converter": "^3.4", + "php": "^7.3|^8.0", + "spatie/url": "^1.3", + "twig/twig": "^2.10|^3" + }, + "require-dev": { + "pestphp/pest": "^1.22", + "phpstan/phpstan": "^0.12.88", + "spatie/pest-plugin-snapshots": "^1.0", + "squizlabs/php_codesniffer": "^3.6" + }, + "type": "library", + "autoload": { + "psr-4": { + "Studiometa\\TwigToolkit\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A set of useful extension and components for Twig.", + "support": { + "issues": "https://github.com/studiometa/twig-toolkit/issues", + "source": "https://github.com/studiometa/twig-toolkit/tree/1.3.5" + }, + "time": "2023-07-13T12:42:07+00:00" + }, + { + "name": "studiometa/ui", + "version": "0.2.40", + "source": { + "type": "git", + "url": "https://github.com/studiometa/ui.git", + "reference": "b586bc5adce0a3ec9c73cede4d4ab43af5706d45" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/studiometa/ui/zipball/b586bc5adce0a3ec9c73cede4d4ab43af5706d45", + "reference": "b586bc5adce0a3ec9c73cede4d4ab43af5706d45", + "shasum": "" + }, + "require": { + "php": "^7.3|^8.0", + "studiometa/twig-toolkit": "^1.3", + "twig/twig": "^2.10|^3" + }, + "require-dev": { + "mallardduck/prettier-php-runner": "^1.0", + "pestphp/pest": "^1.21", + "phpstan/phpstan": "^0.12.100", + "spatie/pest-plugin-snapshots": "^1.1", + "squizlabs/php_codesniffer": "^3.6" + }, + "type": "library", + "autoload": { + "psr-4": { + "Studiometa\\Ui\\": "packages/twig-extension" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A set of opiniated, unstyled and accessible components.", + "support": { + "issues": "https://github.com/studiometa/ui/issues", + "source": "https://github.com/studiometa/ui/tree/0.2.40" + }, + "time": "2023-10-23T13:05:55+00:00" + }, { "name": "studiometa/webpack-config", "version": "4.0.6", @@ -631,6 +995,77 @@ "source": "https://github.com/tighten/collect/tree/v8.83.25" }, "time": "2022-08-22T17:55:07+00:00" + }, + { + "name": "twig/twig", + "version": "v3.7.1", + "source": { + "type": "git", + "url": "https://github.com/twigphp/Twig.git", + "reference": "a0ce373a0ca3bf6c64b9e3e2124aca502ba39554" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/a0ce373a0ca3bf6c64b9e3e2124aca502ba39554", + "reference": "a0ce373a0ca3bf6c64b9e3e2124aca502ba39554", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/polyfill-ctype": "^1.8", + "symfony/polyfill-mbstring": "^1.3" + }, + "require-dev": { + "psr/container": "^1.0|^2.0", + "symfony/phpunit-bridge": "^5.4.9|^6.3" + }, + "type": "library", + "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" + ], + "support": { + "issues": "https://github.com/twigphp/Twig/issues", + "source": "https://github.com/twigphp/Twig/tree/v3.7.1" + }, + "funding": [ + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/twig/twig", + "type": "tidelift" + } + ], + "time": "2023-08-28T11:09:02+00:00" } ], "packages-dev": [ @@ -2864,12 +3299,14 @@ ], "aliases": [], "minimum-stability": "dev", - "stability-flags": [], + "stability-flags": { + "djboris88/twig-commented-include": 20 + }, "prefer-stable": true, "prefer-lowest": false, "platform": { "php": "^7.3|^8.0" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/src/Managers/TwigManager.php b/src/Managers/TwigManager.php new file mode 100644 index 0000000..ef60453 --- /dev/null +++ b/src/Managers/TwigManager.php @@ -0,0 +1,111 @@ +getLoader(); + $twig->addExtension( + new Extension( + $loader, + get_template_directory() . '/templates', + get_template_directory() . '/static/svg', + ) + ); + + return $twig; + } + + /** + * Adds template_from_string to Twig. + * + * @link https://twig.symfony.com/doc/2.x/functions/template_from_string.html + * @param Environment $twig The Twig environment. + * @return Environment + */ + public function add_template_from_string( Environment $twig ): Environment { + $twig->addExtension( new StringLoaderExtension() ); + return $twig; + } + + /** + * Adds template_from_string to Twig. + * + * @link https://github.com/djboris88/twig-commented-include + * @param Environment $twig The Twig environment. + * @return Environment + */ + public function add_template_include_comments( Environment $twig ): Environment { + if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { + return $twig; + } + + $twig->addExtension( new CommentedIncludeExtension() ); + return $twig; + } + + /** + * Add debug comments to Timber::render + * + * @param string $output HTML. + * @param mixed[] $data Data. + * @param string $file Name. + * @return string + */ + public function add_template_render_comments( string $output, array $data, string $file ): string { + if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { + return $output; + } + + return "\n\n" . $output . "\n\n"; + } + + /** + * Add an alias for the SVG folder. + * + * @example {{ source('@svg/icon.svg') }} + * + * @param FilesystemLoader $fs The loader to extend. + * @return FilesystemLoader + */ + public function add_svg_path( FilesystemLoader $fs ): FilesystemLoader { + $fs->addPath( get_template_directory() . '/static/svg', 'svg' ); + return $fs; + } +}