This repository has been archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixture.php
129 lines (108 loc) · 2.92 KB
/
Fixture.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
<?php
namespace Gos\Component\Fixture;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Gos\Component\Parser\Parser;
use Symfony\Component\Finder\Finder;
class Fixture
{
/**
* @var string
*/
protected $fileName;
/**
* @var string
*/
protected $fixturesKey;
/**
* @var \Doctrine\Common\DataFixtures\AbstractFixture
*/
protected $fixture;
/**
* @var string[]
*/
protected $directories;
/**
* @param string[] $directories
*/
public function __construct($directories)
{
$this->directories = (array) $directories;
}
/**
* @param string $fileName
* @param AbstractFixture|null $fixture
* @param string $fixturesKey
*/
public function load($fileName, AbstractFixture $fixture = null, $fixturesKey = 'database')
{
$this->fileName = $fileName;
$this->fixturesKey = $fixturesKey;
$this->fixture = $fixture;
}
/**
* @param string $directory
*/
public function addDirectory($directory)
{
$this->directories[] = $directory;
}
/**
* @return array
*/
public function fetch()
{
$buffer = [];
$finder = new Finder();
$files = $finder->files()
->in($this->directories)
->name($this->fileName)
;
foreach ($files as $file) {
$dataFixtures = Parser::yaml($file->getPathName());
foreach ($dataFixtures[$this->fixturesKey] as $field => $values) {
$i = 0;
foreach ($values as $value) {
$this->parseReference($value);
$this->handleCollection($dataFixtures, $field, $value);
$buffer[$i][$field] = $value;
$i++;
}
}
}
return $buffer;
}
/**
* @param $dataFixtures
* @param $field
* @param $value
*/
protected function handleCollection($dataFixtures, $field, &$value)
{
if (isset($dataFixtures['collection'])) {
if (in_array($field, $dataFixtures['collection']['scope'])) {
$valuesCollection = new ArrayCollection();
$valuesCollection->add($value);
$value = $valuesCollection;
}
}
}
/**
* @param $value
*/
protected function parseReference(&$value)
{
if (is_array($value)) {
return;
}
if (is_string($value)) {
$split = str_split($value);
if ($split[0] === '&') {
if (null === $this->fixture) {
throw new \Exception('Fixture reference is triggered but no AbstractFixture loaded');
}
$value = $this->fixture->getReference(substr($value,1));
}
}
}
}