Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow ForObject attribute to configure EntityRepository services #45

Merged
merged 1 commit into from
Mar 27, 2024
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
21 changes: 19 additions & 2 deletions src/Collection/Symfony/ZenstruckCollectionBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
use Zenstruck\Collection\Doctrine\Grid\ObjectGridDefinition;
use Zenstruck\Collection\Doctrine\ORM\EntityRepository;
use Zenstruck\Collection\Grid\GridDefinition;
use Zenstruck\Collection\Symfony\Attributes\AsGrid;
use Zenstruck\Collection\Symfony\Attributes\ForObject;

Expand Down Expand Up @@ -58,8 +61,22 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
if (isset($builder->getParameter('kernel.bundles')['DoctrineBundle'])) {
$loader->load('doctrine.php');

$builder->registerAttributeForAutoconfiguration(ForObject::class, function(ChildDefinition $definition, ForObject $attribute) {
$definition->addTag('zenstruck_collection.grid_definition', ['key' => $attribute->class, 'as_object' => true]);
$builder->registerAttributeForAutoconfiguration(ForObject::class, function(ChildDefinition $definition, ForObject $attribute, \ReflectionClass $class) { // @phpstan-ignore-line
if ($class->implementsInterface(GridDefinition::class)) {
$definition->addTag('zenstruck_collection.grid_definition', ['key' => $attribute->class, 'as_object' => true]);

return;
}

if (!$class->isSubclassOf(EntityRepository::class)) {
throw new LogicException(\sprintf('Can only use "%s" on classes that implement "%s" or extend "%s".', ForObject::class, GridDefinition::class, EntityRepository::class));
}

if (EntityRepository::class !== $class->getConstructor()?->getDeclaringClass()->name) {
throw new LogicException(\sprintf('Cannot use "%s" on "%s" as it overrides the constructor.', ForObject::class, $class->name));
}

$definition->setArgument('$class', $attribute->class);
});
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Symfony/Fixture/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the zenstruck/collection package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Collection\Tests\Symfony\Fixture\Repository;

use Zenstruck\Collection\Doctrine\ORM\EntityRepository;
use Zenstruck\Collection\Symfony\Attributes\ForObject;
use Zenstruck\Collection\Tests\Symfony\Fixture\Entity\Category;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
#[ForObject(Category::class)]
final class CategoryRepository extends EntityRepository
{
}
8 changes: 6 additions & 2 deletions tests/Symfony/Fixture/Service1.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
namespace Zenstruck\Collection\Tests\Symfony\Fixture;

use Zenstruck\Collection\Doctrine\ObjectRepositoryFactory;
use Zenstruck\Collection\Tests\Symfony\Fixture\Repository\CategoryRepository;
use Zenstruck\Collection\Tests\Symfony\Fixture\Repository\PostRepository;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class Service1
{
public function __construct(public ObjectRepositoryFactory $factory, public PostRepository $postRepo)
{
public function __construct(
public ObjectRepositoryFactory $factory,
public PostRepository $postRepo,
public CategoryRepository $categoryRepo,
) {
}
}
5 changes: 5 additions & 0 deletions tests/Symfony/Fixture/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Zenstruck\Collection\Tests\Symfony\Fixture\Grid\Grid1Definition;
use Zenstruck\Collection\Tests\Symfony\Fixture\Grid\Grid2Definition;
use Zenstruck\Collection\Tests\Symfony\Fixture\Grid\Grid3Definition;
use Zenstruck\Collection\Tests\Symfony\Fixture\Repository\CategoryRepository;
use Zenstruck\Collection\Tests\Symfony\Fixture\Repository\PostRepository;
use Zenstruck\Foundry\ZenstruckFoundryBundle;

Expand Down Expand Up @@ -81,6 +82,10 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
->setAutowired(true)
->setAutoconfigured(true)
;
$c->register(CategoryRepository::class)
->setAutowired(true)
->setAutoconfigured(true)
;
$c->register(Service2::class)
->setPublic(true)
->setAutowired(true)
Expand Down
Loading