From 58aa902eccaa493862796963bb22d56646619bee Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Tue, 23 Feb 2021 13:49:49 +0100 Subject: [PATCH] Update doctrine inflector (#21) * Update doctrine inflector * Changes after review --- .github/workflows/test-application.yaml | 4 ++- Generator/DocumentGenerator.php | 35 ++++++++++++++++++++++--- Mapping/Caser.php | 33 +++++++++++++++++++++-- composer.json | 2 +- 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index d2a17f80..62d90df0 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -9,7 +9,7 @@ on: jobs: php: - name: 'PHP ${{ matrix.php-version }}, ES ${{ matrix.elasticsearch-version }}' + name: '${{ matrix.job-name-prefix }}PHP ${{ matrix.php-version }}, ES ${{ matrix.elasticsearch-version }}' runs-on: ubuntu-latest strategy: @@ -47,12 +47,14 @@ jobs: elasticsearch-package-constraint: '^5.0' - php-version: '7.4' + job-name-prefix: 'Allow to fail: ' elasticsearch-version: '7.11.1' lint: true symfony-version: '^5.0' elasticsearch-package-constraint: '^5.0' - php-version: '8.0' + job-name-prefix: 'Allow to fail: ' elasticsearch-version: '7.11.1' lint: true symfony-version: '^5.0' diff --git a/Generator/DocumentGenerator.php b/Generator/DocumentGenerator.php index c3575358..eca7b10b 100644 --- a/Generator/DocumentGenerator.php +++ b/Generator/DocumentGenerator.php @@ -11,7 +11,8 @@ namespace ONGR\ElasticsearchBundle\Generator; -use Doctrine\Common\Inflector\Inflector; +use Doctrine\Inflector\Inflector; +use Doctrine\Inflector\InflectorFactory; /** * Document Generator @@ -81,6 +82,11 @@ public function __construct() }'; + /** + * @var Inflector + */ + private $inflector; + /** * @param array $metadata * @@ -255,7 +261,7 @@ private function generatePropertyDocBlock(array $metadata) $column[] = 'options={' . $metadata['property_options'] . '}'; } - $lines[] = $this->spaces . ' * @ES\\' . Inflector::classify($metadata['annotation']) + $lines[] = $this->spaces . ' * @ES\\' . $this->getInflector()->classify($metadata['annotation']) . '(' . implode(', ', $column) . ')'; $lines[] = $this->spaces . ' */'; @@ -276,7 +282,7 @@ private function generateDocumentDocBlock(array $metadata) ['', '', ''], [ $this->getClassName($metadata), - Inflector::classify($metadata['annotation']), + $this->getInflector()->classify($metadata['annotation']), $this->getAnnotationOptions($metadata), ], '/** @@ -331,7 +337,7 @@ private function getAnnotationOptions(array $metadata) return ''; } - if ($metadata['type'] === Inflector::tableize($this->getClassName($metadata))) { + if ($metadata['type'] === $this->getInflector()->tableize($this->getClassName($metadata))) { return ''; } @@ -371,4 +377,25 @@ private function hasMultipleEmbedded(array $metadata) return false; } + + /** + * @return Inflector + */ + private function getInflector() + { + if ($this->inflector === null) { + if (\class_exists(InflectorFactory::class)) { + $this->inflector = InflectorFactory::create()->build(); + } else { + @trigger_error( + 'Using the old inflector is deprecated please upgrade the "doctrine/inflector" package.', + E_USER_DEPRECATED + ); + + $this->inflector = new \Doctrine\Common\Inflector\Inflector(); + } + } + + return $this->inflector; + } } diff --git a/Mapping/Caser.php b/Mapping/Caser.php index 6a0022ae..16fb98a6 100644 --- a/Mapping/Caser.php +++ b/Mapping/Caser.php @@ -11,13 +11,19 @@ namespace ONGR\ElasticsearchBundle\Mapping; -use Doctrine\Common\Inflector\Inflector; +use Doctrine\Inflector\Inflector; +use Doctrine\Inflector\InflectorFactory; /** * Utility for string case transformations. */ class Caser { + /** + * @var Inflector + */ + private static $inflector; + /** * Transforms string to camel case (e.g., resultString). * @@ -27,7 +33,9 @@ class Caser */ public static function camel($string) { - return Inflector::camelize($string); + $inflector = static::getInflector(); + + return $inflector->camelize($string); } /** @@ -44,4 +52,25 @@ public static function snake($string) return strtolower(strtr($string, '-', '_')); } + + /** + * @return Inflector + */ + private static function getInflector() + { + if (static::$inflector === null) { + if (\class_exists(InflectorFactory::class)) { + static::$inflector = InflectorFactory::create()->build(); + } else { + @trigger_error( + 'Using the old inflector is deprecated please upgrade the "doctrine/inflector" package.', + E_USER_DEPRECATED + ); + + static::$inflector = new \Doctrine\Common\Inflector\Inflector(); + } + } + + return static::$inflector; + } } diff --git a/composer.json b/composer.json index 93de84cc..f22f7749 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "symfony/templating": "^2.8|^3.0|^4|^5", "symfony/asset": "^2.8|^3.0|^4|^5", "doctrine/annotations": "~1.2", - "doctrine/inflector": "~1.0", + "doctrine/inflector": "^1.0 || ^2.0", "doctrine/cache": "~1.4", "doctrine/collections": "~1.4", "monolog/monolog": "^1.10 || ^2.0",