Completed
Push — master ( 40812c...72a777 )
by joanhey
19s queued 12s
created

KumbiaException::showDev()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 *
12
 * @copyright  Copyright (c) 2005 - 2020 KumbiaPHP Team (http://www.kumbiaphp.com)
13
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
14
 */
15
16
/**
17
 * Clase principal para el manejo de excepciones.
18
 *
19
 * @category   Kumbia
20
 */
21
class KumbiaException extends Exception
22
{
23
    /**
24
     * View de error de la Excepción.
25
     *
26
     * @var string|null
27
     */
28
    protected $view;
29
30
    /**
31
     * Error 404 para los siguientes views.
32
     *
33
     * @var array
34
     */
35
    protected static $view404 = array('no_controller', 'no_action', 'num_params', 'no_view');
36
37
    /**
38
     * Path del template de exception.
39
     *
40
     * @var string
41
     */
42
    protected $template = 'views/templates/exception.phtml';
43
44
    /**
45
     * Constructor de la clase;.
46
     *
47
     * @param string $message mensaje
48
     * @param string $view    vista que se mostrara
49
     */
50
    public function __construct($message, $view = 'exception')
51
    {
52
        $this->view = $view;
53
        parent::__construct($message);
54
    }
55
56
    /**
57
     * Maneja las excepciones no capturadas.
58
     *
59
     * @param Exception|KumbiaException $e
60
     * */
61
    public static function handleException($e)
62
    {
63
        self::setHeader($e);
64
65
        if (PRODUCTION) { 
66
            self::cleanBuffer();
67
            include APP_PATH.'views/_shared/errors/404.phtml'; //TODO: añadir error 500.phtml
68
69
            return;
70
        }
71
        // show developer info in development
72
        self::showDev($e);
73
    }
74
    
75
    private static function showDev($e)
76
    {
77
        $data = Router::get();
78
        array_walk_recursive($data, function (&$value) {
79
                $value = htmlspecialchars($value, ENT_QUOTES, APP_CHARSET);
80
            });
81
        extract($data, EXTR_OVERWRITE);
82
        // Registra la autocarga de helpers
83
        spl_autoload_register('kumbia_autoload_helper', true, true);
84
85
        $Controller = Util::camelcase($controller);
86
        ob_start();
87
        
88
        if ($e instanceof self) {
89
            $view = $e->view;
90
            $tpl = $e->template;
91
        } else {
92
            $view = 'exception';
93
            $tpl = 'views/templates/exception.phtml';
94
        }
95
        //Fix problem with action name in REST
96
        $action = $e->getMessage() ?: $action;
97
        $action = htmlspecialchars($action, ENT_QUOTES, APP_CHARSET);
98
99
        include CORE_PATH."views/errors/{$view}.phtml";
100
101
        $content = ob_get_clean();
102
        self::cleanBuffer();
103
        include CORE_PATH.$tpl;
104
    }
105
106
    /**
107
     * cleanBuffer
108
     * termina los buffers abiertos.
109
     */
110
    private static function cleanBuffer()
111
    {
112
        while (ob_get_level()) {
113
            ob_end_clean();
114
        }
115
    }
116
117
    /**
118
     * Añade la cabezera de error http.
119
     *
120
     * @param Exception $e
121
     * */
122
    private static function setHeader($e)
123
    {
124
        if ($e instanceof self && in_array($e->view, self::$view404)) {
125
            http_response_code(404);
126
127
            return;
128
        }
129
        http_response_code(500);
130
        //TODO: mover a los views
131
    }
132
}
133