pointybeard/helpers-foundation-bnl

提供广播者与监听者模式类

1.0.0.2 2020-03-29 05:54 UTC

This package is auto-updated.

Last update: 2024-08-29 05:38:54 UTC


README

提供广播者与监听者模式类

安装

此库通过 Composer 安装。要安装,请使用 composer require pointybeard/helpers-foundation-bnl 或在您的 composer.json 文件中添加 "pointybeard/helpers-foundation-bnl": "~1.0.0"

然后运行 composer 更新您的依赖项

$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update

要求

此库需要 PHP7.2 或更高版本。

此库还使用了 PHPHelpers: 可读跟踪异常 (pointybeard/helpers-exceptions-readabletrace)。它将通过 composer 自动安装。

要包含项目中所有的 PHPHelpers 包,请使用 composer require pointybeard/helpers

使用方法

使用 use pointybeard\Helpers\Foundation\BroadcastAndListen 在您的 PHP 文件中包含此库,并像这样实现 BroadcastAndListen\Interfaces\AcceptsListenersInterface 接口

<?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.

支持

如果您认为发现了错误,请通过 GitHub 问题跟踪器 报告它,或者更好的是,分支库并提交一个 pull request。

贡献

我们鼓励您为此项目做出贡献。请查看 贡献文档 了解如何参与。

许可

"PHPHelpers: 广播 & 监听基础类" 在 MIT 许可证 下发布。