1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Site\Meta; |
4
|
|
|
|
5
|
|
|
use Brendt\Stitcher\Site\Page; |
6
|
|
|
use Pageon\Html\Meta\Meta; |
7
|
|
|
|
8
|
|
|
class MetaCompiler |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var callable[] |
12
|
|
|
*/ |
13
|
|
|
private $compilers = []; |
14
|
|
|
private $metaConfig; |
15
|
|
|
|
16
|
|
|
public function __construct(array $metaConfig = []) { |
17
|
|
|
$this->metaConfig = $metaConfig; |
18
|
|
|
|
19
|
|
|
$this->addCompiler('title', [$this, 'compileTitle']); |
20
|
|
|
$this->addCompiler('description', [$this, 'compileDescription']); |
21
|
|
|
$this->addCompiler('image', [$this, 'compileImage']); |
22
|
|
|
$this->addCompiler('pagination', [$this, 'compilePagination']); |
23
|
|
|
$this->addCompiler('meta', [$this, 'compileMeta']); |
24
|
|
|
$this->addCompiler('_name', [$this, 'compileNamedMeta']); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function setDefaultMeta(Meta &$meta) { |
28
|
|
|
foreach ($this->metaConfig as $name => $value) { |
29
|
|
|
$meta->name($name, $value); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function addCompiler(string $name, callable $callback) : MetaCompiler { |
34
|
|
|
$this->compilers[$name] = $callback; |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function compilePage(Page $page) { |
40
|
|
|
$variables = $page->getVariables(); |
41
|
|
|
|
42
|
|
|
foreach ($variables as $name => $data) { |
43
|
|
|
if (!$page->isParsedVariable($name)) { |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->compilePageVariable($page, $name, $data); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function compilePageVariable(Page $page, string $name, $data, bool $force = false) { |
52
|
|
|
$isCustomCompiler = isset($this->compilers[$name]); |
53
|
|
|
|
54
|
|
|
if (!$isCustomCompiler && !$force) { |
55
|
|
|
return; |
56
|
|
|
} else if (!$isCustomCompiler) { |
57
|
|
|
$compileCallable = $this->compilers['_name']; |
58
|
|
|
} else { |
59
|
|
|
$compileCallable = $this->compilers[$name]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$compileCallable($page, $data, $name); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function compileTitle(Page $page, string $data) { |
66
|
|
|
$page->getMeta()->title($data); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function compileDescription(Page $page, string $data) { |
70
|
|
|
$page->getMeta()->description($data); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function compileImage(Page $page, $data) { |
74
|
|
|
if (is_array($data) && isset($data['src'])) { |
75
|
|
|
$page->getMeta()->image($data['src']); |
76
|
|
|
} else if (is_string($data)) { |
77
|
|
|
$page->getMeta()->image($data); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function compilePagination(Page $page, array $pagination) { |
82
|
|
View Code Duplication |
if (isset($pagination['next']['url'])) { |
|
|
|
|
83
|
|
|
$page->getMeta()->link('next', $pagination['next']['url']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
if (isset($pagination['prev']['url'])) { |
|
|
|
|
87
|
|
|
$page->getMeta()->link('prev', $pagination['prev']['url']); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function compileMeta(Page $page, array $data) { |
92
|
|
|
foreach ($data as $name => $item) { |
93
|
|
|
$this->compilePageVariable($page, $name, $item, true); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function compileNamedMeta(Page $page, $data, string $name) { |
98
|
|
|
$page->getMeta()->name($name, $data); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.