-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathCollectionItemHandler.php
73 lines (59 loc) · 2.11 KB
/
CollectionItemHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace TightenCo\Jigsaw\Handlers;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use TightenCo\Jigsaw\File\OutputFile;
class CollectionItemHandler
{
private $config;
private $handlers;
public function __construct(Collection $config, $handlers)
{
$this->config = $config;
$this->handlers = collect($handlers);
}
public function shouldHandle($file)
{
return $this->isInCollectionDirectory($file)
&& ! Str::startsWith($file->getFilename(), ['.', '_']);
}
private function isInCollectionDirectory($file)
{
$base = $file->topLevelDirectory();
return Str::startsWith($base, '_') && $this->hasCollectionNamed($this->getCollectionName($file));
}
private function hasCollectionNamed($candidate)
{
return Arr::get($this->config, 'collections.' . $candidate) !== null;
}
private function getCollectionName($file)
{
return substr($file->topLevelDirectory(), 1);
}
public function handle($file, $pageData)
{
$handler = $this->handlers->first(function ($handler) use ($file) {
return $handler->shouldHandle($file);
});
$pageData->setPageVariableToCollectionItem($this->getCollectionName($file), $file->getFilenameWithoutExtension());
if ($pageData->page === null) {
return null;
}
return $handler->handleCollectionItem($file, $pageData)
->map(function ($outputFile, $templateToExtend) use ($file) {
if ($templateToExtend) {
$outputFile->data()->setExtending($templateToExtend);
}
$path = $outputFile->data()->page->getPath();
return $path ? new OutputFile(
$file,
dirname($path),
basename($path, '.' . $outputFile->extension()),
$outputFile->extension(),
$outputFile->contents(),
$outputFile->data(),
) : null;
})->filter()->values();
}
}