1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PODEntender\Application\Service\FileProcessing; |
4
|
|
|
|
5
|
|
|
use PODEntender\Domain\Model\FileProcessing\OutputFile; |
6
|
|
|
use PODEntender\Domain\Model\FileProcessing\OutputFileCollection; |
7
|
|
|
use PODEntender\Domain\Model\FileProcessing\OutputFileRepository; |
8
|
|
|
use PODEntender\Domain\Model\Post\Post; |
9
|
|
|
use PODEntender\Domain\Model\Post\PostRepository; |
10
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
11
|
|
|
|
12
|
|
|
class PostProcessFiles |
13
|
|
|
{ |
14
|
|
|
private $outputFilesRepository; |
15
|
|
|
|
16
|
|
|
private $postRepository; |
17
|
|
|
|
18
|
5 |
|
public function __construct(OutputFileRepository $outputFilesRepository, PostRepository $postRepository) |
19
|
|
|
{ |
20
|
5 |
|
$this->outputFilesRepository = $outputFilesRepository; |
21
|
5 |
|
$this->postRepository = $postRepository; |
22
|
5 |
|
} |
23
|
|
|
|
24
|
5 |
|
public function execute(bool $production): OutputFileCollection |
25
|
|
|
{ |
26
|
5 |
|
$files = $this->outputFilesRepository->all(); |
27
|
5 |
|
$processedFiles = new OutputFileCollection(); |
28
|
|
|
|
29
|
5 |
|
foreach ($files as $file) { |
30
|
5 |
|
$crawler = new Crawler(); |
31
|
5 |
|
$crawler->addHtmlContent($file->content()); |
32
|
|
|
|
33
|
5 |
|
$this->decorateParagraphs($crawler); |
34
|
5 |
|
$this->decorateHeadings($crawler); |
35
|
|
|
|
36
|
5 |
|
$content = $crawler->getNode(0)->ownerDocument->saveHTML(); |
37
|
|
|
|
38
|
5 |
|
if ($production) { |
39
|
4 |
|
$content = $this->decorateJekyllRedirects($crawler, $content); |
40
|
|
|
} |
41
|
|
|
|
42
|
5 |
|
$processedFiles->add( |
43
|
5 |
|
new OutputFile($file->path(), $content) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
5 |
|
return $processedFiles; |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
private function decorateJekyllRedirects(Crawler $crawler, string $content): string |
51
|
|
|
{ |
52
|
4 |
|
$canonical = $crawler->filter('link[rel="canonical"]'); |
53
|
4 |
|
if ($canonical->count() === 0) { |
54
|
|
|
return $content; |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
$canonicalLink = $canonical->first()->attr('href'); |
58
|
|
|
$post = $this->postRepository->withAudio()->filter(function (Post $post) use ($canonicalLink) { |
59
|
4 |
|
return $canonicalLink === $post->url(); |
60
|
4 |
|
})->first(); |
61
|
|
|
|
62
|
4 |
|
if (is_null($post) || count($post->redirects()) === 0) { |
63
|
|
|
return $content; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$header = [ |
67
|
4 |
|
'---' , |
68
|
|
|
'redirect_from:', |
69
|
|
|
]; |
70
|
|
|
|
71
|
4 |
|
foreach ($post->redirects() as $redirect) { |
72
|
4 |
|
$header[] = ' - ' . $redirect; |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
$header[] = '---'; |
76
|
|
|
|
77
|
4 |
|
return implode(PHP_EOL, $header) . PHP_EOL . $content; |
78
|
|
|
} |
79
|
|
|
|
80
|
5 |
|
private function decorateParagraphs(Crawler $crawler): void |
81
|
|
|
{ |
82
|
5 |
|
foreach ($crawler->filter('.paragraphs-list p') as $paragraph) { |
83
|
5 |
|
$classes = array_merge( |
84
|
5 |
|
explode(' ', $paragraph->getAttribute('class')), |
85
|
5 |
|
['paragraph'] |
86
|
|
|
); |
87
|
|
|
|
88
|
5 |
|
$paragraph->setAttribute('class', trim(implode(' ', $classes))); |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
foreach ($crawler->filter('.paragraphs-list strong') as $boldItem) { |
92
|
5 |
|
$classes = array_merge( |
93
|
5 |
|
explode(' ', $boldItem->getAttribute('class')), |
94
|
5 |
|
['paragraph--bold'] |
95
|
|
|
); |
96
|
|
|
|
97
|
5 |
|
$boldItem->setAttribute('class', trim(implode(' ', $classes))); |
98
|
|
|
} |
99
|
|
|
|
100
|
5 |
|
foreach ($crawler->filter('.paragraphs-list a') as $linkItem) { |
101
|
5 |
|
$classes = array_merge( |
102
|
5 |
|
explode(' ', $linkItem->getAttribute('class')), |
103
|
5 |
|
['link'] |
104
|
|
|
); |
105
|
|
|
|
106
|
5 |
|
$linkItem->setAttribute('class', trim(implode(' ', $classes))); |
107
|
5 |
|
$linkItem->setAttribute('target', '_blank'); |
108
|
|
|
} |
109
|
5 |
|
} |
110
|
|
|
|
111
|
5 |
|
private function decorateHeadings(Crawler $crawler): void |
112
|
|
|
{ |
113
|
5 |
|
$secondaryHeadings = ['.paragraphs-list h2', '.paragraphs-list h3']; |
114
|
5 |
|
$filter = implode(',', $secondaryHeadings); |
115
|
5 |
|
foreach ($crawler->filter($filter) as $heading) { |
116
|
5 |
|
$classes = array_merge( |
117
|
5 |
|
explode(' ', $heading->getAttribute('class')), |
118
|
5 |
|
['heading', 'heading__secondary'] |
119
|
|
|
); |
120
|
|
|
|
121
|
5 |
|
$heading->setAttribute('class', trim(implode(' ', $classes))); |
122
|
|
|
} |
123
|
5 |
|
} |
124
|
|
|
} |
125
|
|
|
|