|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2018 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2018 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Processor; |
|
18
|
|
|
|
|
19
|
|
|
use SWP\Bundle\ContentBundle\File\FileExtensionCheckerInterface; |
|
20
|
|
|
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface; |
|
21
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
|
22
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMedia; |
|
23
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface; |
|
24
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
25
|
|
|
|
|
26
|
|
View Code Duplication |
final class EmbeddedVideoProcessor implements ArticleBodyProcessorInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var FileExtensionCheckerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $fileExtensionChecker; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var MediaManagerInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $mediaManager; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(FileExtensionCheckerInterface $fileExtensionChecker, MediaManagerInterface $mediaManager) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->fileExtensionChecker = $fileExtensionChecker; |
|
41
|
|
|
$this->mediaManager = $mediaManager; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function process(ArticleInterface $article, ArticleMediaInterface $articleMedia): void |
|
45
|
|
|
{ |
|
46
|
|
|
if (null === $articleMedia->getFile()) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$body = $article->getBody(); |
|
51
|
|
|
preg_match( |
|
52
|
|
|
'/<video[^>]*>/', |
|
53
|
|
|
str_replace(PHP_EOL, '', $body), |
|
54
|
|
|
$matches |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
if (empty($matches) || !isset($matches[0])) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$videoString = $matches[0]; |
|
62
|
|
|
$crawler = new Crawler($videoString); |
|
63
|
|
|
$videos = $crawler->filter('video'); |
|
64
|
|
|
|
|
65
|
|
|
/** @var \DOMElement $videoElement */ |
|
66
|
|
|
foreach ($videos as $videoElement) { |
|
67
|
|
|
if (false !== strpos($videoElement->getAttribute('src'), ArticleMedia::getOriginalMediaId($articleMedia->getFile()->getAssetId()))) { |
|
68
|
|
|
$videoElement->setAttribute('src', $this->mediaManager->getMediaUri($articleMedia->getFile())); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$article->setBody(str_replace($videoString, $crawler->filter('body')->html(), $body)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function supports(string $type): bool |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->fileExtensionChecker->isVideo($type); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|