Skip to content

Commit

Permalink
feature #95 Autowire factories and repositories by class and name (pa…
Browse files Browse the repository at this point in the history
…mil)

This PR was merged into the 1.6-dev branch.

Discussion
----------

Replaces and bases on Sylius/Sylius#10217 by @loic425.

Allows for autowiring `FactoryClass $resourceFactory` and `RepositoryClass $resourceRepository`.

Commits
-------

d4f26d0 Provide failing test for autowiring factories and repositories by class
adbed78 Implement autowiring factories and repositories by class
c0d3cf4 Make tests passing
  • Loading branch information
lchrusciel authored Jun 17, 2019
2 parents f491bef + c0d3cf4 commit d9f9fcb
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Bundle/DependencyInjection/Driver/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ protected function addFactory(ContainerBuilder $container, MetadataInterface $me
$container->setDefinition($metadata->getServiceId('factory'), $definition);

if (method_exists($container, 'registerAliasForArgument')) {
foreach (class_implements($factoryClass) as $typehintClass) {
$typehintClasses = array_merge(
class_implements($factoryClass),
[$factoryClass],
class_parents($factoryClass)
);

foreach ($typehintClasses as $typehintClass) {
$container->registerAliasForArgument(
$metadata->getServiceId('factory'),
$typehintClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ protected function addRepository(ContainerBuilder $container, MetadataInterface
$container->setDefinition($metadata->getServiceId('repository'), $definition);

if (method_exists($container, 'registerAliasForArgument')) {
foreach (class_implements($repositoryClass) as $typehintClass) {
$typehintClasses = array_merge(
class_implements($repositoryClass),
[$repositoryClass],
class_parents($repositoryClass)
);

foreach ($typehintClasses as $typehintClass) {
$container->registerAliasForArgument(
$metadata->getServiceId('repository'),
$typehintClass,
Expand Down
9 changes: 9 additions & 0 deletions src/Bundle/test/src/AppBundle/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
declare(strict_types=1);

use AppBundle\Service\FirstAutowiredService;
use AppBundle\Service\NoInterfaceAutowiredService;
use AppBundle\Service\SecondAutowiredService;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

if (method_exists($container, 'registerAliasForArgument')) {
$container->autowire(FirstAutowiredService::class)->setPublic(true);
$container->autowire(SecondAutowiredService::class)->setPublic(true);
$container->autowire(NoInterfaceAutowiredService::class)->setPublic(true);
} else {
$container->setDefinition(FirstAutowiredService::class, (new Definition(FirstAutowiredService::class, [
new Reference('app.factory.book'),
Expand All @@ -31,4 +33,11 @@
new Reference('app.repository.book'),
new Reference('app.manager.book'),
]))->setPublic(true));

$container->setDefinition(NoInterfaceAutowiredService::class, (new Definition(NoInterfaceAutowiredService::class, [
new Reference('app.factory.book'),
new Reference('app.repository.book'),
new Reference('app.factory.comic_book'),
new Reference('app.repository.comic_book'),
]))->setPublic(true));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace AppBundle\Service;

use AppBundle\Factory\BookFactory;
use AppBundle\Repository\BookRepository;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Component\Resource\Factory\Factory;

final class NoInterfaceAutowiredService
{
/** @var BookFactory */
private $bookFactory;

/** @var BookRepository */
private $bookRepository;

/** @var Factory */
private $comicBookFactory;

/** @var EntityRepository */
private $comicBookRepository;

public function __construct(
BookFactory $bookFactory,
BookRepository $bookRepository,
Factory $comicBookFactory,
EntityRepository $comicBookRepository
) {
$this->bookFactory = $bookFactory;
$this->bookRepository = $bookRepository;
$this->comicBookFactory = $comicBookFactory;
$this->comicBookRepository = $comicBookRepository;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use AppBundle\Entity\Book;
use AppBundle\Entity\BookTranslation;
use AppBundle\Entity\ComicBook;
use AppBundle\Factory\BookFactory;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Sylius\Bundle\ResourceBundle\DependencyInjection\SyliusResourceExtension;

Expand Down Expand Up @@ -82,6 +84,36 @@ public function it_registers_default_translation_parameters()
$this->assertContainerBuilderHasAlias('sylius.translation_locale_provider', 'test.custom_locale_provider');
}

/**
* @test
*/
public function it_does_not_break_when_aliasing_two_resources_use_same_factory_class(): void
{
// TODO: Move Resource-Grid integration to a dedicated compiler pass
$this->setParameter('kernel.bundles', []);
$this->load([
'resources' => [
'app.book' => [
'classes' => [
'model' => Book::class,
'factory' => BookFactory::class,
],
],
'app.comic_book' => [
'classes' => [
'model' => ComicBook::class,
'factory' => BookFactory::class,
],
],
],
]);
$this->assertContainerBuilderHasService('app.factory.book');
$this->assertContainerBuilderHasService('app.factory.comic_book');

$this->assertContainerBuilderHasAlias(sprintf('%s $bookFactory', BookFactory::class), 'app.factory.book');
$this->assertContainerBuilderHasAlias(sprintf('%s $comicBookFactory', BookFactory::class), 'app.factory.comic_book');
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit d9f9fcb

Please sign in to comment.