1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,192 +0,0 @@ |
1 |
-<?php |
|
2 |
- |
|
3 |
-namespace Illuminate\View\Concerns; |
|
4 |
- |
|
5 |
-use Closure; |
|
6 |
-use Illuminate\Support\Str; |
|
7 |
-use Illuminate\Contracts\View\View as ViewContract; |
|
8 |
- |
|
9 |
-trait ManagesEvents |
|
10 |
-{ |
|
11 |
- /** |
|
12 |
- * Register a view creator event. |
|
13 |
- * |
|
14 |
- * @param array|string $views |
|
15 |
- * @param \Closure|string $callback |
|
16 |
- * @return array |
|
17 |
- */ |
|
18 |
- public function creator($views, $callback) |
|
19 |
- { |
|
20 |
- $creators = []; |
|
21 |
- |
|
22 |
- foreach ((array) $views as $view) { |
|
23 |
- $creators[] = $this->addViewEvent($view, $callback, 'creating: '); |
|
24 |
- } |
|
25 |
- |
|
26 |
- return $creators; |
|
27 |
- } |
|
28 |
- |
|
29 |
- /** |
|
30 |
- * Register multiple view composers via an array. |
|
31 |
- * |
|
32 |
- * @param array $composers |
|
33 |
- * @return array |
|
34 |
- */ |
|
35 |
- public function composers(array $composers) |
|
36 |
- { |
|
37 |
- $registered = []; |
|
38 |
- |
|
39 |
- foreach ($composers as $callback => $views) { |
|
40 |
- $registered = array_merge($registered, $this->composer($views, $callback)); |
|
41 |
- } |
|
42 |
- |
|
43 |
- return $registered; |
|
44 |
- } |
|
45 |
- |
|
46 |
- /** |
|
47 |
- * Register a view composer event. |
|
48 |
- * |
|
49 |
- * @param array|string $views |
|
50 |
- * @param \Closure|string $callback |
|
51 |
- * @return array |
|
52 |
- */ |
|
53 |
- public function composer($views, $callback) |
|
54 |
- { |
|
55 |
- $composers = []; |
|
56 |
- |
|
57 |
- foreach ((array) $views as $view) { |
|
58 |
- $composers[] = $this->addViewEvent($view, $callback, 'composing: '); |
|
59 |
- } |
|
60 |
- |
|
61 |
- return $composers; |
|
62 |
- } |
|
63 |
- |
|
64 |
- /** |
|
65 |
- * Add an event for a given view. |
|
66 |
- * |
|
67 |
- * @param string $view |
|
68 |
- * @param \Closure|string $callback |
|
69 |
- * @param string $prefix |
|
70 |
- * @return \Closure|null |
|
71 |
- */ |
|
72 |
- protected function addViewEvent($view, $callback, $prefix = 'composing: ') |
|
73 |
- { |
|
74 |
- $view = $this->normalizeName($view); |
|
75 |
- |
|
76 |
- if ($callback instanceof Closure) { |
|
77 |
- $this->addEventListener($prefix.$view, $callback); |
|
78 |
- |
|
79 |
- return $callback; |
|
80 |
- } elseif (is_string($callback)) { |
|
81 |
- return $this->addClassEvent($view, $callback, $prefix); |
|
82 |
- } |
|
83 |
- } |
|
84 |
- |
|
85 |
- /** |
|
86 |
- * Register a class based view composer. |
|
87 |
- * |
|
88 |
- * @param string $view |
|
89 |
- * @param string $class |
|
90 |
- * @param string $prefix |
|
91 |
- * @return \Closure |
|
92 |
- */ |
|
93 |
- protected function addClassEvent($view, $class, $prefix) |
|
94 |
- { |
|
95 |
- $name = $prefix.$view; |
|
96 |
- |
|
97 |
- // When registering a class based view "composer", we will simply resolve the |
|
98 |
- // classes from the application IoC container then call the compose method |
|
99 |
- // on the instance. This allows for convenient, testable view composers. |
|
100 |
- $callback = $this->buildClassEventCallback( |
|
101 |
- $class, $prefix |
|
102 |
- ); |
|
103 |
- |
|
104 |
- $this->addEventListener($name, $callback); |
|
105 |
- |
|
106 |
- return $callback; |
|
107 |
- } |
|
108 |
- |
|
109 |
- /** |
|
110 |
- * Build a class based container callback Closure. |
|
111 |
- * |
|
112 |
- * @param string $class |
|
113 |
- * @param string $prefix |
|
114 |
- * @return \Closure |
|
115 |
- */ |
|
116 |
- protected function buildClassEventCallback($class, $prefix) |
|
117 |
- { |
|
118 |
- [$class, $method] = $this->parseClassEvent($class, $prefix); |
|
119 |
- |
|
120 |
- // Once we have the class and method name, we can build the Closure to resolve |
|
121 |
- // the instance out of the IoC container and call the method on it with the |
|
122 |
- // given arguments that are passed to the Closure as the composer's data. |
|
123 |
- return function () use ($class, $method) { |
|
124 |
- return call_user_func_array( |
|
125 |
- [$this->container->make($class), $method], func_get_args() |
|
126 |
- ); |
|
127 |
- }; |
|
128 |
- } |
|
129 |
- |
|
130 |
- /** |
|
131 |
- * Parse a class based composer name. |
|
132 |
- * |
|
133 |
- * @param string $class |
|
134 |
- * @param string $prefix |
|
135 |
- * @return array |
|
136 |
- */ |
|
137 |
- protected function parseClassEvent($class, $prefix) |
|
138 |
- { |
|
139 |
- return Str::parseCallback($class, $this->classEventMethodForPrefix($prefix)); |
|
140 |
- } |
|
141 |
- |
|
142 |
- /** |
|
143 |
- * Determine the class event method based on the given prefix. |
|
144 |
- * |
|
145 |
- * @param string $prefix |
|
146 |
- * @return string |
|
147 |
- */ |
|
148 |
- protected function classEventMethodForPrefix($prefix) |
|
149 |
- { |
|
150 |
- return Str::contains($prefix, 'composing') ? 'compose' : 'create'; |
|
151 |
- } |
|
152 |
- |
|
153 |
- /** |
|
154 |
- * Add a listener to the event dispatcher. |
|
155 |
- * |
|
156 |
- * @param string $name |
|
157 |
- * @param \Closure $callback |
|
158 |
- * @return void |
|
159 |
- */ |
|
160 |
- protected function addEventListener($name, $callback) |
|
161 |
- { |
|
162 |
- if (Str::contains($name, '*')) { |
|
163 |
- $callback = function ($name, array $data) use ($callback) { |
|
164 |
- return $callback($data[0]); |
|
165 |
- }; |
|
166 |
- } |
|
167 |
- |
|
168 |
- $this->events->listen($name, $callback); |
|
169 |
- } |
|
170 |
- |
|
171 |
- /** |
|
172 |
- * Call the composer for a given view. |
|
173 |
- * |
|
174 |
- * @param \Illuminate\Contracts\View\View $view |
|
175 |
- * @return void |
|
176 |
- */ |
|
177 |
- public function callComposer(ViewContract $view) |
|
178 |
- { |
|
179 |
- $this->events->dispatch('composing: '.$view->name(), [$view]); |
|
180 |
- } |
|
181 |
- |
|
182 |
- /** |
|
183 |
- * Call the creator for a given view. |
|
184 |
- * |
|
185 |
- * @param \Illuminate\Contracts\View\View $view |
|
186 |
- * @return void |
|
187 |
- */ |
|
188 |
- public function callCreator(ViewContract $view) |
|
189 |
- { |
|
190 |
- $this->events->dispatch('creating: '.$view->name(), [$view]); |
|
191 |
- } |
|
192 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,192 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+namespace Illuminate\View\Concerns; |
|
4 |
+ |
|
5 |
+use Closure; |
|
6 |
+use Illuminate\Support\Str; |
|
7 |
+use Illuminate\Contracts\View\View as ViewContract; |
|
8 |
+ |
|
9 |
+trait ManagesEvents |
|
10 |
+{ |
|
11 |
+ /** |
|
12 |
+ * Register a view creator event. |
|
13 |
+ * |
|
14 |
+ * @param array|string $views |
|
15 |
+ * @param \Closure|string $callback |
|
16 |
+ * @return array |
|
17 |
+ */ |
|
18 |
+ public function creator($views, $callback) |
|
19 |
+ { |
|
20 |
+ $creators = []; |
|
21 |
+ |
|
22 |
+ foreach ((array) $views as $view) { |
|
23 |
+ $creators[] = $this->addViewEvent($view, $callback, 'creating: '); |
|
24 |
+ } |
|
25 |
+ |
|
26 |
+ return $creators; |
|
27 |
+ } |
|
28 |
+ |
|
29 |
+ /** |
|
30 |
+ * Register multiple view composers via an array. |
|
31 |
+ * |
|
32 |
+ * @param array $composers |
|
33 |
+ * @return array |
|
34 |
+ */ |
|
35 |
+ public function composers(array $composers) |
|
36 |
+ { |
|
37 |
+ $registered = []; |
|
38 |
+ |
|
39 |
+ foreach ($composers as $callback => $views) { |
|
40 |
+ $registered = array_merge($registered, $this->composer($views, $callback)); |
|
41 |
+ } |
|
42 |
+ |
|
43 |
+ return $registered; |
|
44 |
+ } |
|
45 |
+ |
|
46 |
+ /** |
|
47 |
+ * Register a view composer event. |
|
48 |
+ * |
|
49 |
+ * @param array|string $views |
|
50 |
+ * @param \Closure|string $callback |
|
51 |
+ * @return array |
|
52 |
+ */ |
|
53 |
+ public function composer($views, $callback) |
|
54 |
+ { |
|
55 |
+ $composers = []; |
|
56 |
+ |
|
57 |
+ foreach ((array) $views as $view) { |
|
58 |
+ $composers[] = $this->addViewEvent($view, $callback, 'composing: '); |
|
59 |
+ } |
|
60 |
+ |
|
61 |
+ return $composers; |
|
62 |
+ } |
|
63 |
+ |
|
64 |
+ /** |
|
65 |
+ * Add an event for a given view. |
|
66 |
+ * |
|
67 |
+ * @param string $view |
|
68 |
+ * @param \Closure|string $callback |
|
69 |
+ * @param string $prefix |
|
70 |
+ * @return \Closure|null |
|
71 |
+ */ |
|
72 |
+ protected function addViewEvent($view, $callback, $prefix = 'composing: ') |
|
73 |
+ { |
|
74 |
+ $view = $this->normalizeName($view); |
|
75 |
+ |
|
76 |
+ if ($callback instanceof Closure) { |
|
77 |
+ $this->addEventListener($prefix.$view, $callback); |
|
78 |
+ |
|
79 |
+ return $callback; |
|
80 |
+ } elseif (is_string($callback)) { |
|
81 |
+ return $this->addClassEvent($view, $callback, $prefix); |
|
82 |
+ } |
|
83 |
+ } |
|
84 |
+ |
|
85 |
+ /** |
|
86 |
+ * Register a class based view composer. |
|
87 |
+ * |
|
88 |
+ * @param string $view |
|
89 |
+ * @param string $class |
|
90 |
+ * @param string $prefix |
|
91 |
+ * @return \Closure |
|
92 |
+ */ |
|
93 |
+ protected function addClassEvent($view, $class, $prefix) |
|
94 |
+ { |
|
95 |
+ $name = $prefix.$view; |
|
96 |
+ |
|
97 |
+ // When registering a class based view "composer", we will simply resolve the |
|
98 |
+ // classes from the application IoC container then call the compose method |
|
99 |
+ // on the instance. This allows for convenient, testable view composers. |
|
100 |
+ $callback = $this->buildClassEventCallback( |
|
101 |
+ $class, $prefix |
|
102 |
+ ); |
|
103 |
+ |
|
104 |
+ $this->addEventListener($name, $callback); |
|
105 |
+ |
|
106 |
+ return $callback; |
|
107 |
+ } |
|
108 |
+ |
|
109 |
+ /** |
|
110 |
+ * Build a class based container callback Closure. |
|
111 |
+ * |
|
112 |
+ * @param string $class |
|
113 |
+ * @param string $prefix |
|
114 |
+ * @return \Closure |
|
115 |
+ */ |
|
116 |
+ protected function buildClassEventCallback($class, $prefix) |
|
117 |
+ { |
|
118 |
+ [$class, $method] = $this->parseClassEvent($class, $prefix); |
|
119 |
+ |
|
120 |
+ // Once we have the class and method name, we can build the Closure to resolve |
|
121 |
+ // the instance out of the IoC container and call the method on it with the |
|
122 |
+ // given arguments that are passed to the Closure as the composer's data. |
|
123 |
+ return function () use ($class, $method) { |
|
124 |
+ return call_user_func_array( |
|
125 |
+ [$this->container->make($class), $method], func_get_args() |
|
126 |
+ ); |
|
127 |
+ }; |
|
128 |
+ } |
|
129 |
+ |
|
130 |
+ /** |
|
131 |
+ * Parse a class based composer name. |
|
132 |
+ * |
|
133 |
+ * @param string $class |
|
134 |
+ * @param string $prefix |
|
135 |
+ * @return array |
|
136 |
+ */ |
|
137 |
+ protected function parseClassEvent($class, $prefix) |
|
138 |
+ { |
|
139 |
+ return Str::parseCallback($class, $this->classEventMethodForPrefix($prefix)); |
|
140 |
+ } |
|
141 |
+ |
|
142 |
+ /** |
|
143 |
+ * Determine the class event method based on the given prefix. |
|
144 |
+ * |
|
145 |
+ * @param string $prefix |
|
146 |
+ * @return string |
|
147 |
+ */ |
|
148 |
+ protected function classEventMethodForPrefix($prefix) |
|
149 |
+ { |
|
150 |
+ return Str::contains($prefix, 'composing') ? 'compose' : 'create'; |
|
151 |
+ } |
|
152 |
+ |
|
153 |
+ /** |
|
154 |
+ * Add a listener to the event dispatcher. |
|
155 |
+ * |
|
156 |
+ * @param string $name |
|
157 |
+ * @param \Closure $callback |
|
158 |
+ * @return void |
|
159 |
+ */ |
|
160 |
+ protected function addEventListener($name, $callback) |
|
161 |
+ { |
|
162 |
+ if (Str::contains($name, '*')) { |
|
163 |
+ $callback = function ($name, array $data) use ($callback) { |
|
164 |
+ return $callback($data[0]); |
|
165 |
+ }; |
|
166 |
+ } |
|
167 |
+ |
|
168 |
+ $this->events->listen($name, $callback); |
|
169 |
+ } |
|
170 |
+ |
|
171 |
+ /** |
|
172 |
+ * Call the composer for a given view. |
|
173 |
+ * |
|
174 |
+ * @param \Illuminate\Contracts\View\View $view |
|
175 |
+ * @return void |
|
176 |
+ */ |
|
177 |
+ public function callComposer(ViewContract $view) |
|
178 |
+ { |
|
179 |
+ $this->events->dispatch('composing: '.$view->name(), [$view]); |
|
180 |
+ } |
|
181 |
+ |
|
182 |
+ /** |
|
183 |
+ * Call the creator for a given view. |
|
184 |
+ * |
|
185 |
+ * @param \Illuminate\Contracts\View\View $view |
|
186 |
+ * @return void |
|
187 |
+ */ |
|
188 |
+ public function callCreator(ViewContract $view) |
|
189 |
+ { |
|
190 |
+ $this->events->dispatch('creating: '.$view->name(), [$view]); |
|
191 |
+ } |
|
192 |
+} |