|
1
|
|
|
<?php |
|
2
|
|
|
namespace Bookdown\Bookdown\Process\Toc; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Log\LoggerInterface; |
|
5
|
|
|
use Bookdown\Bookdown\Content\Page; |
|
6
|
|
|
use Bookdown\Bookdown\Content\IndexPage; |
|
7
|
|
|
use Bookdown\Bookdown\Process\ProcessInterface; |
|
8
|
|
|
|
|
9
|
|
|
class TocProcess implements ProcessInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected $logger; |
|
12
|
|
|
protected $tocEntries; |
|
13
|
|
|
|
|
14
|
12 |
|
public function __construct(LoggerInterface $logger) |
|
15
|
|
|
{ |
|
16
|
12 |
|
$this->logger = $logger; |
|
17
|
12 |
|
} |
|
18
|
|
|
|
|
19
|
11 |
|
public function __invoke(Page $page) |
|
20
|
|
|
{ |
|
21
|
11 |
|
if (! $page->isIndex()) { |
|
22
|
3 |
|
$this->logger->info(" Skipping TOC entries for non-index {$page->getTarget()}"); |
|
23
|
3 |
|
return; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
10 |
|
$this->logger->info(" Adding TOC entries for {$page->getTarget()}"); |
|
27
|
10 |
|
$this->tocEntries = array(); |
|
28
|
|
|
// if there are multiple books, ensure correct toc level |
|
29
|
10 |
|
$this->addTocEntries($page, $page->getConfig()->getTocDepth(), $page->isRoot() ? 0 : 1); |
|
|
|
|
|
|
30
|
10 |
|
$page->setTocEntries($this->tocEntries); |
|
|
|
|
|
|
31
|
10 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* A toc depth of 0 means render all headings. A toc depth of 1 is a special case |
|
35
|
|
|
* |
|
36
|
|
|
* @param IndexPage $index |
|
37
|
|
|
* @param $tocDepth |
|
38
|
|
|
* @param int $level |
|
39
|
|
|
*/ |
|
40
|
10 |
|
protected function addTocEntries(IndexPage $index, $tocDepth, $level = 0) |
|
41
|
|
|
{ |
|
42
|
10 |
|
$maxLevel = $level + $tocDepth; |
|
43
|
|
|
|
|
44
|
10 |
|
if ($tocDepth !== 1 && $tocDepth && $index->isRoot()) { |
|
45
|
2 |
|
$maxLevel --; |
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
10 |
|
foreach ($index->getChildren() as $child) { |
|
49
|
10 |
|
$headings = $child->getHeadings(); |
|
50
|
10 |
|
foreach ($headings as $heading) { |
|
51
|
10 |
|
if ($tocDepth && $heading->getLevel() > $maxLevel) { |
|
52
|
5 |
|
continue; |
|
53
|
|
|
} |
|
54
|
10 |
|
$this->tocEntries[] = $heading; |
|
55
|
10 |
|
} |
|
56
|
10 |
|
if ($child->isIndex() && $tocDepth !== 1) { |
|
57
|
5 |
|
$this->addTocEntries($child, $tocDepth, $index->isRoot() ? $level : ($level + 1)); |
|
|
|
|
|
|
58
|
5 |
|
} |
|
59
|
10 |
|
} |
|
60
|
10 |
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: