Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\View\Concerns;
4
 
5
use Closure;
6
use Illuminate\Contracts\View\View as ViewContract;
7
use Illuminate\Support\Str;
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);
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 $this->container->make($class)->{$method}(...func_get_args());
125
        };
126
    }
127
 
128
    /**
129
     * Parse a class based composer name.
130
     *
131
     * @param  string  $class
132
     * @param  string  $prefix
133
     * @return array
134
     */
135
    protected function parseClassEvent($class, $prefix)
136
    {
137
        return Str::parseCallback($class, $this->classEventMethodForPrefix($prefix));
138
    }
139
 
140
    /**
141
     * Determine the class event method based on the given prefix.
142
     *
143
     * @param  string  $prefix
144
     * @return string
145
     */
146
    protected function classEventMethodForPrefix($prefix)
147
    {
148
        return str_contains($prefix, 'composing') ? 'compose' : 'create';
149
    }
150
 
151
    /**
152
     * Add a listener to the event dispatcher.
153
     *
154
     * @param  string  $name
155
     * @param  \Closure  $callback
156
     * @return void
157
     */
158
    protected function addEventListener($name, $callback)
159
    {
160
        if (str_contains($name, '*')) {
161
            $callback = function ($name, array $data) use ($callback) {
162
                return $callback($data[0]);
163
            };
164
        }
165
 
166
        $this->events->listen($name, $callback);
167
    }
168
 
169
    /**
170
     * Call the composer for a given view.
171
     *
172
     * @param  \Illuminate\Contracts\View\View  $view
173
     * @return void
174
     */
175
    public function callComposer(ViewContract $view)
176
    {
177
        $this->events->dispatch('composing: '.$view->name(), [$view]);
178
    }
179
 
180
    /**
181
     * Call the creator for a given view.
182
     *
183
     * @param  \Illuminate\Contracts\View\View  $view
184
     * @return void
185
     */
186
    public function callCreator(ViewContract $view)
187
    {
188
        $this->events->dispatch('creating: '.$view->name(), [$view]);
189
    }
190
}