From 537d95d6145760f40ebaacaff52272a09a6ed3d3 Mon Sep 17 00:00:00 2001 From: Roman Glushko <r.glushko@atwix.com> Date: Wed, 27 Mar 2019 10:57:15 +0200 Subject: [PATCH] #15 Extended the fetch media files logic using the composite pattern and di.xml configuration --- Api/MediaFilesParserInterface.php | 15 ++++++ Model/Config/Parser/ContentParser.php | 14 +++--- .../Parser/Query/FetchMediaFilesChain.php | 50 +++++++++++++++++++ .../MediaDirectiveFileParser.php} | 10 ++-- etc/di.xml | 8 +++ 5 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 Api/MediaFilesParserInterface.php create mode 100644 Model/Config/Parser/Query/FetchMediaFilesChain.php rename Model/Config/Parser/Query/{FetchMediaFilesFromContent.php => Media/MediaDirectiveFileParser.php} (66%) diff --git a/Api/MediaFilesParserInterface.php b/Api/MediaFilesParserInterface.php new file mode 100644 index 0000000..ed01279 --- /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 7a92582..a331122 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 0000000..ce8e147 --- /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 857b9e3..8987f0b 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 fece9a8..18cd8df 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> -- GitLab