Skip to content

Commit e01a84f

Browse files
committed
Fix AP4 4.3.x and Symfony 8.x compatibility
- Migrate API Platform resource config from XML files to PHP attributes (Form, Collection, PageDataMetadata, MediaObject, ResourceMetadata) and switch DependencyInjection mapping to directory-based paths - Fix Symfony\Component\Serializer\Annotation\Groups import to Symfony\Component\Serializer\Attribute\Groups across all affected classes - Fix Count and Length constraint constructors to use named arguments (array-style constructor removed in Symfony 8.x) - Add api_sub_level context when normalizing ResourceMetadata to prevent PartialCollectionViewNormalizer injecting pagination view into array properties (e.g. mercureSubscribeTopics) when request URI has query params - Remove routePrefix from PageDataMetadata ApiResource to avoid double prefix with RoutingPrefixResourceMetadataCollectionFactory in AP4 4.x
1 parent 7535bf4 commit e01a84f

28 files changed

Lines changed: 105 additions & 267 deletions

src/DependencyInjection/SilverbackApiComponentsExtension.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,28 +274,29 @@ private function prependDoctrineConfiguration(ContainerBuilder $container): void
274274
);
275275
}
276276

277-
private function appendMappingPaths(&$mappingPaths, $srcBase, $name): void
278-
{
279-
$configBasePath = $srcBase . '/Resources/config/api_platform';
280-
$mappingPaths[] = \sprintf('%s/%s/resource.xml', $configBasePath, $name);
281-
$propertiesPath = \sprintf('%s/%s/properties.xml', $configBasePath, $name);
282-
if (file_exists($propertiesPath)) {
283-
$mappingPaths[] = $propertiesPath;
284-
}
285-
}
286-
287277
private function prependApiPlatformConfig(ContainerBuilder $container, array $config): void
288278
{
289279
$srcBase = __DIR__ . '/..';
290-
$mappingPaths = [$srcBase . '/Entity/Core', $srcBase . '/ApiResource'];
291-
$this->appendMappingPaths($mappingPaths, $srcBase, 'uploadable');
292-
$this->appendMappingPaths($mappingPaths, $srcBase, 'page_data_metadata');
293-
$this->appendMappingPaths($mappingPaths, $srcBase, 'resource_metadata');
280+
$mappingPaths = [
281+
$srcBase . '/Entity/Core',
282+
$srcBase . '/ApiResource',
283+
$srcBase . '/Model/Uploadable',
284+
$srcBase . '/Metadata',
285+
];
286+
287+
$componentDirMap = [
288+
'form' => $srcBase . '/Entity/Component',
289+
'collection' => $srcBase . '/Entity/Component',
290+
];
291+
$enabledComponentDirs = [];
294292
foreach ($config['enabled_components'] as $component => $is_enabled) {
295-
if (true === $is_enabled) {
296-
$this->appendMappingPaths($mappingPaths, $srcBase, $component);
293+
if (true === $is_enabled && isset($componentDirMap[$component])) {
294+
$enabledComponentDirs[$componentDirMap[$component]] = true;
297295
}
298296
}
297+
foreach (array_keys($enabledComponentDirs) as $dir) {
298+
$mappingPaths[] = $dir;
299+
}
299300

300301
$websiteName = $config['website_name'];
301302
$container->prependExtensionConfig(

src/Entity/Component/Collection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Silverback\ApiComponentsBundle\Entity\Component;
1313

1414
use ApiPlatform\Metadata\ApiProperty;
15+
use ApiPlatform\Metadata\ApiResource;
1516
use Doctrine\ORM\Mapping as ORM;
1617
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
1718
use Silverback\ApiComponentsBundle\Validator\Constraints as AcbAssert;
@@ -21,6 +22,7 @@
2122
* @author Daniel West <daniel@silverback.is>
2223
*/
2324
#[ORM\Entity]
25+
#[ApiResource]
2426
class Collection extends AbstractComponent
2527
{
2628
#[ORM\Column(name: 'resource_class', nullable: true)]

src/Entity/Component/Form.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212
namespace Silverback\ApiComponentsBundle\Entity\Component;
1313

1414
use ApiPlatform\Metadata\ApiProperty;
15+
use ApiPlatform\Metadata\ApiResource;
16+
use ApiPlatform\Metadata\Delete;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Metadata\Patch;
20+
use ApiPlatform\Metadata\Post;
21+
use ApiPlatform\Metadata\Put;
1522
use Doctrine\ORM\Mapping as ORM;
1623
use Silverback\ApiComponentsBundle\Annotation as Silverback;
24+
use Silverback\ApiComponentsBundle\DataProvider\StateProvider\FormStateProvider;
1725
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
1826
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait;
1927
use Silverback\ApiComponentsBundle\Model\Form\FormView;
@@ -26,6 +34,37 @@
2634
*/
2735
#[Silverback\Timestamped]
2836
#[ORM\Entity]
37+
#[ApiResource(
38+
provider: FormStateProvider::class,
39+
operations: [
40+
new GetCollection(),
41+
new Post(),
42+
new Get(),
43+
new Delete(),
44+
new Put(),
45+
new Patch(),
46+
new Patch(
47+
name: '_api_/forms/{id}/submit{._format}_patch',
48+
uriTemplate: '/forms/{id}/submit{._format}',
49+
read: true,
50+
deserialize: false,
51+
validate: false,
52+
write: false,
53+
serialize: true,
54+
requirements: ['id' => '[^/]+'],
55+
),
56+
new Post(
57+
name: '_api_/forms/{id}/submit{._format}_post',
58+
uriTemplate: '/forms/{id}/submit{._format}',
59+
read: true,
60+
deserialize: false,
61+
validate: false,
62+
write: false,
63+
serialize: true,
64+
requirements: ['id' => '[^/]+'],
65+
),
66+
]
67+
)]
2968
class Form extends AbstractComponent
3069
{
3170
use TimestampedTrait;

src/Entity/Core/AbstractPage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Silverback\ApiComponentsBundle\Entity\Utility\IdTrait;
1717
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait;
1818
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
19-
use Symfony\Component\Serializer\Annotation\Groups;
19+
use Symfony\Component\Serializer\Attribute\Groups;
2020
use Symfony\Component\Validator\Constraints as Assert;
2121
use Symfony\Component\Validator\Context\ExecutionContextInterface;
2222

src/Entity/Core/AbstractPageData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use ApiPlatform\Metadata\ApiResource;
1515
use ApiPlatform\Metadata\Get;
1616
use Doctrine\ORM\Mapping as ORM;
17-
use Symfony\Component\Serializer\Annotation\Groups;
17+
use Symfony\Component\Serializer\Attribute\Groups;
1818
use Symfony\Component\Validator\Constraints as Assert;
1919

2020
/**

src/Entity/Core/ComponentGroup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Silverback\ApiComponentsBundle\Entity\Utility\IdTrait;
2121
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait;
2222
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
23-
use Symfony\Component\Serializer\Annotation\Groups;
23+
use Symfony\Component\Serializer\Attribute\Groups;
2424
use Symfony\Component\Validator\Constraints as Assert;
2525

2626
/**

src/Entity/Core/ComponentPosition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Silverback\ApiComponentsBundle\Entity\Utility\IdTrait;
1919
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait;
2020
use Silverback\ApiComponentsBundle\Validator\Constraints as AcbAssert;
21-
use Symfony\Component\Serializer\Annotation\Groups;
21+
use Symfony\Component\Serializer\Attribute\Groups;
2222
use Symfony\Component\Validator\Constraints as Assert;
2323

2424
/**

src/Entity/Core/Page.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Doctrine\ORM\Mapping as ORM;
2020
use Silverback\ApiComponentsBundle\Entity\Utility\UiTrait;
2121
use Silverback\ApiComponentsBundle\Filter\OrSearchFilter;
22-
use Symfony\Component\Serializer\Annotation\Groups;
22+
use Symfony\Component\Serializer\Attribute\Groups;
2323
use Symfony\Component\Validator\Constraints as Assert;
2424
use Symfony\Component\Validator\Mapping\ClassMetadata;
2525

src/Entity/Core/Route.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
use Silverback\ApiComponentsBundle\Filter\OrSearchFilter;
3131
use Silverback\ApiComponentsBundle\Repository\Core\RouteRepository;
3232
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
33-
use Symfony\Component\Serializer\Annotation\Groups;
33+
use Symfony\Component\Serializer\Attribute\Groups;
3434
use Symfony\Component\Validator\Constraints as Assert;
3535

3636
/**

src/Entity/User/AbstractUser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
2525
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
2626
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
27-
use Symfony\Component\Serializer\Annotation\Groups;
27+
use Symfony\Component\Serializer\Attribute\Groups;
2828
use Symfony\Component\Validator\Constraints as Assert;
2929

3030
/**

0 commit comments

Comments
 (0)