10000 Allow installing doctrine/inflector 2.0 by alcaeus · Pull Request #600 · symfony/maker-bundle · GitHub
[go: up one dir, main page]

Skip to content

Allow installing doctrine/inflector 2.0 #600

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

Merged
merged 3 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Allow installing doctrine/inflector 2.0
  • Loading branch information
alcaeus committed May 7, 2020
commit 48211390ad6201ad737ef0977ef26e853fba805e
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"minimum-stability": "dev",
"require": {
"php": "^7.1.3",
"doctrine/inflector": "^1.2",
"doctrine/inflector": "^1.2 || ^2.0",
"nikic/php-parser": "^4.0",
"symfony/config": "^3.4|^4.0|^5.0",
"symfony/console": "^3.4|^4.0|^5.0",
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/MakerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\MakerBundle\DependencyInjection;

use Doctrine\Inflector\InflectorFactory;
use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\MakeCommandRegistrationPass;
use Symfony\Bundle\MakerBundle\MakerInterface;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -48,6 +49,10 @@ public function load(array $configs, ContainerBuilder $container)
$doctrineHelperDefinition = $container->getDefinition('maker.doctrine_helper');
$doctrineHelperDefinition->replaceArgument(0, $rootNamespace.'\\Entity');

if (!class_exists(InflectorFactory::class)) {
$container->removeDefinition('maker.doctrine_inflector');
}

$container->registerForAutoconfiguration(MakerInterface::class)
->addTag(MakeCommandRegistrationPass::MAKER_TAG);
}
Expand Down
42 changes: 37 additions & 5 deletions src/Maker/MakeCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace Symfony\Bundle\MakerBundle\Maker;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Common\Inflector\Inflector;
use Doctrine\Common\Inflector\Inflector as LegacyInflector;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
Expand Down Expand Up @@ -41,10 +43,22 @@ final class MakeCrud extends AbstractMaker

private $formTypeRenderer;

public function __construct(DoctrineHelper $doctrineHelper, FormTypeRenderer $formTypeRenderer)
/** @var Inflector|null */
private $inflector;

public function __construct(DoctrineHelper $doctrineHelper, FormTypeRenderer $formTypeRenderer, Inflector $inflector = null)
{
$this->doctrineHelper = $doctrineHelper;
$this->formTypeRenderer = $formTypeRenderer;
$this->inflector = $inflector;

if (null === $inflector) {
@trigger_error(sprintf('"%s" will require a value for the $inflector argument in 2.0.', __METHOD__), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit tricky. When testing with lowest deps (so doctrine/inflector 1.0), MakerBundle's config will not pass this argument, and so it triggers a deprecation. In other words: if you use MakeBundle with doctrine/inflector 1.0, you would see this deprecation no matter what (unless you upgraded doctrine/inflector to 2.0.

One easy fix would be to not add a new argument: just InflectorFactory::create()->build(); like we do with Validator. Obviously, that's not normally something we would do, but is there any practical downside?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No practical downside. If you were to support multiple languages, you’d want to inject it, but given how we hardcode to English we may as well push this change back, at least until we require 1.4 and always have the new API.


if (class_exists(InflectorFactory::class)) {
$this->inflector = InflectorFactory::create()->build();
}
}
}

public static function getCommandName(): string
Expand Down Expand Up @@ -103,7 +117,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(Inflector::singularize($repositoryClassDetails->getShortName())),
'repository_var' => lcfirst($this->singularize($repositoryClassDetails->getShortName())),
];
}

Expand All @@ -123,8 +137,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
++$iter;
} while (class_exists($formClassDetails->getFullName()));

$entityVarPlural = lcfirst(Inflector::pluralize($entityClassDetails->getShortName()));
$entityVarSingular = lcfirst(Inflector::singularize($entityClassDetails->getShortName()));
$entityVarPlural = lcfirst($this->pluralize($entityClassDetails->getShortName()));
$entityVarSingular = lcfirst($this->singularize($entityClassDetails->getShortName()));

$entityTwigVarPlural = Str::asTwigVariable($entityVarPlural);
$entityTwigVarSingular = Str::asTwigVariable($entityVarSingular);
Expand Down Expand Up @@ -248,4 +262,22 @@ public function configureDependencies(DependencyBuilder $dependencies)
'annotations'
);
}

private function pluralize(string $word): string
{
if (null !== $this->inflector) {
return $this->inflector->pluralize($word);
}

return LegacyInflector::pluralize($word);
}

private function singularize(string $word): string
{
if (null !== $this->inflector) {
return $this->inflector->singularize($word);
}

return LegacyInflector::singularize($word);
}
}
1 change: 1 addition & 0 deletions src/Resources/config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<service id="maker.maker.make_crud" class="Symfony\Bundle\MakerBundle\Maker\MakeCrud">
<argument type="service" id="maker.doctrine_helper" />
<argument type="service" id="maker.renderer.form_type_renderer" />
<argument type="service" id="maker.doctrine_inflector" on-invalid="null" />
<tag name="maker.command" />
</service>

Expand Down
8 changes: 8 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
<argument type="service" id="doctrine" on-invalid="ignore" />
</service>

<service id="maker.doctrine_inflector_factory" class="Doctrine\Inflector\InflectorFactory">
<factory class="Doctrine\Inflector\InflectorFactory" method="create" />
</service>

<service id="maker.doctrine_inflector" class="Doctrine\Inflector\Inflector">
<factory service="maker.doctrine_inflector_factory" method="build" />
</service>

<service id="maker.auto_command.abstract" class="Symfony\Bundle\MakerBundle\Command\MakerCommand" abstract="true">
<argument /> <!-- maker -->
<argument type="service" id="maker.file_manager" />
Expand Down
38 changes: 35 additions & 3 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Bundle\MakerBundle;

use Doctrine\Common\Inflector\Inflector;
use Doctrine\Common\Inflector\Inflector as LegacyInflector;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use Symfony\Component\DependencyInjection\Container;

/**
Expand All @@ -20,6 +22,9 @@
*/
final class Str
{
/** @var Inflector|null */
private static $inflector;

/**
* Looks for suffixes in strings in a case-insensitive way.
*/
Expand Down Expand Up @@ -141,7 +146,7 @@ public static function singularCamelCaseToPluralCamelCase(string $camelCase): st
{
$snake = self::asSnakeCase($camelCase);
$words = explode('_', $snake);
$words[\count($words) - 1] = Inflector::pluralize($words[\count($words) - 1]);
$words[\count($words) - 1] = self::pluralize($words[\count($words) - 1]);
$reSnaked = implode('_', $words);

return self::asLowerCamelCase($reSnaked);
Expand All @@ -151,7 +156,7 @@ public static function pluralCamelCaseToSingular(string $camelCase): string
{
$snake = self::asSnakeCase($camelCase);
$words = explode('_', $snake);
$words[\count($words) - 1] = Inflector::singularize($words[\count($words) - 1]);
$words[\count($words) - 1] = self::singularize($words[\count($words) - 1]);
$reSnaked = implode('_', $words);

return self::asLowerCamelCase($reSnaked);
Expand Down Expand Up @@ -210,4 +215,31 @@ public static function asHumanWords(string $variableName): string
{
return implode(' ', preg_split('/(?=[A-Z])/', $variableName));
}

private static function pluralize(string $word): string
{
if (class_exists(Inflector::class)) {
return static::getInflector()->pluralize($word);
}

return LegacyInflector::pluralize($word);
}

private static function singularize(string $word): string
{
if (class_exists(Inflector::class)) {
return static::getInflector()->singularize($word);
}

return LegacyInflector::singularize($word);
}

private static function getInflector(): Inflector
{
if (null === static::$inflector) {
static::$inflector = InflectorFactory::create()->build();
}

return static::$inflector;
}
}
1 change: 1 addition & 0 deletions src/Test/MakerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ abstract class MakerTestCase extends TestCase

/**
* @dataProvider getTestDetails
* @group legacy
*/
public function testExecute(MakerTestDetails $makerTestDetails)
{
Expand Down
3 changes: 3 additions & 0 deletions tests/Maker/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\MakerBundle\Tests\Maker;

use Doctrine\Inflector\InflectorFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\MakerBundle\Command\MakerCommand;
Expand All @@ -24,6 +25,8 @@ class FunctionalTest extends TestCase
/**
* Smoke test to make sure the DI autowiring works and all makers
* are registered and have the correct arguments.
*
* @group legacy
*/
public function testWiring()
{
Expand Down
0