shopsys/plugin-interface
Composer 安装命令:
composer require shopsys/plugin-interface
包简介
General Shopsys Platform plugin interface
README 文档
README
Package of interfaces providing compatibility between Shopsys Platform and plugins.
This repository is maintained by shopsys/shopsys monorepo, information about changes is in its CHANGELOG file.
Features
This package contains interfaces responsible for general functionality usable in almost any plugin. For specific functionality, such as generating product feeds, there are separate repositories.
Example
For example usage see the AcmeProductCrudExtension in the CRUD extension section below.
Storing data
Best way to store your plugin data is to use Doctrine entities.
Create a folder (e.g. src/Entity) in your plugin and put your entities there.
Then you need to create DoctrineOrmMappingPass and add it as CompilerPass in your YourBundleNameBundle class. This can be done like this:
// vendor/your-bundle-name-bundle/src/YourBundleNameBundle.php // ... /** * @inheritdoc */ public function build(ContainerBuilder $container) { parent::build($container); $container->addCompilerPass( DoctrineOrmMappingsPass::createAttributeMappingDriver( [$this->getNamespace() . '\Entity'], [$this->getPath() . '/Entity'] ) ); } // ...
This tells Doctrine where to look for your entities. Now you can create Repository and manage your data as you are used to.
CRUD extension
Sometimes your plugin needs some extra information to be included in an entity, for example, you need to track the weight of products. This can be solved by extending the entity CRUD model with your custom sub-form.
To do so you should implement PluginCrudExtensionInterface and tag the service in a DI container with shopsys.crud_extension tag.
The tag should have a type attribute defining which CRUD model should be extended (eg. "product").
Each form extension has its label, form type and methods for managing the form data.
Example
services: acme.acme_product_crud_extension: class: AcmePlugin\AcmeProductCrudExtension tags: - { name: shopsys.crud_extension, type: product } acme.acme_data_form_type: class: AcmePlugin\AcmeProductFormType tags: - { name: form.type }
// ... class AcmeProductCrudExtension implements PluginCrudExtensionInterface { private $acmeProductFacade; public function __construct(AcmeProductFacade $acmeProductFacade) { $this->acmeProductFacade = $acmeProductFacade; } public function getFormTypeClass() { return AcmeProductFormType::class; } public function getFormLabel() { return 'ACME data'; } public function getData($productId) { $acmeProduct = $this->acmeProductFacade->findByProductId($productId); $pluginData = [ 'attribute' => $acmeProduct->getAttribute(), ]; return $pluginData; } public function saveData($productId, $data) { $acmeProductData = new AcmeProductData(); $acmeProductData->attribute = $data['attribute']; $this->acmeProductFacade->save($productId, $acmeProductData); } public function removeData($productId) { $this->acmeProductFacade->remove($productId); } }
Demo Data
In order to enable easy testing or to demonstrate usage of your plugin, you might want to provide demonstrational data with it.
In that case, you should implement PluginDataFixtureInterface that will take care of loading demonstrational data into the core.
All you got to do is to implement PluginDataFixtureInterface::load() method and tag the service in a DI container with shopsys.data_fixture tag.
Example
services: acme.acme_bundle.data_fixture: class: AcmePlugin\AcmeDataFixture tags: - { name: shopsys.data_fixture }
class AcmeDataFixture implements PluginDataFixtureInterface { private $acmeProductFacade; public function __construct(AcmeProductFacade $acmeProductFacade) { $this->acmeProductFacade = $acmeProductFacade; } public function load() { $firstAcmeProductData = new AcmeProductData(); $firstAcmeProductData->enableWeightCalculation = true; $firstAcmeProductData->weight = 42; $firstAcmeProductData->domainId = 1; $this->acmeProductFacade->save($firstAcmeProductData); $secondAcmeProductData = new AcmeProductData(); $secondAcmeProductData->enableWeightCalculation = false; $secondAcmeProductData->weight = null; $secondAcmeProductData->domainId = 2; $this->acmeProductFacade->save($secondAcmeProductData); } }
CRON modules
When your plugin needs to execute some task periodically, for example downloading currency exchange rates every six hours, you can use a CRON module.
There are 2 types of CRON module interfaces:
SimpleCronModuleInterface- for short tasks that do not take too long to execute
IteratedCronModuleInterface- for long-running tasks that can be divided into smaller parts
- if the module takes too long to run it will be suspended and will be woken up and re-run during the next opportunity
You can implement either one of these interfaces and tag the service in a DI container with shopsys.cron tag.
CRON modules are started automatically every time the current system time matches the specified mask in the tag attributes hours and minutes.
Example
acme.data_download_cron_module: class: AcmePlugin\AcmeDataDownloadCronModule tags: - { name: shopsys.cron, hours: '*/6', minutes: '0' }
// ... class AcmeDataDownloadCronModule implements SimpleCronModuleInterface { /** * @var \Monolog\Logger */ private $logger; public function setLogger(Logger $logger) { $this->logger = $logger; } public function run() { $data = $this->downloadData(); $this->saveData($data); $this->logger->info(sprintf('Downloaded %d new records.', count($data))); } // ... }
How to implement a plugin
Plugins are implemented in a form of a Symfony bundle. For tips on how to write a new bundle see Best Practices for Reusable Bundles.
Contributing
Thank you for your contributions to Shopsys Plugin Interface package. Together we are making Shopsys Platform better.
This repository is READ-ONLY. If you want to report issues and/or send pull requests, please use the main Shopsys repository.
Please check our Contribution Guide before contributing.
Support
What to do when you are in troubles or need some help? The best way is to join our Slack.
If you want to report issues, please use the main Shopsys repository.
shopsys/plugin-interface 适用场景与选型建议
shopsys/plugin-interface 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 255.01k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「plugin」 「SSFW」 「Shopsys Platform」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 shopsys/plugin-interface 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shopsys/plugin-interface 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 shopsys/plugin-interface 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shopsys Platform Google Cloud Bundle
CakePHP 4.x AdminLTE Theme.
[experimental] Read model layer for Shopsys Framework
Shopsys Framework plugin interface for product feed generation
Shopsys Platform plugin interface for Luigi's Box XML product feed generation
Shopsys Platform plugin interface for Luigis Box XML article feed generation
统计信息
- 总下载量: 255.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 15
- 依赖项目数: 21
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-17
