Completed
Push — 1.0 ( 898b65...a18b26 )
by joanhey
01:25
created

KumbiaException::cleanBuffer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
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.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://wiki.kumbiaphp.com/Licencia
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @category   Kumbia
16
 *
17
 * @copyright  Copyright (c) 2005 - 2017 Kumbia Team (http://www.kumbiaphp.com)
18
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
19
 */
20
21
/**
22
 * Clase principal para el manejo de excepciones.
23
 *
24
 * @category   Kumbia
25
 */
26
class KumbiaException extends Exception
27
{
28
    /**
29
     * View de error de la Excepción.
30
     *
31
     * @var string
32
     */
33
    protected $view;
34
35
    /**
36
     * Error 404 para los siguientes views.
37
     *
38
     * @var array
39
     */
40
    protected static $view404 = array('no_controller', 'no_action', 'num_params', 'no_view');
41
42
    /**
43
     * Path del template de exception.
44
     *
45
     * @var string
46
     */
47
    protected $template = 'views/templates/exception.phtml';
48
49
    /**
50
     * Constructor de la clase;.
51
     *
52
     * @param string $message mensaje
53
     * @param string $view    vista que se mostrara
54
     */
55
    public function __construct($message, $view = 'exception')
56
    {
57
        $this->view = $view;
58
        parent::__construct($message);
59
    }
60
61
    /**
62
     * Maneja las excepciones no capturadas.
63
     *
64
     * @param Exception|KumbiaException $e
65
     * */
66
    public static function handleException($e)
67
    {
68
        self::setHeader($e);
69
        //TODO quitar el extract, que el view pida los que necesite
70
        extract(Router::get(), EXTR_OVERWRITE);
0 ignored issues
show
Bug introduced by
\Router::get() cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
71
        // Registra la autocarga de helpers
72
        spl_autoload_register('kumbia_autoload_helper', true, true);
73
74
        $Controller = Util::camelcase($controller);
75
        ob_start();
76
        if (PRODUCTION) { //TODO: añadir error 500.phtml
77
            self::cleanBuffer();
78
            include APP_PATH.'views/_shared/errors/404.phtml';
79
80
            return;
81
        }
82
        if ($e instanceof self) {
83
            $view = $e->view;
84
            $tpl = $e->template;
85
        } else {
86
            $view = 'exception';
87
            $tpl = 'views/templates/exception.phtml';
88
        }
89
        //Fix problem with action name in REST
90
        $action = $e->getMessage() ?: $action;
91
92
        include CORE_PATH."views/errors/{$view}.phtml";
93
94
        $content = ob_get_clean();
95
        self::cleanBuffer();
96
        include CORE_PATH.$tpl;
97
    }
98
99
    /**
100
     * cleanBuffer
101
     * termina los buffers abiertos.
102
     */
103
    private static function cleanBuffer()
104
    {
105
        while (ob_get_level()) {
106
            ob_end_clean();
107
        }
108
    }
109
110
    /**
111
     * Añade la cabezera de error http.
112
     *
113
     * @param Exception $e
114
     * */
115
    private static function setHeader($e)
116
    {
117
        if ($e instanceof self && in_array($e->view, self::$view404)) {
118
            http_response_code(404);
119
120
            return;
121
        }
122
        http_response_code(500);
123
        //TODO: mover a los views
124
    }
125
}
126