Skip to content

Commit 8ff7e03

Browse files
committed
Add Symfony Uid support
1 parent 1e62c99 commit 8ff7e03

File tree

10 files changed

+303
-1
lines changed

10 files changed

+303
-1
lines changed

.github/workflows/ci.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ jobs:
113113
run: |
114114
composer global require --prefer-dist --no-progress --no-suggest --ansi \
115115
symfony/flex
116+
- name: Require symfony uid
117+
if: (!startsWith(matrix.php, '7.1'))
118+
run: composer require symfony/uid --dev --no-progress --ansi
116119
- name: Update project dependencies
117120
run: |
118121
mkdir -p /tmp/api-platform/core/vendor
@@ -235,6 +238,9 @@ jobs:
235238
composer remove --dev --no-progress --no-update --ansi \
236239
doctrine/mongodb-odm \
237240
doctrine/mongodb-odm-bundle \
241+
- name: Require symfony uid
242+
if: (!startsWith(matrix.php, '7.1'))
243+
run: composer require symfony/uid --dev --no-progress --ansi
238244
- name: Update project dependencies
239245
run: |
240246
mkdir -p /tmp/api-platform/core/vendor
@@ -377,6 +383,9 @@ jobs:
377383
composer remove --dev --no-progress --no-update --ansi \
378384
doctrine/mongodb-odm \
379385
doctrine/mongodb-odm-bundle \
386+
- name: Require symfony uid
387+
if: (!startsWith(matrix.php, '7.1'))
388+
run: composer require symfony/uid --dev --no-progress --ansi
380389
- name: Update project dependencies
381390
run: |
382391
mkdir -p /tmp/api-platform/core/vendor
@@ -521,6 +530,9 @@ jobs:
521530
run: |
522531
composer global require --prefer-dist --no-progress --no-suggest --ansi \
523532
symfony/flex
533+
- name: Require symfony uid
534+
if: (!startsWith(matrix.php, '7.1'))
535+
run: composer require symfony/uid --dev --no-progress --ansi
524536
- name: Update project dependencies
525537
run: |
526538
mkdir -p /tmp/api-platform/core/vendor
@@ -613,6 +625,9 @@ jobs:
613625
mkdir -p /tmp/api-platform/core/vendor
614626
ln -s /tmp/api-platform/core/vendor vendor
615627
composer update --no-progress --no-suggest --prefer-stable --prefer-lowest --ansi
628+
- name: Require symfony uid
629+
if: (!startsWith(matrix.php, '7.1'))
630+
run: composer require symfony/uid --dev --no-progress --ansi
616631
- name: Install PHPUnit
617632
run: vendor/bin/simple-phpunit --version
618633
- name: Clear test app cache
@@ -753,6 +768,9 @@ jobs:
753768
composer remove --dev --no-progress --no-update --ansi \
754769
doctrine/mongodb-odm \
755770
doctrine/mongodb-odm-bundle \
771+
- name: Require symfony uid
772+
if: (!startsWith(matrix.php, '7.1'))
773+
run: composer require symfony/uid --dev --no-progress --ansi
756774
- name: Update project dependencies
757775
run: |
758776
mkdir -p /tmp/api-platform/core/vendor
@@ -876,6 +894,9 @@ jobs:
876894
run: |
877895
composer global require --prefer-dist --no-progress --no-suggest --ansi \
878896
symfony/flex
897+
- name: Require symfony uid
898+
if: (!startsWith(matrix.php, '7.1'))
899+
run: composer require symfony/uid --dev --no-progress --ansi
879900
- name: Update project dependencies
880901
run: |
881902
mkdir -p /tmp/api-platform/core/vendor
@@ -987,6 +1008,9 @@ jobs:
9871008
run: |
9881009
composer global require --prefer-dist --no-progress --no-suggest --ansi \
9891010
symfony/flex
1011+
- name: Require symfony uid
1012+
if: (!startsWith(matrix.php, '7.1'))
1013+
run: composer require symfony/uid --dev --no-progress --ansi
9901014
- name: Update project dependencies
9911015
run: |
9921016
mkdir -p /tmp/api-platform/core/vendor
@@ -1098,6 +1122,9 @@ jobs:
10981122
run: |
10991123
composer global require --prefer-dist --no-progress --no-suggest --ansi \
11001124
symfony/flex
1125+
- name: Require symfony uid
1126+
if: (!startsWith(matrix.php, '7.1'))
1127+
run: composer require symfony/uid --dev --no-progress --ansi
11011128
- name: Update project dependencies
11021129
run: |
11031130
mkdir -p /tmp/api-platform/core/vendor
@@ -1233,6 +1260,9 @@ jobs:
12331260
run: |
12341261
composer global require --prefer-dist --no-progress --no-suggest --ansi \
12351262
symfony/flex
1263+
- name: Require symfony uid
1264+
if: (!startsWith(matrix.php, '7.1'))
1265+
run: composer require symfony/uid --dev --no-progress --ansi
12361266
- name: Update project dependencies
12371267
run: |
12381268
mkdir -p /tmp/api-platform/core/vendor
@@ -1344,6 +1374,9 @@ jobs:
13441374
run: |
13451375
composer global require --prefer-dist --no-progress --no-suggest --ansi \
13461376
symfony/flex
1377+
- name: Require symfony uid
1378+
if: (!startsWith(matrix.php, '7.1'))
1379+
run: composer require symfony/uid --dev --no-progress --ansi
13471380
- name: Update project dependencies
13481381
run: |
13491382
mkdir -p /tmp/api-platform/core/vendor
@@ -1487,6 +1520,9 @@ jobs:
14871520
run: |
14881521
composer global require --prefer-dist --no-progress --no-suggest --ansi \
14891522
symfony/flex
1523+
- name: Require symfony uid
1524+
if: (!startsWith(matrix.php, '7.1'))
1525+
run: composer require symfony/uid --dev --no-progress --ansi
14901526
- name: Update project dependencies
14911527
run: |
14921528
mkdir -p /tmp/api-platform/core/vendor
@@ -1630,6 +1666,9 @@ jobs:
16301666
run: |
16311667
composer global require --prefer-dist --no-progress --no-suggest --ansi \
16321668
symfony/flex
1669+
- name: Require symfony uid
1670+
if: (!startsWith(matrix.php, '7.1'))
1671+
run: composer require symfony/uid --dev --no-progress --ansi
16331672
- name: Update project dependencies
16341673
run: |
16351674
mkdir -p /tmp/api-platform/core/vendor
@@ -1806,6 +1845,9 @@ jobs:
18061845
run: |
18071846
composer global require --prefer-dist --no-progress --no-suggest --ansi \
18081847
symfony/flex
1848+
- name: Require symfony uid
1849+
if: (!startsWith(matrix.php, '7.1'))
1850+
run: composer require symfony/uid --dev --no-progress --ansi
18091851
- name: Update project dependencies
18101852
run: |
18111853
mkdir -p /tmp/api-platform/core/vendor
@@ -1951,6 +1993,9 @@ jobs:
19511993
run: |
19521994
composer global require --prefer-dist --no-progress --no-suggest --ansi \
19531995
symfony/flex
1996+
- name: Require symfony uid
1997+
if: (!startsWith(matrix.php, '7.1'))
1998+
run: composer require symfony/uid --dev --no-progress --ansi
19541999
- name: Update project dependencies
19552000
run: |
19562001
mkdir -p /tmp/api-platform/core/vendor
@@ -2094,6 +2139,9 @@ jobs:
20942139
run: |
20952140
composer global require --prefer-dist --no-progress --no-suggest --ansi \
20962141
symfony/flex
2142+
- name: Require symfony uid
2143+
if: (!startsWith(matrix.php, '7.1'))
2144+
run: composer require symfony/uid --dev --no-progress --ansi
20972145
- name: Update project dependencies
20982146
run: |
20992147
mkdir -p /tmp/api-platform/core/vendor
@@ -2185,6 +2233,9 @@ jobs:
21852233
run: |
21862234
composer global require --prefer-dist --no-progress --no-suggest --ansi \
21872235
symfony/flex
2236+
- name: Require symfony uid
2237+
if: (!startsWith(matrix.php, '7.1'))
2238+
run: composer require symfony/uid --dev --no-progress --ansi
21882239
- name: Allow unstable project dependencies
21892240
run: |
21902241
jq '. + {"minimum-stability": "dev"}' composer.json | sponge composer.json
@@ -2293,6 +2344,9 @@ jobs:
22932344
run: |
22942345
composer global require --prefer-dist --no-progress --no-suggest --ansi \
22952346
symfony/flex
2347+
- name: Require symfony uid
2348+
if: (!startsWith(matrix.php, '7.1'))
2349+
run: composer require symfony/uid --dev --no-progress --ansi
22962350
- name: Allow unstable project dependencies
22972351
run: |
22982352
jq '. + {"minimum-stability": "dev"}' composer.json | sponge composer.json

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
"symfony/expression-language": "To use authorization features.",
105105
"symfony/security": "To use authorization features.",
106106
"symfony/twig-bundle": "To use the Swagger UI integration.",
107+
"symfony/uid": "To support Symfony UUID/ULID identifiers.",
107108
"symfony/web-profiler-bundle": "To use the data collector.",
108109
"webonyx/graphql-php": "To support GraphQL."
109110
},

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use Symfony\Component\Finder\Finder;
5454
use Symfony\Component\HttpClient\HttpClientTrait;
5555
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
56+
use Symfony\Component\Uid\AbstractUid;
5657
use Symfony\Component\Validator\Validator\ValidatorInterface;
5758
use Symfony\Component\Yaml\Yaml;
5859

@@ -167,6 +168,10 @@ private function registerCommonConfiguration(ContainerBuilder $container, array
167168
$loader->load('ramsey_uuid.xml');
168169
}
169170

171+
if (class_exists(AbstractUid::class)) {
172+
$loader->load('symfony_uid.xml');
173+
}
174+
170175
$container->setParameter('api_platform.enable_entrypoint', $config['enable_entrypoint']);
171176
$container->setParameter('api_platform.enable_docs', $config['enable_docs']);
172177
$container->setParameter('api_platform.title', $config['title']);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
<service id="api_platform.identifier.symfony_ulid_normalizer" class="ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer\UlidNormalizer" public="false">
9+
<tag name="api_platform.identifier.denormalizer" />
10+
</service>
11+
12+
<service id="api_platform.identifier.symfony_uuid_normalizer" class="ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer\UuidNormalizer" public="false">
13+
<tag name="api_platform.identifier.denormalizer" />
14+
</service>
15+
</services>
16+
</container>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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 ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer;
15+
16+
use ApiPlatform\Core\Exception\InvalidIdentifierException;
17+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
18+
use Symfony\Component\Uid\Ulid;
19+
20+
/**
21+
* Denormalizes an ULID string to an instance of Symfony\Component\Uid\Ulid.
22+
*/
23+
final class UlidNormalizer implements DenormalizerInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function denormalize($data, $class, $format = null, array $context = [])
29+
{
30+
try {
31+
return Ulid::fromString($data);
32+
} catch (\InvalidArgumentException $e) {
33+
throw new InvalidIdentifierException($e->getMessage(), $e->getCode(), $e);
34+
}
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function supportsDenormalization($data, $type, $format = null)
41+
{
42+
return \is_string($data) && is_a($type, Ulid::class, true);
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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 ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer;
15+
16+
use ApiPlatform\Core\Exception\InvalidIdentifierException;
17+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
18+
use Symfony\Component\Uid\Uuid;
19+
20+
/**
21+
* Denormalizes an UUID string to an instance of Symfony\Component\Uid\Uuid.
22+
*/
23+
final class UuidNormalizer implements DenormalizerInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function denormalize($data, $class, $format = null, array $context = [])
29+
{
30+
try {
31+
return Uuid::fromString($data);
32+
} catch (\InvalidArgumentException $e) {
33+
throw new InvalidIdentifierException($e->getMessage(), $e->getCode(), $e);
34+
}
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function supportsDenormalization($data, $type, $format = null)
41+
{
42+
return \is_string($data) && is_a($type, Uuid::class, true);
43+
}
44+
}

src/JsonSchema/TypeFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use ApiPlatform\Core\Util\ResourceClassInfoTrait;
1818
use Ramsey\Uuid\UuidInterface;
1919
use Symfony\Component\PropertyInfo\Type;
20+
use Symfony\Component\Uid\Ulid;
21+
use Symfony\Component\Uid\Uuid;
2022

2123
/**
2224
* {@inheritdoc}
@@ -106,12 +108,18 @@ private function getClassType(?string $className, string $format, ?bool $readabl
106108
'format' => 'duration',
107109
];
108110
}
109-
if (is_a($className, UuidInterface::class, true)) {
111+
if (is_a($className, UuidInterface::class, true) || is_a($className, Uuid::class, true)) {
110112
return [
111113
'type' => 'string',
112114
'format' => 'uuid',
113115
];
114116
}
117+
if (is_a($className, Ulid::class, true)) {
118+
return [
119+
'type' => 'string',
120+
'format' => 'ulid',
121+
];
122+
}
115123

116124
// Skip if $schema is null (filters only support basic types)
117125
if (null === $schema) {

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
use Symfony\Component\DependencyInjection\Reference;
114114
use Symfony\Component\HttpFoundation\Response;
115115
use Symfony\Component\Serializer\Exception\ExceptionInterface;
116+
use Symfony\Component\Uid\AbstractUid;
116117

117118
/**
118119
* @group resource-hog
@@ -986,6 +987,11 @@ private function getPartialContainerBuilderProphecy($configuration = null)
986987
'api_platform.subresource_operation_factory.cached',
987988
];
988989

990+
if (class_exists(AbstractUid::class)) {
991+
$definitions[] = 'api_platform.identifier.symfony_ulid_normalizer';
992+
$definitions[] = 'api_platform.identifier.symfony_uuid_normalizer';
993+
}
994+
989995
foreach ($definitions as $definition) {
990996
$containerBuilderProphecy->setDefinition($definition, Argument::type(Definition::class))->shouldBeCalled();
991997
}

0 commit comments

Comments
 (0)