dstuecken/notify

不依赖框架且轻量级的通知系统,包含多个适配器。

1.0.3 2016-04-06 10:37 UTC

This package is auto-updated.

Last update: 2024-09-12 03:21:29 UTC


README

不依赖框架且轻量级的通知系统,实现了 PSR LoggerInterface 并提供了多个处理适配器。

您可以根据需要实现自己的处理器!

Build Status License Latest Stable Version Latest Unstable Version

要求

  • PHP 5.4

安装

使用 Composer

要使用 Composer 安装 Notify,只需将以下内容添加到您的 composer.json 文件中

{
    "require": {
        "dstuecken/notify": "dev-master"
    }
}

或者运行以下命令

composer require dstuecken/notify

用法

使用 Header 处理器

<?php
$notificationCenter = new NotificationCenter();
$notificationCenter->addHandler(
    new HeaderHandler('Notify', NotificationCenter::ERROR)
);

$notificationCenter->error('There was an error.');

Header 处理器用于发送以下(可更改)格式的 HTTP Header 到浏览器:X-Notify-Notification。然后,可以通过 JavaScript 实现来捕获此头部,并显示一个漂亮且清晰的 JavaScript 错误消息,同时继续使用正常响应来处理应用程序。

使用 Logger 处理器

您可以将通知发送到任何实现 LoggerInterface 的 Logger。

<?php
$logger = new Logger('my-logger');

$notificationCenter = new NotificationCenter();
$notificationCenter->addHandler(
    new LoggerHandler($logger, NotificationCenter::ERROR)
);

$notificationCenter->error('There was an error.');

使用 HipChat 处理器

您还可以将所有 CRITICAL 级别的通知发送到 HipChat,例如。

<?php
$hipchat = new HipChat\HipChat('apiKey123', );

$notificationCenter = new NotificationCenter();
$notificationCenter->addHandler(
    new HipChatHandler($hipchat, 'hipchat-room-id', NotificationCenter::CRITICAL, 'hipChatBotName')
);

$notificationCenter->error('There was an error.');

将通知推送到多个处理器

您可以将通知发送到不同级别的不同处理器。

<?php
$logger = new Logger('my-logger');

$notificationCenter = new NotificationCenter();
$notificationCenter
	->addHandler(
	    new HeaderHandler(
	        'Notify',
	        NotificationCenter::INFO
	    )
	)
	->addHandler(
	    new LoggerHandler(new Logger(), NotificationCenter::ERROR)
	);

$notificationCenter->error('There was an error.', HeaderHandler::formatAttributes(null, null, true));

已实现的处理器

Header

发送一个 HTTP Header,该 Header 可以被 JavaScript 观察到,并将错误表示为类似 growl 的通知消息。

Logger

将通知转发到实现 LoggerInterface 的 Logger。

MacOS

显示 Mac OS X 通知中心消息。

HipChat

将通知投递到您的 HipChat 房间。

NotifySend

通过 Ubuntu 的通知服务发送通知。

Syslog

将通知发送到 syslog(使用 PHP 的 syslog() 函数)。

Smarty

将通知附加到 smarty 模板变量。

Memory

将通知转发到数组。

测试

使用以下命令运行 phpunit 测试

phpunit --bootstrap tests/bootstrap.php tests