1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Facile\MongoDbBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Facile\MongoDbBundle\Services\ExplainQueryService; |
6
|
|
|
|
7
|
|
|
class FacileMongoDbBundleExtension extends \Twig_Extension |
8
|
|
|
{ |
9
|
|
|
private $methodDataTranslationMap = [ |
10
|
|
|
'aggregate' => 'Pipeline', |
11
|
|
|
'insertOne' => 'Document', |
12
|
|
|
'updateOne' => 'Update', |
13
|
|
|
'findOneAndUpdate' => 'Update', |
14
|
|
|
'replaceOne' => 'Replacement', |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
public function getFunctions() |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
|
|
new \Twig_Simplefunction('filterLabelTranslate', array($this, 'queryFilterTranslate')), |
24
|
|
|
new \Twig_Simplefunction('dataLabelTranslate', array($this, 'queryDataTranslate')), |
25
|
|
|
new \Twig_Simplefunction('isQueryExplainable', array($this, 'isQueryExplainable')), |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $label |
31
|
|
|
* @param string $methodName |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
1 |
|
public function queryFilterTranslate(string $label, string $methodName): string |
|
|
|
|
36
|
|
|
{ |
37
|
1 |
|
return $label; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $label |
42
|
|
|
* @param string $methodName |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
6 |
|
public function queryDataTranslate(string $label, string $methodName): string |
47
|
|
|
{ |
48
|
6 |
|
return $this->methodDataTranslationMap[$methodName] ?? $label; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function isQueryExplainable(string $methodName): bool |
52
|
|
|
{ |
53
|
|
|
return in_array($methodName, ExplainQueryService::$acceptedMethods); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the name of the extension. |
58
|
|
|
* |
59
|
|
|
* @return string The extension name |
60
|
|
|
*/ |
61
|
|
|
public function getName() |
62
|
|
|
{ |
63
|
|
|
return 'facile_mongo_db_extesion'; |
64
|
|
|
} |
65
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.