Skip to content

Commit

Permalink
Merge pull request shopware#135 from soebbing/next-10918/add-support-…
Browse files Browse the repository at this point in the history
…for-autowiring-of-entity-repositories

NEXT-10918 - Add entity repository autowiring
  • Loading branch information
shyim committed Sep 17, 2020
2 parents e7337df + e88136d commit 5f864ab
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Add support for autowiring of entity repositories
issue: NEXT-10918
author: Hendrik Söbbing
author_email: hendrik@soebbing.de
author_github: @soebbing
---
# Core
* Changed `\Shopware\Core\Framework\DependencyInjection\CompilerPass\EntityCompilerPass` to also register an alias for arguments
___
# Upgrade Information

## Entity Repository Autowiring

The DAL entity repositories can now be injected into your services using autowiring. Necessary for this to work
(apart from having your service configured for [autowiring](https://symfony.com/doc/current/service_container/autowiring.html) generally)
are:
- The type of the parameter. It needs to be `EntityRepositoryInterface`
- The name of the variable. It must be the same as the id of the service in the DIC, written in `camelCase` instead of `snake_case`, followed by the word `Repository`.

So for example, a media_thumbnail repository (id `media_thumbnail.repository`) would be requested (and injected) like this:
```php
public function __construct(EntityRepositoryInterface $mediaThumbnailRepository) {}
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Read\EntityReaderInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntityAggregatorInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearcherInterface;
Expand Down Expand Up @@ -69,6 +70,7 @@ private function collectDefinitions(ContainerBuilder $container): void
$repository->setPublic(true);

$container->setDefinition($repositoryId, $repository);
$container->registerAliasForArgument($repositoryId, EntityRepositoryInterface::class);
}
$repositoryNameMap[$entity] = $repositoryId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\Test\DependencyInjection\CompilerPass;

use PHPUnit\Framework\TestCase;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressDefinition;
use Shopware\Core\Checkout\Customer\CustomerDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
use Shopware\Core\Framework\DependencyInjection\CompilerPass\EntityCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class EntityCompilerPassTest extends TestCase
{
public function testEntityRepositoryAutowiring(): void
{
$container = new ContainerBuilder();

$container->register(CustomerAddressDefinition::class, CustomerAddressDefinition::class)
->addTag('shopware.entity.definition');
$container->register(CustomerDefinition::class, CustomerDefinition::class)
->addTag('shopware.entity.definition');

$container->register(DefinitionInstanceRegistry::class, DefinitionInstanceRegistry::class)
->addArgument(new Reference('service_container'))
->addArgument([
CustomerDefinition::ENTITY_NAME => CustomerDefinition::class,
CustomerAddressDefinition::ENTITY_NAME => CustomerAddressDefinition::class,
])
->addArgument([
CustomerDefinition::ENTITY_NAME => 'customer.repository',
CustomerAddressDefinition::ENTITY_NAME => 'customer_address.repository',
]);

$entityCompilerPass = new EntityCompilerPass();
$entityCompilerPass->process($container);

// Make sure the correct aliases have been set
static::assertNotNull($container->getAlias('Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface $customerRepository'));
static::assertNotNull($container->getAlias('Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface $customerAddressRepository'));
}
}
5 changes: 4 additions & 1 deletion src/Docs/Resources/current/20-developer-guide/50-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public function __construct (EntityRepositoryInterface $productRepository)
}
```

Then, configure the `product.repository` service to be injected:
If you're using [service autowiring](https://symfony.com/doc/current/service_container/autowiring.html), and the
type and argument variable names are correct, the repository will be injected automatically.

Alternatively, configure the `product.repository` service to be injected explicitly:

```xml
<!-- SwagExamplePlugin/src/Resources/config/service.xml -->
Expand Down

0 comments on commit 5f864ab

Please sign in to comment.