trivia_core /ROUTER.php

Language PHP Lines 285
MD5 Hash 433798a452fdab380db1a98b6ab50ef6 Estimated Cost $5,195 (why?)
Repository https://bitbucket.org/vodolaz095/trivia_core.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
class RouterException extends Exception
{

}

class ROUTER
{
    protected static $instance;

    private $assigns = array();
    private $current_dir;
    private $path;
    private $args = array();
    private $is_deployed = false;


    private static function init()
    {
        if (is_null(self::$instance))
        {
            self::$instance = new ROUTER();
        }
        return self::$instance;
    }

    private function __construct()
    {

    }

    /**
     * Sets the current directory for project, where controllers and folder for modules are placed
     * @param string $current_dir - directory, where the BOOTLOADER.php file and main controllers are placed
     * @throws RouterException
     */
    public static function setCurrentDir($current_dir)
    {
        if(is_dir($current_dir))
        {
            $router=ROUTER::init();
            $router->current_dir=$current_dir;
        }
        else
        {
            throw new RouterException('Router can not set '.$currendDir.' as Current Directory for project!');
        }
    }

    /**
     * @return string - directory
     */
        public static function getCurrentDir()
    {
        $router=ROUTER::init();
        return($router->current_dir);
    }
    /**
     * Assign module action for route
     * @param string-regex $route - route to be assigned -
     * @param string $module - module name (consists only from letters, _ and digits) - in folder `modules` subfolder `indx` is a module name
     * @param string $action - action name (consists only from letters, _ and digits) - in folder `indx` `indx.action.php` is a action name
     * @throws RouterException - when something is wrong
     */
    public static function assign($route, $module, $action)
    {
        $router = ROUTER::init();
        if($router->current_dir)
        {
            if (preg_match('~^[a-z0-9_]+$~i', $module) and preg_match('~^[a-z0-9_]+$~i', $action))
            {

                $path = $router->current_dir . '/modules/' . $module . '/' . $action . '.action.php';

                if (is_readable($path))
                {
//*
                    if(isset($router->assigns[$route]))
                    {
                        throw new RouterException('Route '.$route.' is already assignet to '.$module.'-'.$action.' via path '.$router->assigns[$module.'-'.$action]['path']);
                    }
                    else
                    {
                    $router->assigns[$route] = array(
                        'route' => $route,
                        'module' => $module,
                        'action' => $action,
                        'path' => $path
                    );
                    }
//*/
/*
                    $router->assigns[$route] = array(
                        'route' => $route,
                        'module' => $module,
                        'action' => $action,
                        'path' => $path
                    );
//*/
                }
                else
                {
                    throw new RouterException('File "' . $path . '" does\'t exists!');
                }
            }
            else
            {
                throw new RouterException('Bad Folder and Module names!');
            }
        }
        else
        {
            throw new RouterException('Set the current directory from bootloader via command ROUTER::setCurrentDir(__DIR__)!!!');
        }
    }

    /**
     * Assign callback function to be executed when route matches the pattern
     * @param string-regex $route
     * @param $closure - function
     * @throws RouterException - when something is wrong
     */
    public static function assignFunction($route, $closure)
    {
        $router = ROUTER::init();
        if($router->current_dir)
        {
            if (is_callable($closure))
            {
                $router->assigns[] = array(
                    'route' => $route,
                    'function' => $closure
                );
            }
            else
            {
                throw new RouterException('Wrong object type for closure');
            }
        }
        else
        {
            throw new RouterException('Set the current directory from bootloader via command ROUTER::setCurrentDir(__DIR__)!!!');
        }
    }

    /**
     * Sets the module as a default module - server route. So, request with pattern example.com example/dosmth example/list
     * goes to actions index.action.php, dosmth.action.php and list.action.php of a module, defined as ServerRoute
     * @param $dirname
     * @throws RouterException
     */
    public static function setServerRoot($module_name)
    {
        $router = ROUTER::init();
        if($router->current_dir)
        {
            if(preg_match('~^[0-9a-z_]+$~i',$module_name))
            {
                if(is_dir($router->current_dir . '/modules/' . $module_name))
                {
                    $path = $router->current_dir . '/modules/' . $module_name . '/*.action.php';
                    foreach (glob($path) as $filename)
                    {
                        $name=basename($filename,'.action.php');
                        if($name=='index')
                            $router->assign('~^/$~',$module_name,'index');
                        else
                            $router->assign('~^/'.$name.'/?$~',$module_name,$name);
                    }
                }
                else
                {
                    throw new RouterException('Wrong directory name!');
                }
            }
            else
            {
                throw new RouterException('Wrong directory name!');
            }
        }
        else
        {
            throw new RouterException('Set the current directory from bootloader via command ROUTER::setCurrentDir(__DIR__)!!!');
        }
    }

    /**
     * Get array of all assigned routes
     * @return array
     */
    public static function getRoutes()
    {
        $a = array();
        $router = ROUTER::init();
        foreach ($router->assigns as $ass)
        {
            if (isset($ass['module']) and isset($ass['action']))
            {
                $a[] = array('route' => $ass['route'], 'execute' => $router->current_dir . '/' . $ass['module'] . '/' . $ass['action'] . '.action.php');
            }
            elseif (isset($ass['function']))
            {
                $a[] = array('route' => $ass['route'], 'function' => $ass['function']);
            }
            else
            {
                /*do nothing*/;
            }
        }
        return $a;
    }

    /**
     * Returns the route parameters
     * @param  $number_of_argument - if assignet,returns the number of argument in query string
     * @return array
     */
    public static function getParameters($number_of_argument=false)
    {
        $router = ROUTER::init();
        if($number_of_argument)
            {
                return isset($router->args[$number_of_argument]) ? $router->args[$number_of_argument] : false;
            }
        else return $router->args;
    }

    /**
     * The main function for router!!!
     * @param string $override_request_uri - string to override URL of pages from cli enviroment
     * @return true on success, false on errors (no action to route et cetera)
     * @throws RouterException
     */
    public static function deploy($override_request_uri = false)
    {
        $unknown_route = true;
        $router = ROUTER::init();

        if ($router->is_deployed === false)
        {
            $router->is_deployed = true;

            if (PHP_SAPI == 'cli' and $override_request_uri)
            {
                $router->path = $override_request_uri;
            }
            else
            {
                $router->path = parse_url('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], PHP_URL_PATH);
            }

            foreach ($router->assigns as $assign)
            {
                if (preg_match($assign['route'], $router->path, $router->args))
                {
                    $unknown_route = false;
                    if (isset($assign['path']))
                    {
                        require_once $assign['path'];
                    }
                    elseif(isset($assign['function']))
                    {
                        $a = $assign['function'];
                        $a($router->args);
                    }
                    else
                    {
                        throw new RouterException('Wrong type of route?');
                    }
                    break;
                }
            }

            return (!$unknown_route);
        }
        else
        {
            throw new RouterException('Router Already Deployed!');
        }

    }


}
Back to Top