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
|
2 |
|
final public function __sleep() |
34
|
|
|
{ |
35
|
2 |
|
throw new FlashSingletonException('Serialization of Flash is not allowed!'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates flash container from session. |
40
|
|
|
* |
41
|
|
|
* @param TemplateInterface|null $template |
42
|
|
|
* @throws Exceptions\FlashTemplateNotFoundException |
43
|
|
|
*/ |
44
|
58 |
|
public function __construct(TemplateInterface $template = null) |
45
|
|
|
{ |
46
|
58 |
|
if ($assigned = is_null($template)) { |
47
|
56 |
|
$template = TemplateFactory::create(); |
48
|
56 |
|
} |
49
|
|
|
|
50
|
58 |
|
if (!$assigned || !isset(self::$engine)) { |
51
|
6 |
|
self::$engine = new Engine($template); |
52
|
6 |
|
} |
53
|
58 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Invoke Engine methods. |
57
|
|
|
* |
58
|
|
|
* @param string $method - method to invoke |
59
|
|
|
* @param array $arguments - arguments for method |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
54 |
|
protected static function invoke($method, array $arguments) |
64
|
|
|
{ |
65
|
|
|
$target = [ |
66
|
54 |
|
self::$engine, |
67
|
54 |
|
$method, |
68
|
54 |
|
]; |
69
|
|
|
|
70
|
54 |
|
return call_user_func_array($target, $arguments); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Magic methods for static calls. |
75
|
|
|
* |
76
|
|
|
* @param string $method - method to invoke |
77
|
|
|
* @param array $arguments - arguments for method |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
* @throws Exceptions\FlashTemplateNotFoundException |
81
|
|
|
*/ |
82
|
4 |
|
public static function __callStatic($method, array $arguments) |
83
|
|
|
{ |
84
|
4 |
|
new self(); |
85
|
|
|
|
86
|
4 |
|
return self::invoke($method, $arguments); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Magic methods for instances calls. |
91
|
|
|
* |
92
|
|
|
* @param string $method - method to invoke |
93
|
|
|
* @param array $arguments - arguments for method |
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
50 |
|
public function __call($method, array $arguments) |
98
|
|
|
{ |
99
|
50 |
|
return $this->invoke($method, $arguments); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Mimic object __toString method. |
104
|
|
|
* |
105
|
|
|
* @return string - HTML with flash messages |
106
|
|
|
*/ |
107
|
4 |
|
public function __toString() |
108
|
|
|
{ |
109
|
4 |
|
return strval(self::$engine); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|