Skip to content

Commit

Permalink
Update doctrine inflector (#21)
Browse files Browse the repository at this point in the history
* Update doctrine inflector

* Changes after review
  • Loading branch information
alexander-schranz authored Feb 23, 2021
1 parent 59a2878 commit 58aa902
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand Down
35 changes: 31 additions & 4 deletions Generator/DocumentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace ONGR\ElasticsearchBundle\Generator;

use Doctrine\Common\Inflector\Inflector;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;

/**
* Document Generator
Expand Down Expand Up @@ -81,6 +82,11 @@ public function __construct()
<fields>
}';

/**
* @var Inflector
*/
private $inflector;

/**
* @param array $metadata
*
Expand Down Expand Up @@ -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 . ' */';
Expand All @@ -276,7 +282,7 @@ private function generateDocumentDocBlock(array $metadata)
['<className>', '<annotation>', '<options>'],
[
$this->getClassName($metadata),
Inflector::classify($metadata['annotation']),
$this->getInflector()->classify($metadata['annotation']),
$this->getAnnotationOptions($metadata),
],
'/**
Expand Down Expand Up @@ -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 '';
}

Expand Down Expand Up @@ -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;
}
}
33 changes: 31 additions & 2 deletions Mapping/Caser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand All @@ -27,7 +33,9 @@ class Caser
*/
public static function camel($string)
{
return Inflector::camelize($string);
$inflector = static::getInflector();

return $inflector->camelize($string);
}

/**
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 58aa902

Please sign in to comment.