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

core/kumbia/kumbia_exception.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
        //TODO quitar el extract, que el view pida los que necesite
65
        extract(Router::get(), EXTR_OVERWRITE);
0 ignored issues
show
\Router::get() cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
66
        // Registra la autocarga de helpers
67
        spl_autoload_register('kumbia_autoload_helper', true, true);
68
69
        $Controller = Util::camelcase($controller);
70
        ob_start();
71
        if (PRODUCTION) { //TODO: añadir error 500.phtml
72
            self::cleanBuffer();
73
            include APP_PATH.'views/_shared/errors/404.phtml';
74
75
            return;
76
        }
77
        if ($e instanceof self) {
78
            $view = $e->view;
79
            $tpl = $e->template;
80
        } else {
81
            $view = 'exception';
82
            $tpl = 'views/templates/exception.phtml';
83
        }
84
        //Fix problem with action name in REST
85
        $action = $e->getMessage() ?: $action;
86
87
        include CORE_PATH."views/errors/{$view}.phtml";
88
89
        $content = ob_get_clean();
90
        self::cleanBuffer();
91
        include CORE_PATH.$tpl;
92
    }
93
94
    /**
95
     * cleanBuffer
96
     * termina los buffers abiertos.
97
     */
98
    private static function cleanBuffer()
99
    {
100
        while (ob_get_level()) {
101
            ob_end_clean();
102
        }
103
    }
104
105
    /**
106
     * Añade la cabezera de error http.
107
     *
108
     * @param Exception $e
109
     * */
110
    private static function setHeader($e)
111
    {
112
        if ($e instanceof self && in_array($e->view, self::$view404)) {
113
            http_response_code(404);
114
115
            return;
116
        }
117
        http_response_code(500);
118
        //TODO: mover a los views
119
    }
120
}
121