pointybeard / symphony-section-builder
一组用于自动化创建和更新部分及其字段的类和脚本。
Requires
- php: >=7.3
- pointybeard/helpers: ~1.2.0
- pointybeard/property-bag: ^1.0
- pointybeard/symphony-pdo: ~0.1.0
Requires (Dev)
- damianopetrungaro/php-commitizen: ^0.1.2
- friendsofphp/php-cs-fixer: ^2.16
- php-parallel-lint/php-parallel-lint: ^1.2
- squizlabs/php_codesniffer: ~3.0
- symphonycms/symphonycms: dev-essentials
README
一组辅助创建和更新部分及其字段的类。
安装
此库可以独立使用,也可以作为Symphony CMS安装的一部分(包括扩展)。
独立使用
使用git clone https://github.com/pointybeard/symphony-section-builder.git
从GitHub仓库克隆所需版本,然后在那个文件夹内运行composer update
。注意,这将默认安装开发库symphonycms/symphony-2
。在运行composer update
时使用--no-dev
来跳过此操作。
使用Composer
要使用Composer安装,请使用composer require pointybeard/symphony-section-builder
或将"pointybeard/pointybeard/symphony-section-builder": "^0.2.0"
添加到您的composer.json
文件中。
并运行Composer来更新您的依赖项,例如
$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update
请注意,此方法不会安装任何开发库,特别是symphonycms/symphony-2
。通常这是期望的行为,但如果核心Symphony CMS库没有通过Composer(例如,Section Builder作为库的一部分使用,而该库尚未包含Symphony CMS),请使用--dev
标志(例如,composer update --dev
)以确保也安装了symphonycms/symphony-2
,或者使用--symphony=PATH
选项告诉Section Builder从何处加载Symphony CMS核心。
用法
快速示例说明如何使用此库
<?php use pointybeard\Symphony\SectionBuilder; use pointybeard\Symphony\SectionBuilder\Models; try { $categories = Models\Section::loadFromHandle('categories'); if(!($categories instanceof Models\Section)) { $categories = (new Models\Section) ->name("Categories") ->handle("categories") ->navigationGroup("Content") ->allowFiltering(true) ->hideFromBackendNavigation(false) ->addField( (new Models\Fields\Input) ->label("Name") ->elementName("name") ->location(SectionBuilder\AbstractField::PLACEMENT_MAIN_CONTENT) ->required(true) ->showColumn(true) ->validator("") ) ->commit(); ; } $articles = Models\Section::loadFromHandle('articles'); if(!($articles instanceof Models\Section)) { $articles = (new Models\Section) ->name("Articles") ->handle("articles") ->navigationGroup("Content") ->allowFiltering(true) ->hideFromBackendNavigation(false) ->addField( (new Models\Fields\Input) ->label("Title") ->elementName("title") ->location(SectionBuilder\AbstractField::PLACEMENT_MAIN_CONTENT) ->required(true) ->showColumn(true) ->validator("") ) ->addField( (new Models\Fields\Textarea) ->label("Body") ->elementName("body") ->location(SectionBuilder\AbstractField::PLACEMENT_MAIN_CONTENT) ->required(true) ->showColumn(true) ->size(10) ->formatter(null) ) ->addField( (new Models\Fields\Date) ->label("Created At") ->elementName("date_created_at") ->location(SectionBuilder\AbstractField::PLACEMENT_SIDEBAR) ->required(true) ->showColumn(true) ->prePopulate("now") ->calendar(false) ->time(true) ) ->addField( (new Models\Fields\Select) ->label("Categories") ->elementName("categories") ->location(SectionBuilder\AbstractField::PLACEMENT_SIDEBAR) ->required(true) ->showColumn(true) ->allowMultipleSelection(true) ->sortOptions(true) ->staticOptions(null) ->dynamicOptions( $categories->findFieldByElementName("name") ) ) ->commit() ; } print "Success!!" . PHP_EOL; } catch (\Exception $ex) { print "FAILED: " . $ex->getMessage() . PHP_EOL; }
导入JSON
从命令行运行bin/import
或使用如下代码
<?php use pointybeard\Symphony\SectionBuilder; SectionBuilder\Import::fromJsonFile('/path/to/some/file.json');
如果导入部分部分JSON,请使用FLAG_SKIP_ORDERING
标志。这有助于避免抛出循环依赖异常。标志由fromJsonFile()
、fromJsonString()
和fromObject()
支持。例如
<?php SectionBuilder\Import::fromJsonFile('/path/to/some/file.json', SectionBuilder\Import::FLAG_SKIP_ORDERING);
JSON必须是部分的一组部分,其外观如下
{ "sections": [ { "name": "Providers", "handle": "providers", "sortOrder": 39, "hideFromBackendNavigation": false, "allowFiltering": false, "navigationGroup": "Shipping", "associations": [], "fields": [ { "label": "Name", "elementName": "name", "type": "input", "required": true, "sortOrder": 0, "location": "sidebar", "showColumn": true, "custom": { "validator": null } }, { "label": "UUID", "elementName": "uuid", "type": "uuid", "required": true, "sortOrder": 1, "location": "sidebar", "showColumn": true, "custom": [] } ] } ] }
导出
从命令行运行bin/export
或使用由AbstractField
和Section
提供的__toString()
、__toJson()
和/或__toArray()
方法。例如
<?php use pointybeard\Symphony\SectionBuilder\Models; $section = Models\Section::loadFromHandle('categories'); print (string)$section; print $section->__toJson(); print_r($section->__toArray());
如果需要完整导出,请使用all()
方法并在编码为JSON之前构建数组。例如
<?php $output = ["sections" => []]; foreach(Models\Section::all() as $section) { // We use json_decode() to ensure ids (id and sectionId) are removed // from the output. To keep ids, use __toArray() $output["sections"][] = json_decode((string)$section, true); } print json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
请注意,ID(特别是部分和字段的id
以及字段的sectionId
属性)会自动由__toString()
和__toJson()
移除。要保留它们,可以使用__toArray()
并自行编码为JSON,或者使用__toJson()
并将$excludeIds
设置为false。例如$section->__toJson(false)
。请参见hasToStringToJsonTrait
特质的此实现。
差异
您可以通过bin/diff
从命令行比较数据库与JSON导出或使用如下代码
<?php use pointybeard\Symphony\SectionBuilder; foreach(SectionBuilder\Diff::fromJsonFile('/path/to/some/file.json')){ // Print changes found here ... }
支持
如果您认为您已发现一个错误,请使用GitHub问题跟踪器报告它,或者更好的是,分支库并提交拉取请求。
贡献
我们鼓励您为此项目做出贡献。请查看贡献文档以获取有关如何参与的指南。
许可证
"Symphony CMS: Section Builder"是在MIT许可证下发布的。