gigafoxweb/notifier

简单的通知管理器

3.0.2 2016-11-16 08:25 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:26:14 UTC


README

感谢您对此产品的兴趣。希望您会喜欢它。gigafoxweb.com.

Notifier的主要任务是集中注册系统通知,并在需要时显示它们。

安装

composer require gigafoxweb/notifier

#通知 通知是GigaFoxWeb\Notifier\Notification类对象或其扩展。

$notification = new Notification('message', ['status' => 1]);

#存储 存储是可以扩展GigaFoxWeb\Notifier\notification\Storage对象的任何对象。默认情况下,gigafoxweb/notifier有两个存储:内存和会话。

$memoryStorage = new Memory();
$memoryStorage->setNotification($notification);

#处理器 处理器是知道如何处理过滤通知的对象。

$outputHandler = new OutputHandler();

#通知器 通知器是GigaFoxWeb\Notifier\notification\Storage和GigaFoxWeb\Notifier\notification\Handler的容器。它是一个单例池,可以在任何地方调用。

Notifier::instance()->setStorage('memory', $memoryStorage);
Notifier::instance()->setHandler('output', $outputHandler);

#过滤器 过滤器用于从处理列表中移除不需要的通知。

$filter = new RequireParam(['some-required-param', 'another-required-param']);

#过滤器池 过滤器池是Filter对象的集合,用于处理器。

$filtrator = new Filtrator();
$filtrator->addFilter($filter);
Notifier::instance()->getHandler('output')->setFiltrator($filtrator);

#简单应用示例

require_once __DIR__ . '/vendor/autoload.php';

use GigaFoxWeb\Notifier\Notifier;
use GigaFoxWeb\Notifier\notification\storages\Memory;
use GigaFoxWeb\Notifier\notification\storages\Session;
use GigaFoxWeb\Notifier\Notification;
use GigaFoxWeb\Notifier\notification\handlers\OutputHandler;
use GigaFoxWeb\Notifier\notification\Filtrator;
use GigaFoxWeb\Notifier\notification\filters\RequireParam;

session_start();

//setting notification storages
Notifier::instance()->setStorage('memory', new Memory());
Notifier::instance()->setStorage('session', new Session('GFW_notifications'));

//add notification into memory storage
Notifier::instance()->getStorage('memory')->setNotification(
    'hello',
    new Notification('Hello man!', ['required-param' => 'some-required-value',])
);

if (isset($_POST['some-input'])) {
    $message = ($_POST['some-input'] === 'valid') ? 'Value is valid' : 'Value is not valid';
    //add notification into session storage
    Notifier::instance()->getStorage('session')->setNotification(
        'some-input-validation',
        new Notification($message, ['required-param' => 'some-required-value'])
    );
}

//create filtrator if we want to use only filtrated notifications
$filtrator = (new Filtrator());
$filtrator->addFilter(new RequireParam(['required-param']));

//create notifications handler
$outputHandler = new OutputHandler(__DIR__ . '/notification-layout.php');

//setting notification filtrator to handler
$outputHandler->setFiltrator($filtrator);

//setting handler
Notifier::instance()->setHandler('output', $outputHandler);
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <!-- notifications output start -->
    <?= Notifier::instance()->getHandler('output')->processStorage(Notifier::instance()->getStorage('memory')); ?>
    <?= Notifier::instance()->getHandler('output')->processStorage(Notifier::instance()->getStorage('session')); ?>
    <!-- notifications output end -->
    <form action="" method="post">
        <label for="some-input">Some input</label>
        <input type="text" id="some-input" name="some-input">
        <input type="submit">
    </form>
</body>
</html>