1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the QueryResourcesLoaderBundle, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2017 RunOpenCode. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\Bundle\QueryResourcesLoader\Twig; |
11
|
|
|
|
12
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
13
|
|
|
|
14
|
|
|
class DoctrineOrmExtension extends \Twig_Extension |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var RegistryInterface |
18
|
|
|
*/ |
19
|
|
|
private $doctrine; |
20
|
|
|
|
21
|
5 |
|
public function __construct(RegistryInterface $doctrine) |
22
|
|
|
{ |
23
|
5 |
|
$this->doctrine = $doctrine; |
24
|
5 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
1 |
|
public function getName() |
30
|
|
|
{ |
31
|
1 |
|
return 'runopencode_query_resources_loader'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
3 |
View Code Duplication |
public function getFunctions() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
return array( |
40
|
|
|
new \Twig_SimpleFunction('table_name', \Closure::bind(function($entity) { |
41
|
1 |
|
return $this->getTableName($entity); |
42
|
3 |
|
}, $this)), |
43
|
|
|
new \Twig_SimpleFunction('column_name', \Closure::bind(function($field, $entity) { |
44
|
1 |
|
return $this->getColumnName($field, $entity); |
45
|
3 |
|
}, $this)) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
View Code Duplication |
public function getFilters() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
return array( |
55
|
|
|
new \Twig_SimpleFilter('table_name', \Closure::bind(function($entity) { |
56
|
|
|
return $this->getTableName($entity); |
57
|
1 |
|
}, $this)), |
58
|
1 |
|
new \Twig_SimpleFilter('column_name', \Closure::bind(function($field, $entity) { |
59
|
|
|
return $this->getColumnName($field, $entity); |
60
|
1 |
|
}, $this)) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get table name for entity |
66
|
|
|
* |
67
|
|
|
* @param string|object $entity Entity |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
1 |
View Code Duplication |
protected function getTableName($entity) |
|
|
|
|
71
|
|
|
{ |
72
|
1 |
|
if (is_object($entity)) { |
73
|
|
|
$entity = get_class($entity); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return $this->doctrine->getManagerForClass($entity)->getClassMetadata($entity)->getTableName(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get column name for property of the entity |
81
|
|
|
* |
82
|
|
|
* @param string $field Entity property |
83
|
|
|
* @param string|object $entity |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
1 |
View Code Duplication |
protected function getColumnName($field, $entity) |
|
|
|
|
87
|
|
|
{ |
88
|
1 |
|
if (is_object($entity)) { |
89
|
|
|
$entity = get_class($entity); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $this->doctrine->getManagerForClass($entity)->getClassMetadata($entity)->getColumnName($field); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.