Skip to content
Snippets Groups Projects
Commit 537d95d6 authored by Roman Glushko's avatar Roman Glushko :violin:
Browse files

#15 Extended the fetch media files logic using the composite pattern and di.xml configuration

parent 2e857039
No related branches found
No related tags found
2 merge requests!16#15 [Media Files] Add an extensibility point to FetchMediaFilesFromContent component,!19Merge current development to 1.2 version
<?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;
}
......@@ -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)
];
}
}
<?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);
}
}
<?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=\&quot\;(?P<path>.*?)\&quot\;\}\}/', $content, $matches)) {
return $matches['path'];
}
return [];
}
}
......@@ -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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment