Skip to content

Commit e6c46ae

Browse files
committed
build: port prototyped
1 parent b115014 commit e6c46ae

14 files changed

+726
-0
lines changed

Setup/Migration.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php declare(strict_types=1);
2+
/** Copyright © Discorgento. All rights reserved. */
3+
4+
namespace Discorgento\Migrations\Setup;
5+
6+
use Magento\Framework\DB\Adapter\AdapterInterface;
7+
use Magento\Framework\Setup\Patch\DataPatchInterface;
8+
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
9+
10+
abstract class Migration implements DataPatchInterface, PatchRevertableInterface
11+
{
12+
/** @var Migration\Context */
13+
protected $context;
14+
15+
/**
16+
* DO NOT modify this constructor, if need add common new dependencies
17+
* to the Discorgento\Migrations\Setup\Migration\Context::class instead,
18+
* or specific dependencies to your concrete migration class.
19+
* (this prevents already existant custom class that extends this one from breaking)
20+
*/
21+
public function __construct(Migration\Context $context)
22+
{
23+
$this->context = $context;
24+
}
25+
26+
/**
27+
* Your migration logic goes here
28+
*/
29+
abstract protected function execute();
30+
31+
/**
32+
* Undo/revert the migration (optional)
33+
*/
34+
protected function rollback()
35+
{
36+
// optional, override to implement an undo feature in your migration
37+
}
38+
39+
/** @inheritDoc */
40+
final public function apply()
41+
{
42+
$this->getConnection()->startSetup();
43+
44+
try {
45+
$this->context->state->setAreaCode('adminhtml');
46+
} catch (\Throwable $e) {
47+
// area code already set
48+
}
49+
50+
$this->execute();
51+
$this->getConnection()->endSetup();
52+
}
53+
54+
/** @inheritDoc */
55+
final public function revert()
56+
{
57+
$this->getConnection()->startSetup();
58+
$this->rollback();
59+
$this->getConnection()->endSetup();
60+
}
61+
62+
/**
63+
* Shorthand for getting the database connection
64+
* @return AdapterInterface
65+
*/
66+
final protected function getConnection()
67+
{
68+
return $this->context->moduleDataSetup->getConnection();
69+
}
70+
71+
/**
72+
* Get given table name in database
73+
* (including prefix and etc)
74+
*
75+
* @param $rawName
76+
* @return string
77+
*/
78+
final protected function getTableName($rawName)
79+
{
80+
return $this->context->moduleDataSetup->getTable($rawName);
81+
}
82+
83+
/** @inheritdoc */
84+
public function getAliases()
85+
{
86+
return [];
87+
}
88+
89+
/** @inheritdoc */
90+
public static function getDependencies()
91+
{
92+
return [];
93+
}
94+
}

Setup/Migration/Context.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
/** Copyright © Discorgento. All rights reserved. */
3+
4+
namespace Discorgento\Migrations\Setup\Migration;
5+
6+
use Magento\Framework\App\State;
7+
use Magento\Framework\Setup\ModuleDataSetupInterface as ModuleDataSetup;
8+
use Psr\Log\LoggerInterface as Logger;
9+
10+
final class Context
11+
{
12+
public ModuleDataSetup $moduleDataSetup;
13+
public Logger $logger;
14+
public State $state;
15+
16+
public function __construct(
17+
ModuleDataSetup $moduleDataSetup,
18+
Logger $logger,
19+
State $state
20+
) {
21+
$this->moduleDataSetup = $moduleDataSetup;
22+
$this->logger = $logger;
23+
$this->state = $state;
24+
}
25+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php declare(strict_types=1);
2+
/** Copyright © Discorgento. All rights reserved. */
3+
4+
namespace Discorgento\Migrations\Setup\Migration\Plugin;
5+
6+
use Magento\Framework\App\Config\ScopeConfigInterface as ScopeConfig;
7+
use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter;
8+
9+
class AdminConfig
10+
{
11+
protected ConfigWriter $configWriter;
12+
protected ScopeConfig $scopeConfig;
13+
14+
public function __construct(
15+
ConfigWriter $configWriter,
16+
ScopeConfig $scopeConfig
17+
) {
18+
$this->configWriter = $configWriter;
19+
$this->scopeConfig = $scopeConfig;
20+
}
21+
22+
/**
23+
* Retrieve config value from given path
24+
*
25+
* @param string $path
26+
* @param string $scope
27+
* @param int $scopeId
28+
*/
29+
public function get($path, $scope = ScopeConfig::SCOPE_TYPE_DEFAULT, $scopeId = null)
30+
{
31+
$this->scopeConfig->getValue($path, $scope, $scopeId);
32+
}
33+
34+
/**
35+
* Set config value of given path
36+
*
37+
* @param string|array $path can also be a map of multiple config
38+
* @param string|int|array $value
39+
* @param string $scope
40+
* @param int $scopeId
41+
*/
42+
public function set($path, $value = null, $scope = ScopeConfig::SCOPE_TYPE_DEFAULT, $scopeId = 0)
43+
{
44+
if (!is_array($path)) {
45+
return $this->configWriter->save($path, $value, $scope, $scopeId);
46+
}
47+
48+
$paths = $path;
49+
foreach ($paths as $path => $value) {
50+
$this->set(
51+
$path,
52+
$value['value'] ?? $value,
53+
$value['scope'] ?? $scope,
54+
$value['scope_id'] ?? $scopeId,
55+
);
56+
}
57+
}
58+
59+
/**
60+
* Delete given config from core_config_data
61+
* (thus resetting to config.xml value)
62+
*
63+
* @param string|array $path
64+
* @param string $scope
65+
* @param int $scopeId
66+
*/
67+
public function reset($path, $scope = ScopeConfig::SCOPE_TYPE_DEFAULT, $scopeId = 0)
68+
{
69+
$paths = is_string($path) ? [$path] : $path;
70+
foreach ($paths as $path) {
71+
$this->configWriter->delete($path, $scope, $scopeId);
72+
}
73+
}
74+
75+
/**
76+
* Append given value to given path
77+
*
78+
* @param string $path
79+
* @param string|int|array $value
80+
* @param string $scope
81+
* @param int $scopeId
82+
*/
83+
public function append($path, $value, $scope = ScopeConfig::SCOPE_TYPE_DEFAULT, $scopeId = null)
84+
{
85+
$oldValue = $this->scopeConfig->getValue($path, $scope, $scopeId);
86+
$newValue = $oldValue . $value;
87+
$this->configWriter->save($path, $newValue, $scope, $scopeId ?: 0);
88+
}
89+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
/** Copyright © Discorgento. All rights reserved. */
3+
4+
namespace Discorgento\Migrations\Setup\Migration\Plugin;
5+
6+
use Magento\Catalog\Api\Data\CategoryAttributeInterface;
7+
8+
class CategoryAttribute extends Common\EavAttribute
9+
{
10+
public const ENTITY_TYPE = CategoryAttributeInterface::ENTITY_TYPE_CODE;
11+
12+
protected ReposAndCollections $reposAndCollections;
13+
14+
public function __construct(
15+
Common\EavAttribute\Context $context,
16+
ReposAndCollections $reposAndCollections
17+
) {
18+
parent::__construct($context);
19+
$this->reposAndCollections = $reposAndCollections;
20+
}
21+
22+
/**
23+
* @inheritDoc
24+
* @todo Find a faster way of doing this, like with the products
25+
*/
26+
public function massUpdate($entityIds, $data)
27+
{
28+
$categoryRepository = $this->reposAndCollections->getCategoryRepository();
29+
foreach ($entityIds as $categoryId) {
30+
$category = $categoryRepository->get($categoryId);
31+
$category->addData($data);
32+
$categoryRepository->save($category);
33+
}
34+
}
35+
}

Setup/Migration/Plugin/CmsBlock.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
/** Copyright © Discorgento. All rights reserved. */
3+
4+
namespace Discorgento\Migrations\Setup\Migration\Plugin;
5+
6+
use Magento\Cms\Model\BlockFactory;
7+
use Magento\Cms\Model\BlockRepository;
8+
use Magento\Cms\Model\ResourceModel\Block\CollectionFactory as BlockCollectionFactory;
9+
10+
class CmsBlock extends Common\Cms
11+
{
12+
public function __construct(
13+
BlockCollectionFactory $collectionFactory,
14+
BlockFactory $factory,
15+
BlockRepository $repository
16+
) {
17+
parent::__construct($collectionFactory, $factory, $repository);
18+
}
19+
}

Setup/Migration/Plugin/CmsPage.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
/** Copyright © Discorgento. All rights reserved. */
3+
4+
namespace Discorgento\Migrations\Setup\Migration\Plugin;
5+
6+
use Magento\Cms\Model\PageFactory;
7+
use Magento\Cms\Model\PageRepository;
8+
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory as PageCollectionFactory;
9+
10+
class CmsPage extends Common\Cms
11+
{
12+
public function __construct(
13+
PageCollectionFactory $collectionFactory,
14+
PageFactory $factory,
15+
PageRepository $repository
16+
) {
17+
parent::__construct($collectionFactory, $factory, $repository);
18+
}
19+
}

0 commit comments

Comments
 (0)