Completed
Push — master ( 17a1d9...0beb59 )
by Alexander
41:44
created

View::renderPhpFile()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 11
cts 11
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 13
nop 2
crap 7
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\base;
9
10
use Yii;
11
use yii\helpers\FileHelper;
12
use yii\widgets\Block;
13
use yii\widgets\ContentDecorator;
14
use yii\widgets\FragmentCache;
15
16
/**
17
 * View represents a view object in the MVC pattern.
18
 *
19
 * View provides a set of methods (e.g. [[render()]]) for rendering purpose.
20
 *
21
 * For more details and usage information on View, see the [guide article on views](guide:structure-views).
22
 *
23
 * @property string|bool $viewFile The view file currently being rendered. False if no view file is being
24
 * rendered. This property is read-only.
25
 *
26
 * @author Qiang Xue <[email protected]>
27
 * @since 2.0
28
 */
29
class View extends Component
30
{
31
    /**
32
     * @event Event an event that is triggered by [[beginPage()]].
33
     */
34
    const EVENT_BEGIN_PAGE = 'beginPage';
35
    /**
36
     * @event Event an event that is triggered by [[endPage()]].
37
     */
38
    const EVENT_END_PAGE = 'endPage';
39
    /**
40
     * @event ViewEvent an event that is triggered by [[renderFile()]] right before it renders a view file.
41
     */
42
    const EVENT_BEFORE_RENDER = 'beforeRender';
43
    /**
44
     * @event ViewEvent an event that is triggered by [[renderFile()]] right after it renders a view file.
45
     */
46
    const EVENT_AFTER_RENDER = 'afterRender';
47
48
    /**
49
     * @var ViewContextInterface the context under which the [[renderFile()]] method is being invoked.
50
     */
51
    public $context;
52
    /**
53
     * @var mixed custom parameters that are shared among view templates.
54
     */
55
    public $params = [];
56
    /**
57
     * @var array a list of available renderers indexed by their corresponding supported file extensions.
58
     * Each renderer may be a view renderer object or the configuration for creating the renderer object.
59
     * For example, the following configuration enables both Smarty and Twig view renderers:
60
     *
61
     * ```php
62
     * [
63
     *     'tpl' => ['class' => 'yii\smarty\ViewRenderer'],
64
     *     'twig' => ['class' => 'yii\twig\ViewRenderer'],
65
     * ]
66
     * ```
67
     *
68
     * If no renderer is available for the given view file, the view file will be treated as a normal PHP
69
     * and rendered via [[renderPhpFile()]].
70
     */
71
    public $renderers;
72
    /**
73
     * @var string the default view file extension. This will be appended to view file names if they don't have file extensions.
74
     */
75
    public $defaultExtension = 'php';
76
    /**
77
     * @var Theme|array|string the theme object or the configuration for creating the theme object.
78
     * If not set, it means theming is not enabled.
79
     */
80
    public $theme;
81
    /**
82
     * @var array a list of named output blocks. The keys are the block names and the values
83
     * are the corresponding block content. You can call [[beginBlock()]] and [[endBlock()]]
84
     * to capture small fragments of a view. They can be later accessed somewhere else
85
     * through this property.
86
     */
87
    public $blocks;
88
    /**
89
     * @var array a list of currently active fragment cache widgets. This property
90
     * is used internally to implement the content caching feature. Do not modify it directly.
91
     * @internal
92
     */
93
    public $cacheStack = [];
94
    /**
95
     * @var array a list of placeholders for embedding dynamic contents. This property
96
     * is used internally to implement the content caching feature. Do not modify it directly.
97
     * @internal
98
     */
99
    public $dynamicPlaceholders = [];
100
101
    /**
102
     * @var array the view files currently being rendered. There may be multiple view files being
103
     * rendered at a moment because one view may be rendered within another.
104
     */
105
    private $_viewFiles = [];
106
107
108
    /**
109
     * Initializes the view component.
110
     */
111 136
    public function init()
112
    {
113 136
        parent::init();
114 136
        if (is_array($this->theme)) {
115
            if (!isset($this->theme['class'])) {
116
                $this->theme['class'] = 'yii\base\Theme';
117
            }
118
            $this->theme = Yii::createObject($this->theme);
119 136
        } elseif (is_string($this->theme)) {
120
            $this->theme = Yii::createObject($this->theme);
121
        }
122 136
    }
123
124
    /**
125
     * Renders a view.
126
     *
127
     * The view to be rendered can be specified in one of the following formats:
128
     *
129
     * - path alias (e.g. "@app/views/site/index");
130
     * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes.
131
     *   The actual view file will be looked for under the [[Application::viewPath|view path]] of the application.
132
     * - absolute path within current module (e.g. "/site/index"): the view name starts with a single slash.
133
     *   The actual view file will be looked for under the [[Module::viewPath|view path]] of the [[Controller::module|current module]].
134
     * - relative view (e.g. "index"): the view name does not start with `@` or `/`. The corresponding view file will be
135
     *   looked for under the [[ViewContextInterface::getViewPath()|view path]] of the view `$context`.
136
     *   If `$context` is not given, it will be looked for under the directory containing the view currently
137
     *   being rendered (i.e., this happens when rendering a view within another view).
138
     *
139
     * @param string $view the view name.
140
     * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
141
     * @param object $context the context to be assigned to the view and can later be accessed via [[context]]
142
     * in the view. If the context implements [[ViewContextInterface]], it may also be used to locate
143
     * the view file corresponding to a relative view name.
144
     * @return string the rendering result
145
     * @throws ViewNotFoundException if the view file does not exist.
146
     * @throws InvalidCallException if the view cannot be resolved.
147
     * @see renderFile()
148
     */
149 32
    public function render($view, $params = [], $context = null)
150
    {
151 32
        $viewFile = $this->findViewFile($view, $context);
152 32
        return $this->renderFile($viewFile, $params, $context);
0 ignored issues
show
Bug introduced by
It seems like $viewFile defined by $this->findViewFile($view, $context) on line 151 can also be of type boolean; however, yii\base\View::renderFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
153
    }
154
155
    /**
156
     * Finds the view file based on the given view name.
157
     * @param string $view the view name or the path alias of the view file. Please refer to [[render()]]
158
     * on how to specify this parameter.
159
     * @param object $context the context to be assigned to the view and can later be accessed via [[context]]
160
     * in the view. If the context implements [[ViewContextInterface]], it may also be used to locate
161
     * the view file corresponding to a relative view name.
162
     * @return string the view file path. Note that the file may not exist.
163
     * @throws InvalidCallException if a relative view name is given while there is no active context to
164
     * determine the corresponding view file.
165
     */
166 32
    protected function findViewFile($view, $context = null)
167
    {
168 32
        if (strncmp($view, '@', 1) === 0) {
169
            // e.g. "@app/views/main"
170 21
            $file = Yii::getAlias($view);
171 11
        } elseif (strncmp($view, '//', 2) === 0) {
172
            // e.g. "//layouts/main"
173
            $file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
174 11
        } elseif (strncmp($view, '/', 1) === 0) {
175
            // e.g. "/site/index"
176
            if (Yii::$app->controller !== null) {
177
                $file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
178
            } else {
179
                throw new InvalidCallException("Unable to locate view file for view '$view': no active controller.");
180
            }
181 11
        } elseif ($context instanceof ViewContextInterface) {
182 6
            $file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;
183 5
        } elseif (($currentViewFile = $this->getViewFile()) !== false) {
184 5
            $file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view;
185
        } else {
186
            throw new InvalidCallException("Unable to resolve view file for view '$view': no active view context.");
187
        }
188
189 32
        if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
190 20
            return $file;
191
        }
192 12
        $path = $file . '.' . $this->defaultExtension;
193 12
        if ($this->defaultExtension !== 'php' && !is_file($path)) {
194
            $path = $file . '.php';
195
        }
196
197 12
        return $path;
198
    }
199
200
    /**
201
     * Renders a view file.
202
     *
203
     * If [[theme]] is enabled (not null), it will try to render the themed version of the view file as long
204
     * as it is available.
205
     *
206
     * The method will call [[FileHelper::localize()]] to localize the view file.
207
     *
208
     * If [[renderers|renderer]] is enabled (not null), the method will use it to render the view file.
209
     * Otherwise, it will simply include the view file as a normal PHP file, capture its output and
210
     * return it as a string.
211
     *
212
     * @param string $viewFile the view file. This can be either an absolute file path or an alias of it.
213
     * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
214
     * @param object $context the context that the view should use for rendering the view. If null,
215
     * existing [[context]] will be used.
216
     * @return string the rendering result
217
     * @throws ViewNotFoundException if the view file does not exist
218
     */
219 69
    public function renderFile($viewFile, $params = [], $context = null)
220
    {
221 69
        $viewFile = Yii::getAlias($viewFile);
222
223 69
        if ($this->theme !== null) {
224
            $viewFile = $this->theme->applyTo($viewFile);
225
        }
226 69
        if (is_file($viewFile)) {
227 68
            $viewFile = FileHelper::localize($viewFile);
228
        } else {
229 1
            throw new ViewNotFoundException("The view file does not exist: $viewFile");
230
        }
231
232 68
        $oldContext = $this->context;
233 68
        if ($context !== null) {
234 22
            $this->context = $context;
235
        }
236 68
        $output = '';
237 68
        $this->_viewFiles[] = $viewFile;
238
239 68
        if ($this->beforeRender($viewFile, $params)) {
240 68
            Yii::trace("Rendering view file: $viewFile", __METHOD__);
241 68
            $ext = pathinfo($viewFile, PATHINFO_EXTENSION);
242 68
            if (isset($this->renderers[$ext])) {
243
                if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) {
244
                    $this->renderers[$ext] = Yii::createObject($this->renderers[$ext]);
245
                }
246
                /* @var $renderer ViewRenderer */
247
                $renderer = $this->renderers[$ext];
248
                $output = $renderer->render($this, $viewFile, $params);
249
            } else {
250 68
                $output = $this->renderPhpFile($viewFile, $params);
251
            }
252 68
            $this->afterRender($viewFile, $params, $output);
253
        }
254
255 68
        array_pop($this->_viewFiles);
256 68
        $this->context = $oldContext;
257
258 68
        return $output;
259
    }
260
261
    /**
262
     * @return string|bool the view file currently being rendered. False if no view file is being rendered.
263
     */
264 5
    public function getViewFile()
265
    {
266 5
        return end($this->_viewFiles);
267
    }
268
269
    /**
270
     * This method is invoked right before [[renderFile()]] renders a view file.
271
     * The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event.
272
     * If you override this method, make sure you call the parent implementation first.
273
     * @param string $viewFile the view file to be rendered.
274
     * @param array $params the parameter array passed to the [[render()]] method.
275
     * @return bool whether to continue rendering the view file.
276
     */
277 68
    public function beforeRender($viewFile, $params)
278
    {
279 68
        $event = new ViewEvent([
280 68
            'viewFile' => $viewFile,
281 68
            'params' => $params,
282
        ]);
283 68
        $this->trigger(self::EVENT_BEFORE_RENDER, $event);
284
285 68
        return $event->isValid;
286
    }
287
288
    /**
289
     * This method is invoked right after [[renderFile()]] renders a view file.
290
     * The default implementation will trigger the [[EVENT_AFTER_RENDER]] event.
291
     * If you override this method, make sure you call the parent implementation first.
292
     * @param string $viewFile the view file being rendered.
293
     * @param array $params the parameter array passed to the [[render()]] method.
294
     * @param string $output the rendering result of the view file. Updates to this parameter
295
     * will be passed back and returned by [[renderFile()]].
296
     */
297 68
    public function afterRender($viewFile, $params, &$output)
298
    {
299 68
        if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) {
300
            $event = new ViewEvent([
301
                'viewFile' => $viewFile,
302
                'params' => $params,
303
                'output' => $output,
304
            ]);
305
            $this->trigger(self::EVENT_AFTER_RENDER, $event);
306
            $output = $event->output;
307
        }
308 68
    }
309
310
    /**
311
     * Renders a view file as a PHP script.
312
     *
313
     * This method treats the view file as a PHP script and includes the file.
314
     * It extracts the given parameters and makes them available in the view file.
315
     * The method captures the output of the included view file and returns it as a string.
316
     *
317
     * This method should mainly be called by view renderer or [[renderFile()]].
318
     *
319
     * @param string $_file_ the view file.
320
     * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file.
321
     * @return string the rendering result
322
     */
323 68
    public function renderPhpFile($_file_, $_params_ = [])
324
    {
325 68
        $_obInitialLevel_ = ob_get_level();
326 68
        ob_start();
327 68
        ob_implicit_flush(false);
328 68
        extract($_params_, EXTR_OVERWRITE);
329
        try {
330 68
            require($_file_);
331
            return ob_get_clean();
332
        } catch (\Exception $e) {
333
            while (ob_get_level() > $_obInitialLevel_) {
334
                if (!@ob_end_clean()) {
335
                    ob_clean();
336
                }
337
            }
338
            throw $e;
339
        } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
340
            while (ob_get_level() > $_obInitialLevel_) {
341
                if (!@ob_end_clean()) {
342 16
                    ob_clean();
343
                }
344 16
            }
345 15
            throw $e;
346 15
        }
347 15
    }
348
349 15
    /**
350
     * Renders dynamic content returned by the given PHP statements.
351 1
     * This method is mainly used together with content caching (fragment caching and page caching)
352
     * when some portions of the content (called *dynamic content*) should not be cached.
353
     * The dynamic content must be returned by some PHP statements.
354
     * @param string $statements the PHP statements for generating the dynamic content.
355
     * @return string the placeholder of the dynamic content, or the dynamic content if there is no
356
     * active content cache currently.
357
     */
358
    public function renderDynamic($statements)
359
    {
360 15
        if (!empty($this->cacheStack)) {
361
            $n = count($this->dynamicPlaceholders);
362 15
            $placeholder = "<![CDATA[YII-DYNAMIC-$n]]>";
363 15
            $this->addDynamicPlaceholder($placeholder, $statements);
364
365 15
            return $placeholder;
366 15
        }
367
        return $this->evaluateDynamicContent($statements);
368
    }
369
370
    /**
371
     * Adds a placeholder for dynamic content.
372
     * This method is internally used.
373
     * @param string $placeholder the placeholder name
374 16
     * @param string $statements the PHP statements for generating the dynamic content
375
     */
376 16
    public function addDynamicPlaceholder($placeholder, $statements)
377
    {
378
        foreach ($this->cacheStack as $cache) {
379
            $cache->dynamicPlaceholders[$placeholder] = $statements;
380
        }
381
        $this->dynamicPlaceholders[$placeholder] = $statements;
382
    }
383
384
    /**
385
     * Evaluates the given PHP statements.
386
     * This method is mainly used internally to implement dynamic content feature.
387
     * @param string $statements the PHP statements to be evaluated.
388
     * @return mixed the return value of the PHP statements.
389
     */
390
    public function evaluateDynamicContent($statements)
391
    {
392
        return eval($statements);
393
    }
394
395
    /**
396
     * Begins recording a block.
397
     * This method is a shortcut to beginning [[Block]]
398
     * @param string $id the block ID.
399
     * @param bool $renderInPlace whether to render the block content in place.
400
     * Defaults to false, meaning the captured block will not be displayed.
401
     * @return Block the Block widget instance
402
     */
403
    public function beginBlock($id, $renderInPlace = false)
404
    {
405
        return Block::begin([
406
            'id' => $id,
407
            'renderInPlace' => $renderInPlace,
408
            'view' => $this,
409
        ]);
410
    }
411
412
    /**
413
     * Ends recording a block.
414
     */
415
    public function endBlock()
416
    {
417
        Block::end();
418
    }
419
420
    /**
421
     * Begins the rendering of content that is to be decorated by the specified view.
422
     * This method can be used to implement nested layout. For example, a layout can be embedded
423
     * in another layout file specified as '@app/views/layouts/base.php' like the following:
424
     *
425
     * ```php
426
     * <?php $this->beginContent('@app/views/layouts/base.php'); ?>
427
     * //...layout content here...
428
     * <?php $this->endContent(); ?>
429
     * ```
430
     *
431
     * @param string $viewFile the view file that will be used to decorate the content enclosed by this widget.
432
     * This can be specified as either the view file path or path alias.
433
     * @param array $params the variables (name => value) to be extracted and made available in the decorative view.
434
     * @return ContentDecorator the ContentDecorator widget instance
435
     * @see ContentDecorator
436
     */
437
    public function beginContent($viewFile, $params = [])
438
    {
439
        return ContentDecorator::begin([
440
            'viewFile' => $viewFile,
441
            'params' => $params,
442
            'view' => $this,
443
        ]);
444
    }
445
446
    /**
447
     * Ends the rendering of content.
448
     */
449
    public function endContent()
450
    {
451
        ContentDecorator::end();
452
    }
453
454
    /**
455
     * Begins fragment caching.
456
     * This method will display cached content if it is available.
457 6
     * If not, it will start caching and would expect an [[endCache()]]
458
     * call to end the cache and save the content into cache.
459 6
     * A typical usage of fragment caching is as follows,
460 6
     *
461
     * ```php
462 6
     * if ($this->beginCache($id)) {
463 6
     *     // ...generate content here
464 4
     *     $this->endCache();
465
     * }
466 4
     * ```
467
     *
468 6
     * @param string $id a unique ID identifying the fragment to be cached.
469
     * @param array $properties initial property values for [[FragmentCache]]
470
     * @return bool whether you should generate the content for caching.
471
     * False if the cached version is available.
472
     */
473
    public function beginCache($id, $properties = [])
474 6
    {
475
        $properties['id'] = $id;
476 6
        $properties['view'] = $this;
477 6
        /* @var $cache FragmentCache */
478
        $cache = FragmentCache::begin($properties);
479
        if ($cache->getCachedContent() !== false) {
480
            $this->endCache();
481
482 48
            return false;
483
        }
484 48
        return true;
485 48
    }
486
487 48
    /**
488 48
     * Ends fragment caching.
489
     */
490
    public function endCache()
491
    {
492
        FragmentCache::end();
493
    }
494
495
    /**
496
     * Marks the beginning of a page.
497
     */
498
    public function beginPage()
499
    {
500
        ob_start();
501
        ob_implicit_flush(false);
502
503
        $this->trigger(self::EVENT_BEGIN_PAGE);
504
    }
505
506
    /**
507
     * Marks the ending of a page.
508
     */
509
    public function endPage()
510
    {
511
        $this->trigger(self::EVENT_END_PAGE);
512
        ob_end_flush();
513
    }
514
}
515