diff --git a/composer.json b/composer.json index a3662a6e6..a59b73e75 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,6 @@ "minimum-stability": "dev", "require": { "php": ">=7.2.5", - "doctrine/inflector": "^2.0", "nikic/php-parser": "^4.11", "symfony/config": "^5.4.7|^6.0", "symfony/console": "^5.4.7|^6.0", @@ -23,7 +22,8 @@ "symfony/filesystem": "^5.4.7|^6.0", "symfony/finder": "^5.4.7|^6.0", "symfony/framework-bundle": "^5.4.7|^6.0", - "symfony/http-kernel": "^5.4.7|^6.0" + "symfony/http-kernel": "^5.4.7|^6.0", + "symfony/string": "^5.4|^6.0" }, "require-dev": { "composer/semver": "^3.0", diff --git a/src/Maker/MakeCrud.php b/src/Maker/MakeCrud.php index e01676bcd..3343cc6c0 100644 --- a/src/Maker/MakeCrud.php +++ b/src/Maker/MakeCrud.php @@ -12,7 +12,6 @@ namespace Symfony\Bundle\MakerBundle\Maker; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; -use Doctrine\Inflector\InflectorFactory; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\MakerBundle\ConsoleStyle; @@ -31,6 +30,7 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Csrf\CsrfTokenManager; +use Symfony\Component\String\Inflector\EnglishInflector; use Symfony\Component\Validator\Validation; /** @@ -50,7 +50,8 @@ public function __construct(DoctrineHelper $doctrineHelper, FormTypeRenderer $fo { $this->doctrineHelper = $doctrineHelper; $this->formTypeRenderer = $formTypeRenderer; - $this->inflector = InflectorFactory::create()->build(); + + $this->inflector = new EnglishInflector(); } public static function getCommandName(): string @@ -120,7 +121,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $repositoryVars = [ 'repository_full_class_name' => $repositoryClassDetails->getFullName(), 'repository_class_name' => $repositoryClassDetails->getShortName(), - 'repository_var' => lcfirst($this->inflector->singularize($repositoryClassDetails->getShortName())), + 'repository_var' => lcfirst($this->inflector->singularize($repositoryClassDetails->getShortName())[0]), ]; } @@ -140,8 +141,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen ++$iter; } while (class_exists($formClassDetails->getFullName())); - $entityVarPlural = lcfirst($this->inflector->pluralize($entityClassDetails->getShortName())); - $entityVarSingular = lcfirst($this->inflector->singularize($entityClassDetails->getShortName())); + $entityVarPlural = lcfirst($this->inflector->pluralize($entityClassDetails->getShortName())[0]); + $entityVarSingular = lcfirst($this->inflector->singularize($entityClassDetails->getShortName())[0]); $entityTwigVarPlural = Str::asTwigVariable($entityVarPlural); $entityTwigVarSingular = Str::asTwigVariable($entityVarSingular); diff --git a/src/Str.php b/src/Str.php index b64585403..5e5b392c2 100644 --- a/src/Str.php +++ b/src/Str.php @@ -11,9 +11,9 @@ namespace Symfony\Bundle\MakerBundle; -use Doctrine\Inflector\Inflector; -use Doctrine\Inflector\InflectorFactory; use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\String\Inflector\EnglishInflector; +use function Symfony\Component\String\u; /** * @author Javier Eguiluz @@ -21,9 +21,6 @@ */ final class Str { - /** @var Inflector|null */ - private static $inflector; - /** * Looks for suffixes in strings in a case-insensitive way. */ @@ -39,7 +36,9 @@ public static function hasSuffix(string $value, string $suffix): bool */ public static function addSuffix(string $value, string $suffix): string { - return self::removeSuffix($value, $suffix).$suffix; + $string = u($value)->ignoreCase(); + + return $string->trimSuffix($suffix)->append($suffix); } /** @@ -49,7 +48,7 @@ public static function addSuffix(string $value, string $suffix): string */ public static function removeSuffix(string $value, string $suffix): string { - return self::hasSuffix($value, $suffix) ? substr($value, 0, -\strlen($suffix)) : $value; + return u($value)->ignoreCase()->trimSuffix($suffix); } /** @@ -86,12 +85,12 @@ public static function asTwigVariable(string $value): string public static function asLowerCamelCase(string $str): string { - return lcfirst(self::asCamelCase($str)); + return u($str)->camel(); } public static function asCamelCase(string $str): string { - return strtr(ucwords(strtr($str, ['_' => ' ', '.' => ' ', '\\' => ' '])), [' ' => '']); + return u($str)->camel()->title(); } public static function asRoutePath(string $value): string @@ -108,7 +107,7 @@ public static function asRouteName(string $value): string public static function asSnakeCase(string $value): string { - return self::asTwigVariable($value); + return u($value)->snake(); } public static function asCommand(string $value): string @@ -145,8 +144,7 @@ public static function asFilePath(string $value): string public static function singularCamelCaseToPluralCamelCase(string $camelCase): string { - $snake = self::asSnakeCase($camelCase); - $words = explode('_', $snake); + $words = explode('_', u($camelCase)->snake()); $words[\count($words) - 1] = self::pluralize($words[\count($words) - 1]); $reSnaked = implode('_', $words); @@ -155,8 +153,7 @@ public static function singularCamelCaseToPluralCamelCase(string $camelCase): st public static function pluralCamelCaseToSingular(string $camelCase): string { - $snake = self::asSnakeCase($camelCase); - $words = explode('_', $snake); + $words = explode('_', u($camelCase)->snake()); $words[\count($words) - 1] = self::singularize($words[\count($words) - 1]); $reSnaked = implode('_', $words); @@ -219,20 +216,15 @@ public static function asHumanWords(string $variableName): string private static function pluralize(string $word): string { - return static::getInflector()->pluralize($word); - } + $result = (new EnglishInflector())->pluralize($word); - private static function singularize(string $word): string - { - return static::getInflector()->singularize($word); + return $result[0]; } - private static function getInflector(): Inflector + private static function singularize(string $word): string { - if (null === static::$inflector) { - static::$inflector = InflectorFactory::create()->build(); - } + $result = (new EnglishInflector())->singularize($word); - return static::$inflector; + return $result[0]; } }