From eb6c4977932439a1bac988e7603abc49b92f77aa Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Wed, 8 May 2024 11:30:31 +0200 Subject: [PATCH 01/11] Create a separate cache per tool and branch --- phpstan.neon | 1 + tools/ecs/config/default.php | 2 +- tools/ecs/config/legacy.php | 2 +- tools/ecs/config/template.php | 2 +- tools/phpstan/config/config.php | 17 +++++++++++++++++ 5 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tools/phpstan/config/config.php diff --git a/phpstan.neon b/phpstan.neon index 7f9262abc67..045be2c339a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,4 +1,5 @@ includes: + - tools/phpstan/config/config.php - tools/phpstan/vendor/phpstan/phpstan-phpunit/extension.neon - tools/phpstan/vendor/phpstan/phpstan-phpunit/rules.neon - tools/phpstan/vendor/phpstan/phpstan-symfony/extension.neon diff --git a/tools/ecs/config/default.php b/tools/ecs/config/default.php index 5fa214077a4..1cb8d433797 100644 --- a/tools/ecs/config/default.php +++ b/tools/ecs/config/default.php @@ -34,5 +34,5 @@ $ecsConfig->lineEnding("\n"); $parameters = $ecsConfig->parameters(); - $parameters->set(Option::CACHE_DIRECTORY, sys_get_temp_dir().'/ecs_default_cache'); + $parameters->set(Option::CACHE_DIRECTORY, sys_get_temp_dir().'/ecs/contao413'); }; diff --git a/tools/ecs/config/legacy.php b/tools/ecs/config/legacy.php index a49165684f4..829365d3b73 100644 --- a/tools/ecs/config/legacy.php +++ b/tools/ecs/config/legacy.php @@ -117,5 +117,5 @@ $ecsConfig->lineEnding("\n"); $parameters = $ecsConfig->parameters(); - $parameters->set(Option::CACHE_DIRECTORY, sys_get_temp_dir().'/ecs_legacy_cache'); + $parameters->set(Option::CACHE_DIRECTORY, sys_get_temp_dir().'/ecs/contao413-legacy'); }; diff --git a/tools/ecs/config/template.php b/tools/ecs/config/template.php index bdcc5d078fa..ce059cafcfd 100644 --- a/tools/ecs/config/template.php +++ b/tools/ecs/config/template.php @@ -36,5 +36,5 @@ $parameters = $ecsConfig->parameters(); $parameters->set(Option::FILE_EXTENSIONS, ['html5']); - $parameters->set(Option::CACHE_DIRECTORY, sys_get_temp_dir().'/ecs_template_cache'); + $parameters->set(Option::CACHE_DIRECTORY, sys_get_temp_dir().'/ecs/contao413-template'); }; diff --git a/tools/phpstan/config/config.php b/tools/phpstan/config/config.php new file mode 100644 index 00000000000..3bd81f3d34d --- /dev/null +++ b/tools/phpstan/config/config.php @@ -0,0 +1,17 @@ + [ + 'tmpDir' => sys_get_temp_dir().'/phpstan/contao413', + ], +]; From 06a71403cd18a4e83b8e97c3861baaca07f615d5 Mon Sep 17 00:00:00 2001 From: veronikaplenta <88315148+veronikaplenta@users.noreply.github.com> Date: Wed, 8 May 2024 13:45:12 +0200 Subject: [PATCH 02/11] Fix the CSS class of legacy templates in new elements and modules (see #7202) Description ----------- Fixes using the complete template name instead of the frontend module/content element type as CSS class for legacy template variants Commits ------- c5c89cc9 Use frontend module/content element type as CSS class again --- .../ContentElement/AbstractContentElementController.php | 2 +- .../FrontendModule/AbstractFrontendModuleController.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core-bundle/src/Controller/ContentElement/AbstractContentElementController.php b/core-bundle/src/Controller/ContentElement/AbstractContentElementController.php index 5bb466f5956..b6014dec94a 100644 --- a/core-bundle/src/Controller/ContentElement/AbstractContentElementController.php +++ b/core-bundle/src/Controller/ContentElement/AbstractContentElementController.php @@ -72,7 +72,7 @@ protected function addDefaultDataToTemplate(FragmentTemplate $template, array $m if ($this->isLegacyTemplate($template->getName())) { // Legacy fragments $this->addHeadlineToTemplate($template, $modelData['headline'] ?? null); - $this->addCssAttributesToTemplate($template, $template->getName(), $modelData['cssID'] ?? null, $classes); + $this->addCssAttributesToTemplate($template, 'ce_'.$this->getType(), $modelData['cssID'] ?? null, $classes); $this->addPropertiesToTemplate($template, $properties); $this->addSectionToTemplate($template, $section); diff --git a/core-bundle/src/Controller/FrontendModule/AbstractFrontendModuleController.php b/core-bundle/src/Controller/FrontendModule/AbstractFrontendModuleController.php index 3443943fe38..dd1cadd36e3 100644 --- a/core-bundle/src/Controller/FrontendModule/AbstractFrontendModuleController.php +++ b/core-bundle/src/Controller/FrontendModule/AbstractFrontendModuleController.php @@ -77,7 +77,7 @@ protected function addDefaultDataToTemplate(FragmentTemplate $template, array $m if ($this->isLegacyTemplate($template->getName())) { // Legacy fragments $this->addHeadlineToTemplate($template, $modelData['headline'] ?? null); - $this->addCssAttributesToTemplate($template, $template->getName(), $modelData['cssID'] ?? null, $classes); + $this->addCssAttributesToTemplate($template, 'mod_'.$this->getType(), $modelData['cssID'] ?? null, $classes); $this->addPropertiesToTemplate($template, $properties); $this->addSectionToTemplate($template, $section); From 3ee448d1a33c3317e98f6d3f2b25ffc64d2c3d0c Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Wed, 8 May 2024 17:33:22 +0200 Subject: [PATCH 03/11] Enable colors in the GitHub actions log files (see #7193) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description ----------- @contao/developers I‘m not sure if this is any better than adding `--ansi` flags to the individual commands. WDYT? Commits ------- 4fc54608 Set `TERM: xterm` in the GitHub workflow files 5c8ab457 Try changing the default shell 7c467c12 Adjust the other .yml files, too cf64c06d Hide the progress bar in the checkout action b6e4897a Simplify the YAMLlint job --- .github/workflows/ci.yml | 62 ++++++++++++++++++++++++---------- .github/workflows/coverage.yml | 9 ++++- .github/workflows/split.yml | 9 ++++- 3 files changed, 60 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bcb016d21a6..5d5c2c9a949 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,6 +5,11 @@ on: permissions: read-all +defaults: + run: + # Simulate an interactive terminal with color support + shell: script -q -e -c "export TERM=xterm; bash {0}" + jobs: rector: name: Rector @@ -18,7 +23,9 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Install the dependencies run: | @@ -40,7 +47,9 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Install the dependencies run: | @@ -62,7 +71,9 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Install the dependencies run: | @@ -86,7 +97,9 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Install the dependencies run: | @@ -101,14 +114,12 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Run YAMLlint - run: | - python3 -m pip install --upgrade pip - python3 -m pip install --upgrade setuptools wheel - python3 -m pip install --upgrade yamllint - /home/runner/.local/bin/yamllint . + run: yamllint --format=github . service-linter: name: Service linter @@ -122,7 +133,9 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Install the dependencies run: | @@ -144,9 +157,10 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 + show-progress: false - name: Install the dependencies run: | @@ -181,7 +195,9 @@ jobs: mysql -uroot -proot -e "CREATE database contao_test" - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Install the dependencies run: composer install --no-interaction --no-progress @@ -211,7 +227,9 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Install the dependencies run: composer install --no-interaction --no-progress @@ -237,7 +255,9 @@ jobs: mysql -uroot -proot -e "CREATE database contao_test" - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Install the dependencies run: composer install --ignore-platform-req=php+ --no-interaction --no-progress @@ -267,7 +287,9 @@ jobs: mysql -uroot -proot -e "CREATE database contao_test" - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Install the dependencies run: composer update --prefer-lowest --prefer-stable --no-interaction --no-progress @@ -297,7 +319,9 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Test the single bundles run: | @@ -337,7 +361,9 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Install the dependencies run: | diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 82a9157764c..b7cd6e7d752 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -10,6 +10,11 @@ on: permissions: read-all +defaults: + run: + # Simulate an interactive terminal with color support + shell: script -q -e -c "export TERM=xterm; bash {0}" + jobs: coverage: name: Codecov @@ -23,7 +28,9 @@ jobs: coverage: pcov - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Install the dependencies run: composer install --no-interaction --no-progress diff --git a/.github/workflows/split.yml b/.github/workflows/split.yml index 4af23f3f984..3759cee3115 100644 --- a/.github/workflows/split.yml +++ b/.github/workflows/split.yml @@ -9,6 +9,11 @@ on: permissions: read-all +defaults: + run: + # Simulate an interactive terminal with color support + shell: script -q -e -c "export TERM=xterm; bash {0}" + jobs: monorepo-split: name: Monorepo split @@ -22,7 +27,9 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Cache the monorepo split uses: actions/cache@v3 From e808972d7cbc647fc099cca6519658ff7674198c Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Wed, 8 May 2024 18:44:54 +0200 Subject: [PATCH 04/11] Use a Composer action that supports caching (see #7204) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description ----------- Let‘s see if that speeds up the actions. Commits ------- 11e0688b Use a Composer action that supports caching --- .github/workflows/ci.yml | 103 +++++++++++++++++++++------------ .github/workflows/coverage.yml | 4 +- 2 files changed, 68 insertions(+), 39 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d5c2c9a949..12ce92b41a5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,10 +27,13 @@ jobs: with: show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer bin rector install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install Rector + uses: ramsey/composer-install@v3 + with: + working-directory: vendor-bin/rector - name: Run Rector run: vendor-bin/rector/vendor/bin/rector --dry-run --no-progress-bar @@ -51,10 +54,13 @@ jobs: with: show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer bin ecs install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install ECS + uses: ramsey/composer-install@v3 + with: + working-directory: vendor-bin/ecs - name: Run ECS run: vendor-bin/ecs/vendor/bin/ecs check --no-progress-bar @@ -75,10 +81,13 @@ jobs: with: show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer bin ecs install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install ECS + uses: ramsey/composer-install@v3 + with: + working-directory: vendor-bin/ecs - name: Run ECS run: | @@ -101,10 +110,13 @@ jobs: with: show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer bin phpstan install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install PHPStan + uses: ramsey/composer-install@v3 + with: + working-directory: vendor-bin/phpstan - name: Run PHPStan run: vendor-bin/phpstan/vendor/bin/phpstan analyse --no-progress @@ -137,10 +149,13 @@ jobs: with: show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer bin service-linter install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install the service linter + uses: ramsey/composer-install@v3 + with: + working-directory: vendor-bin/service-linter - name: Run the service linter run: vendor-bin/service-linter/bin/lint-service-ids @@ -162,11 +177,18 @@ jobs: fetch-depth: 0 show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer -dcore-bundle install --no-interaction --no-progress - composer bin depcheck install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install the core bundle + uses: ramsey/composer-install@v3 + with: + working-directory: core-bundle + + - name: Install depcheck + uses: ramsey/composer-install@v3 + with: + working-directory: vendor-bin/depcheck - name: Check for unknown symbols (contao/contao) run: vendor-bin/depcheck/vendor/bin/composer-dependency-analyser --config=depcheck.php @@ -199,8 +221,8 @@ jobs: with: show-progress: false - - name: Install the dependencies - run: composer install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 - name: Run the unit tests run: vendor/bin/phpunit @@ -231,8 +253,8 @@ jobs: with: show-progress: false - - name: Install the dependencies - run: composer install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 - name: Run the unit tests in reverse order run: vendor/bin/phpunit --order-by=reverse --extensions Contao\\CoreBundle\\Tests\\PhpunitExtension\\GlobalStateWatcher @@ -259,8 +281,10 @@ jobs: with: show-progress: false - - name: Install the dependencies - run: composer install --ignore-platform-req=php+ --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + with: + composer-options: --ignore-platform-req=php+ - name: Run the unit tests run: vendor/bin/phpunit @@ -291,8 +315,10 @@ jobs: with: show-progress: false - - name: Install the dependencies - run: composer update --prefer-lowest --prefer-stable --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + with: + dependency-versions: lowest - name: Run the unit tests run: vendor/bin/phpunit @@ -365,10 +391,13 @@ jobs: with: show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer bin monorepo-tools install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install PHPStan + uses: ramsey/composer-install@v3 + with: + working-directory: vendor-bin/monorepo-tools - name: Validate the composer.json files run: vendor-bin/monorepo-tools/vendor/bin/monorepo-tools composer-json --validate diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index b7cd6e7d752..a0bcdc98001 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -32,8 +32,8 @@ jobs: with: show-progress: false - - name: Install the dependencies - run: composer install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 - name: Generate the coverage report run: php -d pcov.enabled=1 vendor/bin/phpunit --testsuite=coverage --coverage-clover=clover.xml From 688114f8b6e06e790222763a96b37824f16da0de Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Fri, 10 May 2024 11:20:55 +0200 Subject: [PATCH 05/11] Update all workflow actions to their latest version (see #7206) Description ----------- Commits ------- 07cb83b0 Update all workflow actions to their latest version --- .github/workflows/ci.yml | 10 +++++----- .github/workflows/pr.yml | 2 +- .github/workflows/split.yml | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 12ce92b41a5..1140fc45171 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -180,11 +180,6 @@ jobs: - name: Install Contao uses: ramsey/composer-install@v3 - - name: Install the core bundle - uses: ramsey/composer-install@v3 - with: - working-directory: core-bundle - - name: Install depcheck uses: ramsey/composer-install@v3 with: @@ -193,6 +188,11 @@ jobs: - name: Check for unknown symbols (contao/contao) run: vendor-bin/depcheck/vendor/bin/composer-dependency-analyser --config=depcheck.php + - name: Install the core bundle + uses: ramsey/composer-install@v3 + with: + working-directory: core-bundle + - name: Check for unknown symbols (contao/core-bundle) run: vendor-bin/depcheck/vendor/bin/composer-dependency-analyser --config=depcheck.php --composer-json=core-bundle/composer.json diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 604f89cee13..716012f263b 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -14,7 +14,7 @@ jobs: continue-on-error: true steps: - name: Assign the PR - uses: toshimaru/auto-author-assign@v1.4.0 + uses: toshimaru/auto-author-assign@v2.1.0 - name: Assign the milestone uses: zoispag/action-assign-milestone@v1 diff --git a/.github/workflows/split.yml b/.github/workflows/split.yml index 3759cee3115..f3b191d391a 100644 --- a/.github/workflows/split.yml +++ b/.github/workflows/split.yml @@ -32,13 +32,13 @@ jobs: show-progress: false - name: Cache the monorepo split - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: .monorepo-split-cache key: dev-${GITHUB_REF##*/} - name: Install the dependencies - run: composer global require contao/monorepo-tools:dev-main + run: composer global require contao/monorepo-tools:^0.2 - name: Split the monorepo run: $HOME/.composer/vendor/bin/monorepo-tools split ${GITHUB_REF##*/} ${{ github.event.forced && '--force-push' || '' }} From 119935ed169668222447530174f33139e5855b54 Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Fri, 10 May 2024 11:46:04 +0200 Subject: [PATCH 06/11] Update the CI workflows (see #7207) Description ----------- Commits ------- 5884a342 Update the CI workflows b2206fdd Use codecov-action@v4 61867d8d Fix the ci.yml file ac0c6dfe Fix the reverse order tests --- .github/workflows/ci.yml | 133 ++++++++++++------ .github/workflows/coverage.yml | 15 +- .github/workflows/pr.yml | 3 +- .github/workflows/split.yml | 15 +- .../tests/Command/ContaoSetupCommandTest.php | 8 ++ 5 files changed, 119 insertions(+), 55 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c2913def722..2f99f631659 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,6 +5,11 @@ on: permissions: read-all +defaults: + run: + # Simulate an interactive terminal with color support + shell: script -q -e -c "export TERM=xterm; bash {0}" + jobs: ecs: name: ECS @@ -18,12 +23,17 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer bin ecs install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install ECS + uses: ramsey/composer-install@v3 + with: + working-directory: tools/ecs - name: Run ECS run: tools/ecs/vendor/bin/ecs check *-bundle/bin *-bundle/src *-bundle/tests tools/*/src --config tools/ecs/config/default.php --no-progress-bar @@ -40,12 +50,17 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer bin ecs install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install ECS + uses: ramsey/composer-install@v3 + with: + working-directory: tools/ecs - name: Run ECS run: | @@ -64,12 +79,17 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer bin phpstan install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install PHPStan + uses: ramsey/composer-install@v3 + with: + working-directory: tools/phpstan - name: Run PHPStan run: tools/phpstan/vendor/bin/phpstan analyse --no-progress @@ -79,14 +99,12 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Run YAMLlint - run: | - python3 -m pip install --upgrade pip - python3 -m pip install --upgrade setuptools wheel - python3 -m pip install --upgrade yamllint - /home/runner/.local/bin/yamllint . + run: yamllint --format=github . service-linter: name: Service linter @@ -100,12 +118,17 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer bin service-linter install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install the service linter + uses: ramsey/composer-install@v3 + with: + working-directory: tools/service-linter - name: Run the service linter run: tools/service-linter/bin/lint-service-ids @@ -122,9 +145,10 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 + show-progress: false - name: Install the dependencies run: | @@ -167,10 +191,12 @@ jobs: mysql -uroot -proot -e "CREATE database contao_test" - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - - name: Install the dependencies - run: composer install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 - name: Run the unit tests run: vendor/bin/phpunit @@ -197,10 +223,12 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - - name: Install the dependencies - run: composer install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 - name: Run the unit tests in reverse order run: vendor/bin/phpunit --order-by=reverse --extensions Contao\\CoreBundle\\Tests\\PhpunitExtension\\GlobalStateWatcher @@ -223,10 +251,14 @@ jobs: mysql -uroot -proot -e "CREATE database contao_test" - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - - name: Install the dependencies - run: composer install --ignore-platform-req=php --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + with: + composer-options: --ignore-platform-req=php+ - name: Run the unit tests run: vendor/bin/phpunit @@ -253,10 +285,14 @@ jobs: mysql -uroot -proot -e "CREATE database contao_test" - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - - name: Install the dependencies - run: composer update --prefer-lowest --prefer-stable --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + with: + dependency-versions: lowest - name: Run the unit tests run: vendor/bin/phpunit @@ -283,7 +319,9 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Test the single bundles run: | @@ -327,12 +365,17 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - - name: Install the dependencies - run: | - composer install --no-interaction --no-progress - composer bin monorepo install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 + + - name: Install the monorepo tools + uses: ramsey/composer-install@v3 + with: + working-directory: tools/monorepo - name: Validate the composer.json files run: tools/monorepo/vendor/bin/monorepo-tools composer-json --validate diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index d6217ca5209..884b8bd21e0 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -10,6 +10,11 @@ on: permissions: read-all +defaults: + run: + # Simulate an interactive terminal with color support + shell: script -q -e -c "export TERM=xterm; bash {0}" + jobs: coverage: name: Codecov @@ -23,16 +28,18 @@ jobs: coverage: pcov - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - - name: Install the dependencies - run: composer install --no-interaction --no-progress + - name: Install Contao + uses: ramsey/composer-install@v3 - name: Generate the coverage report run: php -d pcov.enabled=1 vendor/bin/phpunit --testsuite=coverage --coverage-clover=clover.xml - name: Upload the coverage report - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v4 with: files: ./clover.xml fail_ci_if_error: true diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 77abb343b65..2ab60e7e057 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -5,7 +5,6 @@ on: types: [opened] permissions: - issues: write pull-requests: write jobs: @@ -15,7 +14,7 @@ jobs: continue-on-error: true steps: - name: Assign the author - uses: toshimaru/auto-author-assign@v1.4.0 + uses: toshimaru/auto-author-assign@v2.1.0 - name: Assign the milestone uses: zoispag/action-assign-milestone@v1 diff --git a/.github/workflows/split.yml b/.github/workflows/split.yml index f5fec7e8d25..611b70fc1b2 100644 --- a/.github/workflows/split.yml +++ b/.github/workflows/split.yml @@ -9,6 +9,11 @@ on: permissions: read-all +defaults: + run: + # Simulate an interactive terminal with color support + shell: script -q -e -c "export TERM=xterm; bash {0}" + jobs: monorepo-split: name: Monorepo split @@ -22,18 +27,20 @@ jobs: coverage: none - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + show-progress: false - name: Cache the monorepo split - uses: actions/cache@v1 + uses: actions/cache@v4 with: path: .monorepo-split-cache key: dev-${GITHUB_REF##*/} - name: Install the dependencies - run: composer global require contao/monorepo-tools:dev-main + run: composer global require contao/monorepo-tools:^0.2 - name: Split the monorepo - run: $HOME/.composer/vendor/bin/monorepo-tools split ${GITHUB_REF##*/} + run: $HOME/.composer/vendor/bin/monorepo-tools split ${GITHUB_REF##*/} ${{ github.event.forced && '--force-push' || '' }} env: GITHUB_TOKEN: ${{ secrets.MONOREPO_SPLIT_TOKEN }} diff --git a/manager-bundle/tests/Command/ContaoSetupCommandTest.php b/manager-bundle/tests/Command/ContaoSetupCommandTest.php index b15abccf06d..d4cd3b9776d 100644 --- a/manager-bundle/tests/Command/ContaoSetupCommandTest.php +++ b/manager-bundle/tests/Command/ContaoSetupCommandTest.php @@ -15,6 +15,7 @@ use Contao\ManagerBundle\Command\ContaoSetupCommand; use Contao\TestCase\ContaoTestCase; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Terminal; use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Dotenv\Dotenv; use Symfony\Component\Filesystem\Filesystem; @@ -24,6 +25,13 @@ class ContaoSetupCommandTest extends ContaoTestCase { + protected function tearDown(): void + { + $this->resetStaticProperties([Terminal::class]); + + parent::tearDown(); + } + public function testIsHidden(): void { $command = new ContaoSetupCommand('project/dir', 'project/dir/public', 'secret'); From 660043b847a2ca953ea0d2bb313306ad28a33370 Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Fri, 10 May 2024 11:46:56 +0200 Subject: [PATCH 07/11] Use version 4 of codecov/codecov-action --- .github/workflows/ci.yml | 2 +- .github/workflows/coverage.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1140fc45171..2a9e828e5df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -394,7 +394,7 @@ jobs: - name: Install Contao uses: ramsey/composer-install@v3 - - name: Install PHPStan + - name: Install the monorepo tools uses: ramsey/composer-install@v3 with: working-directory: vendor-bin/monorepo-tools diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index a0bcdc98001..09efe832906 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -39,7 +39,7 @@ jobs: run: php -d pcov.enabled=1 vendor/bin/phpunit --testsuite=coverage --coverage-clover=clover.xml - name: Upload the coverage report - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v4 with: files: ./clover.xml fail_ci_if_error: true From 59eb1b9cd8507d2946878288f8bc930bacad7455 Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Fri, 10 May 2024 12:14:43 +0200 Subject: [PATCH 08/11] Try out the Twig CS fixer (see #7143) Description ----------- I played with the [Twig CS Fixer](https://github.com/VincentLanglet/Twig-CS-Fixer) and this is roughly what it suggests to change. There were some false positives that I couldn't suppress, so we can't add it to our CI chain yet, but we can discuss the suggestions. The latter was already reported in https://github.com/VincentLanglet/Twig-CS-Fixer/issues/161. Commits ------- b1bd73a6 Try out the Twig CS fixer --- .../templates/twig/component/_picture.html.twig | 12 ++++++------ .../twig/content_element/description_list.html.twig | 6 +++--- core-bundle/templates/Collector/contao.html.twig | 2 +- .../Frontend/preview_toolbar_base_js.html.twig | 8 ++++---- .../templates/Image/Studio/_macros.html.twig | 13 ++++++------- core-bundle/templates/Image/Studio/figure.html.twig | 2 +- 6 files changed, 21 insertions(+), 22 deletions(-) diff --git a/core-bundle/contao/templates/twig/component/_picture.html.twig b/core-bundle/contao/templates/twig/component/_picture.html.twig index 940291fedd8..8adea47869f 100644 --- a/core-bundle/contao/templates/twig/component/_picture.html.twig +++ b/core-bundle/contao/templates/twig/component/_picture.html.twig @@ -27,15 +27,15 @@ {# Just an image (no sources) #} {% block image %} {% set img = figure.image.img %} - {% set defineProportions = img.width|default(false) and img.height|default(false) %} + {% set define_proportions = img.width|default(false) and img.height|default(false) %} {% set img_attributes = attrs() .set('src', img.src) .set('alt', figure.hasMetadata ? figure.metadata.alt|insert_tag : '') .setIfExists('title', figure.hasMetadata ? figure.metadata.title|insert_tag : null) .setIfExists('srcset', img.srcset is defined and img.srcset != img.src ? img.srcset : null) .setIfExists('sizes', img.sizes|default) - .setIfExists('width', defineProportions ? img.width : null) - .setIfExists('height', defineProportions ? img.height : null) + .setIfExists('width', define_proportions ? img.width : null) + .setIfExists('height', define_proportions ? img.height : null) .setIfExists('loading', img.loading|default) .addClass(img.class|default) .mergeWith(figure.options.img_attr|default) @@ -53,14 +53,14 @@ {% block sources %} {% for source in figure.image.sources %} {% block source %} - {% set defineProportions = source.width|default(false) and source.height|default(false) %} + {% set define_proportions = source.width|default(false) and source.height|default(false) %} {% set source_attributes = attrs() .set('srcset', source.srcset) .setIfExists('sizes', source.sizes|default) .setIfExists('media', source.media|default) .setIfExists('type', source.type|default) - .setIfExists('width', defineProportions ? source.width : null) - .setIfExists('height', defineProportions ? source.height : null) + .setIfExists('width', define_proportions ? source.width : null) + .setIfExists('height', define_proportions ? source.height : null) .mergeWith(figure.options.picture_attr|default) .mergeWith(source_attributes|default) %} diff --git a/core-bundle/contao/templates/twig/content_element/description_list.html.twig b/core-bundle/contao/templates/twig/content_element/description_list.html.twig index 03d63f055e6..fc4cfa1457a 100644 --- a/core-bundle/contao/templates/twig/content_element/description_list.html.twig +++ b/core-bundle/contao/templates/twig/content_element/description_list.html.twig @@ -1,14 +1,14 @@ {% extends "@Contao/content_element/_base.html.twig" %} {% block content %} - + {% for description in descriptions %} {% block description %} {% if description.term is not empty %} - {{ description.term|insert_tag_raw }} + {{ description.term|insert_tag_raw }} {% endif %} {% if description.details is not empty %} - {{ description.details|insert_tag_raw }} + {{ description.details|insert_tag_raw }} {% endif %} {% endblock %} {% endfor %} diff --git a/core-bundle/templates/Collector/contao.html.twig b/core-bundle/templates/Collector/contao.html.twig index 79504386ea1..f4cbd6cb52b 100644 --- a/core-bundle/templates/Collector/contao.html.twig +++ b/core-bundle/templates/Collector/contao.html.twig @@ -37,7 +37,7 @@ {% endset %} - {{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { link: true, name: 'contao', additional_classes: (((collector.summary.preview) ? 'sf-toolbar-status-yellow ' : '') ~ 'sf-toolbar-block-right') }) }} + {{ include('@WebProfiler/Profiler/toolbar_item.html.twig', {link: true, name: 'contao', additional_classes: (((collector.summary.preview) ? 'sf-toolbar-status-yellow ' : '') ~ 'sf-toolbar-block-right')}) }} {% endblock %} {% block menu %} diff --git a/core-bundle/templates/Frontend/preview_toolbar_base_js.html.twig b/core-bundle/templates/Frontend/preview_toolbar_base_js.html.twig index bbf07c02a48..010f77ac9d0 100644 --- a/core-bundle/templates/Frontend/preview_toolbar_base_js.html.twig +++ b/core-bundle/templates/Frontend/preview_toolbar_base_js.html.twig @@ -1,6 +1,6 @@ {% do csp_handler.addSource('img-src', 'data:') %} -{% set styleNonce = csp_handler.getNonce('style-src') %} - +{% set style_nonce = csp_handler.getNonce('style-src') %} + .cto-toolbar { font-family: -apple-system,system-ui,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif; font-weight: 400; @@ -208,8 +208,8 @@ } -{% set scriptNonce = csp_handler.getNonce('script-src') %} - +{% set script_nonce = csp_handler.getNonce('script-src') %} + (function () { const toolbar = document.createElement('div'); toolbar.classList.add('cto-toolbar'); diff --git a/core-bundle/templates/Image/Studio/_macros.html.twig b/core-bundle/templates/Image/Studio/_macros.html.twig index 1deeb0be5ac..c4142308677 100644 --- a/core-bundle/templates/Image/Studio/_macros.html.twig +++ b/core-bundle/templates/Image/Studio/_macros.html.twig @@ -40,7 +40,6 @@ : #} - {# Build a
including a picture and - if available - a caption from Studio\Figure data. @@ -81,14 +80,14 @@ {%- if figure.image.sources -%} {% for source in figure.image.sources %} - {%- set defineProportions = source.width|default(false) and source.height|default(false) -%} + {%- set define_proportions = source.width|default(false) and source.height|default(false) -%} {%- set base_attributes = { 'srcset': source.srcset, 'sizes': source.sizes|default(null), 'media': source.media|default(null), 'type': source.type|default(null), - 'width': defineProportions ? source.width : null, - 'height': defineProportions ? source.height : null, + 'width': define_proportions ? source.width : null, + 'height': define_proportions ? source.height : null, } -%} {%- endfor %} @@ -108,7 +107,7 @@ {% set img_attributes = figure.options.img_attr|default({})|merge(options.img_attr|default({})) %} {% set img = figure.image.img %} - {% set defineProportions = img.width|default(false) and img.height|default(false) %} + {% set define_proportions = img.width|default(false) and img.height|default(false) %} {% set base_attributes = { 'src': img.src, @@ -116,8 +115,8 @@ 'title': figure.hasMetadata ? (figure.metadata.title ?: null) : null, 'srcset': img.srcset is defined and img.srcset != img.src ? img.srcset : null, 'sizes': img.sizes|default(null), - 'width': defineProportions ? img.width : null, - 'height': defineProportions ? img.height : null, + 'width': define_proportions ? img.width : null, + 'height': define_proportions ? img.height : null, 'loading': img.loading|default(null), 'class': img.class|default(null), } %} diff --git a/core-bundle/templates/Image/Studio/figure.html.twig b/core-bundle/templates/Image/Studio/figure.html.twig index f995e918475..027ae090b0e 100644 --- a/core-bundle/templates/Image/Studio/figure.html.twig +++ b/core-bundle/templates/Image/Studio/figure.html.twig @@ -1,5 +1,5 @@ {% import "@ContaoCore/Image/Studio/_macros.html.twig" as studio %} {{- studio.figure(figure, figure.options|default({})|merge({ - attr: { class: ('image_container ' ~ figure.options.attr.class|default(''))|trim } + attr: {class: ('image_container ' ~ figure.options.attr.class|default(''))|trim} })) -}} From 133d636ea14c96dc204e4fbb34dbed0b667836e9 Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Tue, 14 May 2024 15:12:26 +0200 Subject: [PATCH 09/11] Fix the unit tests (see #7215) Description ----------- Commits ------- b050a67c Fix the unit tests --- .../src/Twig/Extension/ContaoExtension.php | 42 ++++++++----------- .../Twig/Extension/ContaoExtensionTest.php | 37 ++++++++-------- 2 files changed, 38 insertions(+), 41 deletions(-) diff --git a/core-bundle/src/Twig/Extension/ContaoExtension.php b/core-bundle/src/Twig/Extension/ContaoExtension.php index f37db7562ac..9d2b5e25dc9 100644 --- a/core-bundle/src/Twig/Extension/ContaoExtension.php +++ b/core-bundle/src/Twig/Extension/ContaoExtension.php @@ -56,6 +56,12 @@ public function __construct(Environment $environment, ContaoFilesystemLoader $fi /** @var EscaperExtension $escaperExtension */ $escaperExtension = $environment->getExtension(EscaperExtension::class); + + // Forward compatibility with twig/twig >=3.10.0 + if (method_exists($escaperExtension, 'setEnvironment')) { + $escaperExtension->setEnvironment($environment); + } + $escaperExtension->setEscaper('contao_html', [$contaoEscaper, 'escapeHtml']); $escaperExtension->setEscaper('contao_html_attr', [$contaoEscaper, 'escapeHtmlAttr']); @@ -171,43 +177,31 @@ public function getFilters(): array if (ChunkedText::TYPE_RAW === $type) { $parts[] = $chunk; } else { - // Forward compatibility with twig/twig 4 - if (method_exists(EscaperExtension::class, 'escape')) { - $parts[] = EscaperExtension::escape($env, $chunk, $strategy, $charset); - } else { - $parts[] = twig_escape_filter($env, $chunk, $strategy, $charset); - } + $parts[] = twig_escape_filter($env, $chunk, $strategy, $charset); } } return implode('', $parts); } - // Forward compatibility with twig/twig 4 - if (method_exists(EscaperExtension::class, 'escape')) { - return EscaperExtension::escape($env, $string, $strategy, $charset, $autoescape); - } - return twig_escape_filter($env, $string, $strategy, $charset, $autoescape); }; $twigEscaperFilterIsSafe = static function (Node $filterArgs): array { - // Our escaper strategy variants that tolerate input encoding are - // also safe in the original context (e.g. for the filter argument - // 'contao_html' we will return ['contao_html', 'html']). - if ( - ($expression = iterator_to_array($filterArgs)[0] ?? null) instanceof ConstantExpression - && \in_array($value = $expression->getAttribute('value'), ['contao_html', 'contao_html_attr'], true) - ) { - return [$value, substr($value, 7)]; - } + $expression = iterator_to_array($filterArgs)[0] ?? null; - // Backwards compatibility with twig/twig <3.9 - if (\function_exists('twig_escape_filter_is_safe')) { - return twig_escape_filter_is_safe($filterArgs); + if ($expression instanceof ConstantExpression) { + $value = $expression->getAttribute('value'); + + // Our escaper strategy variants that tolerate input encoding are + // also safe in the original context (e.g. for the filter argument + // 'contao_html' we will return ['contao_html', 'html']). + if (\in_array($value, ['contao_html', 'contao_html_attr'], true)) { + return [$value, substr($value, 7)]; + } } - return EscaperExtension::escapeFilterIsSafe($filterArgs); + return twig_escape_filter_is_safe($filterArgs); }; return [ diff --git a/core-bundle/tests/Twig/Extension/ContaoExtensionTest.php b/core-bundle/tests/Twig/Extension/ContaoExtensionTest.php index f53761b2d19..0f79f0e5ded 100644 --- a/core-bundle/tests/Twig/Extension/ContaoExtensionTest.php +++ b/core-bundle/tests/Twig/Extension/ContaoExtensionTest.php @@ -37,6 +37,7 @@ use Twig\Node\Node; use Twig\Node\TextNode; use Twig\NodeTraverser; +use Twig\Runtime\EscaperRuntime; use Twig\Source; use Twig\TwigFilter; use Twig\TwigFunction; @@ -159,6 +160,14 @@ public function testThrowsIfCoreIncludeFunctionIsNotFound(): void ]) ; + // Forward compatibility with twig/twig >=3.10.0 + if (class_exists(EscaperRuntime::class)) { + $environment + ->method('getRuntime') + ->willReturn(new EscaperRuntime()) + ; + } + $extension = new ContaoExtension($environment, $this->createMock(ContaoFilesystemLoader::class)); $this->expectException(\RuntimeException::class); @@ -367,18 +376,11 @@ public function testContaoUsesCorrectTwigFunctionSignatures($reflector, array $e public function provideTwigFunctionSignatures(): \Generator { - // Make sure the functions outside the class scope are loaded + // Backwards compatibility with twig/twig <3.9.0 new \ReflectionClass(EscaperExtension::class); - // Forward compatibility with twig/twig 4 - if (method_exists(EscaperExtension::class, 'escape')) { - $escape = new \ReflectionMethod(EscaperExtension::class.'::escape'); - } else { - $escape = new \ReflectionFunction('twig_escape_filter'); - } - yield [ - $escape, + new \ReflectionFunction('twig_escape_filter'), [ [Environment::class, 'env'], [null, 'string'], @@ -388,14 +390,7 @@ public function provideTwigFunctionSignatures(): \Generator ], ]; - // Backwards compatibility with twig/twig <3.9 - if (\function_exists('twig_escape_filter_is_safe')) { - $escapeIsSafe = new \ReflectionFunction('twig_escape_filter_is_safe'); - } else { - $escapeIsSafe = new \ReflectionMethod(EscaperExtension::class.'::escapeFilterIsSafe'); - } - - yield [$escapeIsSafe, [[Node::class, 'filterArgs']]]; + yield [new \ReflectionFunction('twig_escape_filter_is_safe'), [[Node::class, 'filterArgs']]]; } /** @@ -414,6 +409,14 @@ private function getContaoExtension(Environment $environment = null, ContaoFiles ]) ; + // Forward compatibility with twig/twig >=3.10.0 + if (class_exists(EscaperRuntime::class)) { + $environment + ->method('getRuntime') + ->willReturn(new EscaperRuntime()) + ; + } + return new ContaoExtension($environment, $filesystemLoader); } } From ce0526391d402f0b053312248ca47e92c7e4ae35 Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Tue, 14 May 2024 15:20:39 +0200 Subject: [PATCH 10/11] Add a conflict for twig/twig 3.10.0 and 3.10.1 --- composer.json | 2 +- core-bundle/composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 37a1040a566..0c6fddc90a8 100644 --- a/composer.json +++ b/composer.json @@ -190,7 +190,7 @@ "terminal42/contao-ce-access": "<3.0", "thecodingmachine/safe": "<1.2", "twig/intl-extra": "3.9.0", - "twig/twig": "3.9.0", + "twig/twig": "3.9.0 || 3.10.0 || 3.10.1", "zendframework/zend-code": "<3.3.1" }, "autoload": { diff --git a/core-bundle/composer.json b/core-bundle/composer.json index 923fb77049e..9fea6d3d691 100644 --- a/core-bundle/composer.json +++ b/core-bundle/composer.json @@ -164,7 +164,7 @@ "terminal42/contao-ce-access": "<3.0", "thecodingmachine/safe": "<1.2", "twig/intl-extra": "3.9.0", - "twig/twig": "3.9.0" + "twig/twig": "3.9.0 || 3.10.0 || 3.10.1" }, "suggest": { "contao/tcpdf-bundle": "To export articles as PDF files" From 95ac936f88765af00e9c4d790708e70fc2f3e47e Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Wed, 15 May 2024 17:06:03 +0200 Subject: [PATCH 11/11] Make Twig 3.10.2 the minimum requirement (see #7214) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description ----------- This pull request makes Twig 3.10.2 the minimum version and therefore does not need all the BC layers. @contao/developers Do we want to add this to Contao 5.3 or only in Contao 5.4? Commits ------- e3f56025 Make Twig 3.10.2 the minimum requirement 4e8915e4 Remove a Twig 3.8 BC layer 04205f95 Clean up after the upstream merge 10b05f0e Use EscaperExtension::escapeFilterIsSafe() instead of twig_escape_fil… 025eadbd Do not use the internal EscaperExtension::escapeFilterIsSafe() method… 6f7ccc2f Apply the suggestions from the pull request 6dea4c84 Drop the "testContaoUsesCorrectTwigFunctionSignatures" test --- composer.json | 4 +- core-bundle/composer.json | 6 +- .../src/Twig/Extension/ContaoExtension.php | 50 +++++++------- .../src/Twig/Interop/ContaoEscaper.php | 5 +- .../Twig/Extension/ContaoExtensionTest.php | 66 +++---------------- .../tests/Twig/Interop/ContaoEscaperTest.php | 5 +- .../PhpTemplateParentReferenceNodeTest.php | 8 +-- .../Twig/Interop/PhpTemplateProxyNodeTest.php | 8 +-- .../Twig/ResponseContext/AddNodeTest.php | 8 +-- 9 files changed, 46 insertions(+), 114 deletions(-) diff --git a/composer.json b/composer.json index ecf2148e25e..a55d94f2eb9 100644 --- a/composer.json +++ b/composer.json @@ -165,7 +165,7 @@ "toflar/psr6-symfony-http-cache-store": "^4.0", "twig/extra-bundle": "^3.0", "twig/string-extra": "^3.0", - "twig/twig": "^3.8", + "twig/twig": "^3.10.2", "ua-parser/uap-php": "^3.9", "webignition/robots-txt-file": "^3.0", "wikimedia/less.php": "^1.7" @@ -202,8 +202,6 @@ "nikic/php-parser": "4.7.0", "terminal42/contao-ce-access": "<3.0", "thecodingmachine/safe": "<1.2", - "twig/intl-extra": "3.9.0", - "twig/twig": "3.9.0 || 3.10.0 || 3.10.1", "zendframework/zend-code": "<3.3.1" }, "autoload": { diff --git a/core-bundle/composer.json b/core-bundle/composer.json index f6cc3500d96..33e7d47dcaf 100644 --- a/core-bundle/composer.json +++ b/core-bundle/composer.json @@ -151,7 +151,7 @@ "terminal42/service-annotation-bundle": "^1.1", "toflar/cronjob-supervisor": "^2.0", "twig/string-extra": "^3.0", - "twig/twig": "^3.8", + "twig/twig": "^3.10.2", "ua-parser/uap-php": "^3.9", "webignition/robots-txt-file": "^3.0", "wikimedia/less.php": "^1.7" @@ -179,9 +179,7 @@ "contao/manager-plugin": "<2.0 || >=3.0", "doctrine/cache": "<1.10", "terminal42/contao-ce-access": "<3.0", - "thecodingmachine/safe": "<1.2", - "twig/intl-extra": "3.9.0", - "twig/twig": "3.9.0 || 3.10.0 || 3.10.1" + "thecodingmachine/safe": "<1.2" }, "autoload": { "psr-4": { diff --git a/core-bundle/src/Twig/Extension/ContaoExtension.php b/core-bundle/src/Twig/Extension/ContaoExtension.php index 3d1ce37129a..b07b2be4e31 100644 --- a/core-bundle/src/Twig/Extension/ContaoExtension.php +++ b/core-bundle/src/Twig/Extension/ContaoExtension.php @@ -48,10 +48,10 @@ use Twig\Environment; use Twig\Extension\AbstractExtension; use Twig\Extension\CoreExtension; -use Twig\Extension\EscaperExtension; use Twig\Extension\GlobalsInterface; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Node; +use Twig\Runtime\EscaperRuntime; use Twig\TwigFilter; use Twig\TwigFunction; @@ -69,15 +69,10 @@ public function __construct( private readonly ContaoVariable $contaoVariable, ) { $contaoEscaper = new ContaoEscaper(); - $escaperExtension = $environment->getExtension(EscaperExtension::class); - // Forward compatibility with twig/twig >=3.10.0 - if (method_exists($escaperExtension, 'setEnvironment')) { - $escaperExtension->setEnvironment($environment); - } - - $escaperExtension->setEscaper('contao_html', $contaoEscaper->escapeHtml(...)); - $escaperExtension->setEscaper('contao_html_attr', $contaoEscaper->escapeHtmlAttr(...)); + $escaperRuntime = $this->environment->getRuntime(EscaperRuntime::class); + $escaperRuntime->setEscaper('contao_html', $contaoEscaper->escapeHtml(...)); + $escaperRuntime->setEscaper('contao_html_attr', $contaoEscaper->escapeHtmlAttr(...)); // Use our escaper on all templates in the "@Contao" and "@Contao_*" namespaces, // as well as the existing bundle templates we're already shipping. @@ -85,8 +80,8 @@ public function __construct( $this->addContaoEscaperRule('%^@ContaoCore/%'); // Mark classes as safe for HTML that already escape their output themselves - $escaperExtension->addSafeClass(HtmlAttributes::class, ['html', 'contao_html']); - $escaperExtension->addSafeClass(HighlightResult::class, ['html', 'contao_html']); + $escaperRuntime->addSafeClass(HtmlAttributes::class, ['html', 'contao_html']); + $escaperRuntime->addSafeClass(HighlightResult::class, ['html', 'contao_html']); $this->environment->addGlobal( 'request_token', @@ -242,7 +237,9 @@ function (Environment $env, $context, $template, $variables = [], $withContext = public function getFilters(): array { - $escaperFilter = static function (Environment $env, $string, $strategy = 'html', $charset = null, $autoescape = false) { + $escaperFilter = static function (Environment $env, $string, string $strategy = 'html', string|null $charset = null, bool $autoescape = false) { + $runtime = $env->getRuntime(EscaperRuntime::class); + if ($string instanceof ChunkedText) { $parts = []; @@ -250,31 +247,36 @@ public function getFilters(): array if (ChunkedText::TYPE_RAW === $type) { $parts[] = $chunk; } else { - $parts[] = twig_escape_filter($env, $chunk, $strategy, $charset); + $parts[] = $runtime->escape($chunk, $strategy, $charset); } } return implode('', $parts); } - return twig_escape_filter($env, $string, $strategy, $charset, $autoescape); + return $runtime->escape($string, $strategy, $charset, $autoescape); }; + /** @see \Twig\Extension\EscaperExtension::escapeFilterIsSafe() */ $twigEscaperFilterIsSafe = static function (Node $filterArgs): array { - $expression = iterator_to_array($filterArgs)[0] ?? null; - - if ($expression instanceof ConstantExpression) { - $value = $expression->getAttribute('value'); + foreach ($filterArgs as $arg) { + if ($arg instanceof ConstantExpression) { + $value = $arg->getAttribute('value'); + + // Our escaper strategy variants that tolerate input encoding are also safe in + // the original context (e.g. for the filter argument 'contao_html' we will + // return ['contao_html', 'html']). + if (\in_array($value, ['contao_html', 'contao_html_attr'], true)) { + return [$value, substr($value, 7)]; + } - // Our escaper strategy variants that tolerate input encoding are also safe in - // the original context (e.g. for the filter argument 'contao_html' we will - // return ['contao_html', 'html']). - if (\in_array($value, ['contao_html', 'contao_html_attr'], true)) { - return [$value, substr($value, 7)]; + return [$value]; } + + return []; } - return twig_escape_filter_is_safe($filterArgs); + return ['html']; }; return [ diff --git a/core-bundle/src/Twig/Interop/ContaoEscaper.php b/core-bundle/src/Twig/Interop/ContaoEscaper.php index ac7a67a048d..2cdbcc11fb2 100644 --- a/core-bundle/src/Twig/Interop/ContaoEscaper.php +++ b/core-bundle/src/Twig/Interop/ContaoEscaper.php @@ -13,7 +13,6 @@ namespace Contao\CoreBundle\Twig\Interop; use Contao\StringUtil; -use Twig\Environment; use Twig\Error\RuntimeError; /** @@ -33,7 +32,7 @@ final class ContaoEscaper * * @see twig_escape_filter */ - public function escapeHtml(Environment $environment, mixed $string, string|null $charset): string + public function escapeHtml(mixed $string, string|null $charset): string { if (null !== $charset && 'UTF-8' !== strtoupper($charset)) { throw new RuntimeError(sprintf('The "contao_html" escape filter does not support the %s charset, use UTF-8 instead.', $charset)); @@ -50,7 +49,7 @@ public function escapeHtml(Environment $environment, mixed $string, string|null * * @see twig_escape_filter */ - public function escapeHtmlAttr(Environment $environment, mixed $string, string|null $charset): string + public function escapeHtmlAttr(mixed $string, string|null $charset): string { if (null !== $charset && 'UTF-8' !== strtoupper($charset)) { throw new RuntimeError(sprintf('The "contao_html_attr" escape filter does not support the %s charset, use UTF-8 instead.', $charset)); diff --git a/core-bundle/tests/Twig/Extension/ContaoExtensionTest.php b/core-bundle/tests/Twig/Extension/ContaoExtensionTest.php index 0b7d48a3204..cae6f8b7d2b 100644 --- a/core-bundle/tests/Twig/Extension/ContaoExtensionTest.php +++ b/core-bundle/tests/Twig/Extension/ContaoExtensionTest.php @@ -176,6 +176,11 @@ public function testIncludeFunctionDelegatesToTwigInclude(): void public function testThrowsIfCoreIncludeFunctionIsNotFound(): void { $environment = $this->createMock(Environment::class); + $environment + ->method('getRuntime') + ->willReturn(new EscaperRuntime()) + ; + $environment ->method('getExtension') ->willReturnMap([ @@ -185,14 +190,6 @@ public function testThrowsIfCoreIncludeFunctionIsNotFound(): void ]) ; - // Forward compatibility with twig/twig >=3.10.0 - if (class_exists(EscaperRuntime::class)) { - $environment - ->method('getRuntime') - ->willReturn(new EscaperRuntime()) - ; - } - $extension = new ContaoExtension( $environment, $this->createMock(ContaoFilesystemLoader::class), @@ -379,46 +376,6 @@ public static function provideTemplateNames(): iterable yield 'core-bundle template' => ['@ContaoCore/Image/Studio/figure.html.twig']; } - /** - * We need to adjust some of Twig's core functions (e.g. the escape filter) but - * still delegate to the original implementation for maximum compatibility. This - * test makes sure the function's signatures remains the same and changes to the - * original codebase do not stay unnoticed. - * - * @dataProvider provideTwigFunctionSignatures - */ - public function testContaoUsesCorrectTwigFunctionSignatures(\ReflectionFunction|\ReflectionMethod $reflector, array $expectedParameters): void - { - $parameters = array_map( - static fn (\ReflectionParameter $parameter): array => [ - ($type = $parameter->getType()) instanceof \ReflectionNamedType ? $type->getName() : null, - $parameter->getName(), - ], - $reflector->getParameters(), - ); - - $this->assertSame($parameters, $expectedParameters); - } - - public static function provideTwigFunctionSignatures(): iterable - { - // Backwards compatibility with twig/twig <3.9.0 - new \ReflectionClass(EscaperExtension::class); - - yield [ - new \ReflectionFunction('twig_escape_filter'), - [ - [Environment::class, 'env'], - [null, 'string'], - [null, 'strategy'], - [null, 'charset'], - [null, 'autoescape'], - ], - ]; - - yield [new \ReflectionFunction('twig_escape_filter_is_safe'), [[Node::class, 'filterArgs']]]; - } - /** * @param Environment&MockObject $environment */ @@ -427,6 +384,11 @@ private function getContaoExtension(Environment|null $environment = null, Contao $environment ??= $this->createMock(Environment::class); $filesystemLoader ??= $this->createMock(ContaoFilesystemLoader::class); + $environment + ->method('getRuntime') + ->willReturn(new EscaperRuntime()) + ; + $environment ->method('getExtension') ->willReturnMap([ @@ -435,14 +397,6 @@ private function getContaoExtension(Environment|null $environment = null, Contao ]) ; - // Forward compatibility with twig/twig >=3.10.0 - if (class_exists(EscaperRuntime::class)) { - $environment - ->method('getRuntime') - ->willReturn(new EscaperRuntime()) - ; - } - return new ContaoExtension( $environment, $filesystemLoader, diff --git a/core-bundle/tests/Twig/Interop/ContaoEscaperTest.php b/core-bundle/tests/Twig/Interop/ContaoEscaperTest.php index 270f20ac7b0..7eec2775710 100644 --- a/core-bundle/tests/Twig/Interop/ContaoEscaperTest.php +++ b/core-bundle/tests/Twig/Interop/ContaoEscaperTest.php @@ -21,7 +21,6 @@ use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpKernel\Fragment\FragmentHandler; -use Twig\Environment; use Twig\Error\RuntimeError; class ContaoEscaperTest extends TestCase @@ -140,11 +139,11 @@ public function testEscapeHtmlAttrThrowsErrorIfCharsetIsNotUtf8(): void private function invokeEscapeHtml(int|string $input, string|null $charset): string { - return (new ContaoEscaper())->escapeHtml($this->createMock(Environment::class), $input, $charset); + return (new ContaoEscaper())->escapeHtml($input, $charset); } private function invokeEscapeHtmlAttr(int|string $input, string|null $charset): string { - return (new ContaoEscaper())->escapeHtmlAttr($this->createMock(Environment::class), $input, $charset); + return (new ContaoEscaper())->escapeHtmlAttr($input, $charset); } } diff --git a/core-bundle/tests/Twig/Interop/PhpTemplateParentReferenceNodeTest.php b/core-bundle/tests/Twig/Interop/PhpTemplateParentReferenceNodeTest.php index cf11c609d46..a2ebe1e31a3 100644 --- a/core-bundle/tests/Twig/Interop/PhpTemplateParentReferenceNodeTest.php +++ b/core-bundle/tests/Twig/Interop/PhpTemplateParentReferenceNodeTest.php @@ -14,7 +14,6 @@ use Contao\CoreBundle\Tests\TestCase; use Contao\CoreBundle\Twig\Interop\PhpTemplateParentReferenceNode; -use Twig\Attribute\YieldReady; use Twig\Compiler; use Twig\Environment; @@ -27,15 +26,10 @@ public function testCompilesParentReferenceCode(): void (new PhpTemplateParentReferenceNode())->compile($compiler); $expectedSource = <<<'SOURCE' - echo sprintf('[[TL_PARENT_%s]]', \Contao\CoreBundle\Framework\ContaoFramework::getNonce()); + yield sprintf('[[TL_PARENT_%s]]', \Contao\CoreBundle\Framework\ContaoFramework::getNonce()); SOURCE; - // Forward compatibility with twig/twig >=3.9.0 - if (class_exists(YieldReady::class)) { - $expectedSource = str_replace('echo', 'yield', $expectedSource); - } - $this->assertSame($expectedSource, $compiler->getSource()); } } diff --git a/core-bundle/tests/Twig/Interop/PhpTemplateProxyNodeTest.php b/core-bundle/tests/Twig/Interop/PhpTemplateProxyNodeTest.php index 7edb743ffc3..a2784730cc7 100644 --- a/core-bundle/tests/Twig/Interop/PhpTemplateProxyNodeTest.php +++ b/core-bundle/tests/Twig/Interop/PhpTemplateProxyNodeTest.php @@ -15,7 +15,6 @@ use Contao\CoreBundle\Tests\TestCase; use Contao\CoreBundle\Twig\Extension\ContaoExtension; use Contao\CoreBundle\Twig\Interop\PhpTemplateProxyNode; -use Twig\Attribute\YieldReady; use Twig\Compiler; use Twig\Environment; @@ -28,7 +27,7 @@ public function testCompilesProxyCode(): void (new PhpTemplateProxyNode(ContaoExtension::class))->compile($compiler); $expectedSource = <<<'SOURCE' - echo $this->extensions["Contao\\CoreBundle\\Twig\\Extension\\ContaoExtension"]->renderLegacyTemplate( + yield $this->extensions["Contao\\CoreBundle\\Twig\\Extension\\ContaoExtension"]->renderLegacyTemplate( $this->getTemplateName(), array_map( function(callable $block) use ($context): string { @@ -47,11 +46,6 @@ function(callable $block) use ($context): string { SOURCE; - // Forward compatibility with twig/twig >=3.9.0 - if (class_exists(YieldReady::class)) { - $expectedSource = str_replace('echo', 'yield', $expectedSource); - } - $this->assertSame($expectedSource, $compiler->getSource()); } } diff --git a/core-bundle/tests/Twig/ResponseContext/AddNodeTest.php b/core-bundle/tests/Twig/ResponseContext/AddNodeTest.php index 04a8c6cdcff..8fbdc4cac22 100644 --- a/core-bundle/tests/Twig/ResponseContext/AddNodeTest.php +++ b/core-bundle/tests/Twig/ResponseContext/AddNodeTest.php @@ -16,7 +16,6 @@ use Contao\CoreBundle\Twig\Extension\ContaoExtension; use Contao\CoreBundle\Twig\ResponseContext\AddNode; use Contao\CoreBundle\Twig\ResponseContext\DocumentLocation; -use Twig\Attribute\YieldReady; use Twig\Compiler; use Twig\Environment; use Twig\Node\Expression\ConstantExpression; @@ -43,7 +42,7 @@ public function testCompilesAddNode(): void $__contao_document_content = ''; foreach((function () use (&$context, $macros, $blocks) { // line 42 - echo "foobar"; + yield "foobar"; yield ''; })() as $__contao_document_chunk) { $__contao_document_content .= ob_get_contents() . $__contao_document_chunk; @@ -56,11 +55,6 @@ public function testCompilesAddNode(): void SOURCE; - // Forward compatibility with twig/twig >=3.9.0 - if (class_exists(YieldReady::class)) { - $expectedSource = str_replace('echo', 'yield', $expectedSource); - } - $this->assertSame($expectedSource, $compiler->getSource()); } }