Source of file MockDataBuilder.php
Size: 7,910 Bytes - Last Modified: 2021-12-23T10:56:24+00:00
/var/www/docs.ssmods.com/process/src/code/tasks/MockDataBuilder.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 | <?php /** * This class creates a process that generates mock data records. It accepts an assortment * of options to customise its output. * * @package silverstripe-mock-data * @author Uncle Cheese <unclecheese@leftandmain.com> */ class MockDataBuilder extends Object { /** * @var array An arbitrary list of messages */ protected $log = array(); /** * @var string The class of records to create */ protected $subjectClass; /** * @var DataObject The parent record, if applicable */ protected $parentObj; /** * @var mixed The ID or URLSegment of the parent record */ protected $parentIdentifier; /** * @var string The foreign key on the child record */ protected $parentField = "ParentID"; /** * @var int The number of records to create */ protected $count = 10; /** * @var bool Only populate empty fields */ protected $onlyEmpty = true; /** * @var int The number of records to create for a has_many or many_many relation */ protected $relationCreateLimit = 5; /** * @var bool Download images from the web to populate the record's file relations */ protected $downloadImages = true; /** * @var bool Populate has_many and many_many relations */ protected $includeRelations = true; /** * @var bool If the subject class is a SiteTree descendant */ protected $isSiteTree = false; /** * Constructor. * * @param string The class of mock records to create * @return MockDataBuilder */ public function __construct($className) { $this->subjectClass = $className; if (!class_exists($className) || !is_subclass_of($className, "DataObject")) { throw new Exception("$className doesn't exist, or it is not a DataObject."); } if (!Injector::inst()->get($className)->hasExtension("MockDataObject")) { throw new Exception("$className does not have the MockDataObject extension applied."); } $this->isSiteTree = is_subclass_of($className, "SiteTree"); return $this; } /** * Generates new records for the subject class * * @return array A list of the new ids inserted to the table */ public function generate() { if ($this->parentIdentifier && !$this->parentObj) { $this->determineParentObj(); } $i = 0; $ids = array(); $parentField = $this->parentField; while ($i < $this->count) { $obj = Injector::inst()->create($this->subjectClass); if ($this->parentObj) { $obj->$parentField = $this->parentObj->ID; } $obj->fill(array( 'only_empty' => $this->onlyEmpty, 'include_relations' => $this->includeRelations, 'download_images' => $this->downloadImages, 'relation_create_limit' => $this->relationCreateLimit )); if ($this->parentObj) { $obj->write(); } if ($this->isSiteTree) { $ids[] = $obj->write(); $obj->publish("Stage", "Live"); } $this->log("Created {$this->subjectClass} \"{$obj->getTitle()}\"."); $i++; } return $ids; } /** * Populates existing records with mock data */ public function populate() { if ($this->parentIdentifier && !$this->parentObj) { $this->determineParentObj(); } $set = DataList::create($this->subjectClass); if ($this->parentObj) { $set = $set->filter(array( $this->parentField => $this->parentObj->ID )); } foreach ($set as $obj) { $obj->fill(array( 'only_empty' => $this->onlyEmpty, 'include_relations' => $this->includeRelations, 'download_images' => $this->downloadImages, 'relation_create_limit' => $this->relationCreateLimit )); if ($this->isSiteTree) { $obj->write(); $obj->publish("Stage", "Live"); } $this->log("Updated {$this->subjectClass} \"{$obj->getTitle()}\"."); } } /** * Given a {@link $parentIdentifier} value, figure out what the parent record is. * Parent identifier could be a numeric ID or a URLSegment. */ protected function determineParentObj() { $parent = $this->parentIdentifier; $parentPage = SiteTree::get()->byID((int) $parent); if (!$parentPage) { $parentPage = SiteTree::get_by_link($parent); } if (!$parentPage) { $parentPage = SiteTree::get()->filter(array('Title' => trim($parent)))->first(); } if (!$parentPage) { throw new Exception("Could not find a page with ID, URLSegment, or Title \"$parent\""); } if (!Injector::inst()->get($this->subjectClass)->hasField($this->parentField)) { throw new Exception("{$this->subjectClass} has no field {$this->parentField}."); } $this->parentObj = $parentPage; $this->log("Parent page is #{$parentPage->ID} {$parentPage->getTitle()}"); } /** * Sets the parent object that will own the created mock records * * @param DataObject * @return MockDataBuilder */ public function setParentObj(DataObject $obj) { $this->parentObj = $obj; return $this; } /** * Sets the foreign key field for the created records, e.g. "MyPageHolderID" * * @param string The field name * @param MockDataBuilder */ public function setParentField($field) { $this->parentField = $field; return $this; } /** * Sets the parent identifier for the parent record. Could be a numeric ID or URLSegment * * @param mixed The identifier * @param MockDataBuilder */ public function setParentIdentifier($id) { $this->parentIdentifier = $id; return $this; } /** * Sets the number of records to create * * @param int * @param MockDataBuilder */ public function setCount($count) { $this->count = $count; return $this; } /** * If set to true, only populate fields that are empty * * @param boolean * @param MockDataBuilder */ public function setOnlyEmpty($bool) { $this->onlyEmpty = (bool) $bool; return $this; } /** * Sets the foreign key field for the created records, e.g. "MyPageHolderID" * * @param string The field name * @param MockDataBuilder */ public function setRelationCreateLimit($num) { $this->relationCreateLimit = $num; return $this; } /** * If set to true, download images from the web to populate file relatiosn * * @param boolean * @param MockDataBuilder */ public function setDownloadImages($bool) { $this->downloadImages = (bool) $bool; return $this; } /** * If set to true, populate has_many and many_many relations * * @param boolean * @param MockDataBuilder */ public function setIncludeRelations($bool) { $this->includeRelations = (bool) $bool; return $this; } /** * Logs a message. Either output to console or store internally * * @param string The message */ protected function log($msg) { if (Director::is_cli()) { echo "$msg\n"; } else { $this->log[] = $msg; } } } |