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 f9df1a0..0000000 --- a/.github/workflows/cs-tests.yml +++ /dev/null @@ -1,46 +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.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/static-analysis.yml b/.github/workflows/static-analysis.yml deleted file mode 100644 index 1c328d6..0000000 --- a/.github/workflows/static-analysis.yml +++ /dev/null @@ -1,46 +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.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-test.yml b/.github/workflows/unit-test.yml deleted file mode 100644 index e1ce1ac..0000000 --- a/.github/workflows/unit-test.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.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 75f8acf..9bf2db2 100644 --- a/README.md +++ b/README.md @@ -17,102 +17,94 @@ This package can clean up your code, by getting rid of all the factories you wri [![SymfonyInsight](https://insight.symfony.com/projects/a0d7016e-fc3f-46b8-9b36-571ff060d744/big.svg)](https://insight.symfony.com/projects/a0d7016e-fc3f-46b8-9b36-571ff060d744) - ## Installation Install `dot-annotated-services` by running the following command in your project directory: composer require dotkernel/dot-annotated-services - After installing, register `dot-annotated-services` in your project by adding the below line to your configuration aggregate (usually: `config/config.php`): Dot\AnnotatedServices\ConfigProvider::class, - ## Usage ### Using the AttributedServiceFactory You can register services in the service manager using `AttributedServiceFactory` as seen in the below example: -```php -return [ - 'factories' => [ - ServiceClass::class => AttributedServiceFactory::class, - ], -]; -``` - + return [ + 'factories' => [ + ServiceClass::class => AttributedServiceFactory::class, + ], + ]; ### NOTE + > You can use only the fully qualified class name as the service key The next step is to add the `#[Inject]` attribute to the service constructor with the service FQCNs to inject: -```php use Dot\AnnotatedServices\Attribute\Inject; -#[Inject( - App\Srevice\Dependency1::class, - App\Srevice\Dependency2::class, - "config", -)] -public function __construct( - protected App\Srevice\Dependency1 $dep1, - protected App\Srevice\Dependency2 $dep2, - protected array $config -) { -} -``` + #[Inject( + App\Srevice\Dependency1::class, + App\Srevice\Dependency2::class, + "config", + )] + public function __construct( + protected App\Srevice\Dependency1 $dep1, + protected App\Srevice\Dependency2 $dep2, + protected array $config + ) { + } The `#[Inject]` attribute is telling `AttributedServiceFactory` to inject the services specified as parameters. Valid service names should be provided, as registered in the service manager. To inject an array value from the service manager, you can use dot notation as below -```php use Dot\AnnotatedServices\Attribute\Inject; -#[Inject( - "config.debug", -)] -``` + #[Inject( + "config.debug", + )] + which will inject `$container->get('config')['debug'];`. +### NOTE -### NOTE > Even if using dot notation, `AttributedServiceFactory` will check first if a service name exists with that name. +### Using the AttributedRepositoryFactory -### Using the AttributedRepositoryFactory You can register doctrine repositories and inject them using the `AttributedRepositoryFactory` as below: -```php -return [ - 'factories' => [ - ExampleRepository::class => AttributedRepositoryFactory::class, - ], -]; -``` + + return [ + 'factories' => [ + ExampleRepository::class => AttributedRepositoryFactory::class, + ], + ]; The next step is to add the `#[Entity]` attribute in the repository class. The `name` field has to be the fully qualified class name. Every repository should extend `Doctrine\ORM\EntityRepository`. -```php -use Api\App\Entity\Example; -use Doctrine\ORM\EntityRepository; -use Dot\AnnotatedServices\Attribute\Entity; -#[Entity(name: Example::class)] -class ExampleRepository extends EntityRepository -{ -} -``` + use Api\App\Entity\Example; + use Doctrine\ORM\EntityRepository; + use Dot\AnnotatedServices\Attribute\Entity; + + #[Entity(name: Example::class)] + class ExampleRepository extends EntityRepository + { + } ### NOTE + Starting from version `5.0` of `dot-annotated-services`: + - services can only be injected using the `#[Inject]` attribute (`@Inject` and `@Service` annotations are no longer supported) - repository-entity relation can only be established using the `#[Entity]` attribute (`@Entity` annotation is no longer supported) - dependencies injected via the`#[Entity]`/`#[Inject]` attributes are not cached diff --git a/composer.json b/composer.json index fd85127..81727e7 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ ], "require": { "php": "~8.2.0 || ~8.3.0", - "doctrine/orm": "^2.0 || ^3.0", + "doctrine/orm": "^2.9 || ^3.0", "psr/container": "^1.0 || ^2.0" }, "require-dev": { diff --git a/docs/book/v4/cache.md b/docs/book/v4/cache.md index 9bbf51a..d8ca4b4 100644 --- a/docs/book/v4/cache.md +++ b/docs/book/v4/cache.md @@ -2,7 +2,6 @@ `dot-annotated-services` reads class annotations using [doctrine/annotations](https://github.com/doctrine/annotations) and caches them using [doctrine/cache](https://github.com/doctrine/cache). - ## Configuration In order to cache annotations, you should register a service factory at key `AbstractAnnotatedFactory::CACHE_SERVICE` that should return a valid `Doctrine\Common\Cache\Cache` cache driver. @@ -19,7 +18,9 @@ You can add this configuration values to your application's Doctrine config file ], ]; ``` + where `AnnotationsCacheFactory` is a custom factory that needs to return a [Doctrine Cache Driver](https://github.com/doctrine/cache/tree/1.13.x/lib/Doctrine/Common/Cache): + ```php