Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Muradov committed May 20, 2022
0 parents commit f4d53f7
Show file tree
Hide file tree
Showing 30 changed files with 2,883 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Api/Config/ConfigClass/ArgumentInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Amasty\ImportExportCore\Api\Config\ConfigClass;

interface ArgumentInterface
{
/**
* @return string
*/
public function getName();

/**
* @param string $name
*
* @return void
*/
public function setName($name);

/**
* @return string
*/
public function getType();

/**
* @param string $type
*
* @return void
*/
public function setType($type);

/**
* @return string
*/
public function getValue();

/**
* @param string $value
*
* @return void
*/
public function setValue($value);

/**
* @return \Amasty\ImportExportCore\Api\Config\ConfigClass\ArgumentInterface[]
*/
public function getItems();

/**
* @param \Amasty\ImportExportCore\Api\Config\ConfigClass\ArgumentInterface[] $items
*
* @return void
*/
public function setItems($items);
}
16 changes: 16 additions & 0 deletions Api/Config/ConfigClass/ConfigClassInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Amasty\ImportExportCore\Api\Config\ConfigClass;

interface ConfigClassInterface
{
/**
* @return string
*/
public function getName(): string;

/**
* @return \Amasty\ImportExportCore\Api\Config\ConfigClass\ArgumentInterface[]
*/
public function getArguments(): ?array;
}
56 changes: 56 additions & 0 deletions Config/ConfigClass/Argument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Amasty\ImportExportCore\Config\ConfigClass;

use Amasty\ImportExportCore\Api\Config\ConfigClass\ArgumentInterface;
use Magento\Framework\DataObject;

class Argument extends DataObject implements ArgumentInterface
{
public const NAME = 'name';
public const VALUE = 'value';
public const TYPE = 'type';
public const ITEMS = 'items';

public function getName()
{
return $this->getData(self::NAME);
}

public function setName($name)
{
$this->setData(self::NAME, $name);
}

public function getType()
{
return $this->getData(self::TYPE);
}

public function setType($type)
{
$this->setData(self::TYPE, $type);
}

public function getValue()
{
return $this->getData(self::VALUE);
}

public function setValue($value)
{
$this->setData(self::VALUE, $value);
}

public function getItems()
{
return $this->getData(self::ITEMS);
}

public function setItems($items)
{
$this->setData(self::ITEMS, $items);
}
}
45 changes: 45 additions & 0 deletions Config/ConfigClass/ConfigClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Amasty\ImportExportCore\Config\ConfigClass;

use Amasty\ImportExportCore\Api\Config\ConfigClass\ConfigClassInterface;

class ConfigClass implements ConfigClassInterface
{
/**
* @var string
*/
private $name;

/**
* @var array
*/
private $arguments = [];

public function __construct(
string $name,
?string $baseType = '',
?array $arguments = []
) {
if (!empty($baseType) && !is_subclass_of($name, $baseType)) {
throw new \LogicException(
'Class ' . $name . ' doesn\'t implement ' . $baseType
);
}

$this->name = $name;
$this->arguments = $arguments;
}

public function getName(): string
{
return $this->name;
}

public function getArguments(): ?array
{
return $this->arguments;
}
}
86 changes: 86 additions & 0 deletions Config/ConfigClass/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Amasty\ImportExportCore\Config\ConfigClass;

use Amasty\ImportExportCore\Api\Config\ConfigClass\ArgumentInterface;
use Amasty\ImportExportCore\Api\Config\ConfigClass\ConfigClassInterface;
use Magento\Framework\Data\Argument\InterpreterInterface;
use Magento\Framework\ObjectManagerInterface;

class Factory
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var InterpreterInterface
*/
private $argumentInterpreter;

public function __construct(
ObjectManagerInterface $objectManager,
InterpreterInterface $argumentInterpreter
) {
$this->objectManager = $objectManager;
$this->argumentInterpreter = $argumentInterpreter;
}

/**
* Creates instance based on specified config class
*
* @param ConfigClassInterface $configClass
* @return mixed
*/
public function createObject(ConfigClassInterface $configClass)
{
return $this->objectManager->create(
$configClass->getName(),
[
'config' => $this->prepareArguments(
$this->convertArguments($configClass->getArguments())
)
]
);
}

private function prepareArguments(array $arguments): array
{
$result = [];
foreach ($arguments as $key => $argument) {
$result[$key] = $this->argumentInterpreter->evaluate($argument);
}

return $result;
}

/**
* @param ArgumentInterface[] $arguments
* @return array
*/
private function convertArguments(array $arguments): array
{
$result = [];
foreach ($arguments as $argument) {
$argName = $argument->getName();
$argType = $argument->getType();

$row = [
'name' => $argName,
'xsi:type' => $argType
];
if ($argType === 'array') {
$row['item'] = $this->convertArguments($argument->getItems());
} else {
$row['value'] = $argument->getValue();
}

$result[$argName] = $row;
}

return $result;
}
}
26 changes: 26 additions & 0 deletions Config/SchemaReader/ConfigCompiler/IncludeElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Amasty\ImportExportCore\Config\SchemaReader\ConfigCompiler;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Module\Dir;

class IncludeElement extends \Magento\Config\Model\Config\Compiler\IncludeElement
{
protected function getContent($includePath)
{
list($moduleName, $filename) = explode('::', $includePath);

$directoryRead = $this->readFactory->create(
$this->moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, $moduleName)
);

if ($directoryRead->isExist($filename) && $directoryRead->isFile($filename)) {
return $directoryRead->readFile($filename);
}

throw new LocalizedException(__('The file "%1" does not exist', $filename));
}
}
Loading

0 comments on commit f4d53f7

Please sign in to comment.