1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tamtamchik\SimpleFlash; |
4
|
|
|
|
5
|
|
|
use Tamtamchik\SimpleFlash\Exceptions\FlashSingletonException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Flash. |
9
|
|
|
* |
10
|
|
|
* @method static Engine message($message, $type = 'info') Base method for adding messages to flash. |
11
|
|
|
* @method static string display($type = null) Returns Bootstrap ready HTML for Engine messages. |
12
|
|
|
* @method static bool hasMessages($type = null) Returns if there are any messages in container. |
13
|
|
|
* @method static Engine clear($type = null) Clears messages from session store. |
14
|
|
|
* @method static Engine error($message) Shortcut for error message. |
15
|
|
|
* @method static Engine warning($message) Shortcut for warning message. |
16
|
|
|
* @method static Engine info($message) Shortcut for info message. |
17
|
|
|
* @method static Engine success($message) Shortcut for success message. |
18
|
|
|
* @method static Engine setTemplate(TemplateInterface $template) Change render template. |
19
|
|
|
* @method static TemplateInterface getTemplate() Get template for modifications. |
20
|
|
|
*/ |
21
|
|
|
class Flash |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Base instance of Flash engine. |
25
|
|
|
* |
26
|
|
|
* @var Engine |
27
|
|
|
*/ |
28
|
|
|
private static $engine; |
29
|
|
|
|
30
|
|
|
// Don't allow instantiation |
31
|
|
|
final private function __clone() |
32
|
|
|
{ |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
final public function __sleep() |
36
|
|
|
{ |
37
|
3 |
|
throw new FlashSingletonException('Serialization of Flash is not allowed!'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Creates flash container from session. |
42
|
|
|
* |
43
|
|
|
* @param TemplateInterface|null $template |
44
|
|
|
* @throws Exceptions\FlashTemplateNotFoundException |
45
|
|
|
*/ |
46
|
87 |
|
public function __construct(TemplateInterface $template = null) |
47
|
|
|
{ |
48
|
87 |
|
if ($assigned = is_null($template)) { |
49
|
84 |
|
$template = TemplateFactory::create(); |
50
|
56 |
|
} |
51
|
|
|
|
52
|
87 |
|
if (!$assigned || !isset(self::$engine)) { |
53
|
9 |
|
self::$engine = new Engine($template); |
54
|
6 |
|
} |
55
|
87 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Invoke Engine methods. |
59
|
|
|
* |
60
|
|
|
* @param string $method - method to invoke |
61
|
|
|
* @param array $arguments - arguments for method |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
81 |
|
protected static function invoke($method, array $arguments) |
66
|
|
|
{ |
67
|
|
|
$target = [ |
68
|
81 |
|
self::$engine, |
69
|
81 |
|
$method, |
70
|
54 |
|
]; |
71
|
|
|
|
72
|
81 |
|
return call_user_func_array($target, $arguments); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Magic methods for static calls. |
77
|
|
|
* |
78
|
|
|
* @param string $method - method to invoke |
79
|
|
|
* @param array $arguments - arguments for method |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
* @throws Exceptions\FlashTemplateNotFoundException |
83
|
|
|
*/ |
84
|
6 |
|
public static function __callStatic($method, array $arguments) |
85
|
|
|
{ |
86
|
6 |
|
new self(); |
87
|
|
|
|
88
|
6 |
|
return self::invoke($method, $arguments); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Magic methods for instances calls. |
93
|
|
|
* |
94
|
|
|
* @param string $method - method to invoke |
95
|
|
|
* @param array $arguments - arguments for method |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
75 |
|
public function __call($method, array $arguments) |
100
|
|
|
{ |
101
|
75 |
|
return $this->invoke($method, $arguments); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Mimic object __toString method. |
106
|
|
|
* |
107
|
|
|
* @return string - HTML with flash messages |
108
|
|
|
*/ |
109
|
6 |
|
public function __toString() |
110
|
|
|
{ |
111
|
6 |
|
return strval(self::$engine); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|