Skip to content

Commit 5d9d688

Browse files
committed
Intorduced media_content config
1 parent 2b5975c commit 5d9d688

File tree

11 files changed

+312
-27
lines changed

11 files changed

+312
-27
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContent\Model\Content;
9+
10+
use Magento\Framework\Config\DataInterface;
11+
12+
/**
13+
* Media content config
14+
*/
15+
class Config
16+
{
17+
/**
18+
* @var DataInterface
19+
*/
20+
private $data;
21+
22+
/**
23+
* @param DataInterface $data
24+
*/
25+
public function __construct(DataInterface $data)
26+
{
27+
$this->data = $data;
28+
}
29+
30+
/**
31+
* Get config value by key.
32+
*
33+
* @param string|null $key
34+
* @param string|null $default
35+
* @return array
36+
*/
37+
public function get($key = null, $default = null)
38+
{
39+
return $this->data->get($key, $default);
40+
}
41+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContent\Model\Content\Config;
9+
10+
use Magento\Framework\Config\ConverterInterface;
11+
12+
/**
13+
* Class Converter
14+
*/
15+
class Converter implements ConverterInterface
16+
{
17+
/**
18+
* Blacklist tag name
19+
*/
20+
private const BLACKLIST_TAG_NAME = 'search';
21+
22+
/**
23+
* Patterns tag name
24+
*/
25+
private const PATTERNS_TAG_NAME = 'patterns';
26+
27+
/**
28+
* Pattern tag name
29+
*/
30+
private const PATTERN_TAG_NAME = 'pattern';
31+
32+
/**
33+
* Convert dom node to array
34+
*
35+
* @param \DOMDocument $source
36+
* @return array
37+
*/
38+
public function convert($source) : array
39+
{
40+
$result = [];
41+
42+
if (!$source instanceof \DOMDocument) {
43+
return $result;
44+
}
45+
46+
foreach ($source->getElementsByTagName(self::BLACKLIST_TAG_NAME) as $blacklist) {
47+
$result[self::BLACKLIST_TAG_NAME] = [];
48+
foreach ($blacklist->getElementsByTagName(self::PATTERNS_TAG_NAME) as $patterns) {
49+
$result[self::BLACKLIST_TAG_NAME][self::PATTERNS_TAG_NAME] = [];
50+
foreach ($patterns->getElementsByTagName(self::PATTERN_TAG_NAME) as $pattern) {
51+
$result[self::BLACKLIST_TAG_NAME][self::PATTERNS_TAG_NAME]
52+
[$pattern->attributes->getNamedItem('name')->nodeValue] = $pattern->nodeValue;
53+
}
54+
}
55+
}
56+
57+
return $result;
58+
}
59+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContent\Model\Content\Config;
9+
10+
use Magento\Framework\App\Area;
11+
use Magento\Framework\Config\Dom;
12+
use Magento\Framework\Config\FileResolverInterface;
13+
use Magento\Framework\Config\Reader\Filesystem;
14+
use Magento\Framework\Config\ReaderInterface;
15+
use Magento\Framework\Config\ValidationStateInterface;
16+
17+
/**
18+
* Media content config reader
19+
*/
20+
class Reader extends Filesystem implements ReaderInterface
21+
{
22+
/**
23+
* List of id attributes for merge
24+
*
25+
* @var array
26+
*/
27+
protected $_idAttributes = [
28+
'/config/patterns' => 'patterns',
29+
'/config/patterns/pattern' => 'name',
30+
];
31+
32+
/**
33+
* XML Configuration file name
34+
*/
35+
private const XML_FILE_NAME = 'media_content.xml';
36+
37+
/**
38+
* @param FileResolverInterface $fileResolver
39+
* @param Converter $converter
40+
* @param SchemaLocator $schemaLocator
41+
* @param ValidationStateInterface $validationState
42+
* @param string $fileName
43+
* @param array $idAttributes
44+
* @param string $domDocumentClass
45+
* @param string $defaultScope
46+
*/
47+
public function __construct(
48+
FileResolverInterface $fileResolver,
49+
Converter $converter,
50+
SchemaLocator $schemaLocator,
51+
ValidationStateInterface $validationState,
52+
$fileName = self::XML_FILE_NAME,
53+
$idAttributes = [],
54+
$domDocumentClass = Dom::class,
55+
$defaultScope = Area::AREA_GLOBAL
56+
) {
57+
parent::__construct(
58+
$fileResolver,
59+
$converter,
60+
$schemaLocator,
61+
$validationState,
62+
$fileName,
63+
$idAttributes,
64+
$domDocumentClass,
65+
$defaultScope
66+
);
67+
}
68+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContent\Model\Content\Config;
9+
10+
use Magento\Framework\Config\SchemaLocatorInterface;
11+
use Magento\Framework\Module\Dir;
12+
use Magento\Framework\Module\Dir\Reader;
13+
14+
class SchemaLocator implements SchemaLocatorInterface
15+
{
16+
/**
17+
* Path to corresponding XSD file with validation rules for both individual and merged configs
18+
*
19+
* @var string
20+
*/
21+
private $schema;
22+
23+
/**
24+
* @param Reader $moduleReader
25+
*/
26+
public function __construct(Reader $moduleReader)
27+
{
28+
$this->schema = $moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, 'Magento_MediaContent') . '/media_content.xsd';
29+
}
30+
31+
/**
32+
* Get path to merged config schema
33+
*
34+
* @return string|null
35+
*/
36+
public function getSchema()
37+
{
38+
return $this->schema;
39+
}
40+
41+
/**
42+
* Get path to per file validation schema
43+
*
44+
* @return string|null
45+
*/
46+
public function getPerFileSchema()
47+
{
48+
return $this->schema;
49+
}
50+
}

app/code/Magento/MediaContentApi/Model/ExtractAssetsFromContent.php renamed to app/code/Magento/MediaContent/Model/ExtractAssetsFromContent.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,23 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\MediaContentApi\Model;
8+
namespace Magento\MediaContent\Model;
99

10+
use Magento\MediaContent\Model\Content\Config;
1011
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;
1112
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
1213
use Magento\MediaGalleryApi\Model\Asset\Command\GetByPathInterface;
1314
use Psr\Log\LoggerInterface;
1415

1516
/**
1617
* Used for extracting media asset list from a media content by the search pattern.
17-
*
18-
* This class should be used for DI configuration only, please use the interface in the code
19-
*
20-
* @api
2118
*/
2219
class ExtractAssetsFromContent implements ExtractAssetsFromContentInterface
2320
{
2421
/**
25-
* @var string
22+
* @var Config
2623
*/
27-
private $searchPatterns;
24+
private $config;
2825

2926
/**
3027
* @var GetByPathInterface
@@ -37,18 +34,18 @@ class ExtractAssetsFromContent implements ExtractAssetsFromContentInterface
3734
private $logger;
3835

3936
/**
37+
* @param Config $config
4038
* @param GetByPathInterface $getMediaAssetByPath
4139
* @param LoggerInterface $logger
42-
* @param array $searchPatterns
4340
*/
4441
public function __construct(
42+
Config $config,
4543
GetByPathInterface $getMediaAssetByPath,
46-
LoggerInterface $logger,
47-
array $searchPatterns
44+
LoggerInterface $logger
4845
) {
46+
$this->config = $config;
4947
$this->getMediaAssetByPath = $getMediaAssetByPath;
5048
$this->logger = $logger;
51-
$this->searchPatterns = $searchPatterns;
5249
}
5350

5451
/**
@@ -61,7 +58,7 @@ public function execute(string $content): array
6158
{
6259
$paths = [];
6360

64-
foreach ($this->searchPatterns as $pattern) {
61+
foreach ($this->config->get('search/patterns') as $pattern) {
6562
if (empty($pattern)) {
6663
continue;
6764
}

app/code/Magento/MediaContent/etc/di.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<preference for="Magento\MediaContentApi\Api\UnassignAssetsInterface" type="Magento\MediaContent\Model\UnassignAssets"/>
1111
<preference for="Magento\MediaContentApi\Api\GetAssetIdsUsedInContentInterface" type="Magento\MediaContent\Model\GetAssetIdsUsedInContent"/>
1212
<preference for="Magento\MediaContentApi\Api\GetContentWithAssetsInterface" type="Magento\MediaContent\Model\GetContentWithAssets"/>
13-
<preference for="Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface" type="Magento\MediaContentApi\Model\ExtractAssetsFromContent"/>
13+
<preference for="Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface" type="Magento\MediaContent\Model\ExtractAssetsFromContent"/>
1414
<preference for="Magento\MediaContentApi\Api\UpdateRelationsInterface" type="Magento\MediaContent\Model\UpdateRelations"/>
1515
<preference for="Magento\MediaContentApi\Api\Data\ContentIdentityInterface" type="Magento\MediaContent\Model\Content"/>
1616
<type name="Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface">
@@ -19,4 +19,22 @@
1919
<type name="Magento\MediaGalleryApi\Model\Asset\Command\DeleteByDirectoryPathInterface">
2020
<plugin name="remove_media_content_after_asset_is_removed_by_directory_path" type="Magento\MediaContent\Model\Plugin\MediaGalleryAssetDeleteByDirectoryPath" />
2121
</type>
22+
<type name="Magento\MediaContent\Model\Content\Config\Reader">
23+
<arguments>
24+
<argument name="fileName" xsi:type="string">media_content.xml</argument>
25+
<argument name="converter" xsi:type="object">Magento\MediaContent\Model\Content\Config\Converter</argument>
26+
<argument name="schemaLocator" xsi:type="object">Magento\MediaContent\Model\Content\Config\SchemaLocator</argument>
27+
</arguments>
28+
</type>
29+
<virtualType name="Magento\MediaContent\Model\Directory\Config\Data" type="Magento\Framework\Config\Data">
30+
<arguments>
31+
<argument name="reader" xsi:type="object">Magento\MediaContent\Model\Content\Config\Reader</argument>
32+
<argument name="cacheId" xsi:type="string">Media_Content_Patterns_CacheId</argument>
33+
</arguments>
34+
</virtualType>
35+
<type name="Magento\MediaContent\Model\Content\Config">
36+
<arguments>
37+
<argument name="data" xsi:type="object">Magento\MediaContent\Model\Content\Config\Data</argument>
38+
</arguments>
39+
</type>
2240
</config>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
9+
<xs:element name="config" type="configType">
10+
</xs:element>
11+
12+
<xs:complexType name="configType">
13+
<xs:sequence>
14+
<xs:element type="searchType" name="search" maxOccurs="unbounded" minOccurs="1"/>
15+
</xs:sequence>
16+
</xs:complexType>
17+
18+
<xs:complexType name="searchType">
19+
<xs:sequence>
20+
<xs:element type="patternsType" name="patterns" maxOccurs="unbounded" minOccurs="0"/>
21+
</xs:sequence>
22+
</xs:complexType>
23+
24+
<xs:complexType name="patternsType">
25+
<xs:sequence>
26+
<xs:element type="patternType" name="pattern" maxOccurs="unbounded" minOccurs="0"/>
27+
</xs:sequence>
28+
</xs:complexType>
29+
30+
<xs:complexType name="patternType">
31+
<xs:simpleContent>
32+
<xs:extension base="xs:string">
33+
<xs:attribute type="xs:string" name="name" use="required"/>
34+
<xs:attribute type="xs:string" name="example" use="optional"/>
35+
</xs:extension>
36+
</xs:simpleContent>
37+
</xs:complexType>
38+
</xs:schema>

app/code/Magento/MediaContentCatalog/etc/di.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,4 @@
2222
</argument>
2323
</arguments>
2424
</type>
25-
<type name="Magento\MediaContentApi\Model\ExtractAssetsFromContent">
26-
<arguments>
27-
<argument name="searchPatterns" xsi:type="array">
28-
<item name="catalog_image" xsi:type="string">/^\/?media\/(.*)/</item>
29-
</argument>
30-
</arguments>
31-
</type>
3225
</config>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_MediaGallery:etc/directory.xsd">
9+
<search>
10+
<patterns>
11+
<pattern name="catalog_image">/^\/?media\/(.*)/</pattern>
12+
</patterns>
13+
</search>
14+
</config>

app/code/Magento/MediaContentCms/etc/di.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,4 @@
2020
</argument>
2121
</arguments>
2222
</type>
23-
<type name="Magento\MediaContentApi\Model\ExtractAssetsFromContent">
24-
<arguments>
25-
<argument name="searchPatterns" xsi:type="array">
26-
<item name="media_gallery" xsi:type="string">/{{media url="?(.*?)"?}}/</item>
27-
</argument>
28-
</arguments>
29-
</type>
3023
</config>

0 commit comments

Comments
 (0)