diff --git a/Api/MediaFilesParserInterface.php b/Api/MediaFilesParserInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..ed0127942405cd55bd58f17c6e03410683a12f25 --- /dev/null +++ b/Api/MediaFilesParserInterface.php @@ -0,0 +1,15 @@ +<?php +declare(strict_types=1); + +namespace Firegento\ContentProvisioning\Api; + +interface MediaFilesParserInterface +{ + /** + * Parse media files in CMS content + * + * @param string $content + * @return array + */ + public function execute(string $content): array; +} diff --git a/Model/Config/Parser/ContentParser.php b/Model/Config/Parser/ContentParser.php index 7a925823081ea47285dfcc9c4c811fc56ac66a81..a3311229d1e006d1243b31b57ac988921262986d 100644 --- a/Model/Config/Parser/ContentParser.php +++ b/Model/Config/Parser/ContentParser.php @@ -8,7 +8,7 @@ use Firegento\ContentProvisioning\Api\ConfigParserInterface; use Firegento\ContentProvisioning\Api\Data\EntryInterface; use Firegento\ContentProvisioning\Api\Data\PageEntryInterface; use Firegento\ContentProvisioning\Model\Config\Parser\Query\FetchAttributeValue; -use Firegento\ContentProvisioning\Model\Config\Parser\Query\FetchMediaFilesFromContent; +use Firegento\ContentProvisioning\Model\Config\Parser\Query\FetchMediaFilesChain; use Firegento\ContentProvisioning\Model\Resolver\ContentResolverProvider; use Magento\Framework\Exception\LocalizedException; @@ -30,26 +30,26 @@ class ContentParser implements ConfigParserInterface private $arrayKey; /** - * @var FetchMediaFilesFromContent + * @var FetchMediaFilesChain */ - private $fetchMediaFilesFromContent; + private $fetchMediaFilesChain; /** * @param ContentResolverProvider $contentResolverProvider * @param FetchAttributeValue $fetchAttributeValue - * @param FetchMediaFilesFromContent $fetchMediaFilesFromContent + * @param FetchMediaFilesChain $fetchMediaFilesChain * @param string $arrayKey */ public function __construct( ContentResolverProvider $contentResolverProvider, FetchAttributeValue $fetchAttributeValue, - FetchMediaFilesFromContent $fetchMediaFilesFromContent, + FetchMediaFilesChain $fetchMediaFilesChain, string $arrayKey = PageEntryInterface::CONTENT ) { $this->contentResolverProvider = $contentResolverProvider; $this->fetchAttributeValue = $fetchAttributeValue; $this->arrayKey = $arrayKey; - $this->fetchMediaFilesFromContent = $fetchMediaFilesFromContent; + $this->fetchMediaFilesFromContent = $fetchMediaFilesChain; } /** @@ -66,7 +66,7 @@ class ContentParser implements ConfigParserInterface return [ $this->arrayKey => $content, - EntryInterface::MEDIA_FILES => $this->fetchMediaFilesFromContent->execute($content) + EntryInterface::MEDIA_FILES => $this->fetchMediaFilesChain->execute($content) ]; } } diff --git a/Model/Config/Parser/Query/FetchMediaFilesChain.php b/Model/Config/Parser/Query/FetchMediaFilesChain.php new file mode 100644 index 0000000000000000000000000000000000000000..ce8e147d0e4a59262e7cf214a9dfc6bf50bebfc6 --- /dev/null +++ b/Model/Config/Parser/Query/FetchMediaFilesChain.php @@ -0,0 +1,50 @@ +<?php +declare(strict_types=1); + +namespace Firegento\ContentProvisioning\Model\Config\Parser\Query; + +use Firegento\ContentProvisioning\Api\MediaFilesParserInterface; +use Magento\Framework\Exception\LocalizedException; + +class FetchMediaFilesChain implements MediaFilesParserInterface +{ + /** + * @var array + */ + protected $parsers; + + /** + * @param array $parsers + * @throws LocalizedException + */ + public function __construct(array $parsers) + { + foreach ($parsers as $parserInstance) { + if (!($parserInstance instanceof MediaFilesParserInterface)) { + throw new LocalizedException(__( + 'Parser needs to be instance of %interface', + ['interface' => MediaFilesParserInterface::class] + )); + } + } + + $this->parsers = $parsers; + } + + /** + * Parse media files from CMS content delegating the parsing strategy to child components + * + * @param string $content + * @return array + */ + public function execute(string $content): array + { + $mediaFiles = []; + + foreach ($this->parsers as $parser) { + $mediaFiles[] = $parser->execute($content); + } + + return array_merge(...$mediaFiles); + } +} diff --git a/Model/Config/Parser/Query/FetchMediaFilesFromContent.php b/Model/Config/Parser/Query/Media/MediaDirectiveFileParser.php similarity index 66% rename from Model/Config/Parser/Query/FetchMediaFilesFromContent.php rename to Model/Config/Parser/Query/Media/MediaDirectiveFileParser.php index 857b9e38c519a17797bd2c62e82cb9972a9a8f26..8987f0bab3b1c99bf5e6cccae85ba168bf85eb24 100644 --- a/Model/Config/Parser/Query/FetchMediaFilesFromContent.php +++ b/Model/Config/Parser/Query/Media/MediaDirectiveFileParser.php @@ -1,22 +1,24 @@ <?php declare(strict_types=1); -namespace Firegento\ContentProvisioning\Model\Config\Parser\Query; +namespace Firegento\ContentProvisioning\Model\Config\Parser\Query\Media; -use Magento\Framework\Exception\LocalizedException; +use Firegento\ContentProvisioning\Api\MediaFilesParserInterface; -class FetchMediaFilesFromContent +class MediaDirectiveFileParser implements MediaFilesParserInterface { /** + * Parse media files from {media} directives + * * @param string $content * @return array - * @throws LocalizedException */ public function execute(string $content): array { if (preg_match_all('/\{\{media url=\"\;(?P<path>.*?)\"\;\}\}/', $content, $matches)) { return $matches['path']; } + return []; } } diff --git a/etc/di.xml b/etc/di.xml index fece9a8557f1f9ef5578822a7f13e1b7d8da1bef..18cd8dfb61440fbecee1bc019f966df55386427c 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -97,4 +97,12 @@ </argument> </arguments> </type> + + <type name="Firegento\ContentProvisioning\Model\Config\Parser\Query\FetchMediaFilesChain"> + <arguments> + <argument name="parsers" xsi:type="array"> + <item name="media_directive" xsi:type="object">Firegento\ContentProvisioning\Model\Config\Parser\Query\Media\MediaDirectiveFileParser</item> + </argument> + </arguments> + </type> </config>