pointybeard/helpers-foundation-bnl
Composer 安装命令:
composer require pointybeard/helpers-foundation-bnl
包简介
Provides broadcaster & listener pattern classes
README 文档
README
- Version: v1.0.0
- Date: March 01 2020
- Release notes
- GitHub repository
Provides broadcaster & listener pattern classes
Installation
This library is installed via Composer. To install, use composer require pointybeard/helpers-foundation-bnl or add "pointybeard/helpers-foundation-bnl": "~1.0.0" to your composer.json file.
And run composer to update your dependencies:
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar update
Requirements
This library requires PHP7.2 or greater.
This library also makes use of the PHP Helpers: Readable Trace Exception (pointybeard/helpers-exceptions-readabletrace). It is installed automatically via composer.
To include all the PHP Helpers packages on your project, use composer require pointybeard/helpers
Usage
Include this library in your PHP files with use pointybeard\Helpers\Foundation\BroadcastAndListen and implement the BroadcastAndListen\Interfaces\AcceptsListenersInterface interface like so:
<?php declare(strict_types=1); namespace MyApp; include __DIR__.'/vendor/autoload.php'; use pointybeard\Helpers\Foundation\BroadcastAndListen; class Warehouse implements BroadcastAndListen\Interfaces\AcceptsListenersInterface { use BroadcastAndListen\Traits\HasListenerTrait; use BroadcastAndListen\Traits\HasBroadcasterTrait; public const WORK_STARTED = 'Work Started'; public const WORK_COMPLETE = 'Work Completed'; public const WORK_FAILED = 'Work Failed'; private $location; public function __construct(string $location) { $this->location = $location; } public function doSomeWork() { $this->broadcast(self::WORK_STARTED, time()); try { $resultOfHardWork = null; // This is where all the work of the factory is done $resultOfHardWork = '123456'; $this->broadcast(self::WORK_COMPLETE, time(), $resultOfHardWork); } catch (\Exception $ex) { $this->broadcast(self::WORK_FAILED, $ex); } } public function location() { return $this->location; } } class WarehouseNewDelhi extends Warehouse { public function __construct() { parent::__construct("New Delhi"); } public function doSomeWork() { $this->broadcast(self::WORK_STARTED, time()); try { // Simulate something going wrong throw new \Exception('Machinery failed to process job'); } catch (\Exception $ex) { $this->broadcast(self::WORK_FAILED, $ex); } } } class WarehouseCanada extends Warehouse { public function __construct() { parent::__construct("Canada"); } public function doSomeWork() { // Add something that isn't a callback to the listener iterator $this->listeners->append("apples"); return parent::doSomeWork(); } } class Office { public function notificationFromWarehouse($type, ...$arguments) { echo "Recieved Notification from Warehouse in {$arguments[0]->location()}: {$type}".PHP_EOL; // Perform logic depending on the notification type switch ($type) { case Warehouse::WORK_STARTED: echo 'Work started at '.date('c', $arguments[1]).PHP_EOL; break; case Warehouse::WORK_COMPLETE: echo 'Work completed successfully at '.date('c', $arguments[1]).PHP_EOL; echo "The result of that hard work is: {$arguments[2]}".PHP_EOL.PHP_EOL; break; case Warehouse::WORK_FAILED: echo "Work failed to complete. Returned: {$arguments[1]->getMessage()}".PHP_EOL.PHP_EOL; break; } } } $headOffice = new Office; $headOfficeCallback = [$headOffice, 'notificationFromWarehouse']; $shanghai = new Warehouse('Shanghai'); $newdelhi = new WarehouseNewDelhi; // Add the office as a listener to each office location $shanghai->addListener($headOfficeCallback); $newdelhi->addListener($headOfficeCallback); // addListener allows for method chaining $canada = (new WarehouseCanada)->addListener($headOfficeCallback); $shanghai->doSomeWork(); // Recieved Notification from Warehouse in Shanghai: Work Started // Work started at 2020-03-01T10:37:40+00:00 // Recieved Notification from Warehouse in Shanghai: Work Completed // Work completed successfully at 2020-03-01T10:37:40+00:00 // The result of that hard work is: 123456 $newdelhi->doSomeWork(); // Recieved Notification from Warehouse in New Delhi: Work Started // Work started at 2020-03-01T10:37:40+00:00 // Recieved Notification from Warehouse in New Delhi: Work Failed // Work failed to complete. Returned: Machinery failed to process job try{ $canada->doSomeWork(); } catch(\Exception $ex) { echo "[ERROR] Something has gone wrong! Returned: " . $ex->getMessage() . PHP_EOL; } // Recieved Notification from Warehouse in Canada: Work Started // Work started at 2020-03-01T10:39:03+00:00 // [ERROR] Something has gone wrong! Returned: Invalid callback at position 1 of listener iterator.
Support
If you believe you have found a bug, please report it using the GitHub issue tracker, or better yet, fork the library and submit a pull request.
Contributing
We encourage you to contribute to this project. Please check out the Contributing documentation for guidelines about how to get involved.
License
"PHP Helpers: Broadcast & Listen Foundation Classes" is released under the MIT License.
pointybeard/helpers-foundation-bnl 适用场景与选型建议
pointybeard/helpers-foundation-bnl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 786 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pointybeard/helpers-foundation-bnl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pointybeard/helpers-foundation-bnl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 786
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-01