Skip to content
Snippets Groups Projects
Commit 0e8b8596 authored by Vadim Justus's avatar Vadim Justus
Browse files

Extend configuration parsers in order to detect media files

parent f7a3ec73
No related branches found
No related tags found
1 merge request!14Introduce logic for handling with media files
Showing
with 314 additions and 42 deletions
......@@ -8,6 +8,8 @@ interface EntryInterface
const KEY = 'key';
const IS_MAINTAINED = 'is_maintained';
const STORES = 'stores';
const MEDIA_DIRECTORY = 'media_directory';
const MEDIA_FILES = 'media_files';
/**
* @return string
......@@ -38,4 +40,24 @@ interface EntryInterface
* @param array $stores
*/
public function setStores(array $stores): void;
/**
* @return string
*/
public function getMediaDirectory(): string;
/**
* @param string $path
*/
public function setMediaDirectory(string $path): void;
/**
* @return array
*/
public function getMediaFiles(): array;
/**
* @param array $files
*/
public function setMediaFiles(array $files): void;
}
......@@ -55,4 +55,36 @@ class BlockEntry extends Block implements BlockEntryInterface
{
$this->setData(BlockEntryInterface::STORES, $stores);
}
/**
* @return string
*/
public function getMediaDirectory(): string
{
return (string)$this->getData(BlockEntryInterface::MEDIA_DIRECTORY);
}
/**
* @param string $path
*/
public function setMediaDirectory(string $path): void
{
$this->setData(BlockEntryInterface::MEDIA_DIRECTORY, $path);
}
/**
* @return array
*/
public function getMediaFiles(): array
{
return (array)$this->getData(BlockEntryInterface::MEDIA_FILES);
}
/**
* @param array $files
*/
public function setMediaFiles(array $files): void
{
$this->setData(BlockEntryInterface::MEDIA_FILES, $files);
}
}
......@@ -5,8 +5,10 @@ namespace Firegento\ContentProvisioning\Model\Config\Parser;
use DOMElement;
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\Resolver\ContentResolverProvider;
use Magento\Framework\Exception\LocalizedException;
......@@ -27,19 +29,27 @@ class ContentParser implements ConfigParserInterface
*/
private $arrayKey;
/**
* @var FetchMediaFilesFromContent
*/
private $fetchMediaFilesFromContent;
/**
* @param ContentResolverProvider $contentResolverProvider
* @param FetchAttributeValue $fetchAttributeValue
* @param FetchMediaFilesFromContent $fetchMediaFilesFromContent
* @param string $arrayKey
*/
public function __construct(
ContentResolverProvider $contentResolverProvider,
FetchAttributeValue $fetchAttributeValue,
FetchMediaFilesFromContent $fetchMediaFilesFromContent,
string $arrayKey = PageEntryInterface::CONTENT
) {
$this->contentResolverProvider = $contentResolverProvider;
$this->fetchAttributeValue = $fetchAttributeValue;
$this->arrayKey = $arrayKey;
$this->fetchMediaFilesFromContent = $fetchMediaFilesFromContent;
}
/**
......@@ -52,9 +62,11 @@ class ContentParser implements ConfigParserInterface
$contentNode = $element->getElementsByTagName('content')->item(0);
$type = $this->fetchAttributeValue->execute($contentNode, 'type', 'plain');
$contentResolver = $this->contentResolverProvider->get($type);
$content = $contentResolver->execute($contentNode);
return [
$this->arrayKey => $contentResolver->execute($contentNode)
$this->arrayKey => $content,
EntryInterface::MEDIA_FILES => $this->fetchMediaFilesFromContent->execute($content)
];
}
}
<?php
declare(strict_types=1);
namespace Firegento\ContentProvisioning\Model\Config\Parser;
use DOMElement;
use Firegento\ContentProvisioning\Api\ConfigParserInterface;
use Firegento\ContentProvisioning\Api\Data\EntryInterface;
use Firegento\ContentProvisioning\Model\Config\Parser\Query\FetchChildNodeValue;
use Firegento\ContentProvisioning\Model\Resolver\PathResolver;
class MediaDirectoryParser implements ConfigParserInterface
{
/**
* @var FetchChildNodeValue
*/
private $fetchChildNodeValue;
/**
* @var PathResolver
*/
private $pathResolver;
/**
* @param FetchChildNodeValue $fetchChildNodeValue
* @param PathResolver $pathResolver
*/
public function __construct(
FetchChildNodeValue $fetchChildNodeValue,
PathResolver $pathResolver
) {
$this->fetchChildNodeValue = $fetchChildNodeValue;
$this->pathResolver = $pathResolver;
}
/**
* @param DOMElement $element
* @return array
*/
public function execute(DOMElement $element): array
{
$nodeValue = $this->fetchChildNodeValue->execute($element, 'media_directory');
$mediaDirectory = null;
if (!empty($nodeValue)) {
$mediaDirectory = $this->pathResolver->execute($nodeValue);
}
return [
EntryInterface::MEDIA_DIRECTORY => $mediaDirectory
];
}
}
......@@ -5,6 +5,7 @@ namespace Firegento\ContentProvisioning\Model\Config\Parser;
use DOMElement;
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\FetchBooleanAttributeValue;
......@@ -49,12 +50,12 @@ class MetaDataParser implements ConfigParserInterface
public function execute(DOMElement $element): array
{
return [
PageEntryInterface::KEY => $this->fetchAttributeValue->execute($element, 'key'),
EntryInterface::KEY => $this->fetchAttributeValue->execute($element, 'key'),
PageEntryInterface::IDENTIFIER => $this->fetchAttributeValue->execute($element, 'identifier'),
PageEntryInterface::TITLE => $this->fetchChildNodeValue->execute($element, 'title'),
PageEntryInterface::IS_ACTIVE =>
$this->fetchBooleanAttributeValue->execute($element, 'active', 'false'),
PageEntryInterface::IS_MAINTAINED =>
EntryInterface::IS_MAINTAINED =>
$this->fetchBooleanAttributeValue->execute($element, 'maintained', 'false')
];
}
......
<?php
declare(strict_types=1);
namespace Firegento\ContentProvisioning\Model\Config\Parser\Query;
use Magento\Framework\Exception\LocalizedException;
class FetchMediaFilesFromContent
{
/**
* @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 [];
}
}
......@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Firegento\ContentProvisioning\Model\Console;
use Firegento\ContentProvisioning\Api\Data\EntryInterface;
use Firegento\ContentProvisioning\Api\Data\PageEntryInterface;
use Firegento\ContentProvisioning\Model\Query\GetPageEntryList\Proxy as GetPageEntryList;
use Firegento\ContentProvisioning\Model\Query\GetPagesByPageEntry\Proxy as GetPagesByPageEntry;
......@@ -47,6 +48,7 @@ class PageListCommand extends Command
$table = new Table($output);
$table->setHeaders(['Key', 'Identifier', 'Stores', 'Maintained', 'Active', 'Title', 'in DB (IDs)']);
/** @var EntryInterface $entry */
foreach ($this->getAllContentEntries->get() as $entry) {
$table->addRow([
$entry->getKey(),
......@@ -55,7 +57,7 @@ class PageListCommand extends Command
$entry->isMaintained() ? 'yes' : 'no',
$entry->isActive() ? 'yes' : 'no',
$entry->getTitle(),
$this->getExistsInDbValue($entry)
$this->getExistsInDbValue($entry),
]);
}
......
......@@ -55,4 +55,36 @@ class PageEntry extends Page implements PageEntryInterface
{
$this->setData(PageEntryInterface::STORES, $stores);
}
}
\ No newline at end of file
/**
* @return string
*/
public function getMediaDirectory(): string
{
return (string)$this->getData(PageEntryInterface::MEDIA_DIRECTORY);
}
/**
* @param string $path
*/
public function setMediaDirectory(string $path): void
{
$this->setData(PageEntryInterface::MEDIA_DIRECTORY, $path);
}
/**
* @return array
*/
public function getMediaFiles(): array
{
return (array)$this->getData(PageEntryInterface::MEDIA_FILES);
}
/**
* @param array $files
*/
public function setMediaFiles(array $files): void
{
$this->setData(PageEntryInterface::MEDIA_FILES, $files);
}
}
......@@ -12,25 +12,17 @@ use Magento\Framework\Module\Dir\Reader;
class FileContentResolver implements ContentResolverInterface
{
/**
* @var Reader
* @var PathResolver
*/
private $moduleReader;
private $pathResolver;
/**
* @var DirectoryList
*/
private $directoryList;
/**
* @param Reader $moduleReader
* @param DirectoryList $directoryList
* @param PathResolver $pathResolver
*/
public function __construct(
Reader $moduleReader,
DirectoryList $directoryList
PathResolver $pathResolver
) {
$this->moduleReader = $moduleReader;
$this->directoryList = $directoryList;
$this->pathResolver = $pathResolver;
}
/**
......@@ -40,34 +32,11 @@ class FileContentResolver implements ContentResolverInterface
*/
public function execute(DOMElement $node): string
{
$path = $this->resolvePath((string)$node->textContent);
$path = $this->pathResolver->execute((string)$node->textContent);
if (!is_file($path)) {
throw new LocalizedException(__('Given content file %file does not exists.', ['file' => $path]));
}
return file_get_contents($path);
}
/**
* @param string $path
* @return string
*/
private function resolvePath(string $path): string
{
if (strpos($path, '::') !== false) {
$pathParts = explode('::', $path, 2);
$moduleName = $pathParts[0];
$filePath = $pathParts[1];
$moduleDirectory = $this->moduleReader->getModuleDir('', $moduleName);
return implode(DIRECTORY_SEPARATOR, [
$moduleDirectory,
$filePath
]);
} else {
return implode(DIRECTORY_SEPARATOR, [
$this->directoryList->getRoot(),
$path
]);
}
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
namespace Firegento\ContentProvisioning\Model\Resolver;
use DOMElement;
use Firegento\ContentProvisioning\Api\ContentResolverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\Module\Dir\Reader;
class PathResolver
{
/**
* @var Reader
*/
private $moduleReader;
/**
* @var DirectoryList
*/
private $directoryList;
/**
* @param Reader $moduleReader
* @param DirectoryList $directoryList
*/
public function __construct(
Reader $moduleReader,
DirectoryList $directoryList
) {
$this->moduleReader = $moduleReader;
$this->directoryList = $directoryList;
}
/**
* @param string $path
* @return string
*/
public function execute(string $path): string
{
if (strpos($path, '::') !== false) {
$pathParts = explode('::', $path, 2);
$moduleName = $pathParts[0];
$filePath = $pathParts[1];
$moduleDirectory = $this->moduleReader->getModuleDir('', $moduleName);
return implode(DIRECTORY_SEPARATOR, [
$moduleDirectory,
$filePath
]);
} else {
return implode(DIRECTORY_SEPARATOR, [
$this->directoryList->getRoot(),
$path
]);
}
}
}
\ No newline at end of file
......@@ -27,6 +27,11 @@
<theme_id>3</theme_id>
</custom_design>
</page>
<page key="test.page.3" identifier="test-page-3" active="true" maintained="true">
<title>Page With Images</title>
<content type="file">Firegento_ContentProvisioning::Test/Integration/Model/Config/_files/test-files/content-with-images-1.html</content>
<media_directory>Firegento_ContentProvisioning::Test/Integration/Model/Config/_files/test-files/media</media_directory>
</page>
<block key="test.block.1" identifier="test-block-1" maintained="true" active="true">
<title>Test Block 1</title>
<content><![CDATA[<h2>test foobar Aenean commodo ligula eget dolor aenean massa</h2>]]></content>
......@@ -39,4 +44,9 @@
<store code="admin" />
</stores>
</block>
<block key="test.block.3" identifier="test-block-3" maintained="true" active="true">
<title>Block With Images</title>
<content type="file">Firegento_ContentProvisioning::Test/Integration/Model/Config/_files/test-files/content-with-images-1.html</content>
<media_directory>Firegento_ContentProvisioning::Test/Integration/Model/Config/_files/test-files/media</media_directory>
</block>
</config>
\ No newline at end of file
......@@ -14,6 +14,8 @@ return [
PageEntryInterface::IS_MAINTAINED => true,
PageEntryInterface::STORES => ['admin'],
PageEntryInterface::CONTENT_HEADING => '',
PageEntryInterface::MEDIA_DIRECTORY => null,
PageEntryInterface::MEDIA_FILES => [],
],
'test.page.2' => [
PageEntryInterface::TITLE => 'Title 2',
......@@ -33,6 +35,24 @@ return [
PageEntryInterface::CUSTOM_THEME_TO => '2019-03-29',
PageEntryInterface::CUSTOM_THEME => '3',
PageEntryInterface::CUSTOM_ROOT_TEMPLATE => '3columns',
PageEntryInterface::MEDIA_DIRECTORY => null,
PageEntryInterface::MEDIA_FILES => [],
],
'test.page.3' => [
PageEntryInterface::TITLE => 'Page With Images',
PageEntryInterface::CONTENT => file_get_contents(__DIR__ . '/test-files/content-with-images-1.html'),
PageEntryInterface::KEY => 'test.page.3',
PageEntryInterface::IDENTIFIER => 'test-page-3',
PageEntryInterface::IS_ACTIVE => true,
PageEntryInterface::IS_MAINTAINED => true,
PageEntryInterface::STORES => ['admin'],
PageEntryInterface::CONTENT_HEADING => '',
PageEntryInterface::MEDIA_DIRECTORY => __DIR__ . '/test-files/media',
PageEntryInterface::MEDIA_FILES => [
'image-1.png',
'some-test-image.png',
'foobar/test.png',
],
]
],
'blocks' => [
......@@ -44,6 +64,8 @@ return [
BlockEntryInterface::IS_ACTIVE => true,
BlockEntryInterface::IS_MAINTAINED => true,
BlockEntryInterface::STORES => ['admin'],
BlockEntryInterface::MEDIA_DIRECTORY => null,
BlockEntryInterface::MEDIA_FILES => [],
],
'test.block.2' => [
BlockEntryInterface::TITLE => 'Test Block 2',
......@@ -53,6 +75,23 @@ return [
BlockEntryInterface::IS_ACTIVE => true,
BlockEntryInterface::IS_MAINTAINED => false,
BlockEntryInterface::STORES => ['default', 'admin'],
BlockEntryInterface::MEDIA_DIRECTORY => null,
BlockEntryInterface::MEDIA_FILES => [],
],
'test.block.3' => [
BlockEntryInterface::TITLE => 'Block With Images',
BlockEntryInterface::CONTENT => file_get_contents(__DIR__ . '/test-files/content-with-images-1.html'),
BlockEntryInterface::KEY => 'test.block.3',
BlockEntryInterface::IDENTIFIER => 'test-block-3',
BlockEntryInterface::IS_ACTIVE => true,
BlockEntryInterface::IS_MAINTAINED => true,
BlockEntryInterface::STORES => ['admin'],
BlockEntryInterface::MEDIA_DIRECTORY => __DIR__ . '/test-files/media',
BlockEntryInterface::MEDIA_FILES => [
'image-1.png',
'some-test-image.png',
'foobar/test.png',
],
],
],
];
<h5>Some foobar</h5>
<p>Foobar: <a href="{{media url=&quot;image-1.png&quot;}}">Link zu einem Bilder</a></p>
<p>Dummy content...</p>
<p>Image:&nbsp;<img src="{{media url=&quot;some-test-image.png&quot;}}" alt="fooo" width="300"></p>
<p>Image 2:&nbsp;<img src="{{media url=&quot;foobar/test.png&quot;}}" alt="" width="450" height="150"></p>
\ No newline at end of file
Test/Integration/Model/Config/_files/test-files/media/foobar/test.png

1.21 MiB

Test/Integration/Model/Config/_files/test-files/media/image-1.png

136 KiB

Test/Integration/Model/Config/_files/test-files/media/some-test-image.png

144 KiB

......@@ -30,6 +30,13 @@
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element minOccurs="0" maxOccurs="1" name="media_directory">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element minOccurs="0" maxOccurs="1" type="stores" name="stores" />
<xs:element minOccurs="0" maxOccurs="1" type="seo" name="seo" />
<xs:element minOccurs="0" maxOccurs="1" type="design" name="design" />
......@@ -69,6 +76,13 @@
<xs:choice maxOccurs="unbounded">
<xs:element minOccurs="0" maxOccurs="1" type="stores" name="stores" />
<xs:element minOccurs="1" maxOccurs="1" type="content" name="content" />
<xs:element minOccurs="0" maxOccurs="1" name="media_directory">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element minOccurs="1" maxOccurs="1" type="xs:string" name="title" />
</xs:choice>
<xs:attribute name="key" type="key" use="required" />
......
......@@ -20,6 +20,7 @@
<item name="seo" xsi:type="object">Firegento\ContentProvisioning\Model\Config\Parser\SeoParser</item>
<item name="design" xsi:type="object">Firegento\ContentProvisioning\Model\Config\Parser\DesignParser</item>
<item name="custom_design" xsi:type="object">Firegento\ContentProvisioning\Model\Config\Parser\CustomDesignParser</item>
<item name="media_directory" xsi:type="object">Firegento\ContentProvisioning\Model\Config\Parser\MediaDirectoryParser</item>
</argument>
</arguments>
</virtualType>
......@@ -35,6 +36,7 @@
<item name="meta" xsi:type="object">Firegento\ContentProvisioning\Model\Config\Parser\MetaDataParser</item>
<item name="stores" xsi:type="object">Firegento\ContentProvisioning\Model\Config\Parser\StoresParser</item>
<item name="content" xsi:type="object">Firegento\ContentProvisioning\Virtual\Config\Parser\BlockContentParser</item>
<item name="media_directory" xsi:type="object">Firegento\ContentProvisioning\Model\Config\Parser\MediaDirectoryParser</item>
</argument>
</arguments>
</virtualType>
......
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