/
IamDecoratable.php
71 lines (61 loc) · 1.64 KB
/
IamDecoratable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace Imanghafoori\Decorator;
trait IamDecoratable
{
/**
* The decorators for resolved object instances of the facade.
*
* @var array
*/
protected static $decorations = [];
/**
* The decorators for all the methods.
*
* @var array
*/
protected static $classDecorations = [];
public static function decorateMethod($method, $decorator)
{
foreach ((array) $method as $m) {
static::$decorations[$m][] = $decorator;
}
}
public static function decorateAll($decorator)
{
foreach ((array) $decorator as $d) {
static::$classDecorations[] = $d;
}
}
public static function getDecorations($method)
{
return static::$decorations[$method] ?? [];
}
public static function forgetDecorations($method = null)
{
if ($method) {
unset(static::$decorations[$method]);
} elseif (is_null($method)) {
static::$decorations = [];
static::$classDecorations = [];
}
}
/**
* Handle dynamic, static calls to the object.
*
* @param string $method
* @param array $args
*
* @throws \RuntimeException
*
* @return mixed
*/
public static function __callStatic($method, $args)
{
$callback = function (...$args) use ($method) {
return parent::__callStatic($method, $args);
};
$decorators = self::getDecorations($method) + static::$classDecorations;
$callback = app(Decorator::class)->decorateWith($callback, $decorators);
return $callback(...$args);
}
}