YamlFixture.php
6.94 KB
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
<?php
require_once 'thirdparty/spyc/spyc.php';
/**
* Uses the Spyc library to parse a YAML document (see http://yaml.org).
* YAML is a simple markup languages that uses tabs and colons instead of the more verbose XML tags,
* and because of this much better for developers creating files by hand.
*
* The contents of the YAML file are broken into three levels:
* - Top level: class names - Page and ErrorPage. This is the name of the dataobject class that should be created.
* The fact that ErrorPage is actually a subclass is irrelevant to the system populating the database.
* Each identifier you specify delimits a new database record.
* This means that every record needs to have an identifier, whether you use it or not.
* - Third level: fields - each field for the record is listed as a 3rd level entry.
* In most cases, the field's raw content is provided.
* However, if you want to define a relationship, you can do so using "=>"
*
* There are a couple of lines like this:
* <code>
* Parent: =>Page.about
* </code>
* This will tell the system to set the ParentID database field to the ID of the Page object with the identifier
* 'about'. This can be used on any has-one or many-many relationship.
* Note that we use the name of the relationship (Parent), and not the name of the database field (ParentID)
*
* On many-many relationships, you should specify a comma separated list of values.
* <code>
* MyRelation: =>Class.inst1,=>Class.inst2,=>Class.inst3
* </code>
*
* An crucial thing to note is that the YAML file specifies DataObjects, not database records.
* The database is populated by instantiating DataObject objects, setting the fields listed, and calling write().
* This means that any onBeforeWrite() or default value logic will be executed as part of the test.
* This forms the basis of our testURLGeneration() test above.
*
* For example, the URLSegment value of Page.staffduplicate is the same as the URLSegment value of Page.staff.
* When the fixture is set up, the URLSegment value of Page.staffduplicate will actually be my-staff-2.
*
* Finally, be aware that requireDefaultRecords() is not called by the database populator -
* so you will need to specify standard pages such as 404 and home in your YAML file.
*
* <code>
* Page:
* home:
* Title: Home
* about:
* Title: About Us
* staff:
* Title: Staff
* URLSegment: my-staff
* Parent: =>Page.about
* staffduplicate:
* Title: Staff
* URLSegment: my-staff
* Parent: =>Page.about
* products:
* Title: Products
* ErrorPage:
* 404:
* Title: Page not Found
* ErrorCode: 404
* </code>
*
* @package framework
* @subpackage core
*
* @see http://code.google.com/p/spyc/
*/
class YamlFixture extends Object {
/**
* Absolute path to the .yml fixture file
*
* @var string
*/
protected $fixtureFile;
/**
* String containing fixture
*
* @var String
*/
protected $fixtureString;
/**
* @var FixtureFactory
* @deprecated 3.1 Use writeInto() and FixtureFactory instead
*/
protected $factory;
/**
* @param String Absolute file path, or relative path to {@link Director::baseFolder()}
*/
public function __construct($fixture) {
if(false !== strpos($fixture, "\n")) {
$this->fixtureString = $fixture;
} else {
if(!Director::is_absolute($fixture)) $fixture = Director::baseFolder().'/'. $fixture;
if(!file_exists($fixture)) {
throw new InvalidArgumentException('YamlFixture::__construct(): Fixture path "' . $fixture
. '" not found');
}
$this->fixtureFile = $fixture;
}
parent::__construct();
}
/**
* @return String Absolute file path
*/
public function getFixtureFile() {
return $this->fixtureFile;
}
/**
* @return String Fixture string
*/
public function getFixtureString() {
return $this->fixtureString;
}
/**
* Get the ID of an object from the fixture.
*
* @deprecated 3.1 Use writeInto() and FixtureFactory accessors instead
*
* @param $className The data class, as specified in your fixture file. Parent classes won't work
* @param $identifier The identifier string, as provided in your fixture file
*/
public function idFromFixture($className, $identifier) {
Deprecation::notice('3.1', 'Use writeInto() and FixtureFactory accessors instead');
if(!$this->factory) $this->factory = Injector::inst()->create('FixtureFactory');
return $this->factory->getId($className, $identifier);
}
/**
* Return all of the IDs in the fixture of a particular class name.
*
* @deprecated 3.1 Use writeInto() and FixtureFactory accessors instead
*
* @return A map of fixture-identifier => object-id
*/
public function allFixtureIDs($className) {
Deprecation::notice('3.1', 'Use writeInto() and FixtureFactory accessors instead');
if(!$this->factory) $this->factory = Injector::inst()->create('FixtureFactory');
return $this->factory->getIds($className);
}
/**
* Get an object from the fixture.
*
* @deprecated 3.1 Use writeInto() and FixtureFactory accessors instead
*
* @param $className The data class, as specified in your fixture file. Parent classes won't work
* @param $identifier The identifier string, as provided in your fixture file
*/
public function objFromFixture($className, $identifier) {
Deprecation::notice('3.1', 'Use writeInto() and FixtureFactory accessors instead');
if(!$this->factory) $this->factory = Injector::inst()->create('FixtureFactory');
return $this->factory->get($className, $identifier);
}
/**
* Load a YAML fixture file into the database.
* Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture.
*
* Caution: In order to support reflexive relations which need a valid object ID,
* the record is written twice: first after populating all non-relational fields,
* then again after populating all relations (has_one, has_many, many_many).
*
* @deprecated 3.1 Use writeInto() and FixtureFactory instance instead
*/
public function saveIntoDatabase(DataModel $model) {
Deprecation::notice('3.1', 'Use writeInto() and FixtureFactory instance instead');
if(!$this->factory) $this->factory = Injector::inst()->create('FixtureFactory');
$this->writeInto($this->factory);
}
/**
* Persists the YAML data in a FixtureFactory,
* which in turn saves them into the database.
* Please use the passed in factory to access the fixtures afterwards.
*
* @param FixtureFactory $factory
*/
public function writeInto(FixtureFactory $factory) {
$parser = new Spyc();
if (isset($this->fixtureString)) {
$fixtureContent = $parser->load($this->fixtureString);
} else {
$fixtureContent = $parser->loadFile($this->fixtureFile);
}
foreach($fixtureContent as $class => $items) {
foreach($items as $identifier => $data) {
if(ClassInfo::exists($class)) {
$factory->createObject($class, $identifier, $data);
} else {
$factory->createRaw($class, $identifier, $data);
}
}
}
}
}