1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,115 +0,0 @@ |
1 |
-<?php |
|
2 |
- |
|
3 |
-namespace Illuminate\Support\Traits; |
|
4 |
- |
|
5 |
-use Closure; |
|
6 |
-use ReflectionClass; |
|
7 |
-use ReflectionMethod; |
|
8 |
-use BadMethodCallException; |
|
9 |
- |
|
10 |
-trait Macroable |
|
11 |
-{ |
|
12 |
- /** |
|
13 |
- * The registered string macros. |
|
14 |
- * |
|
15 |
- * @var array |
|
16 |
- */ |
|
17 |
- protected static $macros = []; |
|
18 |
- |
|
19 |
- /** |
|
20 |
- * Register a custom macro. |
|
21 |
- * |
|
22 |
- * @param string $name |
|
23 |
- * @param object|callable $macro |
|
24 |
- * |
|
25 |
- * @return void |
|
26 |
- */ |
|
27 |
- public static function macro($name, $macro) |
|
28 |
- { |
|
29 |
- static::$macros[$name] = $macro; |
|
30 |
- } |
|
31 |
- |
|
32 |
- /** |
|
33 |
- * Mix another object into the class. |
|
34 |
- * |
|
35 |
- * @param object $mixin |
|
36 |
- * @param bool $replace |
|
37 |
- * @return void |
|
38 |
- * |
|
39 |
- * @throws \ReflectionException |
|
40 |
- */ |
|
41 |
- public static function mixin($mixin, $replace = true) |
|
42 |
- { |
|
43 |
- $methods = (new ReflectionClass($mixin))->getMethods( |
|
44 |
- ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED |
|
45 |
- ); |
|
46 |
- |
|
47 |
- foreach ($methods as $method) { |
|
48 |
- if ($replace || ! static::hasMacro($method->name)) { |
|
49 |
- $method->setAccessible(true); |
|
50 |
- static::macro($method->name, $method->invoke($mixin)); |
|
51 |
- } |
|
52 |
- } |
|
53 |
- } |
|
54 |
- |
|
55 |
- /** |
|
56 |
- * Checks if macro is registered. |
|
57 |
- * |
|
58 |
- * @param string $name |
|
59 |
- * @return bool |
|
60 |
- */ |
|
61 |
- public static function hasMacro($name) |
|
62 |
- { |
|
63 |
- return isset(static::$macros[$name]); |
|
64 |
- } |
|
65 |
- |
|
66 |
- /** |
|
67 |
- * Dynamically handle calls to the class. |
|
68 |
- * |
|
69 |
- * @param string $method |
|
70 |
- * @param array $parameters |
|
71 |
- * @return mixed |
|
72 |
- * |
|
73 |
- * @throws \BadMethodCallException |
|
74 |
- */ |
|
75 |
- public static function __callStatic($method, $parameters) |
|
76 |
- { |
|
77 |
- if (! static::hasMacro($method)) { |
|
78 |
- throw new BadMethodCallException(sprintf( |
|
79 |
- 'Method %s::%s does not exist.', static::class, $method |
|
80 |
- )); |
|
81 |
- } |
|
82 |
- |
|
83 |
- if (static::$macros[$method] instanceof Closure) { |
|
84 |
- return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters); |
|
85 |
- } |
|
86 |
- |
|
87 |
- return call_user_func_array(static::$macros[$method], $parameters); |
|
88 |
- } |
|
89 |
- |
|
90 |
- /** |
|
91 |
- * Dynamically handle calls to the class. |
|
92 |
- * |
|
93 |
- * @param string $method |
|
94 |
- * @param array $parameters |
|
95 |
- * @return mixed |
|
96 |
- * |
|
97 |
- * @throws \BadMethodCallException |
|
98 |
- */ |
|
99 |
- public function __call($method, $parameters) |
|
100 |
- { |
|
101 |
- if (! static::hasMacro($method)) { |
|
102 |
- throw new BadMethodCallException(sprintf( |
|
103 |
- 'Method %s::%s does not exist.', static::class, $method |
|
104 |
- )); |
|
105 |
- } |
|
106 |
- |
|
107 |
- $macro = static::$macros[$method]; |
|
108 |
- |
|
109 |
- if ($macro instanceof Closure) { |
|
110 |
- return call_user_func_array($macro->bindTo($this, static::class), $parameters); |
|
111 |
- } |
|
112 |
- |
|
113 |
- return call_user_func_array($macro, $parameters); |
|
114 |
- } |
|
115 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,115 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+namespace Illuminate\Support\Traits; |
|
4 |
+ |
|
5 |
+use Closure; |
|
6 |
+use ReflectionClass; |
|
7 |
+use ReflectionMethod; |
|
8 |
+use BadMethodCallException; |
|
9 |
+ |
|
10 |
+trait Macroable |
|
11 |
+{ |
|
12 |
+ /** |
|
13 |
+ * The registered string macros. |
|
14 |
+ * |
|
15 |
+ * @var array |
|
16 |
+ */ |
|
17 |
+ protected static $macros = []; |
|
18 |
+ |
|
19 |
+ /** |
|
20 |
+ * Register a custom macro. |
|
21 |
+ * |
|
22 |
+ * @param string $name |
|
23 |
+ * @param object|callable $macro |
|
24 |
+ * |
|
25 |
+ * @return void |
|
26 |
+ */ |
|
27 |
+ public static function macro($name, $macro) |
|
28 |
+ { |
|
29 |
+ static::$macros[$name] = $macro; |
|
30 |
+ } |
|
31 |
+ |
|
32 |
+ /** |
|
33 |
+ * Mix another object into the class. |
|
34 |
+ * |
|
35 |
+ * @param object $mixin |
|
36 |
+ * @param bool $replace |
|
37 |
+ * @return void |
|
38 |
+ * |
|
39 |
+ * @throws \ReflectionException |
|
40 |
+ */ |
|
41 |
+ public static function mixin($mixin, $replace = true) |
|
42 |
+ { |
|
43 |
+ $methods = (new ReflectionClass($mixin))->getMethods( |
|
44 |
+ ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED |
|
45 |
+ ); |
|
46 |
+ |
|
47 |
+ foreach ($methods as $method) { |
|
48 |
+ if ($replace || ! static::hasMacro($method->name)) { |
|
49 |
+ $method->setAccessible(true); |
|
50 |
+ static::macro($method->name, $method->invoke($mixin)); |
|
51 |
+ } |
|
52 |
+ } |
|
53 |
+ } |
|
54 |
+ |
|
55 |
+ /** |
|
56 |
+ * Checks if macro is registered. |
|
57 |
+ * |
|
58 |
+ * @param string $name |
|
59 |
+ * @return bool |
|
60 |
+ */ |
|
61 |
+ public static function hasMacro($name) |
|
62 |
+ { |
|
63 |
+ return isset(static::$macros[$name]); |
|
64 |
+ } |
|
65 |
+ |
|
66 |
+ /** |
|
67 |
+ * Dynamically handle calls to the class. |
|
68 |
+ * |
|
69 |
+ * @param string $method |
|
70 |
+ * @param array $parameters |
|
71 |
+ * @return mixed |
|
72 |
+ * |
|
73 |
+ * @throws \BadMethodCallException |
|
74 |
+ */ |
|
75 |
+ public static function __callStatic($method, $parameters) |
|
76 |
+ { |
|
77 |
+ if (! static::hasMacro($method)) { |
|
78 |
+ throw new BadMethodCallException(sprintf( |
|
79 |
+ 'Method %s::%s does not exist.', static::class, $method |
|
80 |
+ )); |
|
81 |
+ } |
|
82 |
+ |
|
83 |
+ if (static::$macros[$method] instanceof Closure) { |
|
84 |
+ return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters); |
|
85 |
+ } |
|
86 |
+ |
|
87 |
+ return call_user_func_array(static::$macros[$method], $parameters); |
|
88 |
+ } |
|
89 |
+ |
|
90 |
+ /** |
|
91 |
+ * Dynamically handle calls to the class. |
|
92 |
+ * |
|
93 |
+ * @param string $method |
|
94 |
+ * @param array $parameters |
|
95 |
+ * @return mixed |
|
96 |
+ * |
|
97 |
+ * @throws \BadMethodCallException |
|
98 |
+ */ |
|
99 |
+ public function __call($method, $parameters) |
|
100 |
+ { |
|
101 |
+ if (! static::hasMacro($method)) { |
|
102 |
+ throw new BadMethodCallException(sprintf( |
|
103 |
+ 'Method %s::%s does not exist.', static::class, $method |
|
104 |
+ )); |
|
105 |
+ } |
|
106 |
+ |
|
107 |
+ $macro = static::$macros[$method]; |
|
108 |
+ |
|
109 |
+ if ($macro instanceof Closure) { |
|
110 |
+ return call_user_func_array($macro->bindTo($this, static::class), $parameters); |
|
111 |
+ } |
|
112 |
+ |
|
113 |
+ return call_user_func_array($macro, $parameters); |
|
114 |
+ } |
|
115 |
+} |