承接 felixdorn/flash 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

felixdorn/flash

Composer 安装命令:

composer require felixdorn/flash

包简介

Framework agnostic flash notifications for PHP 7.3+

README 文档

README

Library logo

Flash

Packagist Version Build Status codecov

  • Extensible
  • Fully tested and easily testable
  • Allow functional use
  • Support templates

Getting started

composer require felixdorn/flash

Before using Flash, you need a Driver that implements the DriverInterface and a template.

use Felix\Flash\Drivers\SessionDriver;
use Felix\Flash\Flash;
use Felix\Flash\Templates\SpectreTemplate;

$driver = new SessionDriver([
    // session_start options
    // see https://www.php.net/manual/fr/function.session-start.php
]);


// A random template
$template = new SpectreTemplate();

$flash = new Flash($driver, $template);

Usage

These are common examples to work with flash.

In these example, i assume that you have already made the setup and have a working Flash instance.

Flashing

$flash->success('message');

$flash->warning('message');

$flash->error('message');

$flash->info('message');

$flash->flash('type', 'message');

Rendering

$flash->success('message');
$flash->warning('message1');

$flash->render(); // implicitly it's $flash->render('all')

// render 'message' and 'message1' using the defined template

$flash->render('success');

// render 'message' but not 'message1'

Custom types

$flash->message('really-important', 'In movie gone in 60 seconds, things are gone in 60 seconds');

$flash->render(); // will also render `really-important` as a normal type.

$flash->render('really-important'); // will render `really-important` flashes using the default template

Clear messages

$flash->success('Cool!');

$flash->clear('success'); // delete every success flash using the Driver

$flash->render(); // Won't render anything

$flash->success('Cool!');
$flash->error('Uhhhh.');

$flash->clear(); // Clear every flashes
 
$flash->render(); // Won't render anything

Enabling & Disabling

$flash->error('Bad thing.');

$flash->disable();
// NOTE: Here, when disabling, you can still clear or render

$flash->success('Good thing');

$flash->enable();

$flash->render('all'); // 'all' is optional, it's the default value

// Here, only 'Bad thing.' will be rendered.

Drivers

There is only 2 but it's easy to implement a new one (PR appreciated :p).

  • ArrayDriver Driver that should only be used for testing purpose.
  • SessionDriver Driver to work with the most common implementation of session in PHP.

Let's dive into what they do, and how they work. This part is useful if you want to create your own, otherwise jump to the next session.

A driver must implement these 3 methods :

  • clear(): self
  • push(FlashData $data): self
  • all(string $type = 'all'): array

The clear method

This is the simple one. It should clear any flash pushed.

The push method

This one receive a FlashData with the type of flash and the value and should register it to be able to get it back.

The all method

This one should if $type = 'all' return every flash pushed formatted like this :

return [
    '{someType}' => [
        'someFlash'
        // ...
    ],
    // ...
];

If the $type is something else then, it should return an array with only the flash inside this type.

return [
    'someFlash'
];

Templates

TestableTemplate just returns {type}: {value}, so it's easier to test if a flash worked well instead of typing all of the boring HTML.

Registering a template

$flash->setTemplate(...);

You can also set it when constructing the Flash object

Template using a string

This one is the most basic, but also the most useful.

<div class="alert {type}">{flash}</div>

So, if you flash an success Hello! flash. It will output

<div class="alert success">Hello!</div>

Template using a callable

function (string $type, string $message): string {
    return sprintf('<div class="alert %s">%s</div>', $type, $message);
}

In this case, you don't need a callable, and you could just use a string based template but this is how it works.

Template using TemplateInterface

use Felix\Flash\Templates\TemplateInterface;

class MyTemplate implements TemplateInterface {
    public function toHtml(string $type,string $message){
        return sprintf('<div class="alert %s">%s</div>', $type, $message);
    }
}

In this case, you don't need a Template class, and you could just use a string based template but this is how it works.

Testing

composer test

Security

If you discover any security related issues, please email github@felixdorn.fr instead of using the issue tracker.

Credits

Contributors

统计信息

  • 总下载量: 55
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固