Skip to content

WIP - replace doctrine/inflector w/ symfony/string #1100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
11 changes: 6 additions & 5 deletions src/Maker/MakeCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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
Expand Down Expand Up @@ -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]),
];
}

Expand All @@ -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);
Expand Down
40 changes: 16 additions & 24 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@

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 <[email protected]>
* @author Ryan Weaver <[email protected]>
*/
final class Str
{
/** @var Inflector|null */
private static $inflector;

/**
* Looks for suffixes in strings in a case-insensitive way.
*/
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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];
}
}