silverstripe-frameworktest/code/tasks/FTPageMakerTask.php

60 lines
1.5 KiB
PHP
Raw Normal View History

2012-02-29 15:49:37 +01:00
<?php
use SilverStripe\Dev\BuildTask;
use SilverStripe\Core\ClassInfo;
2012-02-29 15:49:37 +01:00
/**
* Creates sample page structure, useful to test tree performance,
* UI behaviour on deeply nested pages etc.
*
* @todo Allow passing in counts
*/
2015-12-17 21:20:49 +01:00
class FTPageMakerTask extends BuildTask
{
/**
* Defaults create 2,000 pages
*/
protected $pageCountByDepth = [
5,
100,
1,
1,
1
];
2012-02-29 15:49:37 +01:00
2015-12-17 21:20:49 +01:00
public function run($request)
{
$this->generatePages();
2015-12-17 21:20:49 +01:00
}
protected function generatePages($depth = 0, $prefix = "", $parentID = 0)
2015-12-17 21:20:49 +01:00
{
$maxDepth = count($this->pageCountByDepth);
$pageCount = $this->pageCountByDepth[$depth];
$testPageClasses = ClassInfo::implementorsOf('TestPageInterface');
$testPageClasses[] = 'Page';
for ($i=1; $i<=$pageCount; $i++) {
$fullPrefix = $prefix ? "{$prefix}-{$i}" : $i;
$randomIndex = array_rand($testPageClasses);
$pageClass = $testPageClasses[$randomIndex];
$page = new $pageClass();
2015-12-17 21:20:49 +01:00
$page->ParentID = $parentID;
$page->Title = "Test page {$fullPrefix}";
2015-12-17 21:20:49 +01:00
$page->write();
$page->publish('Stage', 'Live');
2012-02-29 15:49:37 +01:00
echo "Created '$page->Title' ($page->ClassName)\n";
$pageID = $page->ID;
unset($page);
if ($depth < $maxDepth-1) {
$this->generatePages($depth+1, $fullPrefix, $pageID);
2015-12-17 21:20:49 +01:00
}
}
}
2015-12-17 21:20:49 +01:00
}