Skip to content

Commit 58aa902

Browse files
Update doctrine inflector (#21)
* Update doctrine inflector * Changes after review
1 parent 59a2878 commit 58aa902

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

.github/workflows/test-application.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99

1010
jobs:
1111
php:
12-
name: 'PHP ${{ matrix.php-version }}, ES ${{ matrix.elasticsearch-version }}'
12+
name: '${{ matrix.job-name-prefix }}PHP ${{ matrix.php-version }}, ES ${{ matrix.elasticsearch-version }}'
1313
runs-on: ubuntu-latest
1414

1515
strategy:
@@ -47,12 +47,14 @@ jobs:
4747
elasticsearch-package-constraint: '^5.0'
4848

4949
- php-version: '7.4'
50+
job-name-prefix: 'Allow to fail: '
5051
elasticsearch-version: '7.11.1'
5152
lint: true
5253
symfony-version: '^5.0'
5354
elasticsearch-package-constraint: '^5.0'
5455

5556
- php-version: '8.0'
57+
job-name-prefix: 'Allow to fail: '
5658
elasticsearch-version: '7.11.1'
5759
lint: true
5860
symfony-version: '^5.0'

Generator/DocumentGenerator.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
namespace ONGR\ElasticsearchBundle\Generator;
1313

14-
use Doctrine\Common\Inflector\Inflector;
14+
use Doctrine\Inflector\Inflector;
15+
use Doctrine\Inflector\InflectorFactory;
1516

1617
/**
1718
* Document Generator
@@ -81,6 +82,11 @@ public function __construct()
8182
<fields>
8283
}';
8384

85+
/**
86+
* @var Inflector
87+
*/
88+
private $inflector;
89+
8490
/**
8591
* @param array $metadata
8692
*
@@ -255,7 +261,7 @@ private function generatePropertyDocBlock(array $metadata)
255261
$column[] = 'options={' . $metadata['property_options'] . '}';
256262
}
257263

258-
$lines[] = $this->spaces . ' * @ES\\' . Inflector::classify($metadata['annotation'])
264+
$lines[] = $this->spaces . ' * @ES\\' . $this->getInflector()->classify($metadata['annotation'])
259265
. '(' . implode(', ', $column) . ')';
260266

261267
$lines[] = $this->spaces . ' */';
@@ -276,7 +282,7 @@ private function generateDocumentDocBlock(array $metadata)
276282
['<className>', '<annotation>', '<options>'],
277283
[
278284
$this->getClassName($metadata),
279-
Inflector::classify($metadata['annotation']),
285+
$this->getInflector()->classify($metadata['annotation']),
280286
$this->getAnnotationOptions($metadata),
281287
],
282288
'/**
@@ -331,7 +337,7 @@ private function getAnnotationOptions(array $metadata)
331337
return '';
332338
}
333339

334-
if ($metadata['type'] === Inflector::tableize($this->getClassName($metadata))) {
340+
if ($metadata['type'] === $this->getInflector()->tableize($this->getClassName($metadata))) {
335341
return '';
336342
}
337343

@@ -371,4 +377,25 @@ private function hasMultipleEmbedded(array $metadata)
371377

372378
return false;
373379
}
380+
381+
/**
382+
* @return Inflector
383+
*/
384+
private function getInflector()
385+
{
386+
if ($this->inflector === null) {
387+
if (\class_exists(InflectorFactory::class)) {
388+
$this->inflector = InflectorFactory::create()->build();
389+
} else {
390+
@trigger_error(
391+
'Using the old inflector is deprecated please upgrade the "doctrine/inflector" package.',
392+
E_USER_DEPRECATED
393+
);
394+
395+
$this->inflector = new \Doctrine\Common\Inflector\Inflector();
396+
}
397+
}
398+
399+
return $this->inflector;
400+
}
374401
}

Mapping/Caser.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@
1111

1212
namespace ONGR\ElasticsearchBundle\Mapping;
1313

14-
use Doctrine\Common\Inflector\Inflector;
14+
use Doctrine\Inflector\Inflector;
15+
use Doctrine\Inflector\InflectorFactory;
1516

1617
/**
1718
* Utility for string case transformations.
1819
*/
1920
class Caser
2021
{
22+
/**
23+
* @var Inflector
24+
*/
25+
private static $inflector;
26+
2127
/**
2228
* Transforms string to camel case (e.g., resultString).
2329
*
@@ -27,7 +33,9 @@ class Caser
2733
*/
2834
public static function camel($string)
2935
{
30-
return Inflector::camelize($string);
36+
$inflector = static::getInflector();
37+
38+
return $inflector->camelize($string);
3139
}
3240

3341
/**
@@ -44,4 +52,25 @@ public static function snake($string)
4452

4553
return strtolower(strtr($string, '-', '_'));
4654
}
55+
56+
/**
57+
* @return Inflector
58+
*/
59+
private static function getInflector()
60+
{
61+
if (static::$inflector === null) {
62+
if (\class_exists(InflectorFactory::class)) {
63+
static::$inflector = InflectorFactory::create()->build();
64+
} else {
65+
@trigger_error(
66+
'Using the old inflector is deprecated please upgrade the "doctrine/inflector" package.',
67+
E_USER_DEPRECATED
68+
);
69+
70+
static::$inflector = new \Doctrine\Common\Inflector\Inflector();
71+
}
72+
}
73+
74+
return static::$inflector;
75+
}
4776
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"symfony/templating": "^2.8|^3.0|^4|^5",
2323
"symfony/asset": "^2.8|^3.0|^4|^5",
2424
"doctrine/annotations": "~1.2",
25-
"doctrine/inflector": "~1.0",
25+
"doctrine/inflector": "^1.0 || ^2.0",
2626
"doctrine/cache": "~1.4",
2727
"doctrine/collections": "~1.4",
2828
"monolog/monolog": "^1.10 || ^2.0",

0 commit comments

Comments
 (0)