diff --git a/src/Annotation/AttributesHydratorTrait.php b/src/Annotation/AttributesHydratorTrait.php index aba28a5fedb..1211f73b6a1 100644 --- a/src/Annotation/AttributesHydratorTrait.php +++ b/src/Annotation/AttributesHydratorTrait.php @@ -14,7 +14,7 @@ namespace ApiPlatform\Core\Annotation; use ApiPlatform\Core\Exception\InvalidArgumentException; -use Doctrine\Common\Inflector\Inflector; +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 0fb5c5260fc..c7d42878e36 100644 --- a/src/Bridge/Elasticsearch/Metadata/Document/Factory/CatDocumentMetadataFactory.php +++ b/src/Bridge/Elasticsearch/Metadata/Document/Factory/CatDocumentMetadataFactory.php @@ -16,7 +16,7 @@ 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 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 d765dc9ef0e..89b3ca3ac5f 100644 --- a/src/Bridge/Symfony/Routing/RouteNameGenerator.php +++ b/src/Bridge/Symfony/Routing/RouteNameGenerator.php @@ -16,7 +16,7 @@ use ApiPlatform\Core\Api\OperationType; use ApiPlatform\Core\Api\OperationTypeDeprecationHelper; use ApiPlatform\Core\Exception\InvalidArgumentException; -use Doctrine\Common\Inflector\Inflector; +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 64d0d47a75c..237f1746e5c 100644 --- a/src/GraphQl/Type/FieldsBuilder.php +++ b/src/GraphQl/Type/FieldsBuilder.php @@ -21,7 +21,7 @@ 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 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 07acc591ecc..1a31f1fa7fc 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\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 a48f051fa75..c0e5694ac53 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\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 756dab0d376..33be708471a 100644 --- a/src/Util/AnnotationFilterExtractorTrait.php +++ b/src/Util/AnnotationFilterExtractorTrait.php @@ -15,7 +15,6 @@ use ApiPlatform\Core\Annotation\ApiFilter; use Doctrine\Common\Annotations\Reader; -use Doctrine\Common\Inflector\Inflector; /** * Generates a service id for a generic filter. diff --git a/src/Util/Inflector.php b/src/Util/Inflector.php new file mode 100644 index 00000000000..0c9cf904316 --- /dev/null +++ b/src/Util/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\Util; + +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); + } +}