2008-08-11 01:17:51 +02:00
|
|
|
<?php
|
2008-08-17 03:08:10 +02:00
|
|
|
|
2015-09-08 17:07:56 +02:00
|
|
|
use Symfony\Component\Yaml\Parser;
|
2008-08-17 03:08:10 +02:00
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
/**
|
2015-09-08 17:07:56 +02:00
|
|
|
* Uses Symfony's YAML component to parse a YAML document (see http://yaml.org).
|
2016-03-08 21:50:18 +01:00
|
|
|
* YAML is a simple markup languages that uses tabs and colons instead of the more verbose XML tags,
|
2008-08-11 01:17:51 +02:00
|
|
|
* and because of this much better for developers creating files by hand.
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
2008-08-11 01:17:51 +02:00
|
|
|
* The contents of the YAML file are broken into three levels:
|
2016-03-08 21:50:18 +01:00
|
|
|
* - 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.
|
2008-08-11 01:17:51 +02:00
|
|
|
* This means that every record needs to have an identifier, whether you use it or not.
|
2016-03-08 21:50:18 +01:00
|
|
|
* - 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.
|
2008-08-11 01:17:51 +02:00
|
|
|
* However, if you want to define a relationship, you can do so using "=>"
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
2008-08-11 01:17:51 +02:00
|
|
|
* There are a couple of lines like this:
|
2010-04-23 02:11:41 +02:00
|
|
|
* <code>
|
|
|
|
* Parent: =>Page.about
|
|
|
|
* </code>
|
2012-09-26 23:34:00 +02:00
|
|
|
* This will tell the system to set the ParentID database field to the ID of the Page object with the identifier
|
2016-03-08 21:50:18 +01:00
|
|
|
* 'about'. This can be used on any has-one or many-many relationship.
|
2008-08-11 01:17:51 +02:00
|
|
|
* 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.
|
2010-04-23 02:11:41 +02:00
|
|
|
* <code>
|
|
|
|
* MyRelation: =>Class.inst1,=>Class.inst2,=>Class.inst3
|
|
|
|
* </code>
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-08-11 01:17:51 +02:00
|
|
|
* This forms the basis of our testURLGeneration() test above.
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
|
|
|
* For example, the URLSegment value of Page.staffduplicate is the same as the URLSegment value of Page.staff.
|
2008-08-11 01:17:51 +02:00
|
|
|
* When the fixture is set up, the URLSegment value of Page.staffduplicate will actually be my-staff-2.
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
|
|
|
* Finally, be aware that requireDefaultRecords() is not called by the database populator -
|
2008-08-11 01:17:51 +02:00
|
|
|
* so you will need to specify standard pages such as 404 and home in your YAML file.
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
2008-08-11 01:17:51 +02:00
|
|
|
* <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>
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-11 01:17:51 +02:00
|
|
|
* @subpackage core
|
|
|
|
*/
|
|
|
|
class YamlFixture extends Object {
|
2012-12-07 18:44:00 +01:00
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
/**
|
2011-03-30 08:47:30 +02:00
|
|
|
* Absolute path to the .yml fixture file
|
2008-08-11 01:17:51 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $fixtureFile;
|
|
|
|
|
2012-06-29 00:33:00 +02:00
|
|
|
/**
|
|
|
|
* String containing fixture
|
|
|
|
*
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $fixtureString;
|
|
|
|
|
2011-03-30 08:47:30 +02:00
|
|
|
/**
|
|
|
|
* @param String Absolute file path, or relative path to {@link Director::baseFolder()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($fixture) {
|
2012-06-29 00:33:00 +02:00
|
|
|
if(false !== strpos($fixture, "\n")) {
|
|
|
|
$this->fixtureString = $fixture;
|
|
|
|
} else {
|
|
|
|
if(!Director::is_absolute($fixture)) $fixture = Director::baseFolder().'/'. $fixture;
|
|
|
|
|
|
|
|
if(!file_exists($fixture)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
throw new InvalidArgumentException('YamlFixture::__construct(): Fixture path "' . $fixture
|
|
|
|
. '" not found');
|
2012-06-29 00:33:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->fixtureFile = $fixture;
|
2008-08-11 01:29:30 +02:00
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2009-09-18 05:02:19 +02:00
|
|
|
parent::__construct();
|
2008-08-11 01:17:51 +02:00
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2011-03-30 08:47:30 +02:00
|
|
|
/**
|
|
|
|
* @return String Absolute file path
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getFixtureFile() {
|
2011-03-30 08:47:30 +02:00
|
|
|
return $this->fixtureFile;
|
|
|
|
}
|
2012-06-29 00:33:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String Fixture string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getFixtureString() {
|
2012-06-29 00:33:00 +02:00
|
|
|
return $this->fixtureString;
|
|
|
|
}
|
2012-12-07 18:44:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
2012-12-07 18:44:00 +01:00
|
|
|
* @param FixtureFactory $factory
|
|
|
|
*/
|
|
|
|
public function writeInto(FixtureFactory $factory) {
|
2015-09-08 17:07:56 +02:00
|
|
|
$parser = new Parser();
|
2012-06-29 00:33:00 +02:00
|
|
|
if (isset($this->fixtureString)) {
|
2015-09-08 17:07:56 +02:00
|
|
|
$fixtureContent = $parser->parse($this->fixtureString);
|
2012-06-29 00:33:00 +02:00
|
|
|
} else {
|
2015-09-08 17:07:56 +02:00
|
|
|
if (!file_exists($this->fixtureFile)) return;
|
|
|
|
|
|
|
|
$contents = file_get_contents($this->fixtureFile);
|
|
|
|
$fixtureContent = $parser->parse($contents);
|
|
|
|
|
|
|
|
if (!$fixtureContent) return;
|
2012-06-29 00:33:00 +02:00
|
|
|
}
|
2009-11-21 03:33:39 +01:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
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);
|
2008-08-11 01:17:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|