From 5e81659bf7ee25c0320051fa88e790e7733a4068 Mon Sep 17 00:00:00 2001 From: Robert Allport Date: Mon, 18 Mar 2024 10:07:01 +0000 Subject: [PATCH] Drop legacy support (#29) * Delete .github/workflows/run-tests-laravel-7.yml * Apply fixes from StyleCI (#28) * Update composer.json * Update and rename run-tests-laravel-8-9.yml to run-tests.yml * Update README.md * Create UPGRADING.md * Update meta helper Previously the `meta` helper method could return a specific meta tag i.e. `meta('my-tag')`. This is a shortcut to writing `meta()->get('my-tag')`. The same function could also return an instance of the main `Meta` class. This caused autocompletion issues. The helper function now returns an instance of the meta class. * Update HelperTest.php * Update HelperTest.php * Update UPGRADING.md * Update README.md --- .github/workflows/run-tests-laravel-8-9.yml | 54 ------------------- ...{run-tests-laravel-7.yml => run-tests.yml} | 46 +++++++++------- README.md | 24 ++++----- UPGRADING.md | 8 +++ composer.json | 18 +++---- src/GuessesTitles.php | 2 + src/Tags/Canonical.php | 2 + src/Tags/Description.php | 2 + src/Tags/Title.php | 4 +- src/TitleGuessor.php | 7 ++- src/helpers.php | 16 ++---- tests/HelperTest.php | 3 +- tests/TestCase.php | 1 + 13 files changed, 74 insertions(+), 113 deletions(-) delete mode 100644 .github/workflows/run-tests-laravel-8-9.yml rename .github/workflows/{run-tests-laravel-7.yml => run-tests.yml} (55%) create mode 100644 UPGRADING.md diff --git a/.github/workflows/run-tests-laravel-8-9.yml b/.github/workflows/run-tests-laravel-8-9.yml deleted file mode 100644 index 77ea326..0000000 --- a/.github/workflows/run-tests-laravel-8-9.yml +++ /dev/null @@ -1,54 +0,0 @@ -name: run-tests-laravel-8-9 - -on: [push, pull_request] - -jobs: - tests: - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest] - php: [7.4,8.0,8.1] - laravel: [8.*, 9.*, 10.*] - dependency-version: [prefer-stable] - include: - - laravel: 10.* - testbench: 8.* - - laravel: 8.* - testbench: 6.* - - laravel: 9.* - testbench: 7.* - exclude: - - laravel: 9.* - php: 7.4 - - laravel: 10.* - php: 8.0 - - laravel: 10.* - php: 7.4 - - name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - - steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Cache dependencies - uses: actions/cache@v3 - with: - path: ~/.composer/cache/files - key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php }} - extensions: curl, mbstring, zip, pcntl, sqlite, pdo_sqlite, iconv - coverage: none - - - name: Install dependencies - run: | - composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update - composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction - - name: Execute tests - run: vendor/bin/phpunit --color=always tests diff --git a/.github/workflows/run-tests-laravel-7.yml b/.github/workflows/run-tests.yml similarity index 55% rename from .github/workflows/run-tests-laravel-7.yml rename to .github/workflows/run-tests.yml index 0788b4e..88467ce 100644 --- a/.github/workflows/run-tests-laravel-7.yml +++ b/.github/workflows/run-tests.yml @@ -1,4 +1,4 @@ -name: run-tests-laravel-7 +name: "Run Tests - Current" on: [push, pull_request] @@ -9,33 +9,39 @@ jobs: strategy: fail-fast: false matrix: - php: [8.0, 7.4, 7.2] - laravel: [7.*, 6.*, 5.8.*] - dependency-version: [prefer-stable] + php: [8.3, 8.2, 8.1, 8.0] + laravel: ["^11.0", "^10.0", "^9.0", "^8.12"] + dependency-version: [prefer-lowest, prefer-stable] include: - - laravel: 7.* - testbench: 5.* - - laravel: 6.* - testbench: 4.* - - laravel: 5.8.* - testbench: 3.* + - laravel: ^11.0 + testbench: 9.* + - laravel: ^10.0 + testbench: 8.* + - laravel: ^9.0 + testbench: 7.* + - laravel: ^8.12 + testbench: ^6.23 exclude: - - laravel: 5.8.* - php: 8.0 - - laravel: 5.8.* - php: 7.4 + - laravel: ^10.0 + php: 8.0 + - laravel: ^8.12 + php: 8.3 + - laravel: ^11.0 + php: 8.1 + - laravel: ^11.0 + php: 8.0 name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ~/.composer/cache/files - key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -48,5 +54,9 @@ jobs: run: | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "symfony/console:>=4.3.4" "mockery/mockery:^1.3.2" --no-interaction --no-update composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction + + - name: Display PHP version + run: php -v | grep ^PHP | cut -d' ' -f2 + - name: Execute tests - run: vendor/bin/phpunit --color=always tests + run: vendor/bin/phpunit diff --git a/README.md b/README.md index 5312d43..d623ad0 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ Easily render meta tags within your Laravel application, using a fluent API ## Features - Simple API -- Laravel `>=5.8 | 6.x | 7.x | 8.x | 9.x | 10.x` supported - Render named, property, raw, Twitter card and OpenGraph type meta tags - [Optionally, render default tags on every request](#default-tags) - [Conditionally set tags](#conditionally-setting-tags) @@ -23,7 +22,14 @@ Easily render meta tags within your Laravel application, using a fluent API ## Requirements -PHP >= 7.2, Laravel >= 5.8. +- PHP `^8.0` +- Laravel `^8.12`, `^9.0`, `^10.0` or `^11.0` + +### Legacy Support / Upgrading + +For PHP `<8.0` and Laravel `<8.12` / support, use package version [`^1.7.7`](https://github.com/f9webltd/laravel-meta/tree/1.7.7) + +If upgrading from `^1.0`, see [UPGRADING](UPGRADING.md) for details. ## Installation @@ -98,21 +104,11 @@ The `when()` is fluent and can be called multiple times: ```php meta() ->set('title', 'the title') - -when(true, function ($meta) { - $meta->set('og:description', 'og description'); - }) - -when(false, function ($meta) { - $meta->set('referrer', 'no-referrer-when-downgrade'); - }) + -when(true, fn ($meta) => $meta->set('og:description', 'og description')) + -when(false, fn ($meta) => $meta->set('referrer', 'no-referrer-when-downgrade')) ->noIndex(); ``` -If using PHP >=7.4, [arrow functions](https://www.php.net/manual/en/functions.arrow.php) can be optionally used. - -```php -meta()->when(true, fn($meta) => $meta->noIndex()); -``` - ### Blade Directives Blade directives are available, as an alternative to using PHP function within templates. diff --git a/UPGRADING.md b/UPGRADING.md new file mode 100644 index 0000000..34cffb7 --- /dev/null +++ b/UPGRADING.md @@ -0,0 +1,8 @@ +# Upgrading + +## From 1.x to 2.x + +- Run the following command to fetch the latets version of the package: `composer require f9webltd/laravel-meta:^2.0` +- The package now requires PHP `^8.0` and Laravel `^8.12` / `^9.0`, `^10.0` or `^11.0` +- The `meta()` helper function now **only** returns an instance of the `Meta` class, this is breaking change. Previously the helper acted as a short cut to fetch a specific tag. For example, `meta('title')` was previously the same as `meta()->get('title')`. This behaviour was removed for simplicity and to help with autocompletion. If using this function calls in the format `meta('title')` shoulod be adjusted to `meta()->get('title')` +- No further changes are required diff --git a/composer.json b/composer.json index 43e2c68..33dae7e 100644 --- a/composer.json +++ b/composer.json @@ -21,15 +21,15 @@ "role": "Developer" } ], - "require": { - "php": "^7.2 | ^8.0", - "illuminate/config": "5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0", - "illuminate/support": "5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0" - }, - "require-dev": { - "orchestra/testbench": ">=3.8", - "phpunit/phpunit": "^7.0|^8.0|^9.3" - }, + "require": { + "php": "^8.0", + "illuminate/config": "^8.12|^9.0|^10.0|^11.0", + "illuminate/support": "^8.12|^9.0|^10.0|^11.0" +}, +"require-dev": { + "orchestra/testbench": "^6.23|^7.0|^8.0|^9.0", + "phpunit/phpunit": "^9.4|^10.1" +}, "autoload": { "psr-4": { "F9Web\\Meta\\": "src" diff --git a/src/GuessesTitles.php b/src/GuessesTitles.php index 23e7b56..2815578 100644 --- a/src/GuessesTitles.php +++ b/src/GuessesTitles.php @@ -3,8 +3,10 @@ namespace F9Web\Meta; use function config; + use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Route; + use function optional; trait GuessesTitles diff --git a/src/Tags/Canonical.php b/src/Tags/Canonical.php index 4364feb..f300577 100644 --- a/src/Tags/Canonical.php +++ b/src/Tags/Canonical.php @@ -5,9 +5,11 @@ namespace F9Web\Meta\Tags; use function config; + use Illuminate\Support\Collection; use Illuminate\Support\Facades\URL; use Illuminate\Support\HtmlString; + use function sprintf; use function str_replace; diff --git a/src/Tags/Description.php b/src/Tags/Description.php index d319bf6..c85a8f2 100644 --- a/src/Tags/Description.php +++ b/src/Tags/Description.php @@ -5,9 +5,11 @@ namespace F9Web\Meta\Tags; use function config; + use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; use Illuminate\Support\Str; + use function sprintf; class Description implements Tag diff --git a/src/Tags/Title.php b/src/Tags/Title.php index 7cc180f..951f835 100644 --- a/src/Tags/Title.php +++ b/src/Tags/Title.php @@ -5,10 +5,12 @@ namespace F9Web\Meta\Tags; use function config; + use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; use Illuminate\Support\Str; + use function sprintf; class Title implements Tag @@ -20,7 +22,7 @@ public function render(string $key, $value = null, Collection $tags = null): Htm { $config = config('f9web-laravel-meta'); - if (! ($title = Arr::get($tags ?? [], 'title'))) { + if (!($title = Arr::get($tags ?? [], 'title'))) { return new HtmlString( sprintf('%s', $config['fallback-meta-title']) ); diff --git a/src/TitleGuessor.php b/src/TitleGuessor.php index 2f53a1e..3f77100 100644 --- a/src/TitleGuessor.php +++ b/src/TitleGuessor.php @@ -3,10 +3,13 @@ namespace F9Web\Meta; use F9Web\Meta\Exceptions\GuessorException; + use function collect; use function config; use function explode; + use Illuminate\Support\Collection; + use function in_array; use function parse_url; use function str_replace; @@ -30,13 +33,13 @@ public function render() { $config = config('f9web-laravel-meta.title-guessor'); - if (! $config['enabled']) { + if (!$config['enabled']) { return null; } $this->setMethod($config['method']); - if (! in_array($method = $this->getMethod(), ['route', 'uri'])) { + if (!in_array($method = $this->getMethod(), ['route', 'uri'])) { throw new GuessorException(); } diff --git a/src/helpers.php b/src/helpers.php index 1cb16a2..7e2db92 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -2,19 +2,9 @@ use F9Web\Meta\Meta; -if (! function_exists('meta')) { - /** - * @param string|null $key - * @return array|\F9Web\Meta\Meta|string|null - */ - function meta(?string $key = null) +if (!function_exists('meta')) { + function meta(?string $key = null): Meta { - $instance = resolve(Meta::class); - - if ($key === null) { - return $instance; - } - - return $instance->get($key); + return resolve(Meta::class); } } diff --git a/tests/HelperTest.php b/tests/HelperTest.php index 0e07ffe..1318a3d 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -18,13 +18,12 @@ function ($mock) { ->andReturnSelf(); $mock->shouldReceive('get') - ->twice() + ->once() ->with('title'); } ); meta()->set('title', 'the meta title'); meta()->get('title'); - meta('title'); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index dde96f4..e450cb0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,7 @@ use F9Web\Meta\Meta; use F9Web\Meta\MetaServiceProvider; use Orchestra\Testbench\TestCase as OrchestraTestCase; + use function resolve; abstract class TestCase extends OrchestraTestCase