├── .gitattributes ├── .github └── FUNDING.yml ├── Api └── Cms │ ├── SimpleBlock.php │ └── SimplePage.php ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── Setup └── Patch │ └── SimpleDataPatch.php ├── composer.json ├── etc └── module.xml └── registration.php /.gitattributes: -------------------------------------------------------------------------------- 1 | /docs export-ignore 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: markshust 4 | -------------------------------------------------------------------------------- /Api/Cms/SimpleBlock.php: -------------------------------------------------------------------------------- 1 | blockInterfaceFactory = $blockInterfaceFactory; 49 | $this->blockRepository = $blockRepository; 50 | $this->getBlockByIdentifier = $getBlockByIdentifier; 51 | $this->logger = $logger; 52 | } 53 | 54 | /** 55 | * Delete a block from a given identifier and optional store id. 56 | * @param string $identifier 57 | * @param int $storeId 58 | */ 59 | public function delete(string $identifier, int $storeId = Store::DEFAULT_STORE_ID): void 60 | { 61 | try { 62 | $block = $this->getBlockByIdentifier->execute($identifier, $storeId); 63 | } catch (NoSuchEntityException $e) { 64 | $this->logger->critical($e->getMessage()); 65 | return; 66 | } 67 | 68 | try { 69 | $this->blockRepository->delete($block); 70 | } catch (CouldNotDeleteException $e) { 71 | $this->logger->critical($e->getMessage()); 72 | } 73 | } 74 | 75 | /** 76 | * If the CMS block identifier is found, attempt to update the record. 77 | * If it is not found, attempt to create a new record. 78 | * @param array $data 79 | */ 80 | public function save(array $data): void 81 | { 82 | $identifier = $data['identifier']; 83 | $storeId = $data['store_id'] ?? Store::DEFAULT_STORE_ID; 84 | 85 | try { 86 | $block = $this->getBlockByIdentifier->execute($identifier, $storeId); 87 | } catch (NoSuchEntityException $e) { 88 | // Rather than throwing an exception, create a new block instance 89 | 90 | /** @var BlockInterface|AbstractModel $block */ 91 | $block = $this->blockInterfaceFactory->create(); 92 | $block->setIdentifier($identifier); 93 | 94 | // Set initial store data to "all stores" 95 | $block->setData('store_id', $storeId); 96 | $block->setData('stores', [$storeId]); 97 | } 98 | 99 | $elements = [ 100 | 'content', 101 | 'is_active', 102 | 'stores', 103 | 'title', 104 | ]; 105 | 106 | foreach ($elements as $element) { 107 | if (isset($data[$element])) { 108 | $block->setData($element, $data[$element]); 109 | } 110 | } 111 | 112 | try { 113 | $this->blockRepository->save($block); 114 | } catch (CouldNotSaveException $e) { 115 | $this->logger->critical($e->getMessage()); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /Api/Cms/SimplePage.php: -------------------------------------------------------------------------------- 1 | getPageByIdentifier = $getPageByIdentifier; 49 | $this->logger = $logger; 50 | $this->pageInterfaceFactory = $pageInterfaceFactory; 51 | $this->pageRepository = $pageRepository; 52 | } 53 | 54 | /** 55 | * Delete a page from a given identifier and optional store id. 56 | * @param string $identifier 57 | * @param int $storeId 58 | */ 59 | public function delete(string $identifier, int $storeId = Store::DEFAULT_STORE_ID): void 60 | { 61 | try { 62 | $page = $this->getPageByIdentifier->execute($identifier, $storeId); 63 | } catch (NoSuchEntityException $e) { 64 | $this->logger->critical($e->getMessage()); 65 | return; 66 | } 67 | 68 | try { 69 | $this->pageRepository->delete($page); 70 | } catch (CouldNotDeleteException $e) { 71 | $this->logger->critical($e->getMessage()); 72 | } 73 | } 74 | 75 | /** 76 | * If the CMS page identifier is found, attempt to update the record. 77 | * If it is not found, attempt to create a new record. 78 | * @param array $data 79 | */ 80 | public function save(array $data): void 81 | { 82 | $identifier = $data['identifier']; 83 | $storeId = $data['store_id'] ?? Store::DEFAULT_STORE_ID; 84 | 85 | try { 86 | $page = $this->getPageByIdentifier->execute($identifier, $storeId); 87 | } catch (NoSuchEntityException $e) { 88 | // Rather than throwing an exception, create a new page instance 89 | 90 | /** @var PageInterface|AbstractModel $page */ 91 | $page = $this->pageInterfaceFactory->create(); 92 | $page->setIdentifier($identifier); 93 | 94 | // Set initial store data to "all stores" 95 | $page->setData('store_id', $storeId); 96 | $page->setData('stores', [$storeId]); 97 | 98 | // Set a default page layout 99 | $page->setData('page_layout', '1column'); 100 | } 101 | 102 | $elements = [ 103 | 'content', 104 | 'content_heading', 105 | 'custom_layout_update_xml', 106 | 'custom_root_template', 107 | 'custom_theme', 108 | 'custom_theme_from', 109 | 'custom_theme_to', 110 | 'is_active', 111 | 'layout_update_selected', 112 | 'layout_update_xml', 113 | 'meta_description', 114 | 'meta_keywords', 115 | 'meta_title', 116 | 'page_layout', 117 | 'sort_order', 118 | 'stores', 119 | 'title', 120 | ]; 121 | 122 | foreach ($elements as $element) { 123 | if (isset($data[$element])) { 124 | $page->setData($element, $data[$element]); 125 | } 126 | } 127 | 128 | try { 129 | $this->pageRepository->save($page); 130 | } catch (CouldNotSaveException $e) { 131 | $this->logger->critical($e->getMessage()); 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## [2.0.2] - 2022-06-28 8 | 9 | ### Fixed 10 | - Change return type to match PatchInterface. 11 | 12 | ## [2.0.1] - 2020-01-13 13 | 14 | ### Fixed 15 | - Removed typed properties for PHP 7.3 support. 16 | 17 | ## [2.0.0] - 2020-01-12 18 | 19 | ### Added 20 | - Added type declarations to class properties. 21 | 22 | ### Removed 23 | - Removed `SimpleConfig` class_alias as it caused issues in production mode ([#4](https://github.com/markshust/magento2-module-simpledata/issues/4)). 24 | 25 | ## [1.0.0] - 2020-05-20 26 | 27 | ### Added 28 | - Initial release. 29 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [year] [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

MarkShust_SimpleData

2 | 3 |
4 |

Simplifies calling Magento data structures.

5 | Supported Magento Versions 6 | Latest Stable Version 7 | Composer Downloads 8 | Maintained - Yes 9 | 10 |
11 | 12 | ## Table of contents 13 | 14 | - [Summary](#summary) 15 | - [Installation](#installation) 16 | - [Usage](#usage) 17 | - [License](#license) 18 | 19 | ## Summary 20 | 21 | Calling Magento data structures can be confusing. There are many classes available, and knowing which to call and when can be confusing & overwhelming. 22 | 23 | This module aims to simplify calling these data structures. All classes are prefixed with `Simple` so they are easy to lookup within IDEs. They also follow a pretty standard naming convention which aligns with Magento's way of naming modules. It also provides a `SimpleDataPatch` class which greatly simplifies writing data patch scripts. 24 | 25 | For example, here is a data patch script to update the content of a CMS page with and without `SimpleData`: 26 | 27 | ![Demo](https://raw.githubusercontent.com/markshust/magento2-module-simpledata/master/docs/demo.png) 28 | 29 | ## Installation 30 | 31 | ```sh 32 | composer require markshust/magento2-module-simpledata 33 | bin/magento module:enable MarkShust_SimpleData 34 | bin/magento setup:upgrade 35 | ``` 36 | 37 | ## Usage 38 | 39 | Here are the signatures of the simplified data structures classes: 40 | 41 | `MarkShust\SimpleData\Api\Data\Cms\SimpleBlock` 42 | 43 | ```php 44 | /** 45 | * Delete a block from a given identifier and optional store id. 46 | * @param string $identifier 47 | * @param int $storeId 48 | */ 49 | public function delete(string $identifier, int $storeId = Store::DEFAULT_STORE_ID): void 50 | 51 | /** 52 | * If the CMS block identifier is found, attempt to update the record. 53 | * If it is not found, attempt to create a new record. 54 | * @param array $data 55 | */ 56 | public function save(array $data): void 57 | ``` 58 | 59 | `MarkShust\SimpleData\Api\Data\Cms\SimplePage` 60 | 61 | ```php 62 | /** 63 | * Delete a page from a given identifier and optional store id. 64 | * @param string $identifier 65 | * @param int $storeId 66 | */ 67 | public function delete(string $identifier, int $storeId = Store::DEFAULT_STORE_ID): void 68 | 69 | /** 70 | * If the CMS page identifier is found, attempt to update the record. 71 | * If it is not found, attempt to create a new record. 72 | * @param array $data 73 | */ 74 | public function save(array $data): void 75 | ``` 76 | 77 | ### Examples using SimpleDataPatch 78 | 79 | #### Create block 80 | 81 | ```php 82 | block->save([ 94 | 'identifier' => 'foo_bar', 95 | 'title' => 'Foo bar', 96 | 'content' => << 98 | Foo bar 99 | 100 | CONTENT, 101 | ]); 102 | } 103 | } 104 | ``` 105 | 106 | #### Delete block 107 | 108 | ```php 109 | block->delete('foo_bar'); 121 | } 122 | } 123 | ``` 124 | 125 | #### Update block 126 | 127 | ```php 128 | block->save([ 140 | 'identifier' => 'foo_bar', 141 | 'title' => 'Foo bar 1', 142 | ]); 143 | } 144 | } 145 | ``` 146 | 147 | #### Create page 148 | 149 | ```php 150 | page->save([ 162 | 'identifier' => 'foo_bar', 163 | 'title' => 'Foo bar', 164 | 'content' => << 166 | Foo bar 167 | 168 | CONTENT, 169 | ]); 170 | } 171 | } 172 | ``` 173 | 174 | #### Update page 175 | 176 | ```php 177 | page->save([ 189 | 'identifier' => 'foo_bar', 190 | 'title' => 'Foo bar 1', 191 | ]); 192 | } 193 | } 194 | ``` 195 | 196 | #### Delete page 197 | 198 | ```php 199 | page->delete('foo_bar'); 211 | } 212 | } 213 | ``` 214 | 215 | #### Create or update config 216 | 217 | ```php 218 | config->save('foo/bar', 'baz'); 229 | } 230 | } 231 | 232 | ``` 233 | 234 | #### Delete config 235 | 236 | ```php 237 | config->delete('foo/bar'); 248 | } 249 | } 250 | ``` 251 | 252 | ### Example using dependency injection 253 | 254 | ```php 255 | block = $simpleBlock; 275 | } 276 | 277 | /** 278 | * {@inheritdoc} 279 | */ 280 | public function execute(): void 281 | { 282 | $this->block->save([ 283 | 'identifier' => 'foo_bar', 284 | 'title' => 'Foo bar', 285 | 'content' => << 287 | Foo bar 288 | 289 | CONTENT, 290 | ]); 291 | 292 | // Carry out other actions... 293 | } 294 | } 295 | ``` 296 | 297 | ## License 298 | 299 | [MIT](https://opensource.org/licenses/MIT) 300 | -------------------------------------------------------------------------------- /Setup/Patch/SimpleDataPatch.php: -------------------------------------------------------------------------------- 1 | block = $simpleBlock; 38 | $this->config = $simpleConfig; 39 | $this->page = $simplePage; 40 | } 41 | 42 | /** 43 | * {@inheritdoc} 44 | */ 45 | public static function getDependencies(): array 46 | { 47 | return []; 48 | } 49 | 50 | /** 51 | * {@inheritdoc} 52 | */ 53 | public function getAliases(): array 54 | { 55 | return []; 56 | } 57 | 58 | /** 59 | * Call your patch updates within this function. 60 | */ 61 | abstract public function apply(): self; 62 | } 63 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "markshust/magento2-module-simpledata", 3 | "description": "The SimpleData module simplifies calling Magento data structures.", 4 | "require": { 5 | "php": ">=7.1", 6 | "magento/framework": ">=101" 7 | }, 8 | "type": "magento2-module", 9 | "version": "2.0.2", 10 | "license": [ 11 | "MIT" 12 | ], 13 | "autoload": { 14 | "files": [ 15 | "registration.php" 16 | ], 17 | "psr-4": { 18 | "MarkShust\\SimpleData\\": "" 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /etc/module.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /registration.php: -------------------------------------------------------------------------------- 1 |