From 260ad323acdc68ca4e96fa685fa3d8fbda1570ff Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 18 May 2017 13:20:53 +1200 Subject: [PATCH] Improved page generation (customise depths) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Similar to FTFileMakerTask, this allows for creation of trees of arbitrary depths. Which is useful to test long page lists, deep trees, or large amount of “broad vs deep” nestings. Yes, it has no unit tests - I’m fine with that :) --- code/tasks/FTPageMakerTask.php | 37 +++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/code/tasks/FTPageMakerTask.php b/code/tasks/FTPageMakerTask.php index 60ec10b..dc9f192 100644 --- a/code/tasks/FTPageMakerTask.php +++ b/code/tasks/FTPageMakerTask.php @@ -10,27 +10,44 @@ use SilverStripe\Dev\BuildTask; class FTPageMakerTask extends BuildTask { + /** + * Defaults create 15,000 pages + */ + protected $pageCountByDepth = [ + 10, + 100, + 5, + 1, + 1 + ]; public function run($request) { - echo "

Making pages

"; - // Creates 3^5 pages - $this->makePages(3, 5); + $this->generatePages(); } - - protected function makePages($count, $depth, $prefix = "", $parentID = 0) + + protected function generatePages($depth = 0, $prefix = "", $parentID = 0) { - for ($i=1;$i<=$count;$i++) { + $maxDepth = count($this->pageCountByDepth); + $pageCount = $this->pageCountByDepth[$depth]; + + for ($i=1; $i<=$pageCount; $i++) { + $fullPrefix = $prefix ? "{$prefix}-{$i}" : $i; $page = new Page(); $page->ParentID = $parentID; - $page->Title = "Test page $prefix$i"; + $page->Title = "Test page {$fullPrefix}"; $page->write(); $page->publish('Stage', 'Live'); - echo "
  • Created '$page->Title'"; - if ($depth > 1) { - $this->makePages($count, $depth-1, $prefix."$i.", $page->ID); + echo "Created '$page->Title'\n"; + + $pageID = $page->ID; + unset($page); + + if ($depth < $maxDepth-1) { + $this->generatePages($depth+1, $fullPrefix, $pageID); } } } + }