1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Symplify |
7
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Symplify\PHP7_Sculpin\Renderable\Routing; |
11
|
|
|
|
12
|
|
|
use Symplify\PHP7_Sculpin\Contract\Renderable\DecoratorInterface; |
13
|
|
|
use Symplify\PHP7_Sculpin\Utils\PathNormalizer; |
14
|
|
|
use Symplify\PHP7_Sculpin\Renderable\File\File; |
15
|
|
|
use Symplify\PHP7_Sculpin\Renderable\File\PostFile; |
16
|
|
|
|
17
|
|
|
final class RouteDecorator implements DecoratorInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $postRoute; |
23
|
|
|
|
24
|
|
|
public function __construct(string $postRoute) |
25
|
|
|
{ |
26
|
|
|
$this->postRoute = $postRoute; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function decorateFile(File $file) |
30
|
|
|
{ |
31
|
|
|
$file->setOutputPath($this->detectFileOutputPath($file)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function detectFileOutputPath(File $file) : string |
35
|
|
|
{ |
36
|
|
|
if ($this->isFileIndex($file)) { |
37
|
|
|
return 'index.html'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($this->isPostFile($file)) { |
41
|
|
|
/* @var PostFile $file */ |
42
|
|
|
return $this->createOutputPathForPostFile($file, $this->postRoute); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($this->isFileNonHtml($file)) { |
46
|
|
|
return $file->getBaseName(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $file->getBaseName().DIRECTORY_SEPARATOR.'index.html'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function isFileIndex(File $file) : bool |
53
|
|
|
{ |
54
|
|
|
return $file->getBaseName() === 'index'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function isFileNonHtml(File $file) : bool |
58
|
|
|
{ |
59
|
|
|
return in_array( |
60
|
|
|
$file->getPrimaryExtension(), |
61
|
|
|
['xml', 'rss', 'json', 'atom'] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function isPostFile(File $file) : bool |
66
|
|
|
{ |
67
|
|
|
return $file instanceof PostFile; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function createOutputPathForPostFile(PostFile $file, string $postRoute) : string |
71
|
|
|
{ |
72
|
|
|
$permalink = $postRoute; |
73
|
|
|
$permalink = preg_replace('/:year/', $file->getDateInFormat('Y'), $permalink); |
74
|
|
|
$permalink = preg_replace('/:month/', $file->getDateInFormat('m'), $permalink); |
75
|
|
|
$permalink = preg_replace('/:day/', $file->getDateInFormat('j'), $permalink); |
76
|
|
|
$permalink = preg_replace('/:filename/', $file->getFilenameWithoutDate(), $permalink); |
77
|
|
|
$permalink .= '/index.html'; |
78
|
|
|
|
79
|
|
|
return PathNormalizer::normalize($permalink); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|