felixdorn/flash

Framework agnostic flash notifications for PHP 7.3+

3.0.0 2022-01-27 08:16 UTC

This package is auto-updated.

Last update: 2024-04-27 12:05:07 UTC


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