|
1
|
|
|
<?php |
|
2
|
|
|
namespace TildBJ\Seeder\Collection; |
|
3
|
|
|
|
|
4
|
|
|
/*************************************************************** |
|
5
|
|
|
* Copyright notice |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2016 Dennis Römmich <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* All rights reserved |
|
10
|
|
|
* |
|
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
12
|
|
|
* free software; you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU General Public License as published by |
|
14
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
15
|
|
|
* (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* The GNU General Public License can be found at |
|
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
19
|
|
|
* |
|
20
|
|
|
* This script is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
26
|
|
|
***************************************************************/ |
|
27
|
|
|
|
|
28
|
|
|
use TildBJ\Seeder\Seed; |
|
29
|
|
|
use TildBJ\Seeder\Seeder; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* SeedCollection |
|
33
|
|
|
* |
|
34
|
|
|
* @author Dennis Römmich<[email protected]> |
|
35
|
|
|
* @copyright Copyright belongs to the respective authors |
|
36
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later |
|
37
|
|
|
*/ |
|
38
|
|
|
class SeedCollection implements \TildBJ\Seeder\SeedCollection |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* seeds |
|
42
|
|
|
* |
|
43
|
|
|
* @var array $seeds |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $seeds = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array $temp |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $temp = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var int |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $i = 0; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function toArray() |
|
61
|
|
|
{ |
|
62
|
|
|
$return = []; |
|
63
|
|
|
|
|
64
|
|
|
/** @var Seed $seed */ |
|
65
|
|
|
foreach ($this->seeds as $seeds) { |
|
66
|
|
|
foreach ($seeds as $key => $seed) { |
|
67
|
|
|
$return[$seed->getTarget()][$key] = $seed->getProperties(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param Seeder $seeder |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public function get(Seeder $seeder) |
|
79
|
|
|
{ |
|
80
|
|
|
if (isset($this->temp[$seeder->getClass()])) { |
|
81
|
|
|
$return = $this->temp[$seeder->getClass()]; |
|
82
|
|
|
unset($this->temp[$seeder->getClass()]); |
|
83
|
|
|
return $return; |
|
84
|
|
|
} |
|
85
|
|
|
$return = []; |
|
86
|
|
|
foreach ($this->seeds as $title => $seeds) { |
|
87
|
|
|
foreach ($seeds as $key => $seed) { |
|
88
|
|
|
if ($title === $seeder->getClass()) { |
|
89
|
|
|
$return[$key] = $seed; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $return; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* each |
|
99
|
|
|
* |
|
100
|
|
|
* @param callable $function |
|
101
|
|
|
* @return $this |
|
102
|
|
|
*/ |
|
103
|
|
|
public function each(callable $function) |
|
104
|
|
|
{ |
|
105
|
|
|
$reflection = new \ReflectionFunction($function); |
|
106
|
|
|
$className = get_class($reflection->getClosureThis()); |
|
107
|
|
|
foreach ($this->seeds[$className] as $key => $seed) { |
|
108
|
|
|
if ($seed->isExecuted()) { |
|
109
|
|
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
$seed->isExecuted(true); |
|
112
|
|
|
$function($seed, \TildBJ\Seeder\Factory\FakerFactory::createFaker()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param Seed $seed |
|
120
|
|
|
* @throws \Exception |
|
121
|
|
|
*/ |
|
122
|
|
|
public function attach(Seed $seed) |
|
123
|
|
|
{ |
|
124
|
|
|
$identifier = 'NEW' . ++$this->i; |
|
125
|
|
|
$this->seeds[$seed->getTitle()][$identifier] = $seed; |
|
126
|
|
|
$this->temp[$seed->getTitle()][$identifier] = $identifier; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns a random seed. Returns null if there is no seed or there are less seeds than you want to create |
|
131
|
|
|
* In this case you may $this->attach a new Seed. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $className |
|
134
|
|
|
* @return Seed |
|
135
|
|
|
*/ |
|
136
|
|
|
public function random($className, $i) |
|
137
|
|
|
{ |
|
138
|
|
|
if (!isset($this->seeds[$className]) || count($this->seeds[$className]) < $i) { |
|
139
|
|
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
$random = array_rand($this->seeds[$className]); |
|
142
|
|
|
return $this->seeds[$className][$random]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* detach |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $key |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
|
|
public function detach($key) |
|
152
|
|
|
{ |
|
153
|
|
|
if (isset($this->seeds[$key])) { |
|
154
|
|
|
unset($this->seeds[$key]); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Checks if current position is valid |
|
160
|
|
|
* |
|
161
|
|
|
* @return bool |
|
162
|
|
|
*/ |
|
163
|
|
|
public function valid() |
|
164
|
|
|
{ |
|
165
|
|
|
return current($this->seeds) !== false; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Return the current element |
|
170
|
|
|
* |
|
171
|
|
|
* @return Seed |
|
172
|
|
|
*/ |
|
173
|
|
|
public function current() |
|
174
|
|
|
{ |
|
175
|
|
|
return current($this->seeds); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Move forward to next element |
|
180
|
|
|
* |
|
181
|
|
|
* @return Seed |
|
182
|
|
|
*/ |
|
183
|
|
|
public function next() |
|
184
|
|
|
{ |
|
185
|
|
|
return next($this->seeds); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Return the key of the current element |
|
190
|
|
|
* |
|
191
|
|
|
* @return Seed |
|
192
|
|
|
*/ |
|
193
|
|
|
public function key() |
|
194
|
|
|
{ |
|
195
|
|
|
return key($this->seeds); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Rewind the Iterator to the first element |
|
200
|
|
|
* |
|
201
|
|
|
* @return Seed |
|
202
|
|
|
*/ |
|
203
|
|
|
public function rewind() |
|
204
|
|
|
{ |
|
205
|
|
|
return reset($this->seeds); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Count elements of an object |
|
210
|
|
|
* |
|
211
|
|
|
* @return int |
|
212
|
|
|
*/ |
|
213
|
|
|
public function count() |
|
214
|
|
|
{ |
|
215
|
|
|
return count($this->seeds); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param string $name |
|
220
|
|
|
* @return int |
|
221
|
|
|
*/ |
|
222
|
|
|
public function countByName($name) |
|
223
|
|
|
{ |
|
224
|
|
|
return count($this->seeds[$name]); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @return void |
|
229
|
|
|
*/ |
|
230
|
|
|
public function clear() |
|
231
|
|
|
{ |
|
232
|
|
|
$this->seeds = []; |
|
233
|
|
|
$this->i = 0; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|