From 6ff941fd1efc3c1861e2c2bee514721d3e96c994 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 16 May 2020 13:36:06 +0200 Subject: [PATCH 1/2] Handle deprecations from Doctrine Inflector. --- src/Annotation/AttributesHydratorTrait.php | 2 +- src/Bridge/Doctrine/Inflector/Inflector.php | 55 +++++++++++++++++++ .../Factory/CatDocumentMetadataFactory.php | 2 +- .../Symfony/Routing/RouteNameGenerator.php | 2 +- src/GraphQl/Type/FieldsBuilder.php | 2 +- .../DashPathSegmentNameGenerator.php | 2 +- .../UnderscorePathSegmentNameGenerator.php | 2 +- src/Util/AnnotationFilterExtractorTrait.php | 2 +- 8 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 src/Bridge/Doctrine/Inflector/Inflector.php diff --git a/src/Annotation/AttributesHydratorTrait.php b/src/Annotation/AttributesHydratorTrait.php index aba28a5fedb..ea1f1ed6fdd 100644 --- a/src/Annotation/AttributesHydratorTrait.php +++ b/src/Annotation/AttributesHydratorTrait.php @@ -13,8 +13,8 @@ namespace ApiPlatform\Core\Annotation; +use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; use ApiPlatform\Core\Exception\InvalidArgumentException; -use Doctrine\Common\Inflector\Inflector; /** * Hydrates attributes from annotation's parameters. diff --git a/src/Bridge/Doctrine/Inflector/Inflector.php b/src/Bridge/Doctrine/Inflector/Inflector.php new file mode 100644 index 00000000000..4d8974b3677 --- /dev/null +++ b/src/Bridge/Doctrine/Inflector/Inflector.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Core\Bridge\Doctrine\Inflector; + +use Doctrine\Common\Inflector\Inflector as LegacyInflector; +use Doctrine\Inflector\Inflector as InflectorObject; +use Doctrine\Inflector\InflectorFactory; + +/** + * Facade for Doctrine Inflector. + * + * This class allows us to maintain compatibility with Doctrine Inflector 1.3 and 2.0 at the same time. + * + * @internal + */ +final class Inflector +{ + /** + * @var InflectorObject + */ + private static $instance; + + private static function getInstance(): InflectorObject + { + return self::$instance + ?? self::$instance = InflectorFactory::create()->build(); + } + + /** + * @see LegacyInflector::tableize() + */ + public static function tableize(string $word): string + { + return class_exists(InflectorFactory::class) ? self::getInstance()->tableize($word) : LegacyInflector::tableize($word); + } + + /** + * @see LegacyInflector::pluralize() + */ + public static function pluralize(string $word): string + { + return class_exists(InflectorFactory::class) ? self::getInstance()->pluralize($word) : LegacyInflector::pluralize($word); + } +} diff --git a/src/Bridge/Elasticsearch/Metadata/Document/Factory/CatDocumentMetadataFactory.php b/src/Bridge/Elasticsearch/Metadata/Document/Factory/CatDocumentMetadataFactory.php index 0fb5c5260fc..890f58bfd06 100644 --- a/src/Bridge/Elasticsearch/Metadata/Document/Factory/CatDocumentMetadataFactory.php +++ b/src/Bridge/Elasticsearch/Metadata/Document/Factory/CatDocumentMetadataFactory.php @@ -13,10 +13,10 @@ namespace ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Document\Factory; +use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; use ApiPlatform\Core\Bridge\Elasticsearch\Exception\IndexNotFoundException; use ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Document\DocumentMetadata; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; -use Doctrine\Common\Inflector\Inflector; use Elasticsearch\Client; use Elasticsearch\Common\Exceptions\Missing404Exception; diff --git a/src/Bridge/Symfony/Routing/RouteNameGenerator.php b/src/Bridge/Symfony/Routing/RouteNameGenerator.php index d765dc9ef0e..781a5866aa8 100644 --- a/src/Bridge/Symfony/Routing/RouteNameGenerator.php +++ b/src/Bridge/Symfony/Routing/RouteNameGenerator.php @@ -15,8 +15,8 @@ use ApiPlatform\Core\Api\OperationType; use ApiPlatform\Core\Api\OperationTypeDeprecationHelper; +use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; use ApiPlatform\Core\Exception\InvalidArgumentException; -use Doctrine\Common\Inflector\Inflector; /** * Generates the Symfony route name associated with an operation name and a resource short name. diff --git a/src/GraphQl/Type/FieldsBuilder.php b/src/GraphQl/Type/FieldsBuilder.php index 64d0d47a75c..acc9f87826c 100644 --- a/src/GraphQl/Type/FieldsBuilder.php +++ b/src/GraphQl/Type/FieldsBuilder.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Core\GraphQl\Type; +use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; use ApiPlatform\Core\DataProvider\Pagination; use ApiPlatform\Core\Exception\ResourceClassNotFoundException; use ApiPlatform\Core\GraphQl\Resolver\Factory\ResolverFactoryInterface; @@ -21,7 +22,6 @@ use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; -use Doctrine\Common\Inflector\Inflector; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\NullableType; use GraphQL\Type\Definition\Type as GraphQLType; diff --git a/src/Operation/DashPathSegmentNameGenerator.php b/src/Operation/DashPathSegmentNameGenerator.php index 07acc591ecc..15a4efdf8dc 100644 --- a/src/Operation/DashPathSegmentNameGenerator.php +++ b/src/Operation/DashPathSegmentNameGenerator.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Core\Operation; -use Doctrine\Common\Inflector\Inflector; +use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; /** * Generate a path name with a dash separator according to a string and whether it's a collection or not. diff --git a/src/Operation/UnderscorePathSegmentNameGenerator.php b/src/Operation/UnderscorePathSegmentNameGenerator.php index a48f051fa75..329407443b4 100644 --- a/src/Operation/UnderscorePathSegmentNameGenerator.php +++ b/src/Operation/UnderscorePathSegmentNameGenerator.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Core\Operation; -use Doctrine\Common\Inflector\Inflector; +use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; /** * Generate a path name with an underscore separator according to a string and whether it's a collection or not. diff --git a/src/Util/AnnotationFilterExtractorTrait.php b/src/Util/AnnotationFilterExtractorTrait.php index 756dab0d376..4f7826fc0ff 100644 --- a/src/Util/AnnotationFilterExtractorTrait.php +++ b/src/Util/AnnotationFilterExtractorTrait.php @@ -14,8 +14,8 @@ namespace ApiPlatform\Core\Util; use ApiPlatform\Core\Annotation\ApiFilter; +use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; use Doctrine\Common\Annotations\Reader; -use Doctrine\Common\Inflector\Inflector; /** * Generates a service id for a generic filter. From e9de52ed3e1366c3f8201279b202ec995defd9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 28 May 2020 15:19:00 +0200 Subject: [PATCH 2/2] Move Inflector to Util --- src/Annotation/AttributesHydratorTrait.php | 2 +- .../Metadata/Document/Factory/CatDocumentMetadataFactory.php | 2 +- src/Bridge/Symfony/Routing/RouteNameGenerator.php | 2 +- src/GraphQl/Type/FieldsBuilder.php | 2 +- src/Operation/DashPathSegmentNameGenerator.php | 2 +- src/Operation/UnderscorePathSegmentNameGenerator.php | 2 +- src/Util/AnnotationFilterExtractorTrait.php | 1 - src/{Bridge/Doctrine/Inflector => Util}/Inflector.php | 2 +- 8 files changed, 7 insertions(+), 8 deletions(-) rename src/{Bridge/Doctrine/Inflector => Util}/Inflector.php (96%) diff --git a/src/Annotation/AttributesHydratorTrait.php b/src/Annotation/AttributesHydratorTrait.php index ea1f1ed6fdd..1211f73b6a1 100644 --- a/src/Annotation/AttributesHydratorTrait.php +++ b/src/Annotation/AttributesHydratorTrait.php @@ -13,8 +13,8 @@ namespace ApiPlatform\Core\Annotation; -use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; use ApiPlatform\Core\Exception\InvalidArgumentException; +use ApiPlatform\Core\Util\Inflector; /** * Hydrates attributes from annotation's parameters. diff --git a/src/Bridge/Elasticsearch/Metadata/Document/Factory/CatDocumentMetadataFactory.php b/src/Bridge/Elasticsearch/Metadata/Document/Factory/CatDocumentMetadataFactory.php index 890f58bfd06..c7d42878e36 100644 --- a/src/Bridge/Elasticsearch/Metadata/Document/Factory/CatDocumentMetadataFactory.php +++ b/src/Bridge/Elasticsearch/Metadata/Document/Factory/CatDocumentMetadataFactory.php @@ -13,10 +13,10 @@ namespace ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Document\Factory; -use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; use ApiPlatform\Core\Bridge\Elasticsearch\Exception\IndexNotFoundException; use ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Document\DocumentMetadata; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; +use ApiPlatform\Core\Util\Inflector; use Elasticsearch\Client; use Elasticsearch\Common\Exceptions\Missing404Exception; diff --git a/src/Bridge/Symfony/Routing/RouteNameGenerator.php b/src/Bridge/Symfony/Routing/RouteNameGenerator.php index 781a5866aa8..89b3ca3ac5f 100644 --- a/src/Bridge/Symfony/Routing/RouteNameGenerator.php +++ b/src/Bridge/Symfony/Routing/RouteNameGenerator.php @@ -15,8 +15,8 @@ use ApiPlatform\Core\Api\OperationType; use ApiPlatform\Core\Api\OperationTypeDeprecationHelper; -use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; use ApiPlatform\Core\Exception\InvalidArgumentException; +use ApiPlatform\Core\Util\Inflector; /** * Generates the Symfony route name associated with an operation name and a resource short name. diff --git a/src/GraphQl/Type/FieldsBuilder.php b/src/GraphQl/Type/FieldsBuilder.php index acc9f87826c..237f1746e5c 100644 --- a/src/GraphQl/Type/FieldsBuilder.php +++ b/src/GraphQl/Type/FieldsBuilder.php @@ -13,7 +13,6 @@ namespace ApiPlatform\Core\GraphQl\Type; -use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; use ApiPlatform\Core\DataProvider\Pagination; use ApiPlatform\Core\Exception\ResourceClassNotFoundException; use ApiPlatform\Core\GraphQl\Resolver\Factory\ResolverFactoryInterface; @@ -22,6 +21,7 @@ use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; +use ApiPlatform\Core\Util\Inflector; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\NullableType; use GraphQL\Type\Definition\Type as GraphQLType; diff --git a/src/Operation/DashPathSegmentNameGenerator.php b/src/Operation/DashPathSegmentNameGenerator.php index 15a4efdf8dc..1a31f1fa7fc 100644 --- a/src/Operation/DashPathSegmentNameGenerator.php +++ b/src/Operation/DashPathSegmentNameGenerator.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Core\Operation; -use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; +use ApiPlatform\Core\Util\Inflector; /** * Generate a path name with a dash separator according to a string and whether it's a collection or not. diff --git a/src/Operation/UnderscorePathSegmentNameGenerator.php b/src/Operation/UnderscorePathSegmentNameGenerator.php index 329407443b4..c0e5694ac53 100644 --- a/src/Operation/UnderscorePathSegmentNameGenerator.php +++ b/src/Operation/UnderscorePathSegmentNameGenerator.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Core\Operation; -use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; +use ApiPlatform\Core\Util\Inflector; /** * Generate a path name with an underscore separator according to a string and whether it's a collection or not. diff --git a/src/Util/AnnotationFilterExtractorTrait.php b/src/Util/AnnotationFilterExtractorTrait.php index 4f7826fc0ff..33be708471a 100644 --- a/src/Util/AnnotationFilterExtractorTrait.php +++ b/src/Util/AnnotationFilterExtractorTrait.php @@ -14,7 +14,6 @@ namespace ApiPlatform\Core\Util; use ApiPlatform\Core\Annotation\ApiFilter; -use ApiPlatform\Core\Bridge\Doctrine\Inflector\Inflector; use Doctrine\Common\Annotations\Reader; /** diff --git a/src/Bridge/Doctrine/Inflector/Inflector.php b/src/Util/Inflector.php similarity index 96% rename from src/Bridge/Doctrine/Inflector/Inflector.php rename to src/Util/Inflector.php index 4d8974b3677..0c9cf904316 100644 --- a/src/Bridge/Doctrine/Inflector/Inflector.php +++ b/src/Util/Inflector.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace ApiPlatform\Core\Bridge\Doctrine\Inflector; +namespace ApiPlatform\Core\Util; use Doctrine\Common\Inflector\Inflector as LegacyInflector; use Doctrine\Inflector\Inflector as InflectorObject;