forked from shopware/shopware
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request shopware#135 from soebbing/next-10918/add-support-…
…for-autowiring-of-entity-repositories NEXT-10918 - Add entity repository autowiring
- Loading branch information
Showing
4 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
changelog/_unreleased/2020-09-17-add-support-for-repository-autowiring.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/Core/Framework/Test/DependencyInjection/CompilerPass/EntityCompilerPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters