From 555557dcdd945d37b3755889e19d2b322415d102 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 10 Jan 2023 14:20:00 +0100 Subject: [PATCH] add method_exists extension --- .php-cs-fixer.php | 13 ------- CHANGELOG.md | 25 ++----------- DEVELOPMENT.md | 11 ------ README.en.md | 37 +++++++----------- README.md | 35 +++++++----------- composer.json | 34 ++++------------- metadata.php | 70 ----------------------------------- phpstan.neon | 10 ----- phpunit.xml | 20 ---------- services.yaml | 6 +++ src/MethodExistsExtension.php | 50 +++++++++++++++++++++++++ 11 files changed, 94 insertions(+), 217 deletions(-) delete mode 100644 .php-cs-fixer.php delete mode 100644 DEVELOPMENT.md delete mode 100644 metadata.php delete mode 100644 phpstan.neon delete mode 100644 phpunit.xml create mode 100644 services.yaml create mode 100644 src/MethodExistsExtension.php diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php deleted file mode 100644 index f0512a0..0000000 --- a/.php-cs-fixer.php +++ /dev/null @@ -1,13 +0,0 @@ -in(__DIR__) -; - -$config = new PhpCsFixer\Config(); -return $config->setRules([ - '@PHP73Migration' => true, - '@PSR12' => true - ]) - ->setFinder($finder) -; \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ff37b2..1036056 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,27 +4,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased](https://git.d3data.de/D3Public/MyModule/compare/1.1.0.0...rel_1.x) +## [Unreleased](https://git.d3data.de/D3Public/oxid_twig_extensions/compare/1.0.0.0...rel_1.x) -## [1.1.0.0](https://git.d3data.de/D3Public/MyModule/compare/1.0.0.0...1.1.0.0) - 2022-07-26 +## [1.0.0.0](https://git.d3data.de/D3Public/oxid_twig_extensions/releases/tag/1.0.0.0) - 2023-01-10 ### Added -- another feature - -### Changed -- changed old code - -### Deprecated -- ... - -### Removed -- ... - -### Fixed -- ... - -### Security -- ... - -## [1.0.0.0](https://git.d3data.de/D3Public/MyModule/releases/tag/1.0.0.0) - 2022-07-25 -### Added -- implemented features \ No newline at end of file +- add method_exists extension \ No newline at end of file diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md deleted file mode 100644 index 0de5505..0000000 --- a/DEVELOPMENT.md +++ /dev/null @@ -1,11 +0,0 @@ -# phpstan - -Install the dev packages manually, then run `./vendor/bin/phpstan -c./vendor/d3/oxtotp/phpstan.neon` from the projects root directory - -Analysis configuration is set in `phpstan.neon` file. - -# php-cs-fixer - -install the dev packages manually, then run `...` from the projects root directory - -Fixer configuration is set in `.php-cs-fixer.php` file. \ No newline at end of file diff --git a/README.en.md b/README.en.md index fcbf452..62f0353 100644 --- a/README.en.md +++ b/README.en.md @@ -1,14 +1,21 @@ [![deutsche Version](https://logos.oxidmodule.com/de2_xs.svg)](README.md) [![english version](https://logos.oxidmodule.com/en2_xs.svg)](README.en.md) -# D³ Modulname for OXID eShop +# D³ Twig extensions for OXID eShop -Insert short description here +additional extensions that can be used in the shop's Twig templates: + +- method_exists - Checks whether the method of an object exists. + + ``` + {% if method_exists(entity, 'getCreatedBy') %} + ({{ entity.createdBy.name }}) + {% endif %} + ``` ## Table of content - [Installation](#installation) -- [How to use](#how-to-use) - [Changelog](#changelog) - [Contributing](#contributing) - [License](#license) @@ -18,27 +25,15 @@ Insert short description here This package requires an OXID eShop installed with Composer in a version defined in the [composer.json](composer.json). -Please enter the following section in the `composer.json` of your project: - -``` - "extra": { - ... - } -``` - Open a command line and navigate to the root directory of the shop (parent directory of source and vendor). Execute the following command. Adapt the path details to your installation environment. ```bash -php composer require d3/modulename:^2.0 +php composer require d3/oxid-twig-extensions:^1.0 ``` If necessary, please confirm that you allow `package-name` to execute code. -Activate the module in Shopadmin under "Extensions -> Modules". - -## How to use - -... +Clear the TMP folder of your shop. ## Changelog @@ -55,7 +50,7 @@ If you have a suggestion that would make this better, please fork the repo and c - Open a Pull Request ## License -(status: 2022-07-30) +(status: 2023-01-10) Distributed under the GPLv3 license. @@ -65,8 +60,4 @@ Copyright (c) D3 Data Development (Inh. Thomas Dartsch) This software is distributed under the GNU GENERAL PUBLIC LICENSE version 3. ``` -For full copyright and licensing information, please see the [LICENSE](LICENSE.md) file distributed with this source code. - -## Further licences and terms of use - -... \ No newline at end of file +For full copyright and licensing information, please see the [LICENSE](LICENSE.md) file distributed with this source code. \ No newline at end of file diff --git a/README.md b/README.md index 91c8820..c94d371 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,21 @@ [![deutsche Version](https://logos.oxidmodule.com/de2_xs.svg)](README.md) [![english version](https://logos.oxidmodule.com/en2_xs.svg)](README.en.md) -# D³ Modulname für OXID eShop +# D³ Twig Extensions für OXID eShop -Hier Kurzbeschreibung einfügen +zusätzliche Erweiterungen, die in den Twig-Templates des Shops verwendet werden können: + +- method_exists - Prüft, ob die Methode eines Objekts existiert. + + ``` + {% if method_exists(entity, 'getCreatedBy') %} + ({{ entity.createdBy.name }}) + {% endif %} + ``` ## Inhaltsverzeichnis - [Installation](#installation) -- [Verwendung](#verwendung) - [Changelog](#changelog) - [Beitragen](#beitragen) - [Lizenz](#lizenz) @@ -18,28 +25,16 @@ Hier Kurzbeschreibung einfügen Dieses Paket erfordert einen mit Composer installierten OXID eShop in einer in der [composer.json](composer.json) definierten Version. -Bitte tragen Sie den folgenden Abschnitt in die `composer.json` Ihres Projektes ein: - -``` - "extra": { - ... - } -``` - Öffnen Sie eine Kommandozeile und navigieren Sie zum Stammverzeichnis des Shops (Elternverzeichnis von source und vendor). Führen Sie den folgenden Befehl aus. Passen Sie die Pfadangaben an Ihre Installationsumgebung an. ```bash -php composer require d3/modulename:^2.0 +php composer require d3/oxid-twig-extensions:^1.0 ``` Sofern nötig, bestätigen Sie bitte, dass Sie `package-name` erlauben, Code auszuführen. -Aktivieren Sie das Modul im Shopadmin unter "Erweiterungen -> Module". - -## Verwendung - -... +Leeren Sie den TMP-Ordner Ihres Shops. ## Changelog @@ -56,7 +51,7 @@ Wenn Sie eine Verbesserungsvorschlag haben, legen Sie einen Fork des Repositorie - Öffnen Sie einen Pull Request ## Lizenz -(Stand: 06.05.2021) +(Stand: 10.01.2023) Vertrieben unter der GPLv3 Lizenz. @@ -67,7 +62,3 @@ Diese Software wird unter der GNU GENERAL PUBLIC LICENSE Version 3 vertrieben. ``` Die vollständigen Copyright- und Lizenzinformationen entnehmen Sie bitte der [LICENSE](LICENSE.md)-Datei, die mit diesem Quellcode verteilt wurde. - -## weitere Lizenzen und Nutzungsbedingungen - -... \ No newline at end of file diff --git a/composer.json b/composer.json index 572359c..60b8207 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,12 @@ { - "name": "d3/modulename", - "description": "the module description", - "type": "oxideshop-module", + "name": "d3/oxid-twig-extensions", + "description": "additonal extensions for the OXID Twig template renderer component", + "type": "oxideshop-component", "keywords": [ "oxid", - "modules", - "eShop", + "twig", + "template", + "extensions", "d3" ], "authors": [ @@ -24,30 +25,11 @@ "GPL-3.0-or-later" ], "require": { - "php": ">=7.3", - "oxid-esales/oxideshop-ce": "6.8 - 6.10" - }, - "require-dev": { - "php": "^7.4", - "phpunit/phpunit" : "^9.5", - "friendsofphp/php-cs-fixer": "^3.9", - "phpstan/phpstan": "^1.8" - }, - "extra": { - "oxideshop": { - "blacklist-filter": [ - "*.md", - "composer.json", - ".php-cs-fixer.php", - "*.xml", - "*.neon" - ], - "target-directory": "d3/thismodule" - } + "oxid-esales/oxideshop-ce": "^7.0.0" }, "autoload": { "psr-4": { - "D3\\ThisModule\\": "../../../source/modules/d3/thismodule" + "D3\\OxidTwigExtensions\\": "./src" } } } diff --git a/metadata.php b/metadata.php deleted file mode 100644 index 0773cf8..0000000 --- a/metadata.php +++ /dev/null @@ -1,70 +0,0 @@ - - * @link https://www.oxidmodule.com - */ - -declare(strict_types=1); - -$sMetadataVersion = '2.1'; - -$sModuleId = 'd3moduleid'; -$logo = '(D3)'; - -/** - * Module information - */ -$aModule = [ - 'id' => $sModuleId, - 'title' => $logo.' Module Name', - 'description' => [ - 'de' => '', - 'en' => '', - ], - 'thumbnail' => 'image-file.png', - 'version' => '1.0.0.0', - 'author' => 'D³ Data Development (Inh.: Thomas Dartsch)', - 'email' => 'support@shopmodule.com', - 'url' => 'https://www.oxidmodule.com/', - 'controllers' => [ -// 'd3ControllerAlias' => D3\ThisModule\Application\Controller\Admin\d3Controller::class, - ], - 'extend' => [ -// \OxidEsales\Eshop\Core\ShopControl::class => \D3\ThisModule\Modules\Core\ShopControl_MyModule::class - ], - 'events' => [ -// 'onActivate' => '\D3\ThisModule\Setup\Events::onActivate', -// 'onDeactivate' => '\D3\ThisModule\Setup\Events::onDeactivate', - ], - 'templates' => [ -// 'd3TemplateAlias.tpl' => 'd3/thismodule/Application/views/admin/tpl/d3Template.tpl', -// 'flow_theme' => [ -// 'd3FlowTemplateAlias.tpl' => 'd3/thismodule/Application/views/tpl/d3FlowTheme.tpl', -// ], - ], - 'settings' => [ -// [ -// 'group' => $sModuleId.'_headline', -// 'name' => $sModuleId.'_name', -// 'type' => 'bool', -// 'value' => false, -// ], - ], - 'blocks' => [ -// [ -// 'template' => 'layout/footer.tpl', -// 'block' => 'footer_main', -// 'file' => 'Application/views/blocks/layout/footer_main_mymodule.tpl' -// ] - ], - 'smartyPluginDirectories' => [ -// 'Core/Smarty/Plugin' - ], -]; diff --git a/phpstan.neon b/phpstan.neon deleted file mode 100644 index b6e0779..0000000 --- a/phpstan.neon +++ /dev/null @@ -1,10 +0,0 @@ -parameters: - scanFiles: - - IntelliSenseHelper.php - - ../../oxid-esales/oxideshop-ce/source/oxfunctions.php - - ../../oxid-esales/oxideshop-ce/source/overridablefunctions.php - level: 9 - phpVersion: 70100 - checkMissingIterableValueType: false - ignoreErrors: - - '#Psr\\Container\\ContainerExceptionInterface is not subtype of Throwable#' diff --git a/phpunit.xml b/phpunit.xml deleted file mode 100644 index 516e222..0000000 --- a/phpunit.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - src - - - - - - - ./Tests - - - diff --git a/services.yaml b/services.yaml new file mode 100644 index 0000000..a740e58 --- /dev/null +++ b/services.yaml @@ -0,0 +1,6 @@ +services: + _defaults: + autowire: true + + D3\OxidTwigExtensions\MethodExistsExtension: + tags: ['twig.extension'] \ No newline at end of file diff --git a/src/MethodExistsExtension.php b/src/MethodExistsExtension.php new file mode 100644 index 0000000..820518d --- /dev/null +++ b/src/MethodExistsExtension.php @@ -0,0 +1,50 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\OxidTwigExtensions; + +use InvalidArgumentException; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + +class MethodExistsExtension extends AbstractExtension +{ + /** + * @return TwigFunction[] + */ + public function getFunctions(): array + { + return [new TwigFunction('method_exists', [$this, 'twig_method_exists'], ['is_safe' => ['html']])]; + } + + /** + * @param object $entity + * @param string $method + * + * @return bool + */ + public function twig_method_exists($entity, string $method ): bool + { + if (!is_object($entity)) { + throw new InvalidArgumentException('Given entity is not of type object, got: '.get_class($entity)); + } + + if (empty($method)) { + throw new InvalidArgumentException('Method name empty'); + } + + return method_exists($entity, $method); + } +} \ No newline at end of file