Skip to content

Instantly share code, notes, and snippets.

@yuka2py
Created December 21, 2013 08:32
Show Gist options
  • Save yuka2py/8066887 to your computer and use it in GitHub Desktop.
Save yuka2py/8066887 to your computer and use it in GitHub Desktop.
PHPでフラッシュを実現する簡単なユーティリティ
<?php
/**
* This is alias of the flash::next() for conbinience.
* @param $class
* @param $content
*/
function flash($class, $content) {
flash::next($class, $content);
}
/**
* This class is simply notification function.
* @author yuka2py
*/
class flash
{
static private $_notice_class_name = 'notification';
static private $_now;
static private $_session_key = '__flash_values__';
/**
* Initilizing flash variables.
* Must be call before using.
*/
static public function init($notice_class_name='notification') {
if (!isset($_SESSION)) {
session_start();
}
self::$_now = (array) (empty($_SESSION[self::$_session_key]) ? array() : $_SESSION[self::$_session_key]);
unset($_SESSION[self::$_session_key]);
$_SESSION[self::$_session_key] = array();
self::$_notice_class_name = $notice_class_name;
}
/**
* Get flash message.
* @return mixed
* @param string $class
* @param mixed $default Use this value if not set $class in flash.
*/
static public function get($class, $default=null) {
if (isset($_SESSION[self::$_session_key][$class])) {
$res = $_SESSION[self::$_session_key][$class];
} else if (isset(self::$_now[$class])) {
$res = self::$_now[$class];
} else {
$res = $default;
}
return $res;
}
/**
* Get all registered all flash message.
* @return array
*/
static public function get_all() {
return array_merge($_SESSION[self::$_session_key], self::$_now);
}
/**
* Remove flash message. and returns poped value.
* @return mixed
* @param string $class
* @param mixed $default Use this value if not set $class in flash.
*/
static public function pop($class, $default=null) {
if (isset($_SESSION[self::$_session_key][$class])) {
$res = $_SESSION[self::$_session_key][$class];
unset($_SESSION[self::$_session_key][$class]);
} else if (isset(self::$_now[$class])) {
$res = self::$_now[$class];
unset(self::$_now[$class]);
} else {
$res = $default;
}
return $res;
}
/**
* Remove all flash message. and returns poped values.
* @return mixed
* @param string $class
* @param mixed $default Use this value if not set $class in flash.
*/
static public function pop_all() {
$all = array_merge($_SESSION[self::$_session_key], self::$_now);
$_SESSION[self::$_session_key] = array();
self::$_now = array();
return $all;
}
/**
* Register flash message for current or next session.
* Note: If already exists same class name value as now context then remove it.
* @param string $class
* @param string $content
*/
static public function next($class, $message) {
$_SESSION[self::$_session_key][$class] = $message;
unset(self::$_now[$class]);
}
/**
* Register flash message for current session.
* Note: If already exists same class name value as next context then remove it.
* @param string $class
* @param string $content
*/
static public function now($class, $message) {
self::$_now[$class] = $message;
unset($_SESSION[self::$_session_key][$class]);
}
/**
* Print all flash message with html tag, if registered it.
* If no flash, then does not printetd and return false.
* When printed flash, returns true.
* Notice: Skip print if class name starts with '_'.
* @return boolean
* @param boolean $nl2br
* @param string $addtional_class_name
*/
static public function print_notice($nl2br=true, $addtional_class_name=null) {
$flashes = self::get_all();
$printed = false;
foreach ($flashes as $class => $message) {
if (strpos($class, '_') !== 0) {
self::pop($class);
$class_names = (array) self::$_notice_class_name;
$class_names[] = $class;
if ($addtional_class_name)
$class_names[] = $addtional_class_name;
if ($nl2br)
$message = nl2br($message);
echo sprintf('<div class="%s">%s</div>', implode(' ', $class_names), $message);
$printed = true;
}
}
return $printed;
}
/**
* notice で使用されるデフォルトのクラス名を変更する。
* @param string $class_name
*/
static public function set_notice_class_name($class_name) {
self::$_notice_class_name = $class_name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment