1 | <?php |
||
27 | class Widget extends Component implements ViewContextInterface |
||
28 | { |
||
29 | /** |
||
30 | * @event Event an event that is triggered when the widget is initialized via [[init()]]. |
||
31 | * @since 2.0.11 |
||
32 | */ |
||
33 | const EVENT_INIT = 'init'; |
||
34 | /** |
||
35 | * @event WidgetEvent an event raised right before executing a widget. |
||
36 | * You may set [[WidgetEvent::isValid]] to be false to cancel the widget execution. |
||
37 | * @since 2.0.11 |
||
38 | */ |
||
39 | const EVENT_BEFORE_RUN = 'beforeRun'; |
||
40 | /** |
||
41 | * @event WidgetEvent an event raised right after executing a widget. |
||
42 | * @since 2.0.11 |
||
43 | */ |
||
44 | const EVENT_AFTER_RUN = 'afterRun'; |
||
45 | |||
46 | /** |
||
47 | * @var int a counter used to generate [[id]] for widgets. |
||
48 | * @internal |
||
49 | */ |
||
50 | public static $counter = 0; |
||
51 | /** |
||
52 | * @var string the prefix to the automatically generated widget IDs. |
||
53 | * @see getId() |
||
54 | */ |
||
55 | public static $autoIdPrefix = 'w'; |
||
56 | /** |
||
57 | * @var Widget[] the widgets that are currently being rendered (not ended). This property |
||
58 | * is maintained by [[begin()]] and [[end()]] methods. |
||
59 | * @internal |
||
60 | */ |
||
61 | public static $stack = []; |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Initializes the object. |
||
66 | * This method is called at the end of the constructor. |
||
67 | * The default implementation will trigger an [[EVENT_INIT]] event. |
||
68 | */ |
||
69 | 74 | public function init() |
|
70 | { |
||
71 | 74 | parent::init(); |
|
72 | 74 | $this->trigger(self::EVENT_INIT); |
|
73 | 74 | } |
|
74 | |||
75 | /** |
||
76 | * Begins a widget. |
||
77 | * This method creates an instance of the calling class. It will apply the configuration |
||
78 | * to the created instance. A matching [[end()]] call should be called later. |
||
79 | * As some widgets may use output buffering, the [[end()]] call should be made in the same view |
||
80 | * to avoid breaking the nesting of output buffers. |
||
81 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
82 | * @return static the newly created widget instance |
||
83 | * @see end() |
||
84 | */ |
||
85 | 42 | public static function begin($config = []) |
|
94 | |||
95 | /** |
||
96 | * Ends a widget. |
||
97 | * Note that the rendering result of the widget is directly echoed out. |
||
98 | * @return static the widget instance that is ended. |
||
99 | * @throws InvalidCallException if [[begin()]] and [[end()]] calls are not properly nested |
||
100 | * @see begin() |
||
101 | */ |
||
102 | 43 | public static function end() |
|
103 | { |
||
104 | 43 | if (!empty(static::$stack)) { |
|
105 | 42 | $widget = array_pop(static::$stack); |
|
106 | 42 | if (get_class($widget) === get_called_class()) { |
|
107 | /* @var $widget Widget */ |
||
108 | 42 | if ($widget->beforeRun()) { |
|
109 | 42 | $result = $widget->run(); |
|
110 | 42 | $result = $widget->afterRun($result); |
|
111 | 42 | echo $result; |
|
112 | } |
||
113 | |||
114 | 42 | return $widget; |
|
115 | } |
||
116 | |||
117 | throw new InvalidCallException('Expecting end() of ' . get_class($widget) . ', found ' . get_called_class()); |
||
118 | } |
||
119 | |||
120 | 1 | throw new InvalidCallException('Unexpected ' . get_called_class() . '::end() call. A matching begin() is not found.'); |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * Creates a widget instance and runs it. |
||
125 | * The widget rendering result is returned by this method. |
||
126 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
127 | * @return string the rendering result of the widget. |
||
128 | * @throws \Exception |
||
129 | */ |
||
130 | 33 | public static function widget($config = []) |
|
153 | |||
154 | private $_id; |
||
155 | |||
156 | /** |
||
157 | * Returns the ID of the widget. |
||
158 | * @param bool $autoGenerate whether to generate an ID if it is not set previously |
||
159 | * @return string ID of the widget. |
||
160 | */ |
||
161 | 85 | public function getId($autoGenerate = true) |
|
169 | |||
170 | /** |
||
171 | * Sets the ID of the widget. |
||
172 | * @param string $value id of the widget. |
||
173 | */ |
||
174 | 27 | public function setId($value) |
|
178 | |||
179 | private $_view; |
||
180 | |||
181 | /** |
||
182 | * Returns the view object that can be used to render views or view files. |
||
183 | * The [[render()]] and [[renderFile()]] methods will use |
||
184 | * this view object to implement the actual view rendering. |
||
185 | * If not set, it will default to the "view" application component. |
||
186 | * @return \yii\web\View the view object that can be used to render views or view files. |
||
187 | */ |
||
188 | 28 | public function getView() |
|
196 | |||
197 | /** |
||
198 | * Sets the view object to be used by this widget. |
||
199 | * @param View $view the view object that can be used to render views or view files. |
||
200 | */ |
||
201 | 51 | public function setView($view) |
|
205 | |||
206 | /** |
||
207 | * Executes the widget. |
||
208 | * @return string the result of widget execution to be outputted. |
||
209 | */ |
||
210 | public function run() |
||
213 | |||
214 | /** |
||
215 | * Renders a view. |
||
216 | * |
||
217 | * The view to be rendered can be specified in one of the following formats: |
||
218 | * |
||
219 | * - [path alias](guide:concept-aliases) (e.g. "@app/views/site/index"); |
||
220 | * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes. |
||
221 | * The actual view file will be looked for under the [[Application::viewPath|view path]] of the application. |
||
222 | * - absolute path within module (e.g. "/site/index"): the view name starts with a single slash. |
||
223 | * The actual view file will be looked for under the [[Module::viewPath|view path]] of the currently |
||
224 | * active module. |
||
225 | * - relative path (e.g. "index"): the actual view file will be looked for under [[viewPath]]. |
||
226 | * |
||
227 | * If the view name does not contain a file extension, it will use the default one `.php`. |
||
228 | * |
||
229 | * @param string $view the view name. |
||
230 | * @param array $params the parameters (name-value pairs) that should be made available in the view. |
||
231 | * @return string the rendering result. |
||
232 | * @throws InvalidArgumentException if the view file does not exist. |
||
233 | */ |
||
234 | public function render($view, $params = []) |
||
238 | |||
239 | /** |
||
240 | * Renders a view file. |
||
241 | * @param string $file the view file to be rendered. This can be either a file path or a [path alias](guide:concept-aliases). |
||
242 | * @param array $params the parameters (name-value pairs) that should be made available in the view. |
||
243 | * @return string the rendering result. |
||
244 | * @throws InvalidArgumentException if the view file does not exist. |
||
245 | */ |
||
246 | public function renderFile($file, $params = []) |
||
250 | |||
251 | /** |
||
252 | * Returns the directory containing the view files for this widget. |
||
253 | * The default implementation returns the 'views' subdirectory under the directory containing the widget class file. |
||
254 | * @return string the directory containing the view files for this widget. |
||
255 | */ |
||
256 | public function getViewPath() |
||
262 | |||
263 | /** |
||
264 | * This method is invoked right before the widget is executed. |
||
265 | * |
||
266 | * The method will trigger the [[EVENT_BEFORE_RUN]] event. The return value of the method |
||
267 | * will determine whether the widget should continue to run. |
||
268 | * |
||
269 | * When overriding this method, make sure you call the parent implementation like the following: |
||
270 | * |
||
271 | * ```php |
||
272 | * public function beforeRun() |
||
273 | * { |
||
274 | * if (!parent::beforeRun()) { |
||
275 | * return false; |
||
276 | * } |
||
277 | * |
||
278 | * // your custom code here |
||
279 | * |
||
280 | * return true; // or false to not run the widget |
||
281 | * } |
||
282 | * ``` |
||
283 | * |
||
284 | * @return bool whether the widget should continue to be executed. |
||
285 | * @since 2.0.11 |
||
286 | */ |
||
287 | 74 | public function beforeRun() |
|
293 | |||
294 | /** |
||
295 | * This method is invoked right after a widget is executed. |
||
296 | * |
||
297 | * The method will trigger the [[EVENT_AFTER_RUN]] event. The return value of the method |
||
298 | * will be used as the widget return value. |
||
299 | * |
||
300 | * If you override this method, your code should look like the following: |
||
301 | * |
||
302 | * ```php |
||
303 | * public function afterRun($result) |
||
304 | * { |
||
305 | * $result = parent::afterRun($result); |
||
306 | * // your custom code here |
||
307 | * return $result; |
||
308 | * } |
||
309 | * ``` |
||
310 | * |
||
311 | * @param mixed $result the widget return result. |
||
312 | * @return mixed the processed widget result. |
||
313 | * @since 2.0.11 |
||
314 | */ |
||
315 | 73 | public function afterRun($result) |
|
322 | } |
||
323 |