1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the "RocketORM" package. |
5
|
|
|
* |
6
|
|
|
* https://github.com/RocketORM/ORM |
7
|
|
|
* |
8
|
|
|
* For the full license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Rocket\ORM\Generator\Schema; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Sylvain Lorinet <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Schema |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $connection; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $namespace; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $escapedNamespace; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public $relativeDirectory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $absoluteDirectory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
public $database; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array|Table[] |
51
|
|
|
*/ |
52
|
|
|
protected $tables = []; |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $schema |
57
|
|
|
* @param array $classes |
58
|
|
|
*/ |
59
|
7 |
|
public function __construct(array $schema, array $classes) |
60
|
|
|
{ |
61
|
7 |
|
$this->connection = $schema['connection']; |
62
|
7 |
|
$this->namespace = $schema['namespace']; |
63
|
7 |
|
$this->relativeDirectory = $schema['directory']; |
64
|
7 |
|
$this->database = $schema['database']; |
65
|
|
|
|
66
|
7 |
|
$tableClass = $classes['table']; |
67
|
7 |
|
unset($classes['table']); |
68
|
|
|
|
69
|
7 |
|
foreach ($schema['tables'] as $tableName => $data) { |
70
|
|
|
/** @var Table $table */ |
71
|
7 |
|
$table = new $tableClass($tableName, $data, $classes); |
72
|
7 |
|
$table->setSchema($this); |
73
|
|
|
|
74
|
7 |
|
$this->tables[] = $table; |
75
|
7 |
|
} |
76
|
7 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return Table[] |
80
|
|
|
*/ |
81
|
1 |
|
public function getTables() |
82
|
|
|
{ |
83
|
1 |
|
return $this->tables; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $name |
88
|
|
|
* |
89
|
|
|
* @return array|Table[] |
90
|
|
|
*/ |
91
|
6 |
|
public function findTables($name) |
92
|
|
|
{ |
93
|
6 |
|
if (false !== strpos($name, '\\')) { |
94
|
2 |
|
$tables = $this->getTablesByNamespace($name); |
95
|
2 |
|
} else { |
96
|
4 |
|
if (false === strpos($name, '.')) { |
97
|
2 |
|
$tables = $this->getTablesByTableName($name); |
98
|
2 |
|
} else { |
99
|
2 |
|
$tables = $this->getTablesByDatabaseAndTableName($name); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
6 |
|
return $tables; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $namespace A table namespace like "Example\Model\MyModel" |
108
|
|
|
* |
109
|
|
|
* @return array|Table[] |
110
|
|
|
*/ |
111
|
2 |
|
protected function getTablesByNamespace($namespace) |
112
|
|
|
{ |
113
|
2 |
|
$namespace = str_replace('\\\\', '\\', $namespace); |
114
|
2 |
|
$tables = []; |
115
|
|
|
|
116
|
2 |
|
foreach ($this->tables as $table) { |
117
|
2 |
|
if ($namespace === sprintf('%s\\%s', $this->namespace, $table->phpName)) { |
118
|
2 |
|
$tables[] = $table; |
119
|
2 |
|
} |
120
|
2 |
|
} |
121
|
|
|
|
122
|
2 |
|
return $tables; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $name A table name like "my_table" |
127
|
|
|
* |
128
|
|
|
* @return array|Table[] |
129
|
|
|
*/ |
130
|
2 |
|
protected function getTablesByTableName($name) |
131
|
|
|
{ |
132
|
2 |
|
$tables = []; |
133
|
2 |
|
foreach ($this->tables as $table) { |
134
|
2 |
|
if ($name === $table->name) { |
135
|
2 |
|
$tables[] = $table; |
136
|
2 |
|
} |
137
|
2 |
|
} |
138
|
|
|
|
139
|
2 |
|
return $tables; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $prefixedName A table name like "database.my_table" |
144
|
|
|
* |
145
|
|
|
* @return array|Table[] |
146
|
|
|
*/ |
147
|
2 |
|
protected function getTablesByDatabaseAndTableName($prefixedName) |
148
|
|
|
{ |
149
|
2 |
|
$tables = []; |
150
|
2 |
|
foreach ($this->tables as $table) { |
151
|
2 |
|
if ($prefixedName === sprintf('%s.%s', $this->database, $table->name)) { |
152
|
2 |
|
$tables[] = $table; |
153
|
2 |
|
} |
154
|
2 |
|
} |
155
|
|
|
|
156
|
2 |
|
return $tables; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|