diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml new file mode 100644 index 0000000..26c5802 --- /dev/null +++ b/.github/workflows/continuous-integration.yml @@ -0,0 +1,11 @@ +name: "Continuous Integration" + +on: + pull_request: + push: + branches: + tags: + +jobs: + ci: + uses: laminas/workflow-continuous-integration/.github/workflows/continuous-integration.yml@1.x diff --git a/.github/workflows/cs-tests.yml b/.github/workflows/cs-tests.yml deleted file mode 100644 index e8bbade..0000000 --- a/.github/workflows/cs-tests.yml +++ /dev/null @@ -1,47 +0,0 @@ -on: - - push - -name: Run phpcs checks - -jobs: - mutation: - name: PHP ${{ matrix.php }}-${{ matrix.os }} - - runs-on: ${{ matrix.os }} - - strategy: - matrix: - os: - - ubuntu-latest - - php: - - "8.1" - - "8.2" - - "8.3" - - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Install PHP - uses: shivammathur/setup-php@v2 - with: - php-version: "${{ matrix.php }}" - tools: composer:v2, cs2pr - coverage: none - - - name: Determine composer cache directory - run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV - - - name: Cache dependencies installed with composer - uses: actions/cache@v3 - with: - path: ${{ env.COMPOSER_CACHE_DIR }} - key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} - restore-keys: | - php${{ matrix.php }}-composer- - - name: Install dependencies with composer - run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi - - - name: Run phpcs checks - run: vendor/bin/phpcs diff --git a/.github/workflows/docs-build.yml b/.github/workflows/docs-build.yml new file mode 100644 index 0000000..1a7aa24 --- /dev/null +++ b/.github/workflows/docs-build.yml @@ -0,0 +1,16 @@ +name: docs-build + +on: + release: + types: [published] + workflow_dispatch: + +jobs: + build-deploy: + runs-on: ubuntu-latest + steps: + - name: Build Docs + uses: dotkernel/documentation-theme/github-actions/docs@main + env: + DEPLOY_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml deleted file mode 100644 index 6f7452d..0000000 --- a/.github/workflows/static-analysis.yml +++ /dev/null @@ -1,47 +0,0 @@ -on: - - push - -name: Run static analysis - -jobs: - mutation: - name: PHP ${{ matrix.php }}-${{ matrix.os }} - - runs-on: ${{ matrix.os }} - - strategy: - matrix: - os: - - ubuntu-latest - - php: - - "8.1" - - "8.2" - - "8.3" - - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Install PHP - uses: shivammathur/setup-php@v2 - with: - php-version: "${{ matrix.php }}" - tools: composer:v2, cs2pr - coverage: none - - - name: Determine composer cache directory - run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV - - - name: Cache dependencies installed with composer - uses: actions/cache@v3 - with: - path: ${{ env.COMPOSER_CACHE_DIR }} - key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} - restore-keys: | - php${{ matrix.php }}-composer- - - name: Install dependencies with composer - run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi - - - name: Run static analysis - run: vendor/bin/psalm --no-cache --output-format=github --show-info=false --threads=4 diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml deleted file mode 100644 index 8f6f990..0000000 --- a/.github/workflows/unit-tests.yml +++ /dev/null @@ -1,47 +0,0 @@ -on: - - push - -name: Run PHPUnit tests - -jobs: - mutation: - name: PHP ${{ matrix.php }}-${{ matrix.os }} - - runs-on: ${{ matrix.os }} - - strategy: - matrix: - os: - - ubuntu-latest - - php: - - "8.1" - - "8.2" - - "8.3" - - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Install PHP - uses: shivammathur/setup-php@v2 - with: - php-version: "${{ matrix.php }}" - tools: composer:v2, cs2pr - coverage: none - - - name: Determine composer cache directory - run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV - - - name: Cache dependencies installed with composer - uses: actions/cache@v3 - with: - path: ${{ env.COMPOSER_CACHE_DIR }} - key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} - restore-keys: | - php${{ matrix.php }}-composer- - - name: Install dependencies with composer - run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi - - - name: Run PHPUnit tests - run: vendor/bin/phpunit --colors=always diff --git a/README.md b/README.md index 33960a1..fb052a4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ DotKernel component providing twig extensions and customizations. [![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-twigrenderer)](https://github.com/dotkernel/dot-twigrenderer/stargazers) [![GitHub license](https://img.shields.io/github/license/dotkernel/dot-twigrenderer)](https://github.com/dotkernel/dot-twigrenderer/blob/3.0/LICENSE.md) -[![Build Static](https://github.com/dotkernel/dot-twigrenderer/actions/workflows/static-analysis.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-twigrenderer/actions/workflows/static-analysis.yml) +[![Build Static](https://github.com/dotkernel/dot-twigrenderer/actions/workflows/continuous-integration.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-twigrenderer/actions/workflows/continuous-integration.yml) [![codecov](https://codecov.io/gh/dotkernel/dot-twigrenderer/graph/badge.svg?token=M2WTS4DCKX)](https://codecov.io/gh/dotkernel/dot-twigrenderer) [![SymfonyInsight](https://insight.symfony.com/projects/b9a7d75d-d00a-44a9-b1c0-aea8670681cc/big.svg)](https://insight.symfony.com/projects/b9a7d75d-d00a-44a9-b1c0-aea8670681cc) diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..d2e9839 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,40 @@ +# Security Policy + +## Supported Versions + + +| Version | Supported | PHP Version | +|---------|--------------------|------------------------------------------------------------------------------------------------------------------| +| 3.x | :white_check_mark: | ![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-twigrenderer/3.4.3) | +| <= 2.x | :x: | | + + +## Reporting Potential Security Issues + +If you have encountered a potential security vulnerability in this project, +please report it to us at . We will work with you to +verify the vulnerability and patch it. + +When reporting issues, please provide the following information: + +- Component(s) affected +- A description indicating how to reproduce the issue +- A summary of the security vulnerability and impact + +We request that you contact us via the email address above and give the +project contributors a chance to resolve the vulnerability and issue a new +release prior to any public exposure; this helps protect the project's +users, and provides them with a chance to upgrade and/or update in order to +protect their applications. + + +## Policy + +If we verify a reported security vulnerability, our policy is: + +- We will patch the current release branch, as well as the immediate prior minor + release branch. + +- After patching the release branches, we will immediately issue new security + fix releases for each patched release branch. + diff --git a/docs/book/index.md b/docs/book/index.md new file mode 100644 index 0000000..ae42a26 --- /dev/null +++ b/docs/book/index.md @@ -0,0 +1 @@ +../../README.md diff --git a/docs/book/v3/configuration.md b/docs/book/v3/configuration.md new file mode 100644 index 0000000..ebe3a80 --- /dev/null +++ b/docs/book/v3/configuration.md @@ -0,0 +1,5 @@ +# Configuration + +Register `dot-twigrenderer`'s ConfigProvider in your application's configurations by adding the following line to `config/config.php`: + + \Dot\Twig\ConfigProvider::class, diff --git a/docs/book/v3/examples.md b/docs/book/v3/examples.md new file mode 100644 index 0000000..0441b4b --- /dev/null +++ b/docs/book/v3/examples.md @@ -0,0 +1,9 @@ +# Examples + +* [Authentication Extension](examples/example-authentication-ext.md) +* [Authorization Extension](examples/example-authorization-ext.md) +* [Date Extension](examples/example-date-ext.md) +* [Flash Messenger Extension](examples/example-flash-messenger-ext.md) +* [Form Elements Extension](examples/example-form-elements-ext.md) +* [Navigation Extension](examples/example-navigation-ext.md) +* [Translation Extension](examples/example-translation-ext.md) diff --git a/docs/book/v3/examples/example-authentication-ext.md b/docs/book/v3/examples/example-authentication-ext.md new file mode 100644 index 0000000..bce8239 --- /dev/null +++ b/docs/book/v3/examples/example-authentication-ext.md @@ -0,0 +1,19 @@ +# Using the authentication extension + +Dot-twigrenderer extends Twig with functions that use functionality from [laminas/laminas-authentication](https://github.com/laminas/laminas-authentication) to check if the user is authenticated and to get the authenticated identity object respectively. + +```php +public function hasIdentity(): bool; + +public function getIdentity(): ?IdentityInterface; +``` + +## Example usage + +```twig +{% if hasIdentity() %} + + Welcome, {% getIdentity().username %}! + +{% endif %} +``` diff --git a/docs/book/v3/examples/example-authorization-ext.md b/docs/book/v3/examples/example-authorization-ext.md new file mode 100644 index 0000000..fa51707 --- /dev/null +++ b/docs/book/v3/examples/example-authorization-ext.md @@ -0,0 +1,25 @@ +# Using the authorization extension + +Dot-twigrenderer extends Twig with a function that uses functionality from [dotkernel/dot-authorization](https://github.com/dotkernel/dot-authorization) to check if a logged user is authorized to access a particular resource. + +```php +public function isGranted(string $permission = ''): bool; +``` + +The function returns a boolean value of true if the logged user has access to the requested permission. + +## Example usage + +Expanding on the example from the authentication extension: + +```twig +{% if hasIdentity() %} + + Welcome, {% getIdentity().username %}! + + {% if isGranted({{ role }}) %} + {# your code #} + {% endif %} + +{% endif %} +``` diff --git a/docs/book/v3/examples/example-date-ext.md b/docs/book/v3/examples/example-date-ext.md new file mode 100644 index 0000000..882c10d --- /dev/null +++ b/docs/book/v3/examples/example-date-ext.md @@ -0,0 +1,31 @@ +# Using the date extension + +Dot-twigrenderer extends Twig with a function that calculates the difference between two dates. The function converts dates to a time ago string like Facebook and Twitter has. If `null` is passed as the second or third parameters, the current time will be used. + +```php +public function diff( + Environment $env, + string|DateTimeInterface|null $date, + string|DateTimeZone|null $now = null + ): string; +``` + +## Example usage + +Pass Twig's Environment to the template + +```php +$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/../../templates/page'); +$twigEnv = new \Twig\Environment($loader); + +$this->template->render('page::templateName', [ + "env" => $twigEnv, + #other parameters + ]); +``` + +This enables the use of the `diff` function: + +```twig +{{ diff(env, '2024-02-20', '2024-02-18') }} +``` diff --git a/docs/book/v3/examples/example-flash-messenger-ext.md b/docs/book/v3/examples/example-flash-messenger-ext.md new file mode 100644 index 0000000..adf1436 --- /dev/null +++ b/docs/book/v3/examples/example-flash-messenger-ext.md @@ -0,0 +1,30 @@ +# Using the flash messenger extension + +Dot-twigrenderer extends Twig with functions that use functionality from [dotkernel/dot-flashMessenger](https://github.com/dotkernel/dot-flashMessenger) to list messages or partial messages. + +```php +public function renderMessages( + ?string $type = null, + string $channel = FlashMessengerInterface::DEFAULT_CHANNEL + ): string; // empty response + +public function function renderMessagesPartial( + string $partial, + array $params = [], + ?string $type = null, + string $channel = FlashMessengerInterface::DEFAULT_CHANNEL + ): string; +``` + +`renderMessagesPartial` returns messages previously passed to `dot-flashMessenger`. The last 3 parameters can be omitted to list all messages sent to FlashMessenger. + +* `$partial` is the template file name +* `$params` is an optional array of items (key-value) passed to the template file +* `$type` is an optional item that identifies a subkey of FlashMessenger's channel array +* `$channel` is an optional item that identifies FlashMessenger's channel + +## Example usage + +```twig +{{ renderMessagesPartial('page:partial') }} +``` diff --git a/docs/book/v3/examples/example-form-elements-ext.md b/docs/book/v3/examples/example-form-elements-ext.md new file mode 100644 index 0000000..57adcd3 --- /dev/null +++ b/docs/book/v3/examples/example-form-elements-ext.md @@ -0,0 +1,17 @@ +# Using the form elements extension + +Dot-twigrenderer extends Twig with a function based on `TwigTest` that checks if each `Form` element is an instance of its class. + +```php +public function getTests(): array; +``` + +## Example usage + +```twig +{% if false not in getTests() %} + + {# your code #} + +{% endif %} +``` diff --git a/docs/book/v3/examples/example-navigation-ext.md b/docs/book/v3/examples/example-navigation-ext.md new file mode 100644 index 0000000..5af31ee --- /dev/null +++ b/docs/book/v3/examples/example-navigation-ext.md @@ -0,0 +1,18 @@ +# Using the navigation extension + +Dot-twigrenderer extends Twig with functions that use functionality from [dotkernel/dot-navigation](https://github.com/dotkernel/dot-navigation) to easily parse a menu and to display escaped HTML inside a template. + +```php +public function htmlAttributes(Page $page): string; + +public function renderMenu(NavigationContainer|string $container): string; //incomplete? + +public function renderMenuPartial( + NavigationContainer|string $container, + string $partial, + array $params = [] + ): string; +``` + +* `$partial` is the template file name +* `$params` is an optional array of items (key-value) passed to the template file diff --git a/docs/book/v3/examples/example-translation-ext.md b/docs/book/v3/examples/example-translation-ext.md new file mode 100644 index 0000000..c7760ba --- /dev/null +++ b/docs/book/v3/examples/example-translation-ext.md @@ -0,0 +1,17 @@ +# Using the translation extension + +Dot-twigrenderer extends Twig with a filter called `trans` that parses translations in singular and plural forms. + +The `local.php.dist` file in [frontend](https://github.com/dotkernel/frontend/blob/4.0/config/autoload/local.php.dist) provides an example for working with multiple languages. The `translator` variable can be expanded to other languages using [Poedit](https://poedit.net/) which can edit `.po` files like the example in `data/language/da_DK/LC_MESSAGES/messages.po`. The compiled file will have the extension `.mo` + +To apply the translations + +- the twig templates need either `{% trans 'translateText' %}` or `{{ translateText|trans }}` +- then the js file needs `translateText("translateText")` + +**NOTE:** +In order to have a proper behaviour of language selector, you need the language pack installed at the Operating System level. + +`dnf install glibc-all-langpacks` + +Then restart PHP-FPM. diff --git a/docs/book/v3/installation.md b/docs/book/v3/installation.md new file mode 100644 index 0000000..30c27d9 --- /dev/null +++ b/docs/book/v3/installation.md @@ -0,0 +1,5 @@ +# Installation + +Run the following command in your project folder + + composer require dotkernel/dot-twigrenderer diff --git a/docs/book/v3/usage.md b/docs/book/v3/usage.md new file mode 100644 index 0000000..999b010 --- /dev/null +++ b/docs/book/v3/usage.md @@ -0,0 +1,6 @@ +# Usage + +Basic usage to access and use the twigrenderer object in your services: + +* [Method #1 - Factory](usage/factory.md) +* [Method #2 - Injection](usage/injection.md) diff --git a/docs/book/v3/usage/factory.md b/docs/book/v3/usage/factory.md new file mode 100644 index 0000000..b37da4f --- /dev/null +++ b/docs/book/v3/usage/factory.md @@ -0,0 +1,69 @@ +# Method #1 - Factory + +## Step 1: Create a factory that retrieves the SessionManger from the container + +```php +class ExampleFactory +{ + // code + + public function __invoke(ContainerInterface $container) + { + return new ExampleService( + $container->get(TemplateRendererInterface::class) + ) + } +} +``` + +## Step 2: Access through your Service + +```php + +class ExampleService +{ + private TemplateRendererInterface $template; + + public function __construct(TemplateRendererInterface $template) + { + $this->template = $template; + } + + //your methods +} +``` + +## Step 3: Register the factory + +Open the ConfigProvider of the module where your repository resides. + +Add a new entry under `factories`, where the key is your service's FQCN and the value is your factory's FQCN. + +See below example for a better understanding of the file structure. + +```php + $this->getDependencies(), + ]; + } + + public function getDependencies(): array + { + return [ + 'factories' => [ + ExampleService::class => ExampleFactory::class, + ], + ]; + } +} +``` diff --git a/docs/book/v3/usage/injection.md b/docs/book/v3/usage/injection.md new file mode 100644 index 0000000..c122281 --- /dev/null +++ b/docs/book/v3/usage/injection.md @@ -0,0 +1,60 @@ +# Method #2 - Injection + +If you are using [dot-annotated-services](https://github.com/dotkernel/dot-annotated-services/) in your project, you don't need to create a separate factory, just follow the below steps. + +## Step 1: Access through your Service + +```php + +class ExampleService +{ + private TemplateRendererInterface $template; + + /** + * @Dot\AnnotatedServices\Annotation\Inject({ + * TemplateRendererInterface::class, + * }) + */ + public function __construct(TemplateRendererInterface $template) + { + $this->template = $template; + } + + //your methods +} +``` + +## Step 2: Register your service + +Open the ConfigProvider of the module where your repository resides. + +Add a new entry under `factories`, where the key is your service's FQCN and the value is `Dot\AnnotatedServices\Factory\AbstractAnnotatedFactory`. + +See below example for a better understanding of the file structure. + +```php + $this->getDependencies(), + ]; + } + + public function getDependencies(): array + { + return [ + 'factories' => [ + ExampleService::class => AbstractAnnotatedFactory::class, + ], + ]; + } +} +``` diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 0000000..476c4cc --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1,30 @@ +docs_dir: docs/book +site_dir: docs/html +extra: + project: Packages + current_version: v3 + versions: + - v3 +nav: + - Home: index.md + - v3: + - Installation: v3/installation.md + - Configuration: v3/configuration.md + - Usage: v3/usage.md + - "Examples Overview": v3/examples.md + - Reference: + - "Inject using a factory": v3/usage/factory.md + - "Inject using dot-annotated-services": v3/usage/injection.md + - Examples: + - "Authentication Extension": v3/examples/example-authentication-ext.md + - "Authentication Extension": v3/examples/example-authorization-ext.md + - "Date Extension": v3/examples/example-date-ext.md + - "Flash Messenger Extension": v3/examples/example-flash-messenger-ext.md + - "Form Elements Extension": v3/examples/example-form-elements-ext.md + - "Navigation Extension": v3/examples/example-navigation-ext.md + - "Translation Extension": v3/examples/example-translation-ext.md +site_name: dot-twigrenderer +site_description: "DotKernel component providing twig extensions and customizations" +repo_url: "https://github.com/dotkernel/dot-twigrenderer" +plugins: + - search