Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
->in(__DIR__)
->exclude('var')
->exclude('tmp')
->exclude('Tests/Fixtures/Php81') // @todo remove this when we drop PHP 7.X support
;

return (new PhpCsFixer\Config())
Expand Down
32 changes: 32 additions & 0 deletions Attribute/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Bdf\PrimeBundle\Attribute;

use Symfony\Component\DependencyInjection\Attribute\AutowireInline;
use Symfony\Component\DependencyInjection\Reference;

/**
* Attribute use to inject a prime repository on a method parameter.
*
* Usage:
* ```php
* class MyUserService
* {
* public function __construct(
* #[Repository(User::class)
* private readonly EntityRepository $repository,
* ) {}
* }
* ```
*/
#[\Attribute(\Attribute::TARGET_PARAMETER)]
final class Repository extends AutowireInline
{
/**
* @param class-string $entityClass
*/
public function __construct(string $entityClass)
{
parent::__construct([new Reference('prime'), 'repository'], [$entityClass]);
}
}
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v1.10.0
-------

* Feat: Add `Repository` attribute to inject repository in constructor (SF 7.1 minimum)

v1.9.0
------

Expand Down
19 changes: 19 additions & 0 deletions Tests/BdfPrimeBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Bdf\Prime\Mapper\ContainerMapperFactory;
use Bdf\Prime\Migration\MigrationManager;
use Bdf\Prime\Platform\Sql\Types\SqlStringType;
use Bdf\Prime\Repository\EntityRepository;
use Bdf\Prime\Schema\RepositoryUpgrader;
use Bdf\Prime\Schema\StructureUpgraderResolverAggregate;
use Bdf\Prime\Schema\StructureUpgraderResolverInterface;
Expand All @@ -30,6 +31,7 @@
use Bdf\PrimeBundle\Tests\Fixtures\A;
use Bdf\PrimeBundle\Tests\Fixtures\DummyMiddleware;
use Bdf\PrimeBundle\Tests\Fixtures\Php81\MyIntEnum;
use Bdf\PrimeBundle\Tests\Fixtures\Php81\MyService;
use Bdf\PrimeBundle\Tests\Fixtures\Php81\MyStringEnum;
use Bdf\PrimeBundle\Tests\Fixtures\Php81\MyUnitEnum;
use Bdf\PrimeBundle\Tests\Fixtures\Php81\WithEnumEntity;
Expand All @@ -45,6 +47,7 @@
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LazyCommand;
use Symfony\Component\DependencyInjection\Attribute\AutowireInline;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel;
Expand Down Expand Up @@ -537,6 +540,22 @@ public function testEnumTypes()
$this->assertEquals($e, WithEnumEntity::first());
}

public function testInjectRepositoryWithAttribute()
{
if (PHP_VERSION_ID < 80100 || !class_exists(AutowireInline::class)) {
$this->markTestSkipped();
}

$kernel = new TestKernel('dev', true);
$kernel->boot();

/** @var MyService $service */
$service = $kernel->getContainer()->get(MyService::class);

$this->assertInstanceOf(EntityRepository::class, $service->repository);
$this->assertSame(TestEntity::class, $service->repository->entityClass());
}

private function getCommand(Application $console, string $name): Command
{
$command = $console->get($name);
Expand Down
29 changes: 29 additions & 0 deletions Tests/Fixtures/Php81/MyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Bdf\PrimeBundle\Tests\Fixtures\Php81;

use Bdf\Prime\Repository\EntityRepository;
use Bdf\PrimeBundle\Attribute\Repository;
use Bdf\PrimeBundle\Tests\Fixtures\TestEntity;
use Symfony\Component\DependencyInjection\Attribute\AutowireInline;

if (PHP_VERSION_ID >= 80100 && class_exists(AutowireInline::class)) {
class MyService
{
/**
* @var EntityRepository<TestEntity>
*/
#[Repository(TestEntity::class)]
public $repository;

public function __construct(
#[Repository(TestEntity::class)]
EntityRepository $repository
) {
$this->repository = $repository;
}
}
} else {
// Add class to avoid error in PHP 7.X
class MyService {}
}
5 changes: 5 additions & 0 deletions Tests/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ services:
exclude:
- './Fixtures/Php81/*Enum.php'

Bdf\PrimeBundle\Tests\Fixtures\Php81\MyService:
autowire: true
autoconfigure: true
public: true

Bdf\PrimeBundle\Tests\Fixtures\A:
arguments: ['bar']
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.9-dev"
"dev-master": "1.10-dev"
}
},
"suggest": {
Expand Down