From c53ff0da7c53539fb58621c0915d8382c8ab642e Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Fri, 15 Oct 2021 18:43:50 -0500 Subject: [PATCH] book end build --- src/PageComponents/PageTitle.php | 68 ++++++++++--------- .../PageComponents/PageTitleBaselineTest.php | 45 +++++++++++- 2 files changed, 80 insertions(+), 33 deletions(-) diff --git a/src/PageComponents/PageTitle.php b/src/PageComponents/PageTitle.php index 4e86fee..a11fcb2 100644 --- a/src/PageComponents/PageTitle.php +++ b/src/PageComponents/PageTitle.php @@ -15,7 +15,7 @@ class PageTitle implements Buildable */ private $store; - private string $titleStyle = 'page'; + private $titles = []; private bool $isReversed = false; @@ -29,12 +29,6 @@ public function __construct(Store $store) $this->store = $store; } - public function bookEnd(): PageTitle - { - $this->titleStyle = 'book-end'; - return $this; - } - public function reversed(): PageTitle { $this->isReversed = true; @@ -44,14 +38,18 @@ public function reversed(): PageTitle public function build(): string { $titles = $this->titles(); - if ($this->titleStyle === 'book-end' and count($titles) > 2) { - $t = []; - $t[] = array_shift($titles); - $t[] = array_pop($titles); + return $this->combineTitles($titles); + } - $titles = $t; - } - return implode(' | ', $titles); + public function buildBookEnd(): string + { + $titles = $this->titles(); + + $t = []; + $t[] = array_shift($titles); + $t[] = array_pop($titles); + + return $this->combineTitles($t); } public function __toString(): string @@ -59,33 +57,41 @@ public function __toString(): string return $this->build(); } + private function combineTitles(array $titles): string + { + if ($this->isReversed()) { + $titles = array_reverse($titles); + } + return implode(' | ', $titles); + } + /** * @return array [description] */ private function titles(): array { - $titles = []; - $s = $this->store(); - while (! $s->isRoot()) { - $m = $s->markdown(); - if (is_object($m)) { - $titles[] = $m->title(); + if (count($this->titles) === 0) { + $titles = []; + $s = $this->store(); + while (! $s->isRoot()) { + $m = $s->markdown(); + if (is_object($m)) { + $titles[] = $m->title(); + } + + $s = $s->up(); } - $s = $s->up(); - } - - if ($s->isRoot()) { - $m = $s->markdown(); - if (is_object($m)) { - $titles[] = $m->title(); + if ($s->isRoot()) { + $m = $s->markdown(); + if (is_object($m)) { + $titles[] = $m->title(); + } } - } - if ($this->isReversed()) { - return array_reverse($titles); + $this->titles = $titles; } - return $titles; + return $this->titles; } private function isReversed(): bool diff --git a/tests/PageComponents/PageTitleBaselineTest.php b/tests/PageComponents/PageTitleBaselineTest.php index 6882c10..c4a2dc0 100644 --- a/tests/PageComponents/PageTitleBaselineTest.php +++ b/tests/PageComponents/PageTitleBaselineTest.php @@ -33,11 +33,11 @@ )->toBe('Sub | Subfolder content title | Index page'); }); -test('Book-end page title style', function() { +test('Book end page title style', function() { $store = Store::create($this->root, '/subfolder/sub'); expect( - PageTitle::create($store)->bookEnd()->build() + PageTitle::create($store)->buildBookEnd() )->toBe( 'Sub | Index page' ); @@ -50,3 +50,44 @@ PageTitle::create($store)->reversed()->build() )->toBe('Index page | Subfolder content title | Sub'); }); + +$ms = function($start, $end) { + $time = $end - $start; + $ms = $time/1e+6; + return $ms; +}; + +test('Performant and cached', function() use ($ms) { + $store = Store::create($this->root, '/subfolder/sub'); + + $start = hrtime(true); + + $pageTitle = PageTitle::create($store); + + $start1 = hrtime(true); + + $build1 = $pageTitle->build(); + + $end1 = hrtime(true); + + $start2 = hrtime(true); + + $build2 = $pageTitle->buildBookend(); + + $end2 = hrtime(true); + + // Total elapsed time + $leadTime = $ms($start, $end2); + + expect($leadTime)->toBeLessThan(0.5); + + // build call + $cycleTime1 = $ms($start1, $end1); + + expect($cycleTime1)->toBeLessThan(0.4); + + // build book end call + $cycleTime2 = $ms($start2, $end2); + + expect($cycleTime2)->toBeLessThan(0.002); +});