Skip to content
This repository was archived by the owner on Jun 21, 2024. It is now read-only.

Commit cc35d28

Browse files
author
Boris Stelmakh
committed
🥳 initial commit
0 parents  commit cc35d28

File tree

6 files changed

+823
-0
lines changed

6 files changed

+823
-0
lines changed

‎.gitignore‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
composer.phar
2+
/vendor/
3+
/test_controllers/
4+
test.php
5+
composer.lock

‎LICENSE‎

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

‎composer.json‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "mikechip/router",
3+
"description": "Simple non-MVC router for any project",
4+
"version": "1.0.0",
5+
"type": "library",
6+
"license": "GPL-2.0-or-later",
7+
"authors": [
8+
{
9+
"name": "Boris Stelmakh",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": ">=7.0.0"
15+
},
16+
"autoload": {
17+
"psr-4": {"Mike4ip\\Router\\": "src"}
18+
}
19+
}

‎src/NotFoundException.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Mike4ip\Router;
4+
5+
/**
6+
* Class NotFoundException
7+
* @package Mike4ip\Router
8+
*/
9+
class NotFoundException extends \Exception
10+
{
11+
}

‎src/Result.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Mike4ip\Router;
4+
5+
/**
6+
* Class Result
7+
* @package Mike4ip\Router
8+
*/
9+
class Result
10+
{
11+
/**
12+
* @var string
13+
*/
14+
public $route;
15+
16+
/**
17+
* @var string
18+
*/
19+
public $controller_dir;
20+
21+
/**
22+
* Get controller script location
23+
* @return string
24+
* @throws NotFoundException
25+
*/
26+
public function getController(): string
27+
{
28+
$filename = $this->controller_dir . '/' . $this->route . '.php';
29+
30+
if(!file_exists($filename) || !is_readable($filename))
31+
throw new NotFoundException('Cannot read ' . $filename);
32+
33+
return $this->controller_dir . '/' . $this->route . '.php';
34+
}
35+
}

‎src/Router.php‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Mike4ip\Router;
4+
5+
/**
6+
* Class Router
7+
* @package Mike4ip\Router
8+
*/
9+
class Router
10+
{
11+
/**
12+
* @var string
13+
*/
14+
protected $request_string;
15+
16+
/**
17+
* @var string
18+
*/
19+
protected $controller_dir;
20+
21+
/**
22+
* For beginners. Just start router without configuration
23+
* @param string $controller_dir
24+
* @return bool
25+
* @throws NotFoundException
26+
*/
27+
public static function autorun(string $controller_dir): bool
28+
{
29+
ob_start();
30+
31+
if(php_sapi_name() === "apache2handler" || php_sapi_name() === "fpm-fcgi")
32+
$route = $_SERVER['REQUEST_URI'];
33+
elseif(isset($_REQUEST['route']))
34+
$route = isset($_REQUEST['route']);
35+
else
36+
$route = '';
37+
38+
$router = new Router($controller_dir, $route);
39+
$result = $router->getResult();
40+
$filename = $result->getController();
41+
unset($controller_dir, $router, $result);
42+
require($filename);
43+
return true;
44+
}
45+
46+
/**
47+
* Router constructor.
48+
* @param string $controller_dir
49+
* @param string|null $request_string
50+
*/
51+
public function __construct(string $controller_dir = '.', string $request_string = null)
52+
{
53+
$this->request_string = is_null($request_string) ? $_SERVER['REQUEST_URI'] : $request_string;
54+
$this->controller_dir = $controller_dir;
55+
}
56+
57+
/**
58+
* @return Result
59+
*/
60+
public function getResult(): Result
61+
{
62+
$route = $this->request_string;
63+
64+
if(strpos($route, '?') !== false)
65+
$route = strstr($route, '?', true);
66+
67+
if(strpos($route, '/') === 0)
68+
$route = substr($route, 1);
69+
70+
if(!strlen($route))
71+
$route = 'index';
72+
73+
$route = str_replace('.', '_', $route); // i am paranoid
74+
$result = new Result();
75+
$result->route = $route;
76+
$result->controller_dir = $this->controller_dir;
77+
return $result;
78+
}
79+
}

0 commit comments

Comments
 (0)