From 864c9940350f3449962961b3dbd76203d8178dc0 Mon Sep 17 00:00:00 2001 From: Thomas Alrek Date: Tue, 16 Jan 2024 14:32:01 +0100 Subject: [PATCH] Initial commit --- .editorconfig | 15 ++++ .gitattributes | 20 +++++ .github/FUNDING.yml | 1 + .github/ISSUE_TEMPLATE/bug.yml | 66 +++++++++++++++++ .github/ISSUE_TEMPLATE/config.yml | 11 +++ .github/dependabot.yml | 19 +++++ .github/workflows/dependabot-auto-merge.yml | 33 +++++++++ .github/workflows/phpstan.yml | 28 +++++++ .github/workflows/update-changelog.yml | 32 ++++++++ .gitignore | 11 +++ CHANGELOG.md | 3 + LICENSE.md | 21 ++++++ README.md | 69 ++++++++++++++++++ composer.json | 48 ++++++++++++ phpstan-baseline.neon | 0 phpstan.neon.dist | 10 +++ src/Concerns/HasLocalizableResourceLabels.php | 73 +++++++++++++++++++ 17 files changed, 460 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .github/FUNDING.yml create mode 100644 .github/ISSUE_TEMPLATE/bug.yml create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/dependabot-auto-merge.yml create mode 100644 .github/workflows/phpstan.yml create mode 100644 .github/workflows/update-changelog.yml create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpstan-baseline.neon create mode 100644 phpstan.neon.dist create mode 100644 src/Concerns/HasLocalizableResourceLabels.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..dd9a2b5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c09f81e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,20 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/phpunit.xml.dist export-ignore +/art export-ignore +/docs export-ignore +/tests export-ignore +/workbench export-ignore +/.editorconfig export-ignore +/.php_cs.dist.php export-ignore +/psalm.xml export-ignore +/psalm.xml.dist export-ignore +/testbench.yaml export-ignore +/UPGRADING.md export-ignore +/phpstan.neon.dist export-ignore +/phpstan-baseline.neon export-ignore diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..49e5a1b --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: Apility diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml new file mode 100644 index 0000000..fe4cfe6 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug.yml @@ -0,0 +1,66 @@ +name: Bug Report +description: Report an Issue or Bug with the Package +title: "[Bug]: " +labels: ["bug"] +body: + - type: markdown + attributes: + value: | + We're sorry to hear you have a problem. Can you help us solve it by providing the following details. + - type: textarea + id: what-happened + attributes: + label: What happened? + description: What did you expect to happen? + placeholder: I cannot currently do X thing because when I do, it breaks X thing. + validations: + required: true + - type: textarea + id: how-to-reproduce + attributes: + label: How to reproduce the bug + description: How did this occur, please add any config values used and provide a set of reliable steps if possible. + placeholder: When I do X I see Y. + validations: + required: true + - type: input + id: package-version + attributes: + label: Package Version + description: What version of our Package are you running? Please be as specific as possible + placeholder: 2.0.0 + validations: + required: true + - type: input + id: php-version + attributes: + label: PHP Version + description: What version of PHP are you running? Please be as specific as possible + placeholder: 8.2.0 + validations: + required: true + - type: input + id: laravel-version + attributes: + label: Laravel Version + description: What version of Laravel are you running? Please be as specific as possible + placeholder: 9.0.0 + validations: + required: true + - type: dropdown + id: operating-systems + attributes: + label: Which operating systems does with happen with? + description: You may select more than one. + multiple: true + options: + - macOS + - Windows + - Linux + - type: textarea + id: notes + attributes: + label: Notes + description: Use this field to provide any other notes that you feel might be relevant to the issue. + validations: + required: false diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..76271c3 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,11 @@ +blank_issues_enabled: false +contact_links: + - name: Ask a question + url: https://github.com/Apility/filament-localizable-resources/discussions/new?category=q-a + about: Ask the community for help + - name: Request a feature + url: https://github.com/Apility/filament-localizable-resources/discussions/new?category=ideas + about: Share ideas for new features + - name: Report a security issue + url: https://github.com/Apility/filament-localizable-resources/security/policy + about: Learn how to notify us for sensitive bugs diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..39b1580 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,19 @@ +# Please see the documentation for all configuration options: +# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" + + - package-ecosystem: "composer" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml new file mode 100644 index 0000000..70d8e7b --- /dev/null +++ b/.github/workflows/dependabot-auto-merge.yml @@ -0,0 +1,33 @@ +name: dependabot-auto-merge +on: pull_request_target + +permissions: + pull-requests: write + contents: write + +jobs: + dependabot: + runs-on: ubuntu-latest + timeout-minutes: 5 + if: ${{ github.actor == 'dependabot[bot]' }} + steps: + + - name: Dependabot metadata + id: metadata + uses: dependabot/fetch-metadata@v1.6.0 + with: + github-token: "${{ secrets.GITHUB_TOKEN }}" + + - name: Auto-merge Dependabot PRs for semver-minor updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + + - name: Auto-merge Dependabot PRs for semver-patch updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..af21e45 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,28 @@ +name: PHPStan + +on: + push: + paths: + - '**.php' + - 'phpstan.neon.dist' + - '.github/workflows/phpstan.yml' + +jobs: + phpstan: + name: phpstan + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + coverage: none + + - name: Install composer dependencies + uses: ramsey/composer-install@v2 + + - name: Run PHPStan + run: ./vendor/bin/phpstan --error-format=github diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml new file mode 100644 index 0000000..39de30d --- /dev/null +++ b/.github/workflows/update-changelog.yml @@ -0,0 +1,32 @@ +name: "Update Changelog" + +on: + release: + types: [released] + +permissions: + contents: write + +jobs: + update: + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: main + + - name: Update Changelog + uses: stefanzweifel/changelog-updater-action@v1 + with: + latest-version: ${{ github.event.release.name }} + release-notes: ${{ github.event.release.body }} + + - name: Commit updated CHANGELOG + uses: stefanzweifel/git-auto-commit-action@v5 + with: + branch: main + commit_message: Update CHANGELOG + file_pattern: CHANGELOG.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a7f372d --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.idea +.phpunit.cache +build +composer.lock +coverage +docs +phpunit.xml +phpstan.neon +testbench.yaml +vendor +node_modules diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..fdd9ba2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# Changelog + +All notable changes to `filament-localizable-resources` will be documented in this file. diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..f3670c1 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Apility + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9caddb5 --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ +# Filament Localizable Resources + +[![Latest Version on Packagist](https://img.shields.io/packagist/v/apility/filament-localizable-resources.svg?style=flat-square)](https://packagist.org/packages/apility/filament-localizable-resources) +[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/apility/filament-localizable-resources/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/apility/filament-localizable-resources/actions?query=workflow%3Arun-tests+branch%3Amain) +[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/apility/filament-localizable-resources/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/apility/filament-localizable-resources/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) +[![Total Downloads](https://img.shields.io/packagist/dt/apility/filament-localizable-resources.svg?style=flat-square)](https://packagist.org/packages/apility/filament-localizable-resources) + +This package provides a re-usable trait for Filament Resources which adds support for localizing the resource labels. + +## Installation + +You can install the package via composer: + +```bash +composer require apility/filament-localizable-resources +``` + +## Usage + +```php +namespace App\Filament\Resources; + +use Apility\Filament\Concerns\HasLocalizableResourceLabels; + +use Filament\Resources\Resource; + +class ArticleResource extends Resource +{ + use HasLocalizableResourceLabels; + + // ... +} + +``` + +The trait modifies the default methods for retrieving the resource labels to use the `__()` helper function to translate the labels. + +It will look for the translation key in the following format: + +```php +return [ + // The resource slug is used as the translation key + 'articles' => [ + 'model_label' => 'article|articles', + 'navigation_label' => 'Articles', // Optional, defaults to model_label + 'slug' => 'articles', // Optional, defaults to resource slug + ], +]; +``` + +## Changelog + +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. + +## Security Vulnerabilities + +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. + +## Credits + +- [All Contributors](../../contributors) + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..af331de --- /dev/null +++ b/composer.json @@ -0,0 +1,48 @@ +{ + "name": "apility/filament-localizable-resources", + "description": "This package provides a re-usable trait for Filament Resources which adds support for localizing the resource labels.", + "keywords": [ + "laravel", + "filament" + ], + "homepage": "https://github.com/apility/filament-localizable-resources", + "license": "MIT", + "authors": [ + { + "name": "Thomas Alrek", + "email": "thomas@apility.no", + "role": "Developer" + } + ], + "require": { + "php": "^8.1", + "spatie/laravel-package-tools": "^1.14.0", + "illuminate/contracts": "^10.0", + "illuminate/support": "^10.0" + }, + "require-dev": { + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-phpunit": "^1.0" + }, + "autoload": { + "psr-4": { + "Apility\\Filament\\": "src/" + } + }, + "scripts": { + "analyse": "vendor/bin/phpstan analyse", + "test": "vendor/bin/pest", + "test-coverage": "vendor/bin/pest --coverage", + "format": "vendor/bin/pint" + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "phpstan/extension-installer": true + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} \ No newline at end of file diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..e69de29 diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..45bac49 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,10 @@ +includes: + - phpstan-baseline.neon + +parameters: + level: 5 + paths: + - src + tmpDir: build/phpstan + checkMissingIterableValueType: false + diff --git a/src/Concerns/HasLocalizableResourceLabels.php b/src/Concerns/HasLocalizableResourceLabels.php new file mode 100644 index 0000000..71bc560 --- /dev/null +++ b/src/Concerns/HasLocalizableResourceLabels.php @@ -0,0 +1,73 @@ +whenContains( + '\\Resources\\', + fn (Stringable $slug): Stringable => $slug->afterLast('\\Resources\\'), + fn (Stringable $slug): Stringable => $slug->classBasename(), + ) + ->beforeLast('Resource') + ->plural() + ->explode('\\') + ->map(fn (string $string) => str($string)->snake()->slug()) + ->implode('.'); + } + + protected static function translatedLabel(string $label, string $fallback, bool $plural = false): string + { + $localizedKey = implode('.', ['resources', static::getResourcePath(), $label]); + + if (Lang::has($localizedKey)) { + return Lang::choice($localizedKey, $plural ? 2 : 1); + } + + return $fallback; + } + + public static function getModelLabel(): string + { + return static::translatedLabel( + label: static::MODEL_LABEL, + fallback: parent::getModelLabel(), + ); + } + + public static function getPluralModelLabel(): string + { + return static::translatedLabel( + label: static::MODEL_LABEL, + fallback: parent::getPluralModelLabel(), + plural: true, + ); + } + + public static function getNavigationLabel(): string + { + return static::translatedLabel( + label: static::NAVIGATION_LABEL, + fallback: parent::getNavigationLabel(), + ); + } + + public static function getSlug(): string + { + return static::translatedLabel( + label: static::SLUG, + fallback: parent::getSlug(), + ); + } +}