Completed
Push — master ( e1a197...682608 )
by Anderson
02:01
created

Middleware::routeMiddleware()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 5
nop 0
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Middleware class
5
 *
6
 * @author    Anderson Salas <[email protected]>
7
 * @copyright 2017
8
 * @license   GNU-3.0
9
 *
10
 */
11
12
namespace Luthier\Core;
13
14
class Middleware
15
{
16
17
    /**
18
     * CodeIgniter instance (in dynamic context)
19
     *
20
     * @var $CI
21
     *
22
     * @access protected
23
     */
24
    protected $CI;
25
26
    /**
27
     * CodeIgniter instance (in static context)
28
     *
29
     * @var static $instance
30
     *
31
     * @access protected
32
     */
33
    protected static $instance;
34
35
    /**
36
     * Current URI string
37
     *
38
     * @var static $uri_string
39
     *
40
     * @access protected
41
     */
42
    protected static $uri_string;
43
44
    /**
45
     * Class constructor
46
     *
47
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
48
     *
49
     * @access public
50
     */
51
    public function __construct()
52
    {
53
        $this->CI =& self::$instance;
54
    }
55
56
    /**
57
     * Class initialization (in static context)
58
     *
59
     * @return void
60
     *
61
     * @access public
62
     * @static
63
     */
64
    public static function init()
65
    {
66
        self::$instance =& get_instance();
67
        self::$uri_string = self::$instance->router->uri->uri_string();
68
69
        // Execute user defined middleware:
70
        self::routeMiddleware();
71
72
        // Execute Luthier's internal middleware
73
        $internalMiddleware = dirname(__DIR__).DIRECTORY_SEPARATOR.'Middleware'.DIRECTORY_SEPARATOR;
74
        require $internalMiddleware.'Request.php';
75
76
        $request = new \Luthier\Middleware\Request();
77
        $request->run();
78
    }
79
80
    /**
81
     * Executes the current route middleware (if any)
82
     *
83
     * @return void
84
     *
85
     * @access public
86
     * @static
87
     */
88
    public static function routeMiddleware()
89
    {
90
        $currentRoute = Route::getRouteByPath(self::$uri_string);
91
92
        $_run = array();
93
94
        // Current route middleware
95
        if($currentRoute !== FALSE)
96
        {
97
            foreach($currentRoute->middleware as $middleware)
98
            {
99
                if(!in_array($middleware,$_run))
100
                    $_run[] = $middleware;
101
            }
102
        }
103
104
        foreach($_run as $middleware)
105
        {
106
            self::runMiddleware($middleware);
107
        }
108
    }
109
110
    /**
111
     * Run middleware
112
     *
113
     * @param  string $middlewareName
114
     *
115
     * @return void
116
     *
117
     * @access private
118
     * @static
119
     */
120
    private static function runMiddleware($middlewareName)
121
    {
122
        $middlewareDir = APPPATH.'middleware'.DIRECTORY_SEPARATOR;
123
124
        $middlewareOriginalName = $middlewareName;
125
126
        $middlewareName = ucfirst($middlewareName).'_middleware';
127
128
        if(!file_exists($middlewareDir))
129
            show_error('Unable to find (or read) the middleware folder: "'.$middlewareDir.'"');
130
131
        $runMiddleware =  $middlewareDir.$middlewareName.'.php';
132
133
        if(!file_exists($runMiddleware))
134
            show_error('Unable to find (or read) the middleware "'.$runMiddleware.'"');
135
136
        require $runMiddleware;
137
138
        if(!class_exists($middlewareName))
139
            show_error('Class "'.$middlewareName.'" not found');
140
141
        $middleware = new $middlewareName();
142
143
        // Call the current controller __beforeMiddleware() method, if exists:
144
        if(method_exists(self::$instance, '_beforeMiddleware'))
145
            self::$instance->_beforeMiddleware();
0 ignored issues
show
Bug introduced by
The method _beforeMiddleware() does not seem to exist on object<Luthier\Core\Middleware>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
147
        // Run the middleware
148
        $middleware->run();
149
150
        // Call the current controller _afterMiddleware() method, if exists:
151
        if(method_exists(self::$instance, '_afterMiddleware'))
152
            self::$instance->_afterMiddleware();
0 ignored issues
show
Bug introduced by
The method _afterMiddleware() does not seem to exist on object<Luthier\Core\Middleware>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
154
    }
155
}
156