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
|
|
|
|
14
|
|
|
class UnderscoredClassNamespacePrefix extends UnderscoreNamingStrategy |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $map; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $whitelist; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $blacklist; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
protected $joinTableFieldSuffix; |
35
|
|
|
|
36
|
16 |
|
public function __construct(array $configuration = array()) |
37
|
|
|
{ |
38
|
16 |
|
$configuration = array_merge(array( |
39
|
16 |
|
'case' => CASE_LOWER, |
40
|
8 |
|
'map' => array(), |
41
|
8 |
|
'whitelist' => array(), |
42
|
8 |
|
'blacklist' => array(), |
43
|
|
|
'joinTableFieldSuffix' => true |
44
|
8 |
|
), $configuration); |
45
|
|
|
|
46
|
16 |
|
if (count($configuration['whitelist']) > 0 && count($configuration['blacklist']) > 0) { |
|
|
|
|
47
|
2 |
|
throw new \RuntimeException('You can use whitelist or blacklist or none of mentioned lists, but not booth.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
14 |
|
$this->map = $configuration['map']; |
51
|
14 |
|
$this->blacklist = $configuration['blacklist']; |
52
|
14 |
|
$this->whitelist = $configuration['whitelist']; |
53
|
14 |
|
$this->joinTableFieldSuffix = $configuration['joinTableFieldSuffix']; |
54
|
|
|
|
55
|
14 |
|
parent::__construct($configuration['case']); |
56
|
|
|
|
57
|
14 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
14 |
|
public function classToTableName($className) |
63
|
|
|
{ |
64
|
14 |
|
return (($prefix = $this->getTableNamePrefix($className)) ? $prefix . '_' : '') . parent::classToTableName($className); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
2 |
View Code Duplication |
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
return |
73
|
2 |
|
parent::joinTableName($sourceEntity, $targetEntity, $propertyName) |
74
|
|
|
. |
75
|
2 |
|
(($this->joinTableFieldSuffix && $propertyName) ? '_' . $this->propertyToColumnName($propertyName, $sourceEntity) : ''); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Find prefix for table from map. |
80
|
|
|
* |
81
|
|
|
* @param string $className |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
14 |
|
private function getTableNamePrefix($className) |
85
|
|
|
{ |
86
|
14 |
|
foreach ($this->blacklist as $blacklistedFqcn) { |
87
|
|
|
|
88
|
2 |
|
if (strpos($className, $blacklistedFqcn) === 0) { |
89
|
2 |
|
return ''; |
90
|
|
|
} |
91
|
6 |
|
} |
92
|
|
|
|
93
|
12 |
|
foreach ($this->map as $fqcnPrefix => $tablePrefix) { |
94
|
|
|
|
95
|
12 |
|
if (strpos($className, $fqcnPrefix) === 0) { |
96
|
|
|
|
97
|
10 |
|
if (count($this->whitelist) > 0) { |
98
|
|
|
|
99
|
|
|
foreach ($this->whitelist as $whitelistedFqcn) { |
100
|
|
|
|
101
|
|
|
if (strpos($className, $whitelistedFqcn) === 0) { |
102
|
|
|
return $this->propertyToColumnName($tablePrefix); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return ''; |
107
|
|
|
|
108
|
|
|
} else { |
109
|
|
|
|
110
|
11 |
|
return $this->propertyToColumnName($tablePrefix); |
111
|
|
|
} |
112
|
|
|
} |
113
|
3 |
|
} |
114
|
|
|
|
115
|
4 |
|
return ''; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.