Skip to content

Commit bd6f4a3

Browse files
authored
bug #847 Fix autowiring for legacy factory and repository (loic425)
This PR was merged into the 1.11 branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | yes (on 1.11) | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT Commits ------- 6d4b752 Fix autowiring for legacy factory and repository f40b94e Trying to fix Psalm error eff5a29 Fix coding standard d637236 Fix PHPUnit tests 4804b19 Handle translatable factory bc-layer
2 parents 122ecad + 4804b19 commit bd6f4a3

File tree

12 files changed

+184
-12
lines changed

12 files changed

+184
-12
lines changed

psalm.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@
8383
</errorLevel>
8484
</InvalidArgument>
8585

86-
<InvalidDocblock>
87-
<errorLevel type="suppress">
88-
<file name="vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php" />
89-
</errorLevel>
90-
</InvalidDocblock>
91-
9286
<InvalidReturnStatement>
9387
<errorLevel type="suppress">
9488
<file name="src/Component/src/Doctrine/Persistence/InMemoryRepository.php" />

src/Bundle/DependencyInjection/Driver/AbstractDriver.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Driver;
1515

16+
use Sylius\Component\Resource\Factory\FactoryInterface as LegacyFactoryInterface;
17+
use Sylius\Component\Resource\Factory\TranslatableFactoryInterface as LegacyTranslatableFactoryInterface;
1618
use Sylius\Resource\Factory\Factory;
1719
use Sylius\Resource\Factory\TranslatableFactoryInterface;
1820
use Sylius\Resource\Metadata\Metadata;
@@ -120,10 +122,14 @@ protected function addFactory(ContainerBuilder $container, MetadataInterface $me
120122

121123
$typehintClasses = array_merge(
122124
$factoryInterfaces,
123-
[$factoryClass],
125+
[$factoryClass, LegacyFactoryInterface::class],
124126
$factoryParents,
125127
);
126128

129+
if (in_array(TranslatableFactoryInterface::class, $factoryInterfaces, true)) {
130+
$typehintClasses[] = LegacyTranslatableFactoryInterface::class;
131+
}
132+
127133
foreach ($typehintClasses as $typehintClass) {
128134
$container->registerAliasForArgument(
129135
$metadata->getServiceId('factory'),

src/Bundle/DependencyInjection/Driver/Doctrine/DoctrineORMDriver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Doctrine\Persistence\ObjectManager;
2222
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
2323
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
24+
use Sylius\Component\Resource\Repository\RepositoryInterface as LegacyRepositoryInterface;
2425
use Sylius\Resource\Metadata\MetadataInterface;
2526
use Symfony\Component\DependencyInjection\ContainerBuilder;
2627
use Symfony\Component\DependencyInjection\Definition;
@@ -95,7 +96,7 @@ protected function addRepository(ContainerBuilder $container, MetadataInterface
9596

9697
$typehintClasses = array_merge(
9798
$repositoryInterfaces,
98-
[$repositoryClass],
99+
[$repositoryClass, LegacyRepositoryInterface::class],
99100
$repositoryParents,
100101
);
101102

tests/Application/config/services.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ services:
8080
connection: default
8181

8282
App\Repository\ComicBookRepository: null
83+
App\Repository\LegacyBookRepository: null
8384

8485
App\Shared\:
8586
resource: '../src/Shared'
@@ -92,3 +93,15 @@ services:
9293

9394
App\Subscription\Factory\SubscriptionFactory:
9495
decorates: 'app.factory.subscription'
96+
97+
app.service.legacy_autowired_repository:
98+
class: App\Service\LegacyAutowiredRepositoryService
99+
autowire: true
100+
101+
app.service.legacy_autowired_factory:
102+
class: App\Service\LegacyAutowiredFactoryService
103+
autowire: true
104+
105+
app.service.legacy_autowired_translatable_factory:
106+
class: App\Service\LegacyAutowiredTranslatableFactoryService
107+
autowire: true

tests/Application/config/sylius/resources.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ sylius_resource:
2929
classes:
3030
model: App\Entity\ComicBook
3131

32+
app.legacy_book:
33+
classes:
34+
model: App\Entity\LegacyBook
35+
factory: App\Factory\LegacyBookFactory
36+
translation:
37+
classes:
38+
model: App\Entity\LegacyBookTranslation
39+
3240
app.science_book:
3341
classes:
3442
model: App\Entity\ScienceBook

tests/Application/src/Entity/LegacyBook.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313

1414
namespace App\Entity;
1515

16+
use App\Repository\LegacyBookRepository;
1617
use Doctrine\ORM\Mapping as ORM;
1718
use Sylius\Component\Resource\Model\ResourceInterface;
1819
use Sylius\Component\Resource\Model\TranslatableInterface;
1920
use Sylius\Component\Resource\Model\TranslatableTrait;
2021

21-
#[ORM\Entity]
22+
#[ORM\Entity(repositoryClass: LegacyBookRepository::class)]
2223
#[ORM\MappedSuperclass]
2324
#[ORM\Table(name: 'app_legacy_book')]
2425
class LegacyBook implements ResourceInterface, TranslatableInterface

tests/Application/src/Factory/BookFactory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
namespace App\Factory;
1515

1616
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
17+
use Sylius\Component\Resource\Factory\FactoryInterface;
18+
use Sylius\Component\Resource\Factory\TranslatableFactoryInterface;
19+
use Sylius\Component\Resource\Model\TranslatableInterface;
1720
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
18-
use Sylius\Resource\Factory\FactoryInterface;
19-
use Sylius\Resource\Model\TranslatableInterface;
2021

21-
final class BookFactory implements BookFactoryInterface
22+
final class BookFactory implements TranslatableFactoryInterface
2223
{
2324
private FactoryInterface $factory;
2425

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Factory;
15+
16+
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
17+
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
18+
use Sylius\Resource\Factory\FactoryInterface;
19+
use Sylius\Resource\Model\TranslatableInterface;
20+
21+
final class LegacyBookFactory implements BookFactoryInterface
22+
{
23+
private FactoryInterface $factory;
24+
25+
private TranslationLocaleProviderInterface $localeProvider;
26+
27+
public function __construct(FactoryInterface $factory, TranslationLocaleProviderInterface $localeProvider)
28+
{
29+
$this->factory = $factory;
30+
$this->localeProvider = $localeProvider;
31+
}
32+
33+
public function createNew()
34+
{
35+
$book = $this->factory->createNew();
36+
37+
if (!$book instanceof TranslatableInterface) {
38+
throw new UnexpectedTypeException($book, TranslatableInterface::class);
39+
}
40+
41+
$book->setCurrentLocale($this->localeProvider->getDefaultLocaleCode());
42+
$book->setFallbackLocale($this->localeProvider->getDefaultLocaleCode());
43+
44+
return $book;
45+
}
46+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Repository;
15+
16+
use App\Entity\LegacyBook;
17+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
18+
use Doctrine\Persistence\ManagerRegistry;
19+
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\ResourceRepositoryTrait;
20+
use Sylius\Component\Resource\Repository\RepositoryInterface;
21+
22+
final class LegacyBookRepository extends ServiceEntityRepository implements RepositoryInterface
23+
{
24+
use ResourceRepositoryTrait;
25+
26+
public function __construct(ManagerRegistry $registry)
27+
{
28+
parent::__construct($registry, LegacyBook::class);
29+
}
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Service;
15+
16+
use Sylius\Component\Resource\Factory\FactoryInterface;
17+
18+
final class LegacyAutowiredFactoryService
19+
{
20+
public function __construct(
21+
private FactoryInterface $legacyBookFactory,
22+
) {
23+
}
24+
}

0 commit comments

Comments
 (0)