Last active
April 6, 2020 11:09
-
-
Save jenky/dc17a813be3aeeb749d6c7e8cc324bed to your computer and use it in GitHub Desktop.
Simple laravel flash message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Support\Flash; | |
use Illuminate\Contracts\Session\Session; | |
use Illuminate\Support\Arr; | |
use Illuminate\Support\Traits\Macroable; | |
class Flash | |
{ | |
use Macroable; | |
/** | |
* The session key name. | |
* | |
* @var string | |
*/ | |
public static $sessionKey = '_flash_message'; | |
/** | |
* The session instance. | |
* | |
* @var \Illuminate\Contracts\Session\Session | |
*/ | |
protected $session; | |
/** | |
* Create new flash instance. | |
* | |
* @param \Illuminate\Contracts\Session\Session $session | |
* @return void | |
*/ | |
public function __construct(Session $session) | |
{ | |
$this->session = $session; | |
} | |
/** | |
* Get the message attribute. | |
* | |
* @param string $name | |
* @return mixed | |
*/ | |
public function __get(string $name) | |
{ | |
return optional($this->message())->$name; | |
} | |
/** | |
* Get the flash message. | |
* | |
* @return \App\Support\Flash\Message|null | |
*/ | |
public function message(): ?Message | |
{ | |
$message = $this->session->get(static::$sessionKey); | |
return $message ? new Message($message) : null; | |
} | |
/** | |
* Flash the message. | |
* | |
* @param \App\Support\Flash\Message $message | |
* @return void | |
*/ | |
public function flash(Message $message): void | |
{ | |
$this->session->flash(static::$sessionKey, $message->toArray()); | |
} | |
/** | |
* Register flash level. | |
* | |
* @param array $options | |
* @return void | |
*/ | |
public static function levels(array $options): void | |
{ | |
foreach ($options as $level => $data) { | |
static::macro($level, function (string $message, array $extra = []) use ($level, $data) { | |
$data = array_merge(Arr::wrap($data), $extra); | |
return $this->flash(Message::make($message, $data, $level)); | |
}); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Support\Flash\Flash; | |
use App\Support\Flash\Message; | |
if (! function_exists('flash')) { | |
/** | |
* Set the page title. | |
* | |
* @param string $title | |
* @param string $delimiter | |
* @return string | |
*/ | |
function flash(?string $message = null, array $data = []): Flash | |
{ | |
$flash = app(Flash::class); | |
if ($message) { | |
$flash->flash(Message::make($message, $data)); | |
} | |
return $flash; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Support\Flash; | |
use Illuminate\Support\Fluent; | |
class Message extends Fluent | |
{ | |
/** | |
* Create new message instance. | |
* | |
* @param string $message | |
* @param array $data | |
* @param string|null $level | |
* @return self | |
*/ | |
public static function make(string $message, array $data = [], ?string $level = null) | |
{ | |
$data['message'] = $message; | |
$data['level'] = $level; | |
return new static($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment