Skip to content

Commit

Permalink
feat: replace doctrine/inflector by symfony/string (#5637)
Browse files Browse the repository at this point in the history
* feat: replace doctrine/inflector by symfony/string

* fix: use the last element for a better match instead of the first

* feat: adding deprecation

* feat: adding config flag to activate new symfony inflector

* fix: revert change on old inflector

* fix: remove unused import
  • Loading branch information
Romaixn authored Jul 5, 2023
1 parent 4ef0ef8 commit 6babb3d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
25 changes: 20 additions & 5 deletions src/Metadata/Util/Inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,39 @@

namespace ApiPlatform\Metadata\Util;

use Doctrine\Inflector\Inflector as InflectorObject;
use Doctrine\Inflector\Inflector as LegacyInflector;
use Doctrine\Inflector\InflectorFactory;
use Symfony\Component\String\Inflector\EnglishInflector;
use Symfony\Component\String\UnicodeString;

/**
* Facade for Doctrine Inflector.
*
* @internal
*/
final class Inflector
{
private static ?InflectorObject $instance = null;
private static bool $keepLegacyInflector;
private static ?LegacyInflector $instance = null;

private static function getInstance(): InflectorObject
private static function getInstance(): LegacyInflector
{
return self::$instance
?? self::$instance = InflectorFactory::create()->build();
}

public static function keepLegacyInflector(bool $keepLegacyInflector): void
{
self::$keepLegacyInflector = $keepLegacyInflector;
}

/**
* @see InflectorObject::tableize()
*/
public static function tableize(string $word): string
{
if (!self::$keepLegacyInflector) {
return (new UnicodeString($word))->snake()->toString();
}

return self::getInstance()->tableize($word);
}

Expand All @@ -44,6 +54,11 @@ public static function tableize(string $word): string
*/
public static function pluralize(string $word): string
{
if (!self::$keepLegacyInflector) {
$pluralize = (new EnglishInflector())->pluralize($word);
return end($pluralize);
}

return self::getInstance()->pluralize($word);
}
}
3 changes: 2 additions & 1 deletion src/Metadata/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"doctrine/inflector": "^2.0",
"psr/cache": "^3.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"symfony/property-info": "^6.1"
"symfony/property-info": "^6.1",
"symfony/string": "^6.1"
},
"require-dev": {
"phpstan/phpdoc-parser": "^1.16",
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use ApiPlatform\GraphQl\Resolver\QueryItemResolverInterface;
use ApiPlatform\GraphQl\Type\Definition\TypeInterface as GraphQlTypeInterface;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Util\Inflector;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\Symfony\GraphQl\Resolver\Factory\DataCollectorResolverFactory;
Expand Down Expand Up @@ -141,6 +142,8 @@ public function load(array $configs, ContainerBuilder $container): void
if (!$container->has('api_platform.state.item_provider')) {
$container->setAlias('api_platform.state.item_provider', 'api_platform.state_provider.object');
}

$this->registerInflectorConfiguration($config);
}

private function registerCommonConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader, array $formats, array $patchFormats, array $errorFormats): void
Expand All @@ -160,6 +163,7 @@ private function registerCommonConfiguration(ContainerBuilder $container, array

$container->setParameter('api_platform.enable_entrypoint', $config['enable_entrypoint']);
$container->setParameter('api_platform.enable_docs', $config['enable_docs']);
$container->setParameter('api_platform.keep_legacy_inflector', $config['keep_legacy_inflector']);
$container->setParameter('api_platform.title', $config['title']);
$container->setParameter('api_platform.description', $config['description']);
$container->setParameter('api_platform.version', $config['version']);
Expand Down Expand Up @@ -786,4 +790,14 @@ private function registerArgumentResolverConfiguration(XmlFileLoader $loader): v
{
$loader->load('argument_resolver.xml');
}

private function registerInflectorConfiguration(array $config): void
{
if ($config['keep_legacy_inflector']) {
Inflector::keepLegacyInflector(true);
trigger_deprecation('api-platform/core', '3.2', 'Using doctrine/inflector is deprecated since API Platform 3.2 and will be removed in API Platform 4. Use symfony/string instead. Run "composer require symfony/string" and set "keep_legacy_inflector" to false in config.');
} else {
Inflector::keepLegacyInflector(false);
}
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->booleanNode('enable_entrypoint')->defaultTrue()->info('Enable the entrypoint')->end()
->booleanNode('enable_docs')->defaultTrue()->info('Enable the docs')->end()
->booleanNode('enable_profiler')->defaultTrue()->info('Enable the data collector and the WebProfilerBundle integration.')->end()
->booleanNode('keep_legacy_inflector')->defaultTrue()->info('Keep doctrine/inflector instead of symfony/string to generate plurals for routes.')->end()
->arrayNode('collection')
->addDefaultsIfNotSet()
->children()
Expand Down
8 changes: 3 additions & 5 deletions src/Util/Inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@

namespace ApiPlatform\Util;

use Doctrine\Inflector\Inflector as InflectorObject;
use Doctrine\Inflector\Inflector as LegacyInflector;
use Doctrine\Inflector\InflectorFactory;

/**
* Facade for Doctrine Inflector.
*
* @internal
*/
final class Inflector
{
private static ?InflectorObject $instance = null;
private static ?LegacyInflector $instance = null;

private static function getInstance(): InflectorObject
private static function getInstance(): LegacyInflector
{
return self::$instance
?? self::$instance = InflectorFactory::create()->build();
Expand Down

0 comments on commit 6babb3d

Please sign in to comment.