Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2019 feb 1: Release Mindarc\Feed and Mindarc\GeoRestriction modules #21

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions app/code/Mindarc/Feed/Config/Converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright © pmk, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Mindarc\Feed\Config;

use Magento\Framework\Config\ConverterInterface;

/**
* Class Converter
* @package Mindarc\Feed\Config
*/
class Converter implements ConverterInterface
{
/**
* Convert dom node tree to array
*
* @param \DOMDocument $source
* @return array
* @throws \InvalidArgumentException
*/
public function convert($source)
{
$output = [];
$xpath = new \DOMXPath($source);
$indexers = $xpath->evaluate('/config/feed');
/** @var $typeNode \DOMNode */
foreach ($indexers as $indexerNode) {
$data = [];
$indexerId = $this->getAttributeValue($indexerNode, 'id');
$data['indexer_id'] = $indexerId;
$data['action_class'] = $this->getAttributeValue($indexerNode, 'class');
$data['feedprefix'] = '';
$data['title'] = '';
$data['description'] = '';

/** @var $childNode \DOMNode */
foreach ($indexerNode->childNodes as $childNode) {
if ($childNode->nodeType != XML_ELEMENT_NODE) {
continue;
}
/** @var $childNode \DOMElement */
$data = $this->convertChild($childNode, $data);
}
$output[$indexerId] = $data;
}

return $output;
}

/**
* Get attribute value
*
* @param \DOMNode $input
* @param string $attributeName
* @param mixed $default
* @return null|string
*/
protected function getAttributeValue(\DOMNode $input, $attributeName, $default = null)
{
$node = $input->attributes->getNamedItem($attributeName);

return $node ? $node->nodeValue : $default;
}

/**
* Convert child from dom to array
*
* @param \DOMElement $childNode
* @param array $data
* @return array
*/
protected function convertChild(\DOMElement $childNode, $data)
{
switch ($childNode->nodeName) {
case 'feedprefix':
$data['feedprefix'] = $childNode->nodeValue;
break;
case 'title':
$data['title'] = $childNode->nodeValue;
break;
case 'description':
$data['description'] = $childNode->nodeValue;
break;
}

return $data;
}
}
31 changes: 31 additions & 0 deletions app/code/Mindarc/Feed/Config/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © pmk, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Mindarc\Feed\Config;

use Magento\Framework\Serialize\SerializerInterface;

/**
* Class Data
* @package Mindarc\Feed\Config
*/
class Data extends \Magento\Framework\Config\Data
{
/**
* Data constructor.
* @param \Magento\Framework\Config\ReaderInterface $reader
* @param \Magento\Framework\Config\CacheInterface $cache
* @param string $cacheId
* @param SerializerInterface|null $serializer
*/
public function __construct(
\Magento\Framework\Config\ReaderInterface $reader,
\Magento\Framework\Config\CacheInterface $cache,
$cacheId = 'feed_config',
SerializerInterface $serializer = null
) {
parent::__construct($reader, $cache, $cacheId, $serializer);
}
}
47 changes: 47 additions & 0 deletions app/code/Mindarc/Feed/Config/SchemaLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © pmk, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Mindarc\Feed\Config;

use Magento\Framework\Config\SchemaLocatorInterface;

/**
* Class SchemaLocator
* @package Mindarc\Feed\Config
*/
class SchemaLocator implements SchemaLocatorInterface
{
/**
* @var \Magento\Framework\Config\Dom\UrnResolver
*/
protected $urnResolver;

/**
*/
public function __construct(\Magento\Framework\Config\Dom\UrnResolver $urnResolver)
{
$this->urnResolver = $urnResolver;
}

/**
* Get path to merged config schema
*
* @return string|null
*/
public function getSchema()
{
return $this->urnResolver->getRealPath('urn:magento:module:Mindarc_Feed:etc/feed.xsd');
}

/**
* Get path to pre file validation schema
*
* @return string|null
*/
public function getPerFileSchema()
{
return $this->urnResolver->getRealPath('urn:magento:module:Mindarc_Feed:etc/feed.xsd');
}
}
120 changes: 120 additions & 0 deletions app/code/Mindarc/Feed/Console/Command/FeedGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Copyright © pmk, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Mindarc\Feed\Console\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class FeedGenerator
* @package Mindarc\Feed\Console\Command
*/
class FeedGenerator extends \Symfony\Component\Console\Command\Command
{
const INPUT_KEY_FEEDS = 'feed';

/**
* @var \Mindarc\Feed\FeedAdapter\GeneratorFactory
*/
protected $generatorFactory;

/**
* @var \Magento\Framework\App\State
*/
protected $appState;

/**
* @var \Mindarc\Feed\Config\Data
*/
protected $config;

/**
* FeedGenerator constructor.
* @param \Mindarc\Feed\FeedAdapter\GeneratorFactory $generatorFactory
* @param \Magento\Framework\App\State $appState
* @param \Mindarc\Feed\Config\Data $config
*/
public function __construct(
\Mindarc\Feed\FeedAdapter\GeneratorFactory $generatorFactory,
\Magento\Framework\App\State $appState,
\Mindarc\Feed\Config\Data $config
) {
$this->generatorFactory = $generatorFactory;
$this->appState = $appState;
$this->config = $config;
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('mindarc:feed')
->setDescription('Generates the feeds.')
->setDefinition($this->getInputList());
}

/**
* @return array
*/
public function getInputList() : array
{
return [
new InputArgument(
self::INPUT_KEY_FEEDS,
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'Add feed name.'
),
];
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return null
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
foreach ($this->getFeedsArgs($input) as $feedId) {
$feedConfig = $this->config->get($feedId);
if (!empty($feedConfig)) {
$feedGenerator = $this->getFeedGenerator($feedConfig['action_class']);
$feedGenerator->generate($feedConfig['feedprefix']);
} else {
throw new \InvalidArgumentException(
"The requested feed types are not supported: '"
);
}
}
}

/**
* @param InputInterface $input
* @return array
*/
protected function getFeedsArgs(InputInterface $input) : array
{
$feedTypes = [];
if ($input->getArgument(self::INPUT_KEY_FEEDS)) {
$requestedTypes = $input->getArgument(self::INPUT_KEY_FEEDS);
$feedTypes = array_filter(array_map('trim', $requestedTypes), 'strlen');
}

return $feedTypes;
}

/**
* @param string $class
* @return \Mindarc\Feed\FeedAdapter\GeneratorInterface
*/
protected function getFeedGenerator(string $class) : \Mindarc\Feed\FeedAdapter\GeneratorInterface
{
return $this->generatorFactory->create($class);
}
}
66 changes: 66 additions & 0 deletions app/code/Mindarc/Feed/Cron/FeedGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © pmk, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Mindarc\Feed\Cron;

/**
* Class FeedGenerator
* @package Mindarc\Feed\Console\Command
*/
class FeedGenerator
{
/**
* @var \Mindarc\Feed\FeedAdapter\GeneratorFactory
*/
protected $generatorFactory;

/**
* @var \Magento\Framework\App\State
*/
protected $appState;

/**
* @var \Mindarc\Feed\Config\Data
*/
protected $config;

/**
* GoogleFeedGenerator constructor.
* @param \Mindarc\Feed\FeedAdapter\GeneratorFactory $generatorFactory
* @param \Magento\Framework\App\State $appState
* @param \Mindarc\Feed\Config\Data $config
*/
public function __construct(
\Mindarc\Feed\FeedAdapter\GeneratorFactory $generatorFactory,
\Magento\Framework\App\State $appState,
\Mindarc\Feed\Config\Data $config
) {
$this->generatorFactory = $generatorFactory;
$this->appState = $appState;
$this->config = $config;
}

/**
* @return null
*/
protected function execute()
{
$this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
$feedConfigs = $this->config->get();
foreach ($feedConfigs as $feedConfig){
$feedGenerator = $this->getFeedGenerator($feedConfig['action_class']);
$feedGenerator->generate($feedConfig['feedprefix']);
}
}

/**
* @param string $class
* @return \Mindarc\Feed\FeedAdapter\GeneratorInterface
*/
protected function getFeedGenerator(string $class) : \Mindarc\Feed\FeedAdapter\GeneratorInterface
{
return $this->generatorFactory->create($class);
}
}
Loading