Skip to content

Commit 4804b19

Browse files
committed
Handle translatable factory bc-layer
1 parent d637236 commit 4804b19

File tree

6 files changed

+87
-3
lines changed

6 files changed

+87
-3
lines changed

src/Bundle/DependencyInjection/Driver/AbstractDriver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Driver;
1515

1616
use Sylius\Component\Resource\Factory\FactoryInterface as LegacyFactoryInterface;
17+
use Sylius\Component\Resource\Factory\TranslatableFactoryInterface as LegacyTranslatableFactoryInterface;
1718
use Sylius\Resource\Factory\Factory;
1819
use Sylius\Resource\Factory\TranslatableFactoryInterface;
1920
use Sylius\Resource\Metadata\Metadata;
@@ -125,6 +126,10 @@ protected function addFactory(ContainerBuilder $container, MetadataInterface $me
125126
$factoryParents,
126127
);
127128

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

tests/Application/config/services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,7 @@ services:
101101
app.service.legacy_autowired_factory:
102102
class: App\Service\LegacyAutowiredFactoryService
103103
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ sylius_resource:
3232
app.legacy_book:
3333
classes:
3434
model: App\Entity\LegacyBook
35+
factory: App\Factory\LegacyBookFactory
36+
translation:
37+
classes:
38+
model: App\Entity\LegacyBookTranslation
3539

3640
app.science_book:
3741
classes:

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: 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\TranslatableFactoryInterface;
17+
18+
final class LegacyAutowiredTranslatableFactoryService
19+
{
20+
public function __construct(
21+
private TranslatableFactoryInterface $legacyBookFactory,
22+
) {
23+
}
24+
}

0 commit comments

Comments
 (0)