Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
7 / 21
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppView
30.00% covered (danger)
30.00%
6 / 20
0.00% covered (danger)
0.00%
0 / 1
3.37
0.00% covered (danger)
0.00%
0 / 1
 _evaluate
30.00% covered (danger)
30.00%
6 / 20
0.00% covered (danger)
0.00%
0 / 1
3.37
1<?php
2
3App::uses('View', 'View');
4
5/**
6 * Application View
7 *
8 * Extends CakePHP's View class with enhanced error handling for templates
9 */
10class AppView extends View
11{
12    /**
13     * Sandbox method to evaluate a template / view script with better error handling.
14     *
15     * Wraps the template include with an error handler that converts PHP errors
16     * to exceptions, ensuring that errors in templates show the correct file and
17     * line number instead of pointing to View.php.
18     *
19     * @param string $viewFile Filename of the view
20     * @param array $dataForView Data to include in rendered view.
21     * @return string Rendered output
22     */
23    protected function _evaluate($viewFile, $dataForView)
24    {
25        $__viewFile = $viewFile;
26        extract($dataForView);
27        ob_start();
28
29        try
30        {
31            include $__viewFile;
32        }
33        catch (Throwable $e)
34        {
35            ob_end_clean();
36            unset($__viewFile);
37
38            // The exception already contains the correct file and line
39            // Just enhance the message to make it clearer this is from a template
40            $errorMessage = sprintf(
41                '%s in [%s, line %d]',
42                $e->getMessage(),
43                $e->getFile(),
44                $e->getLine()
45            );
46
47            throw new RuntimeException(
48                $errorMessage,
49                $e->getCode(),
50                $e
51            );
52        }
53
54        unset($__viewFile);
55
56        return ob_get_clean();
57    }
58
59}