silverstripe-frameworktest/code/tasks/FTPageMakerTask.php

156 lines
4.4 KiB
PHP
Raw Normal View History

2012-02-29 15:49:37 +01:00
<?php
2020-11-03 05:48:30 +01:00
use DNADesign\Elemental\Models\ElementContent;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Image;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Core\ClassInfo;
2020-11-03 05:48:30 +01:00
use DNADesign\Elemental\Models\BaseElement;
use SilverStripe\ElementalBannerBlock\Block\BannerBlock;
use SilverStripe\ElementalFileBlock\Block\FileBlock;
2012-02-29 15:49:37 +01:00
/**
* Creates sample page structure, useful to test tree performance,
* UI behaviour on deeply nested pages etc.
2020-11-03 05:48:30 +01:00
*
2012-02-29 15:49:37 +01:00
* @todo Allow passing in counts
*/
2015-12-17 21:20:49 +01:00
class FTPageMakerTask extends BuildTask
{
2020-11-03 05:48:30 +01:00
/**
* Defaults create 2,000 pages
*/
protected $pageCountByDepth = [
5,
100,
1,
1,
2020-11-03 05:48:30 +01:00
];
/**
* @var array Range of blocks to add (in case elemental is installed).
* Will create between X and Y blocks randomly.
*/
protected $blockCountRange = [
1,
10
];
/**
* @var array
* @config
*/
private static $block_generators = [
'DNADesign\Elemental\Models\ElementContent' => [self::class, 'generateContentBlock'],
'SilverStripe\ElementalBannerBlock\Block\BannerBlock' => [self::class, 'generateBannerBlock'],
'SilverStripe\ElementalFileBlock\Block\FileBlock' => [self::class, 'generateFileBlock'],
];
2012-02-29 15:49:37 +01:00
2015-12-17 21:20:49 +01:00
public function run($request)
{
2020-11-03 05:48:30 +01:00
// Optionally add blocks
$withBlocks = (bool)$request->getVar('withBlocks');
if ($withBlocks && !class_exists('DNADesign\Elemental\Models\BaseElement')) {
throw new \LogicException('withBlocks requested, but BaseElement class not found');
}
$this->generatePages(0, "", 0, $withBlocks);
2015-12-17 21:20:49 +01:00
}
2020-11-03 05:48:30 +01:00
protected function generatePages($depth = 0, $prefix = "", $parentID = 0, $withBlocks = false)
2015-12-17 21:20:49 +01:00
{
$maxDepth = count($this->pageCountByDepth);
$pageCount = $this->pageCountByDepth[$depth];
$testPageClasses = ClassInfo::implementorsOf('TestPageInterface');
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";
2020-11-03 05:48:30 +01:00
if ($withBlocks) {
$this->generateBlocksForPage($page);
}
$pageID = $page->ID;
2020-11-03 05:48:30 +01:00
unset($page);
if ($depth < $maxDepth-1) {
2020-11-03 05:48:30 +01:00
$this->generatePages($depth+1, $fullPrefix, $pageID, $withBlocks);
2015-12-17 21:20:49 +01:00
}
}
}
2020-11-03 05:48:30 +01:00
protected function generateBlocksForPage(Page $page)
{
$classes = array_filter($this->config()->get('block_generators'), function ($callable, $class) {
return class_exists($class);
}, ARRAY_FILTER_USE_BOTH);
// Generate a random amount of blocks in the preferred range
$range = $this->blockCountRange;
foreach(range($range[0], array_rand(range($range[0], $range[1]))) as $i) {
$class = array_rand($classes);
$callable = $classes[$class];
$block = call_user_func($callable);
// Add block to page
$page->ElementalArea()->Elements()->add($block);
// 50% chance of block being published
if (rand(0,1) === 0) {
$block->publishRecursive();
}
echo sprintf(" Added '%s' block #%d to page #%d\n", $class, $block->ID, $page->ID);
}
}
public static function generateContentBlock()
{
$block = new ElementContent([
'HTML' => '<bold>test</bold> 123'
]);
$block->write();
return $block;
}
public static function generateFileBlock()
{
// Supports both images and files
$file = File::get()->shuffle()->First();
if (!$file) {
throw new \LogicException('No files found to associate with FileBlock');
}
$block = new FileBlock();
$block->FileID = $file->ID;
$block->write();
return $block;
}
public static function generateBannerBlock()
{
$block = new BannerBlock([
'Content' => '<bold>test</bold> 123',
'CallToActionLink' => 'http://example.com',
]);
$block->write();
return $block;
}
2015-12-17 21:20:49 +01:00
}