10000 [make:serializer:normalizer] stop using CacheableSupportsMethodInterface · symfony/maker-bundle@ecf591d · GitHub
[go: up one dir, main page]

Skip to content

Commit ecf591d

Browse files
committed
8000
[make:serializer:normalizer] stop using CacheableSupportsMethodInterface
1 parent b0d2d5d commit ecf591d

File tree

7 files changed

+148
-66
lines changed

7 files changed

+148
-66
lines changed

src/Maker/MakeSerializerNormalizer.php

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
use Symfony\Bundle\MakerBundle\Generator;
1818
use Symfony\Bundle\MakerBundle\InputConfiguration;
1919
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
20-
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
2120
use Symfony\Component\Console\Command\Command;
2221
use Symfony\Component\Console\Input\InputArgument;
2322
use Symfony\Component\Console\Input\InputInterface;
24-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
23+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
2524
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2625
use Symfony\Component\Serializer\Serializer;
2726

@@ -30,8 +29,15 @@
3029
*/
3130
final class MakeSerializerNormalizer extends AbstractMaker
3231
{
33-
public function __construct(private FileManager $fileManager)
32+
public function __construct(private ?FileManager $fileManager = null)
3433
{
34+
if (null !== $this->fileManager) {
35+
@trigger_deprecation(
36+
'symfony/maker-bundle',
37+
'1.56.0',
38+
sprintf('Initializing MakeSerializerNormalizer while providing an instance of "%s" is deprecated. The $fileManager param will be removed in a future version.', FileManager::class)
39+
);
40+
}
3541
}
3642

3743
public static function getCommandName(): string
@@ -54,35 +60,41 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
5460

5561
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
5662
{
57-
$nextSteps = [];
58-
5963
$normalizerClassNameDetails = $generator->createClassNameDetails(
6064
$input->getArgument('name'),
6165
'Serializer\\Normalizer\\',
6266
\Normalizer::class
6367
);
6468

65-
$this->generateNormalizer($normalizerClassNameDetails->getFullName(), $generator);
69+
$useStatements = new UseStatementGenerator([
70+
NormalizerInterface::class,
71+
Autowire::class,
72+
sprintf('App\Entity\%s', str_replace('Normalizer', '', $normalizerClassNameDetails->getShortName())),
73+
]);
74+
75+
$entityDetails = $generator->createClassNameDetails(
76+
str_replace('Normalizer', '', $normalizerClassNameDetails->getShortName()),
77+
'Entity\\',
78+
);
6679

67-
try {
68-
$this->configureNormalizerService($normalizerClassNameDetails->getFullName(), $generator);
69-
} catch (\Throwable) {
70-
$nextSteps[] = "Your <info>services.yaml</> could not be updated automatically. You'll need to inject the <info>\$objectNormalizer</> argument to manually.";
80+
if ($entityExists = class_exists($entityDetails->getFullName())) {
81+
$useStatements->addUseStatement($entityDetails->getFullName());
7182
}
7283

84+
$generator->generateClass($normalizerClassNameDetails->getFullName(), 'serializer/Normalizer.tpl.php', [
85+
'use_statements' => $useStatements,
86+
'entity_exists' => $entityExists,
87+
'entity_name' => $entityDetails->getShortName(),
88+
]);
89+
7390
$generator->writeChanges();
7491

7592
$this->writeSuccessMessage($io);
7693

77-
array_push(
78-
$nextSteps,
79-
'Open your new serializer normalizer class and start customizing it.',
80-
'Find the documentation at <fg=yellow>https://symfony.com/doc/current/serializer/custom_normalizer.html</>',
81-
);
82-
8394
$io->text([
8495
'Next:',
85-
...array_map(static fn (string $s): string => sprintf(' - %s', $s), $nextSteps),
96+
' - Open your new serializer normalizer class and start customizing it.',
97+
' - Find the documentation at <fg=yellow>https://symfony.com/doc/current/serializer/custom_normalizer.html</>',
8698
]);
8799
}
88100

@@ -93,35 +105,4 @@ public function configureDependencies(DependencyBuilder $dependencies): void
93105
'serializer'
94106
);
95107
}
96-
97-
private function generateNormalizer(string $className, Generator $generator): void
98-
{
99-
$useStatements = new UseStatementGenerator([
100-
NormalizerInterface::class,
101-
CacheableSupportsMethodInterface::class,
102-
]);
103-
104-
$generator->generateClass($className, 'serializer/Normalizer.tpl.php', [
105-
'use_statements' => $useStatements,
106-
]);
107-
}
108-
109-
private function configureNormalizerService(string $className, Generator $generator): void
110-
{
111-
$servicesFilePath = 'config/services.yaml';
112-
113-
$manipulator = new YamlSourceManipulator($this->fileManager->getFileContents($servicesFilePath));
114-
$servicesData = $manipulator->getData();
115-
116-
if (!isset($servicesData['services'][$className])) {
117-
$servicesData['services'][$className] = [
118-
'arguments' => [
119-
'$objectNormalizer' => '@serializer.normalizer.object',
120-
],
121-
];
122-
}
123-
124-
$manipulator->setData($servicesData);
125-
$generator->dumpFile($servicesFilePath, $manipulator->getContents());
126-
}
127108
}

src/Resources/config/makers.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
</service>
9898

9999
<service id="maker.maker.make_serializer_normalizer" class="Symfony\Bundle\MakerBundle\Maker\MakeSerializerNormalizer">
100-
<argument type="service" id="maker.file_manager" />
101100
<tag name="maker.command" />
102101
</service>
103102

src/Resources/skeleton/serializer/Normalizer.tpl.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
<?= $use_statements; ?>
66

7-
class <?= $class_name ?> implements NormalizerInterface, CacheableSupportsMethodInterface
7+
class <?= $class_name ?> implements NormalizerInterface
88
{
9-
public function __construct(private NormalizerInterface $objectNormalizer)
10-
{
9+
public function __construct(
10+
#[Autowire(service: 'serializer.normalizer.object')]
11+
private NormalizerInterface $normalizer
12+
) {
1113
}
1214

1315
public function normalize($object, string $format = null, array $context = []): array
@@ -21,11 +23,19 @@ public function normalize($object, string $format = null, array $context = []):
2123

2224
public function supportsNormalization($data, string $format = null, array $context = []): bool
2325
{
24-
return $data instanceof \App\Entity\<?= str_replace('Normalizer', '', $class_name) ?>;
26+
<?php if ($entity_exists): ?>
27+
return $data instanceof <?= $entity_name ?>;
28+
<?php else: ?>
29+
// TODO: return $data instanceof Object
30+
<?php endif ?>
2531
}
2632

27-
public function hasCacheableSupportsMethod(): bool
33+
public function getSupportedTypes(?string $format): array
2834
{
29-
return true;
35+
<?php if ($entity_exists): ?>
36+
return [<?= $entity_name ?>::class => true];
37+
<?php else: ?>
38+
// TODO: return [Object::class => true];
39+
<?php endif ?>
3040
}
3141
}

tests/Maker/MakeSerializerNormalizerTest.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,36 @@ protected function getMakerClass(): string
2222
return MakeSerializerNormalizer::class;
2323
}
2424

25-
public function getTestDetails()
25+
public function getTestDetails(): \Generator
2626
{
2727
yield 'it_makes_serializer_normalizer' => [$this->createMakerTest()
28-
// serializer-pack 1.1 requires symfony/property-info >= 5.4
29-
// adding symfony/serializer-pack:* as an extra depends allows
30-
// us to use serializer-pack < 1.1 which does not conflict with
31-
// property-info < 5.4. E.g. Symfony 5.3 tests. See PR 1063
32-
->addExtraDependencies('symfony/serializer-pack:*')
3328
->run(function (MakerTestRunner $runner) {
34-
$runner->runMaker(
35-
[
36-
// normalizer class name
37-
'FooBarNormalizer',
38-
]
29+
$output = $runner->runMaker(
30+
['FooBarNormalizer']
31+
);
32+
33+
$this->assertStringContainsString('Success', $output);
34+
35+
self::assertFileEquals(
36+
\dirname(__DIR__).'/fixtures/make-serializer-normalizer/FooBarNormalizer.php',
37+
$runner->getPath('src/Serializer/Normalizer/FooBarNormalizer.php')
38+
);
39+
}),
40+
];
41+
42+
yield 'it_makes_serializer_normalizer_with_existing_entity' => [$this->createMakerTest()
43+
->run(function (MakerTestRunner $runner) {
44+
$runner->copy('make-serializer-normalizer/EntityFixture.php', 'src/Entity/EntityFixture.php');
45+
46+
$output = $runner->runMaker(
47+
['EntityFixture']
48+
);
49+
50+
$this->assertStringContainsString('Success', $output);
51+
52+
self::assertFileEquals(
53+
\dirname(__DIR__).'/fixtures/make-serializer-normalizer/EntityFixtureNormalizer.php',
54+
$runner->getPath('src/Serializer/Normalizer/EntityFixtureNormalizer.php')
3955
);
4056
}),
4157
];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
class EntityFixture
6+
{
7+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Serializer\Normalizer;
4+
5+
use App\Entity\EntityFixture;
6+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
7+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
8+
9+
class EntityFixtureNormalizer implements NormalizerInterface
10+
{
11+
public function __construct(
12+
#[Autowire(service: 'serializer.normalizer.object')]
13+
private NormalizerInterface $normalizer
14+
) {
15+
}
16+
17+
public function normalize($object, ?string $format = null, array $context = []): array
18+
{
19+
$data = $this->normalizer->normalize($object, $format, $context);
20+
21+
// TODO: add, edit, or delete some data
22+
23+
return $data;
24+
}
25+
26+
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
27+
{
28+
return $data instanceof EntityFixture;
29+
}
30+
31+
public function getSupportedTypes(?string $format): array
32+
{
33+
return [EntityFixture::class => true];
34+
}
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Serializer\Normalizer;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
6+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7+
8+
class FooBarNormalizer implements NormalizerInterface
9+
{
10+
public function __construct(
11+
#[Autowire(service: 'serializer.normalizer.object')]
12+
private NormalizerInterface $normalizer
13+
) {
14+
}
15+
16+
public function normalize($object, ?string $format = null, array $context = []): array
17+
{
18+
$data = $this->normalizer->normalize($object, $format, $context);
19+
20+
// TODO: add, edit, or delete some data
21+
22+
return $data;
23+
}
24+
25+
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
26+
{
27+
// TODO: return $data instanceof Object
28+
}
29+
30+
public function getSupportedTypes(?string $format): array
31+
{
32+
// TODO: return [Object::class => true];
33+
}
34+
}

0 commit comments

Comments
 (0)
0