2012-12-07 18:44:00 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A blueprint on how to create instances of a certain {@link DataObject} subclass.
|
|
|
|
*
|
|
|
|
* Relies on a {@link FixtureFactory} to manage database relationships between instances,
|
|
|
|
* and manage the mappings between fixture identifiers and their database IDs.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-12-07 18:44:00 +01:00
|
|
|
* @package framework
|
|
|
|
* @subpackage core
|
|
|
|
*/
|
|
|
|
class FixtureBlueprint {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Map of field names to values. Supersedes {@link DataObject::$defaults}.
|
|
|
|
*/
|
|
|
|
protected $defaults = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String Arbitrary name by which this fixture type can be referenced.
|
|
|
|
*/
|
|
|
|
protected $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String Subclass of {@link DataObject}
|
|
|
|
*/
|
|
|
|
protected $class;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $callbacks = array(
|
|
|
|
'beforeCreate' => array(),
|
|
|
|
'afterCreate' => array(),
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/** @config */
|
|
|
|
private static $dependencies = array(
|
2012-12-07 18:44:00 +01:00
|
|
|
'factory' => '%$FixtureFactory'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $name
|
|
|
|
* @param String $class Defaults to $name
|
|
|
|
* @param array $defaults
|
|
|
|
*/
|
|
|
|
public function __construct($name, $class = null, $defaults = array()) {
|
|
|
|
if(!$class) $class = $name;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
if(!is_subclass_of($class, 'DataObject')) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'Class "%s" is not a valid subclass of DataObject',
|
|
|
|
$class
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->name = $name;
|
|
|
|
$this->class = $class;
|
|
|
|
$this->defaults = $defaults;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
/**
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param string $identifier Unique identifier for this fixture type
|
|
|
|
* @param array $data Map of property names to their values.
|
|
|
|
* @param array $fixtures Map of fixture names to an associative array of their in-memory
|
2012-12-07 18:44:00 +01:00
|
|
|
* identifiers mapped to their database IDs. Used to look up
|
|
|
|
* existing fixtures which might be referenced in the $data attribute
|
|
|
|
* via the => notation.
|
|
|
|
* @return DataObject
|
2016-05-25 07:09:29 +02:00
|
|
|
* @throws Exception
|
2012-12-07 18:44:00 +01:00
|
|
|
*/
|
|
|
|
public function createObject($identifier, $data = null, $fixtures = null) {
|
|
|
|
// We have to disable validation while we import the fixtures, as the order in
|
|
|
|
// which they are imported doesnt guarantee valid relations until after the import is complete.
|
2015-09-15 04:52:02 +02:00
|
|
|
// Also disable filesystem manipulations
|
|
|
|
Config::nest();
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('DataObject', 'validation_enabled', false);
|
2015-09-15 04:52:02 +02:00
|
|
|
Config::inst()->update('File', 'update_filesystem', false);
|
2012-12-07 18:44:00 +01:00
|
|
|
|
|
|
|
$this->invokeCallbacks('beforeCreate', array($identifier, &$data, &$fixtures));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$class = $this->class;
|
|
|
|
$obj = DataModel::inst()->$class->newObject();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
// If an ID is explicitly passed, then we'll sort out the initial write straight away
|
|
|
|
// This is just in case field setters triggered by the population code in the next block
|
|
|
|
// Call $this->write(). (For example, in FileTest)
|
|
|
|
if(isset($data['ID'])) {
|
|
|
|
$obj->ID = $data['ID'];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
// The database needs to allow inserting values into the foreign key column (ID in our case)
|
2013-06-21 00:32:08 +02:00
|
|
|
$conn = DB::get_conn();
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseTable = DataObject::getSchema()->baseDataTable($class);
|
2012-12-07 18:44:00 +01:00
|
|
|
if(method_exists($conn, 'allowPrimaryKeyEditing')) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$conn->allowPrimaryKeyEditing($baseTable, true);
|
2012-12-07 18:44:00 +01:00
|
|
|
}
|
|
|
|
$obj->write(false, true);
|
|
|
|
if(method_exists($conn, 'allowPrimaryKeyEditing')) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$conn->allowPrimaryKeyEditing($baseTable, false);
|
2012-12-07 18:44:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate defaults
|
|
|
|
if($this->defaults) foreach($this->defaults as $fieldName => $fieldVal) {
|
|
|
|
if(isset($data[$fieldName]) && $data[$fieldName] !== false) continue;
|
|
|
|
|
|
|
|
if(is_callable($fieldVal)) {
|
|
|
|
$obj->$fieldName = $fieldVal($obj, $data, $fixtures);
|
|
|
|
} else {
|
|
|
|
$obj->$fieldName = $fieldVal;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
// Populate overrides
|
|
|
|
if($data) foreach($data as $fieldName => $fieldVal) {
|
|
|
|
// Defer relationship processing
|
2015-02-25 16:21:33 +01:00
|
|
|
if(
|
|
|
|
$obj->manyManyComponent($fieldName)
|
|
|
|
|| $obj->hasManyComponent($fieldName)
|
|
|
|
|| $obj->hasOneComponent($fieldName)
|
|
|
|
) {
|
2012-12-07 18:44:00 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setValue($obj, $fieldName, $fieldVal, $fixtures);
|
|
|
|
}
|
2013-06-05 11:40:36 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
// Save to fixture before relationship processing in case of reflexive relationships
|
|
|
|
if(!isset($fixtures[$class])) {
|
|
|
|
$fixtures[$class] = array();
|
|
|
|
}
|
|
|
|
$fixtures[$class][$identifier] = $obj->ID;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
// Populate all relations
|
|
|
|
if($data) foreach($data as $fieldName => $fieldVal) {
|
2015-02-25 16:21:33 +01:00
|
|
|
if($obj->manyManyComponent($fieldName) || $obj->hasManyComponent($fieldName)) {
|
2014-02-24 09:41:48 +01:00
|
|
|
$obj->write();
|
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
$parsedItems = array();
|
2014-02-24 09:41:48 +01:00
|
|
|
|
|
|
|
if(is_array($fieldVal)) {
|
|
|
|
// handle lists of many_many relations. Each item can
|
2014-08-15 08:53:05 +02:00
|
|
|
// specify the many_many_extraFields against each
|
2014-02-24 09:41:48 +01:00
|
|
|
// related item.
|
|
|
|
foreach($fieldVal as $relVal) {
|
|
|
|
$item = key($relVal);
|
|
|
|
$id = $this->parseValue($item, $fixtures);
|
|
|
|
$parsedItems[] = $id;
|
|
|
|
|
|
|
|
array_shift($relVal);
|
|
|
|
|
|
|
|
$obj->getManyManyComponents($fieldName)->add(
|
|
|
|
$id, $relVal
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$items = preg_split('/ *, */',trim($fieldVal));
|
|
|
|
|
|
|
|
foreach($items as $item) {
|
|
|
|
// Check for correct format: =><relationname>.<identifier>.
|
|
|
|
// Ignore if the item has already been replaced with a numeric DB identifier
|
|
|
|
if(!is_numeric($item) && !preg_match('/^=>[^\.]+\.[^\.]+/', $item)) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'Invalid format for relation "%s" on class "%s" ("%s")',
|
|
|
|
$fieldName,
|
|
|
|
$class,
|
|
|
|
$item
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsedItems[] = $this->parseValue($item, $fixtures);
|
2012-12-07 18:44:00 +01:00
|
|
|
}
|
|
|
|
|
2015-02-25 16:21:33 +01:00
|
|
|
if($obj->hasManyComponent($fieldName)) {
|
2014-02-24 09:41:48 +01:00
|
|
|
$obj->getComponents($fieldName)->setByIDList($parsedItems);
|
2015-02-25 16:21:33 +01:00
|
|
|
} elseif($obj->manyManyComponent($fieldName)) {
|
2014-02-24 09:41:48 +01:00
|
|
|
$obj->getManyManyComponents($fieldName)->setByIDList($parsedItems);
|
|
|
|
}
|
2012-12-07 18:44:00 +01:00
|
|
|
}
|
2013-07-10 02:44:24 +02:00
|
|
|
} else {
|
|
|
|
$hasOneField = preg_replace('/ID$/', '', $fieldName);
|
2015-02-25 16:21:33 +01:00
|
|
|
if($className = $obj->hasOneComponent($hasOneField)) {
|
2013-07-10 02:44:24 +02:00
|
|
|
$obj->{$hasOneField.'ID'} = $this->parseValue($fieldVal, $fixtures, $fieldClass);
|
|
|
|
// Inject class for polymorphic relation
|
|
|
|
if($className === 'DataObject') {
|
|
|
|
$obj->{$hasOneField.'Class'} = $fieldClass;
|
|
|
|
}
|
|
|
|
}
|
2012-12-07 18:44:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
// If LastEdited was set in the fixture, set it here
|
|
|
|
if($data && array_key_exists('LastEdited', $data)) {
|
2015-05-20 18:29:52 +02:00
|
|
|
$this->overrideField($obj, 'LastEdited', $data['LastEdited'], $fixtures);
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-12-07 18:44:00 +01:00
|
|
|
} catch(Exception $e) {
|
2015-09-15 04:52:02 +02:00
|
|
|
Config::unnest();
|
2012-12-07 18:44:00 +01:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
Config::unnest();
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->invokeCallbacks('afterCreate', array($obj, $identifier, &$data, &$fixtures));
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param array $defaults
|
|
|
|
* @return $this
|
2012-12-07 18:44:00 +01:00
|
|
|
*/
|
|
|
|
public function setDefaults($defaults) {
|
|
|
|
$this->defaults = $defaults;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-25 07:09:29 +02:00
|
|
|
* @return array
|
2012-12-07 18:44:00 +01:00
|
|
|
*/
|
|
|
|
public function getDefaults() {
|
|
|
|
return $this->defaults;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-25 07:09:29 +02:00
|
|
|
* @return string
|
2012-12-07 18:44:00 +01:00
|
|
|
*/
|
|
|
|
public function getClass() {
|
|
|
|
return $this->class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See class documentation.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param string $type
|
2012-12-07 18:44:00 +01:00
|
|
|
* @param callable $callback
|
2016-05-25 07:09:29 +02:00
|
|
|
* @return $this
|
2012-12-07 18:44:00 +01:00
|
|
|
*/
|
|
|
|
public function addCallback($type, $callback) {
|
|
|
|
if(!array_key_exists($type, $this->callbacks)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('Invalid type "%s"', $type));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->callbacks[$type][] = $callback;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param string $type
|
2012-12-07 18:44:00 +01:00
|
|
|
* @param callable $callback
|
2016-05-25 07:09:29 +02:00
|
|
|
* @return $this
|
2012-12-07 18:44:00 +01:00
|
|
|
*/
|
|
|
|
public function removeCallback($type, $callback) {
|
|
|
|
$pos = array_search($callback, $this->callbacks[$type]);
|
2016-05-25 07:09:29 +02:00
|
|
|
if($pos !== false) {
|
|
|
|
unset($this->callbacks[$type][$pos]);
|
|
|
|
}
|
2012-12-07 18:44:00 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function invokeCallbacks($type, $args = array()) {
|
|
|
|
foreach($this->callbacks[$type] as $callback) {
|
|
|
|
call_user_func_array($callback, $args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Parse a value from a fixture file. If it starts with =>
|
2012-12-07 18:44:00 +01:00
|
|
|
* it will get an ID from the fixture dictionary
|
|
|
|
*
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param string $value
|
2013-07-10 02:44:24 +02:00
|
|
|
* @param array $fixtures See {@link createObject()}
|
|
|
|
* @param string $class If the value parsed is a class relation, this parameter
|
|
|
|
* will be given the value of that class's name
|
|
|
|
* @return string Fixture database ID, or the original value
|
2012-12-07 18:44:00 +01:00
|
|
|
*/
|
2013-07-10 02:44:24 +02:00
|
|
|
protected function parseValue($value, $fixtures = null, &$class = null) {
|
2012-12-07 18:44:00 +01:00
|
|
|
if(substr($value,0,2) == '=>') {
|
|
|
|
// Parse a dictionary reference - used to set foreign keys
|
|
|
|
list($class, $identifier) = explode('.', substr($value,2), 2);
|
|
|
|
|
|
|
|
if($fixtures && !isset($fixtures[$class][$identifier])) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'No fixture definitions found for "%s"',
|
|
|
|
$value
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fixtures[$class][$identifier];
|
|
|
|
} else {
|
|
|
|
// Regular field value setting
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setValue($obj, $name, $value, $fixtures = null) {
|
|
|
|
$obj->$name = $this->parseValue($value, $fixtures);
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:29:52 +02:00
|
|
|
protected function overrideField($obj, $fieldName, $value, $fixtures = null) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$class = get_class($obj);
|
|
|
|
$table = DataObject::getSchema()->tableForField($class, $fieldName);
|
2015-05-20 18:29:52 +02:00
|
|
|
$value = $this->parseValue($value, $fixtures);
|
|
|
|
|
|
|
|
DB::manipulate(array(
|
|
|
|
$table => array(
|
2016-05-25 07:09:29 +02:00
|
|
|
"command" => "update",
|
|
|
|
"id" => $obj->ID,
|
|
|
|
"class" => $class,
|
|
|
|
"fields" => array($fieldName => $value),
|
2015-05-20 18:29:52 +02:00
|
|
|
)
|
|
|
|
));
|
|
|
|
$obj->$fieldName = $value;
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:34:58 +01:00
|
|
|
}
|