-
-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathFixtures.php
274 lines (231 loc) · 8.44 KB
/
Fixtures.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/*
* This file is part of the Alice package.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\Alice;
use Doctrine\Common\Persistence\ObjectManager;
use Nelmio\Alice\Persister\Doctrine;
use Psr\Log\LoggerInterface;
use Nelmio\Alice\Fixtures\Loader;
class Fixtures
{
/**
* @var Loader[]
*/
private static $loaders = [];
/**
* @var PersisterInterface
*/
protected $persister;
/**
* @var array
*/
protected $defaultOptions = [
'locale' => 'en_US',
'providers' => [],
'seed' => 1,
'logger' => null,
'persist_once' => false,
];
/**
* @var ProcessorInterface[]
*/
protected $processors;
/**
* @param PersisterInterface $persister
* @param array $defaultOptions
* @param array $processors
*
* @throws \InvalidArgumentException
*/
public function __construct(PersisterInterface $persister, array $defaultOptions = [], array $processors = [])
{
$this->persister = $persister;
$this->validateOptions($defaultOptions);
$this->defaultOptions = array_merge($this->defaultOptions, $defaultOptions);
foreach ($processors as $processor) {
if (false === $processor instanceof ProcessorInterface) {
throw new \InvalidArgumentException(
'Expected processor to implement Nelmio\Alice\Fixtures\ProcessorInterface.'
);
}
}
$this->processors = $processors;
}
/**
* Loads a fixture file into an object persister.
*
* @param string|array $files filename, glob mask (e.g. *.yml) or array of filenames to load data from, or data array
* @param object $persister object persister
* @param array $options available options:
* - providers: an array of additional faker providers
* - locale: the faker locale
* - seed: a seed to make sure faker generates data consistently across
* runs, set to null to disable
* - logger: a callable or Psr\Log\LoggerInterface object that will receive progress information
* - persist_once: only persist objects once if multiple files are passed
* @param array $processors optional array of ProcessorInterface instances
*/
public static function load($files, $persister, array $options = [], array $processors = [])
{
$_persister = null;
switch (true) {
case $persister instanceof PersisterInterface:
$_persister = $persister;
break;
case $persister instanceof ObjectManager:
$_persister = new Doctrine($persister);
break;
default:
throw new \InvalidArgumentException('Unknown persister type '.get_class($persister));
}
$fixtures = new static($_persister, $options, $processors);
return $fixtures->loadFiles($files);
}
/**
* @param $files
* @param array $options
*
* @return array
*/
public function loadFiles($files, array $options = [])
{
$this->validateOptions($options);
$options = array_merge($this->defaultOptions, $options);
// glob strings to filenames
if (!is_array($files)) {
$matches = glob($files, (defined('GLOB_BRACE') ? GLOB_BRACE : 0));
if (!$matches && !file_exists($files)) {
throw new \InvalidArgumentException('The file could not be found: '.$files);
}
$files = $matches;
}
// wrap the data array in an array of one data array
if (!is_string(current($files))) {
$files = [$files];
}
$objects = [];
foreach ($files as $file) {
$loader = self::getLoader($options);
if (isset($options['logger'])) {
$loader->setLogger($options['logger']);
}
$loader->setPersister($this->persister);
$set = $loader->load($file);
if (!$options['persist_once']) {
$this->persist($this->persister, $set);
}
$objects = array_merge($objects, $set);
}
if ($options['persist_once']) {
$this->persist($this->persister, $objects);
}
return $objects;
}
public function addProcessor(ProcessorInterface $processor)
{
$this->processors[] = $processor;
}
protected function persist(PersisterInterface $persister, $objects)
{
foreach ($this->processors as $proc) {
foreach ($objects as $obj) {
$proc->preProcess($obj);
}
}
$persister->persist($objects);
foreach ($this->processors as $proc) {
foreach ($objects as $obj) {
$proc->postProcess($obj);
}
}
}
private static function generateLoaderKey(array $options)
{
$providers = '';
if (!empty($options['providers'])) {
foreach ($options['providers'] as $item) {
if (is_object($item)) {
$item = get_class($item);
}
// turn all of the class names into fully-qualified ones
$item = '\\' . ltrim($item, '\\');
$providers .= $item;
}
}
return sprintf(
'%s_%s_%s',
(is_numeric($options['seed'])
? strval($options['seed'])
: gettype($options['seed'])
),
$options['locale'],
(!strlen($providers)
? ''
: md5($providers)
)
);
}
private static function getLoader(array $options)
{
// Generate an array key based on the options, so that separate loaders
// will be created when we want to load several fixtures that use different
// custom providers.
$loaderKey = self::generateLoaderKey($options);
if (!isset(self::$loaders[$loaderKey])) {
self::$loaders[$loaderKey] = new Loader($options['locale'], $options['providers'], $options['seed']);
}
return self::$loaders[$loaderKey];
}
/**
* Checks if the options are valid or not. If not throws an exception.
*
* @param array $options
*
* @throws \InvalidArgumentException
*/
private function validateOptions(array $options)
{
foreach (array_keys($options) as $key) {
if (false === array_key_exists($key, $this->defaultOptions)) {
throw new \InvalidArgumentException(sprintf(
'Unknown key "%s", expected: %s',
$key,
implode(', ', array_keys($this->defaultOptions))
));
}
}
if (isset($options['providers'])) {
$providers = $options['providers'];
if (false === is_array($providers)) {
throw new \InvalidArgumentException('Expected "providers" option value to be an array');
}
foreach ($providers as $provider) {
if (false === is_object($provider) && false === is_string($provider)) {
throw new \InvalidArgumentException(sprintf(
'The provider should be a string or an object, got %s instead',
is_scalar($provider) ? $provider : gettype($provider)
));
}
}
}
if (isset($options['logger'])) {
$logger = $options['logger'];
if (false === is_callable($logger) && false === $logger instanceof LoggerInterface) {
throw new \InvalidArgumentException(
'Expected "logger" option value to be a callable or to implement Psr\Log\LoggerInterface'
);
}
}
if (isset($options['persist_once'])) {
if (false === is_bool($options['persist_once'])) {
throw new \InvalidArgumentException('Expected "persist_once" option value value to be a boolean.');
}
}
}
}