Skip to content

Commit

Permalink
PV-5: Code refactoring for merge xsd schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Kologrivov committed Sep 28, 2015
1 parent 36ddf4f commit f3a12f2
Show file tree
Hide file tree
Showing 24 changed files with 621 additions and 161 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Block/Product/View/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function isMainImage($image)
*/
public function getImageAttribute($imageId, $attributeName, $default = null)
{
$attributes = $this->getConfigView()->getImageAttributes('Magento_Catalog', $imageId);
$attributes = $this->getConfigView()->getMediaAttributes('Magento_Catalog', 'images', $imageId);
return isset($attributes[$attributeName]) ? $attributes[$attributeName] : $default;
}

Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function init($product, $imageId, $attributes = [])

$this->attributes = array_merge(
$attributes,
$this->getConfigView()->getImageAttributes('Magento_Catalog', $imageId)
$this->getConfigView()->getMediaAttributes('Magento_Catalog', 'images', $imageId)
);

$this->setProduct($product);
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Model/Product/Image/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function getData()
'area' => Area::AREA_FRONTEND,
'themeModel' => $theme,
]);
$images = $config->getImages('Magento_Catalog');
$images = $config->getMediaEntities('Magento_Catalog', 'images');
foreach ($images as $imageId => $imageData) {
$this->data[$theme->getCode() . $imageId] = array_merge(['id' => $imageId], $imageData);
}
Expand Down
29 changes: 26 additions & 3 deletions app/code/Magento/ProductVideo/Helper/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class Media extends \Magento\Framework\App\Helper\AbstractHelper
*/
const NODE_CONFIG_VIDEO_AUTO_RESTART = 'video_auto_restart';

/**
* Media type
*/
const MEDIA_TYPE = "videos";

/**
* @var ConfigInterface
*/
Expand Down Expand Up @@ -91,10 +96,16 @@ protected function initConfig()
*/
public function getPlayIfBaseAttribute()
{
return $this->cachedVideoConfig->getVideoAttributeValue(
$videoAttributes = $this->cachedVideoConfig->getMediaAttributes(
self::MODULE_NAME,
self::MEDIA_TYPE,
self::NODE_CONFIG_PLAY_IF_BASE
);
if (isset($videoAttributes[self::NODE_CONFIG_PLAY_IF_BASE])) {
return $videoAttributes[self::NODE_CONFIG_PLAY_IF_BASE];
} else {
return null;
}
}

/**
Expand All @@ -104,10 +115,16 @@ public function getPlayIfBaseAttribute()
*/
public function getShowRelatedAttribute()
{
return $this->cachedVideoConfig->getVideoAttributeValue(
$videoAttributes = $this->cachedVideoConfig->getMediaAttributes(
self::MODULE_NAME,
self::MEDIA_TYPE,
self::NODE_CONFIG_SHOW_RELATED
);
if (isset($videoAttributes[self::NODE_CONFIG_SHOW_RELATED])) {
return $videoAttributes[self::NODE_CONFIG_SHOW_RELATED];
} else {
return null;
}
}

/**
Expand All @@ -117,9 +134,15 @@ public function getShowRelatedAttribute()
*/
public function getVideoAutoRestartAttribute()
{
return $this->cachedVideoConfig->getVideoAttributeValue(
$videoAttributes = $this->cachedVideoConfig->getMediaAttributes(
self::MODULE_NAME,
self::MEDIA_TYPE,
self::NODE_CONFIG_VIDEO_AUTO_RESTART
);
if (isset($videoAttributes[self::NODE_CONFIG_VIDEO_AUTO_RESTART])) {
return $videoAttributes[self::NODE_CONFIG_VIDEO_AUTO_RESTART];
} else {
return null;
}
}
}
34 changes: 34 additions & 0 deletions app/code/Magento/ProductVideo/Model/VideoExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ProductVideo\Model;

class VideoExtractor implements \Magento\Framework\Config\Reader\Xsd\MediaTypeDataExtractorInterface
{
/**
* Extract configuration data of videos from the DOM structure
*
* @param \DOMElement $childNode
* @return array
*/
public function process(\DOMElement $childNode)
{
$result = [];
$moduleName = $childNode->getAttribute('module');
/** @var \DOMElement $node */
foreach ($childNode->getElementsByTagName('video') as $node) {
$imageId = $node->getAttribute('id');
$result[$childNode->tagName][$moduleName][$imageId]['type'] = $node->getAttribute('type');
foreach ($node->childNodes as $attribute) {
if ($attribute->nodeType != XML_ELEMENT_NODE) {
continue;
}
$nodeValue = $attribute->nodeValue;
$result[$childNode->tagName][$moduleName][$imageId][$attribute->tagName] = $nodeValue;
}
}
return $result;
}
}
26 changes: 26 additions & 0 deletions app/code/Magento/ProductVideo/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@
</argument>
</arguments>
</type>
<type name="Magento\Framework\Config\View">
<arguments>
<argument name="xpath" xsi:type="array">
<item name="video" xsi:type="array">
<item name="0" xsi:type="array">
<item name="path" xsi:type="string">/view/videos</item>
<item name="id" xsi:type="string">module</item>
</item>
<item name="1" xsi:type="array">
<item name="path" xsi:type="string">/view/videos/video</item>
<item name="id" xsi:type="array">
<item name="id1" xsi:type="string">id</item>
<item name="id2" xsi:type="string">type</item>
</item>
</item>
</item>
</argument>
</arguments>
</type>
<type name="Magento\Framework\Config\Reader\Xsd\MediaTypeDataExtractorPool">
<arguments>
<argument name="extractors" xsi:type="array">
<item name="videos" xsi:type="string">Magento\ProductVideo\Model\VideoExtractor</item>
</argument>
</arguments>
</type>
<type name="Magento\Catalog\Model\Product\Attribute\Backend\Media">
<plugin name="external_video_media_entry_processor" type="Magento\ProductVideo\Model\Plugin\ExternalVideoEntryProcessor" />
</type>
Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/ProductVideo/etc/view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* See COPYING.txt for license details.
*/
-->
<view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Config/etc/view.xsd">
<media module="Magento_ProductVideo">
<view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="view.xsd">
<videos module="Magento_ProductVideo">
<video id="play_if_base" type="play_if_base">
<play_if_base>1</play_if_base>
</video>
Expand All @@ -16,5 +16,5 @@
<video id="video_auto_restart" type="video_auto_restart">
<video_auto_restart>0</video_auto_restart>
</video>
</media>
</videos>
</view>
40 changes: 40 additions & 0 deletions app/code/Magento/ProductVideo/etc/view.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="view">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="videos" minOccurs="0" type="videosType"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="videosType">
<xs:sequence>
<xs:element name="video" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="play_if_base" type="xs:boolean" minOccurs="0"/>
<xs:element name="show_related" type="xs:boolean" minOccurs="0"/>
<xs:element name="video_auto_restart" type="xs:boolean" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="type" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="play_if_base"/>
<xs:enumeration value="show_related"/>
<xs:enumeration value="video_auto_restart"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="module" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
2 changes: 1 addition & 1 deletion app/code/Magento/Swatches/Helper/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function getImageConfig()
]);
$imageConfig = array_merge(
$imageConfig,
$config->getImages('Magento_Catalog')
$config->getMediaEntities('Magento_Catalog', 'images')
);
}
return $imageConfig;
Expand Down
35 changes: 35 additions & 0 deletions app/code/Magento/Swatches/Model/ImageExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Swatches\Model;


class ImageExtractor implements \Magento\Framework\Config\Reader\Xsd\MediaTypeDataExtractorInterface
{
/**
* Extract configuration data of images from the DOM structure
*
* @param \DOMElement $childNode
* @return array
*/
public function process(\DOMElement $childNode)
{
$result = [];
$moduleName = $childNode->getAttribute('module');
/** @var \DOMElement $node */
foreach ($childNode->getElementsByTagName('image') as $node) {
$imageId = $node->getAttribute('id');
$result[$childNode->tagName][$moduleName][$imageId]['type'] = $node->getAttribute('type');
foreach ($node->childNodes as $attribute) {
if ($attribute->nodeType != XML_ELEMENT_NODE) {
continue;
}
$nodeValue = $attribute->nodeValue;
$result[$childNode->tagName][$moduleName][$imageId][$attribute->tagName] = $nodeValue;
}
}
return $result;
}
}
26 changes: 26 additions & 0 deletions app/code/Magento/Swatches/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,30 @@
</argument>
</arguments>
</type>
<type name="Magento\Framework\Config\View">
<arguments>
<argument name="xpath" xsi:type="array">
<item name="image" xsi:type="array">
<item name="0" xsi:type="array">
<item name="path" xsi:type="string">/view/images</item>
<item name="id" xsi:type="string">module</item>
</item>
<item name="1" xsi:type="array">
<item name="path" xsi:type="string">/view/images/image</item>
<item name="id" xsi:type="array">
<item name="id1" xsi:type="string">id</item>
<item name="id2" xsi:type="string">type</item>
</item>
</item>
</item>
</argument>
</arguments>
</type>
<type name="Magento\Framework\Config\Reader\Xsd\MediaTypeDataExtractorPool">
<arguments>
<argument name="extractors" xsi:type="array">
<item name="images" xsi:type="string">Magento\Swatches\Model\ImageExtractor</item>
</argument>
</arguments>
</type>
</config>
6 changes: 3 additions & 3 deletions app/code/Magento/Swatches/etc/view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* See COPYING.txt for license details.
*/
-->
<view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Config/etc/view.xsd">
<media module="Magento_Catalog">
<view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="view.xsd">
<images module="Magento_Catalog">
<image id="swatch_image" type="swatch_image">
<width>30</width>
<height>20</height>
Expand All @@ -23,5 +23,5 @@
<width>110</width>
<height>90</height>
</image>
</media>
</images>
</view>
52 changes: 52 additions & 0 deletions app/code/Magento/Swatches/etc/view.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="view">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="images" minOccurs="0" type="imagesType"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="imagesType">
<xs:sequence>
<xs:element name="image" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="width" type="xs:positiveInteger" minOccurs="0"/>
<xs:element name="height" type="xs:positiveInteger" minOccurs="0"/>
<xs:element name="constrain" type="xs:boolean" minOccurs="0"/>
<xs:element name="aspect_ratio" type="xs:boolean" minOccurs="0"/>
<xs:element name="frame" type="xs:boolean" minOccurs="0"/>
<xs:element name="transparency" type="xs:boolean" minOccurs="0"/>
<xs:element name="background" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\[(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="type" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="thumbnail"/>
<xs:enumeration value="small_image"/>
<xs:enumeration value="image"/>
<xs:enumeration value="swatch_image"/>
<xs:enumeration value="swatch_thumb"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="module" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
4 changes: 2 additions & 2 deletions app/design/adminhtml/Magento/backend/etc/view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<vars module="Js_Bundle">
<var name="bundle_size">1MB</var>
</vars>
<media module="Magento_Catalog">
<images module="Magento_Catalog">
<image id="product_listing_thumbnail" type="thumbnail">
<width>75</width>
<height>75</height>
Expand All @@ -19,7 +19,7 @@
<width>75</width>
<height>75</height>
</image>
</media>
</images>
<exclude>
<item type="file">Lib::mage/common.js</item>
<item type="file">Lib::mage/cookies.js</item>
Expand Down
Loading

0 comments on commit f3a12f2

Please sign in to comment.