1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PODEntender\Infrastructure\Application\StaticSite; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use PODEntender\Domain\Model\FileProcessing\OutputFile; |
8
|
|
|
use PODEntender\Domain\Model\FileProcessing\OutputFileRepository; |
9
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
10
|
|
|
use TightenCo\Jigsaw\Jigsaw; |
11
|
|
|
|
12
|
|
|
class JigsawGenerateOldPagesAfterBuild implements JigsawEventHandler |
13
|
|
|
{ |
14
|
|
|
public function handle(Jigsaw $jigsaw): void |
15
|
|
|
{ |
16
|
|
|
/** @var OutputFileRepository $outputFileRepository */ |
17
|
|
|
$outputFileRepository = $jigsaw->app->make(OutputFileRepository::class); |
18
|
|
|
$filesystem = new Filesystem(); |
19
|
|
|
|
20
|
|
|
$outputFileRepository->all() |
21
|
|
|
->filter(function (OutputFile $file) { |
22
|
|
|
if (!Str::contains($file->path(), 'episodio/')) { |
23
|
|
|
return false; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$crawler = new Crawler(); |
27
|
|
|
$crawler->addHtmlContent($file->content()); |
28
|
|
|
|
29
|
|
|
return $crawler->filter('link[rel="oldLink"]')->count(); |
30
|
|
|
}) |
31
|
|
|
->each(function (OutputFile $file) use ($jigsaw, $filesystem) { |
32
|
|
|
$crawler = new Crawler(); |
33
|
|
|
$crawler->addHtmlContent($file->content()); |
34
|
|
|
|
35
|
|
|
$newFile = Str::replaceFirst( |
36
|
|
|
'https://podentender.com', |
37
|
|
|
$jigsaw->getDestinationPath(), |
38
|
|
|
$crawler->filter('link[rel="oldLink"]')->first()->attr('href') |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$dirname = $filesystem->dirname($newFile); |
42
|
|
|
if (!$filesystem->exists($dirname)) { |
43
|
|
|
$filesystem->makeDirectory( |
44
|
|
|
$filesystem->dirname($newFile), |
45
|
|
|
0777, |
46
|
|
|
true |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$filesystem->put($newFile, $file->content()); |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|