8000 feature #600 Allow installing doctrine/inflector 2.0 (alcaeus, weaver… · symfony/maker-bundle@5e8dab5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5e8dab5

Browse files
committed
feature #600 Allow installing doctrine/inflector 2.0 (alcaeus, weaverryan)
This PR was merged into the 1.0-dev branch. Discussion ---------- Allow installing doctrine/inflector 2.0 With the release of doctrine/inflector 1.4, the existing API is deprecated in favour of a new API. This PR updates the MakerBundle to provide compatibility to doctrine/inflector 1.2 (required to provide support for PHP 7.1), Inflector 1.4 (providing the new API and deprecating the legacy API), and Inflector 2.0 (which drops the legacy API). This adds a new (currently optional) constructor argument to the crud maker. This is the only maker that gets the inflector injected instead of using it through the `Str` class (which now provides its own static maker). In the long term, this should be made consistent: the new API technically allows us to support pluralising and singularising strings in a language other than English (Inflector 1.4+ supports English, Spanish, Portuguese, and a few more). This is beyond the scope of this PR and thus omitted for the time being. NB: I saw that PHP 7.0 was dropped recently. Dropping PHP 7.1 as well would allow changing the constraint to require at least Inflector 1.4, removing the need for using the legacy inflector API and always using the new API. It is not required to make this bundle compatible with 2.0. Commits ------- 335bd1e simplifying: creating Inflector when it's needed 2cec448 phpcs 4821139 Allow installing doctrine/inflector 2.0
2 parents afd9f3f + 335bd1e commit 5e8dab5

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"minimum-stability": "dev",
1515
"require": {
1616
"php": "^7.1.3",
17-
"doctrine/inflector": "^1.2",
17+
"doctrine/inflector": "^1.2 || ^2.0",
1818
"nikic/php-parser": "^4.0",
1919
"symfony/config": "^3.4|^4.0|^5.0",
2020
"symfony/console": "^3.4|^4.0|^5.0",

src/Maker/MakeCrud.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespace Symfony\Bundle\MakerBundle\Maker;
1313

1414
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
15-
use Doctrine\Common\Inflector\Inflector;
15+
use Doctrine\Common\Inflector\Inflector as LegacyInflector;
16+
use Doctrine\Inflector\InflectorFactory;
1617
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
1718
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1819
use Symfony\Bundle\MakerBundle\DependencyBuilder;
@@ -41,10 +42,16 @@ final class MakeCrud extends AbstractMaker
4142

4243
private $formTypeRenderer;
4344

45+
private $inflector;
46+
4447
public function __construct(DoctrineHelper $doctrineHelper, FormTypeRenderer $formTypeRenderer)
4548
{
4649
$this->doctrineHelper = $doctrineHelper;
4750
$this->formTypeRenderer = $formTypeRenderer;
51+
52+
if (class_exists(InflectorFactory::class)) {
53+
$this->inflector = InflectorFactory::create()->build();
54+
}
4855
}
4956

5057
public static function getCommandName(): string
@@ -103,7 +110,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
103110
$repositoryVars = [
104111
'repository_full_class_name' => $repositoryClassDetails->getFullName(),
105112
'repository_class_name' => $repositoryClassDetails->getShortName(),
106-
'repository_var' => lcfirst(Inflector::singularize($repositoryClassDetails->getShortName())),
113+
'repository_var' => lcfirst($this->singularize($repositoryClassDetails->getShortName())),
107114
];
108115
}
109116

@@ -123,8 +130,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
123130
++$iter;
124131
} while (class_exists($formClassDetails->getFullName()));
125132

126-
$entityVarPlural = lcfirst(Inflector::pluralize($entityClassDetails->getShortName()));
127-
$entityVarSingular = lcfirst(Inflector::singularize($entityClassDetails->getShortName()));
133+
$entityVarPlural = lcfirst($this->pluralize($entityClassDetails->getShortName()));
134+
$entityVarSingular = lcfirst($this->singularize($entityClassDetails->getShortName()));
128135

129136
$entityTwigVarPlural = Str::asTwigVariable($entityVarPlural);
130137
$entityTwigVarSingular = Str::asTwigVariable($entityVarSingular);
@@ -248,4 +255,22 @@ public function configureDependencies(DependencyBuilder $dependencies)
248255
'annotations'
249256
);
250257
}
258+
259+
private function pluralize(string $word): string
260+
{
261+
if (null !== $this->inflector) {
262+
return $this->inflector->pluralize($word);
263+
}
264+
265+
return LegacyInflector::pluralize($word);
266+
}
267+
268+
private function singularize(string $word): string
269+
{
270+
if (null !== $this->inflector) {
271+
return $this->inflector->singularize($word);
272+
}
273+
274+
return LegacyInflector::singularize($word);
275+
}
251276
}

src/Str.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Bundle\MakerBundle;
1313

14-
use Doctrine\Common\Inflector\Inflector;
14+
use Doctrine\Common\Inflector\Inflector as LegacyInflector;
15+
use Doctrine\Inflector\Inflector;
16+
use Doctrine\Inflector\InflectorFactory;
1517
use Symfony\Component\DependencyInjection\Container;
1618

1719
/**
@@ -20,6 +22,9 @@
2022
*/
2123
final class Str
2224
{
25+
/** @var Inflector|null */
26+
private static $inflector;
27+
2328
/**
2429
* Looks for suffixes in strings in a case-insensitive way.
2530
*/
@@ -141,7 +146,7 @@ public static function singularCamelCaseToPluralCamelCase(string $camelCase): st
141146
{
142147
$snake = self::asSnakeCase($camelCase);
143148
$words = explode('_', $snake);
144-
$words[\count($words) - 1] = Inflector::pluralize($words[\count($words) - 1]);
149+
$words[\count($words) - 1] = self::pluralize($words[\count($words) - 1]);
145150
$reSnaked = implode('_', $words);
146151

147152
return self::asLowerCamelCase($reSnaked);
@@ -151,7 +156,7 @@ public static function pluralCamelCaseToSingular(string $camelCase): string
151156
{
152157
$snake = self::asSnakeCase($camelCase);
153158
$words = explode('_', $snake);
154-
$words[\count($words) - 1] = Inflector::singularize($words[\count($words) - 1]);
159+
$words[\count($words) - 1] = self::singularize($words[\count($words) - 1]);
155160
$reSnaked = implode('_', $words);
156161

157162
return self::asLowerCamelCase($reSnaked);
@@ -210,4 +215,31 @@ public static function asHumanWords(string $variableName): string
210215
{
211216
return implode(' ', preg_split('/(?=[A-Z])/', $variableName));
212217
}
218+
219+
private static function pluralize(string $word): string
220+
{
221+
if (class_exists(Inflector::class)) {
222+
return static::getInflector()->pluralize($word);
223+
}
224+
225+
return LegacyInflector::pluralize($word);
226+
}
227+
228+
private static function singularize(string $word): string
229+
{
230+
if (class_exists(Inflector::class)) {
231+
return static::getInflector()->singularize($word);
232+
}
233+
234+
return LegacyInflector::singularize($word);
235+
}
236+
237+
private static function getInflector(): Inflector
238+
{
239+
if (null === static::$inflector) {
240+
static::$inflector = InflectorFactory::create()->build();
241+
}
242+
243+
return static::$inflector;
244+
}
213245
}

0 commit comments

Comments
 (0)
0