1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Doctrine Naming Strategy Bundle, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2015 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\DoctrineNamingStrategy\NamingStrategy; |
11
|
|
|
|
12
|
|
|
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy; |
13
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
14
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
15
|
|
|
|
16
|
|
|
class UnderscoredBundleNamePrefix extends UnderscoreNamingStrategy |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $map; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
protected $joinTableFieldSuffix; |
27
|
|
|
|
28
|
12 |
|
public function __construct(KernelInterface $kernel, array $configuration = array()) |
29
|
|
|
{ |
30
|
12 |
|
$configuration = array_merge(array( |
31
|
12 |
|
'case' => CASE_LOWER, |
32
|
6 |
|
'map' => array(), |
33
|
6 |
|
'whitelist' => array(), |
34
|
6 |
|
'blacklist' => array(), |
35
|
|
|
'joinTableFieldSuffix' => true |
36
|
6 |
|
), $configuration); |
37
|
|
|
|
38
|
12 |
|
if (count($configuration['whitelist']) > 0 && count($configuration['blacklist']) > 0) { |
|
|
|
|
39
|
2 |
|
throw new \RuntimeException('You can use whitelist or blacklist or none of mentioned lists, but not booth.'); |
40
|
|
|
} |
41
|
|
|
|
42
|
10 |
|
parent::__construct($configuration['case']); |
43
|
10 |
|
$this->buildMap($kernel, $configuration); |
44
|
|
|
|
45
|
10 |
|
$this->joinTableFieldSuffix = $configuration['joinTableFieldSuffix']; |
46
|
10 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
10 |
|
public function classToTableName($className) |
52
|
|
|
{ |
53
|
10 |
|
return (($prefix = $this->getTableNamePrefix($className)) ? $prefix . '_' : '') . parent::classToTableName($className); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
2 |
View Code Duplication |
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
return |
62
|
2 |
|
parent::joinTableName($sourceEntity, $targetEntity, $propertyName) |
63
|
|
|
. |
64
|
2 |
|
(($this->joinTableFieldSuffix && $propertyName) ? '_' . $this->propertyToColumnName($propertyName, $sourceEntity) : ''); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Build bundle naming map. |
69
|
|
|
* |
70
|
|
|
* @param KernelInterface $kernel |
71
|
|
|
* @param array $configuration |
72
|
|
|
*/ |
73
|
10 |
|
private function buildMap(KernelInterface $kernel, array $configuration) |
74
|
|
|
{ |
75
|
10 |
|
$this->map = array(); |
76
|
|
|
/** |
77
|
|
|
* @var BundleInterface $bundle; |
78
|
|
|
*/ |
79
|
10 |
|
foreach ($kernel->getBundles() as $bundle) { |
80
|
|
|
|
81
|
10 |
|
if (count($configuration['blacklist']) > 0 && in_array($bundle->getName(), $configuration['blacklist'])) { |
82
|
2 |
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
if (count($configuration['whitelist']) > 0 && !in_array($bundle->getName(), $configuration['whitelist'])) { |
86
|
2 |
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
$bundleClass = new \ReflectionClass(get_class($bundle)); |
90
|
|
|
|
91
|
6 |
|
if (isset($configuration['map'][$bundle->getName()])) { |
92
|
6 |
|
$this->map[$this->propertyToColumnName($configuration['map'][$bundle->getName()])] = $bundleClass->getNamespaceName(); |
93
|
3 |
|
} elseif (isset($configuration['map'][str_replace('Bundle', '', $bundle->getName())])) { |
94
|
|
|
$this->map[$this->propertyToColumnName($configuration['map'][str_replace('Bundle', '', $bundle->getName())])] = $bundleClass->getNamespaceName(); |
95
|
|
|
} else { |
96
|
3 |
|
$this->map[str_replace('_bundle', '', $this->propertyToColumnName($bundle->getName()))] = $bundleClass->getNamespaceName(); |
97
|
|
|
} |
98
|
5 |
|
} |
99
|
10 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Find prefix for table from map. |
103
|
|
|
* |
104
|
|
|
* @param string $className |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
10 |
|
private function getTableNamePrefix($className) |
108
|
|
|
{ |
109
|
10 |
|
foreach ($this->map as $prefix => $namespace) { |
110
|
|
|
|
111
|
6 |
|
if (strpos($className, $namespace) === 0) { |
112
|
6 |
|
return $prefix; |
113
|
|
|
} |
114
|
2 |
|
} |
115
|
|
|
|
116
|
4 |
|
return ''; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.