markshust / magento2-module-simpledata
SimpleData模块简化了调用Magento数据结构。
2.0.2
2022-06-29 11:38 UTC
Requires
- php: >=7.1
- magento/framework: >=101
This package is auto-updated.
Last update: 2024-08-29 05:50:12 UTC
README
简化调用Magento数据结构。
目录
摘要
调用Magento数据结构可能会让人困惑。有许多类可用,知道何时调用哪个可能会令人困惑且难以应对。
本模块旨在简化调用这些数据结构。所有类都以前缀Simple
开头,以便在IDE中轻松查找。它们还遵循相当标准的命名约定,与Magento命名模块的方式相一致。它还提供了一个SimpleDataPatch
类,该类极大地简化了编写数据补丁脚本。
例如,这是一个带有和不带有SimpleData
的数据补丁脚本,用于更新CMS页面的内容:
安装
composer require markshust/magento2-module-simpledata bin/magento module:enable MarkShust_SimpleData bin/magento setup:upgrade
使用
以下是简化数据结构类签名的列表
MarkShust\SimpleData\Api\Data\Cms\SimpleBlock
/** * Delete a block from a given identifier and optional store id. * @param string $identifier * @param int $storeId */ public function delete(string $identifier, int $storeId = Store::DEFAULT_STORE_ID): void /** * If the CMS block identifier is found, attempt to update the record. * If it is not found, attempt to create a new record. * @param array $data */ public function save(array $data): void
MarkShust\SimpleData\Api\Data\Cms\SimplePage
/** * Delete a page from a given identifier and optional store id. * @param string $identifier * @param int $storeId */ public function delete(string $identifier, int $storeId = Store::DEFAULT_STORE_ID): void /** * If the CMS page identifier is found, attempt to update the record. * If it is not found, attempt to create a new record. * @param array $data */ public function save(array $data): void
使用SimpleDataPatch的示例
创建块
<?php declare(strict_types = 1); namespace MarkShust\Data\Setup\Patch\Data; use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch; class BlockFooBarCreate extends SimpleDataPatch { public function apply(): self { $this->block->save([ 'identifier' => 'foo_bar', 'title' => 'Foo bar', 'content' => <<<CONTENT <div class="foo-bar"> Foo bar </div> CONTENT, ]); } }
删除块
<?php declare(strict_types = 1); namespace MarkShust\Data\Setup\Patch\Data; use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch; class BlockFooBarDelete extends SimpleDataPatch { public function apply() { $this->block->delete('foo_bar'); } }
更新块
<?php declare(strict_types = 1); namespace MarkShust\Data\Setup\Patch\Data; use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch; class BlockFooBarUpdate extends SimpleDataPatch { public function apply() { $this->block->save([ 'identifier' => 'foo_bar', 'title' => 'Foo bar 1', ]); } }
创建页面
<?php declare(strict_types = 1); namespace MarkShust\Data\Setup\Patch\Data; use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch; class PageFooBarCreate extends SimpleDataPatch { public function apply() { $this->page->save([ 'identifier' => 'foo_bar', 'title' => 'Foo bar', 'content' => <<<CONTENT <div class="foo-bar"> Foo bar </div> CONTENT, ]); } }
更新页面
<?php declare(strict_types = 1); namespace MarkShust\Data\Setup\Patch\Data; use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch; class MyDataPatch extends SimpleDataPatch { public function apply() { $this->page->save([ 'identifier' => 'foo_bar', 'title' => 'Foo bar 1', ]); } }
删除页面
<?php declare(strict_types = 1); namespace MarkShust\Data\Setup\Patch\Data; use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch; class PageFooBarDelete extends SimpleDataPatch { public function apply() { $this->page->delete('foo_bar'); } }
创建或更新配置
<?php namespace MarkShust\Data\Setup\Patch\Data; use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch; class ConfigFooBarCreate extends SimpleDataPatch { public function apply() { $this->config->save('foo/bar', 'baz'); } }
删除配置
<?php namespace MarkShust\Data\Setup\Patch\Data; use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch; class ConfigFooBarDelete extends SimpleDataPatch { public function apply() { $this->config->delete('foo/bar'); } }
使用依赖注入的示例
<?php declare(strict_types = 1); namespace MarkShust\SimpleData; use MarkShust\SimpleData\Api\Cms\SimpleBlock; class MyClass { /** @var SimpleBlock */ protected $block; /** * SimpleDataPatch constructor. * @param SimpleBlock $simpleBlock */ public function __construct( SimpleBlock $simpleBlock ) { $this->block = $simpleBlock; } /** * {@inheritdoc} */ public function execute(): void { $this->block->save([ 'identifier' => 'foo_bar', 'title' => 'Foo bar', 'content' => <<<CONTENT <div class="foo-bar"> Foo bar </div> CONTENT, ]); // Carry out other actions... } }