diff --git a/app/Config.php b/app/Config.php index 824d2f3..904467c 100644 --- a/app/Config.php +++ b/app/Config.php @@ -6,6 +6,10 @@ namespace Sakura; +use Sakura\Exceptions\ConfigNonExistentException; +use Sakura\Exceptions\ConfigParseException; +use Sakura\Exceptions\ConfigValueNotFoundException; + /** * Handles the configuration settings of Sakura. * @package Sakura @@ -21,13 +25,15 @@ class Config /** * Initialiser, parses the configuration. + * @throws ConfigNonExistentException + * @throws ConfigParseException * @param string $path */ public static function init($path) { // Check if the configuration file exists if (!file_exists($path)) { - throw new Exception('Configuration file does not exist'); + throw new ConfigNonExistentException; } // Attempt to load the configuration file @@ -36,7 +42,7 @@ class Config if (is_array($config)) { self::$config = $config; } else { - throw new Exception('Failed to parse configuration'); + throw new ConfigParseException; } } @@ -44,6 +50,7 @@ class Config * Get a value from the configuration. * @param string $section * @param string $key + * @throws ConfigValueNotFoundException * @return array|string */ public static function get($section, $key = null) @@ -59,6 +66,6 @@ class Config return self::$config[$section]; } - throw new Exception("Couldn't find configuration value"); + throw new ConfigValueNotFoundException; } } diff --git a/app/Controllers/FriendsController.php b/app/Controllers/FriendsController.php index 8e9d4c7..7c7eb50 100644 --- a/app/Controllers/FriendsController.php +++ b/app/Controllers/FriendsController.php @@ -9,7 +9,6 @@ namespace Sakura\Controllers; use Sakura\CurrentSession; use Sakura\Notification; use Sakura\Perms\Site; -use Sakura\Router; use Sakura\User; /** @@ -34,9 +33,9 @@ class FriendsController extends Controller $alert->time = time(); $alert->title = $title; $alert->text = $text; - $alert->image = Router::route('file.avatar', $user->id); + $alert->image = route('file.avatar', $user->id); $alert->timeout = 60000; - $alert->link = Router::route('user.profile', $user->id); + $alert->link = route('user.profile', $user->id); $alert->save(); } diff --git a/app/Exception.php b/app/Exception.php deleted file mode 100644 index ee23015..0000000 --- a/app/Exception.php +++ /dev/null @@ -1,16 +0,0 @@ - - */ -class Exception extends \Exception -{ -} diff --git a/app/Exceptions/BaseException.php b/app/Exceptions/BaseException.php new file mode 100644 index 0000000..6b10165 --- /dev/null +++ b/app/Exceptions/BaseException.php @@ -0,0 +1,18 @@ + + */ +class BaseException extends Exception +{ +} diff --git a/app/Exceptions/ConfigNonExistentException.php b/app/Exceptions/ConfigNonExistentException.php new file mode 100644 index 0000000..92cc5f4 --- /dev/null +++ b/app/Exceptions/ConfigNonExistentException.php @@ -0,0 +1,16 @@ + + */ +class ConfigNonExistentException extends BaseException +{ +} diff --git a/app/Exceptions/ConfigParseException.php b/app/Exceptions/ConfigParseException.php new file mode 100644 index 0000000..5ad5a39 --- /dev/null +++ b/app/Exceptions/ConfigParseException.php @@ -0,0 +1,16 @@ + + */ +class ConfigParseException extends BaseException +{ +} diff --git a/app/Exceptions/ConfigValueNotFoundException.php b/app/Exceptions/ConfigValueNotFoundException.php new file mode 100644 index 0000000..d982089 --- /dev/null +++ b/app/Exceptions/ConfigValueNotFoundException.php @@ -0,0 +1,16 @@ + + */ +class ConfigValueNotFoundException extends BaseException +{ +} diff --git a/app/Exceptions/NetAddressTypeException.php b/app/Exceptions/NetAddressTypeException.php new file mode 100644 index 0000000..f69271c --- /dev/null +++ b/app/Exceptions/NetAddressTypeException.php @@ -0,0 +1,16 @@ + + */ +class NetAddressTypeException extends BaseException +{ +} diff --git a/app/Exceptions/NetInvalidAddressException.php b/app/Exceptions/NetInvalidAddressException.php new file mode 100644 index 0000000..0d02e58 --- /dev/null +++ b/app/Exceptions/NetInvalidAddressException.php @@ -0,0 +1,16 @@ + + */ +class NetInvalidAddressException extends BaseException +{ +} diff --git a/app/Exceptions/RouterInvalidMethodException.php b/app/Exceptions/RouterInvalidMethodException.php new file mode 100644 index 0000000..5428a17 --- /dev/null +++ b/app/Exceptions/RouterInvalidMethodException.php @@ -0,0 +1,16 @@ + + */ +class RouterInvalidMethodException extends BaseException +{ +} diff --git a/app/Exceptions/RouterNonExistentControllerException.php b/app/Exceptions/RouterNonExistentControllerException.php new file mode 100644 index 0000000..27a376c --- /dev/null +++ b/app/Exceptions/RouterNonExistentControllerException.php @@ -0,0 +1,16 @@ + + */ +class RouterNonExistentControllerException extends BaseException +{ +} diff --git a/app/Net.php b/app/Net.php index b22bf42..e857a69 100644 --- a/app/Net.php +++ b/app/Net.php @@ -6,6 +6,9 @@ namespace Sakura; +use Sakura\Exceptions\NetAddressTypeException; +use Sakura\Exceptions\NetInvalidAddressException; + /** * Networking methods. * @package Sakura @@ -49,7 +52,7 @@ class Net /** * Converts a printable IP address into an unpacked binary string. * @param string $ip - * @throws Exception + * @throws NetInvalidAddressException * @return string */ public static function pton($ip) @@ -68,13 +71,13 @@ class Net } // Throw an exception if an invalid IP was supplied - throw new Exception("Invalid IP address supplied."); + throw new NetInvalidAddressException; } /** * Converts a binary unpacked IP to a printable packed IP. * @param string $bin - * @throws Exception + * @throws NetAddressTypeException * @return string */ public static function ntop($bin) @@ -84,7 +87,7 @@ class Net // Throw an exception if it's not 4 or 16 bytes if ($len !== 4 && $len !== 16) { - throw new Exception("Could not handle this IP type."); + throw new NetAddressTypeException; } // Finally pack the IP diff --git a/app/Router/Collection.php b/app/Router/Collection.php new file mode 100644 index 0000000..7623d3e --- /dev/null +++ b/app/Router/Collection.php @@ -0,0 +1,89 @@ + + */ +class Collection +{ + /** + * Contains the path list associated with the routes. + * @var array + */ + private $paths = []; + + /** + * Contains the names list associated with the routes. + * @var array + */ + private $names = []; + + /** + * Add multiple routes. + * @param Route $routes + */ + public function add(Route...$routes) + { + foreach ($routes as $route) { + foreach ($route->methods as $method) { + $this->paths[$method][$route->path] = $route; + } + + if ($route->name !== null) { + $this->names[$route->name] = $route; + } + + foreach ($route->subroutes as $subroute) { + $subroute->setPath($route->path . '/' . $subroute->path); + + if ($subroute->controller === null) { + $subroute->controller($route->controller); + } + + $this->add($subroute); + } + + $route->subroutes = null; + } + } + + /** + * Resolve route by path and method. + * @param string $method + * @param string $path + * @return mixed + */ + public function resolve($method, $path) + { + $path = trim(parse_url($path, PHP_URL_PATH), '/'); + + if (!array_key_exists($method, $this->paths) + || !array_key_exists($path, $this->paths[$method])) { + throw new Exception; + } + + return $this->paths[$method][$path]->fire(); + } + + /** + * Generate a route's url by name. + * @param string $name + * @param array $params + * @return string + */ + public function url($name, $params = []) + { + if (!array_key_exists($name, $this->names)) { + throw new Exception; + } + + return parse_url('/' . $this->names[$name]->path, PHP_URL_PATH); + } +} diff --git a/app/Router/Route.php b/app/Router/Route.php new file mode 100644 index 0000000..16b94b3 --- /dev/null +++ b/app/Router/Route.php @@ -0,0 +1,186 @@ + + */ +class Route +{ + /** + * Collection of handled HTTP request types. + */ + const METHODS = [ + 'GET', + 'POST', + 'PUT', + 'PATCH', + 'DELETE', + 'HEAD', + 'OPTIONS', + 'ANY', + ]; + + /** + * Method this route is intended for. + * @var array + */ + public $methods = []; + + /** + * Path of this route. + * @var string + */ + public $path; + + /** + * Controller class for this route. + * @var string + */ + public $controller; + + /** + * Controller method for this route. + * @var string|Callable + */ + public $action; + + /** + * Name for this route. + * @var string + */ + public $name; + + /** + * Subroutes + * @var array + */ + public $subroutes = []; + + /** + * Construct an instance and set path. + * @param string $path + * @return $this + */ + public static function path($path) + { + $instance = new static; + $instance->setPath($path); + return $instance; + } + + /** + * Set path. + * @param string $path + * @return $this + */ + public function setPath($path) + { + $this->path = trim(parse_url($path, PHP_URL_PATH), '/'); + return $this; + } + + /** + * Define subroutes. + * @param Route $routes + * @return $this + */ + public function group(Route...$routes) + { + foreach ($routes as $route) { + if ($route->controller === null) { + $route->controller($this->controller); + } + + $this->subroutes[] = $route; + } + + return $this; + } + + /** + * Set accepted methods. + * @param string|array $methods + * @throws RouterInvalidMethodException + * @return $this + */ + public function methods($methods) + { + if (!is_array($methods)) { + $methods = [$methods]; + } + + foreach ($methods as $method) { + if (!in_array($method, static::METHODS)) { + throw new RouterInvalidMethodException; + } + + if (!in_array($method, $this->methods)) { + $this->methods[] = $method; + } + } + + return $this; + } + + /** + * Set controller class. + * @param string $class + * @return $this + */ + public function controller($class) + { + $this->controller = $class; + return $this; + } + + /** + * Set action. + * @param string|Callable $action + * @return $this + */ + public function action($action) + { + $this->action = $action; + return $this; + } + + /** + * Set name. + * @param string $name + * @return $this + */ + public function name($name) + { + $this->name = $name; + return $this; + } + + /** + * Call the controller. + * @throws RouterNonExistentControllerException + * @return mixed + */ + public function fire() + { + if (is_callable($this->action)) { + return call_user_func($this->action); + } + + if (!class_exists($this->controller) + || !method_exists($this->controller, $this->action)) { + throw new RouterNonExistentControllerException; + } + + return (new $this->controller)->{$this->action}(); + } +} diff --git a/app/Router/Router.php b/app/Router/Router.php new file mode 100644 index 0000000..51ba92a --- /dev/null +++ b/app/Router/Router.php @@ -0,0 +1,36 @@ + + */ +class Router +{ + /** + * Contains the collection. + * @var Collection + */ + protected static $instance = null; + + /** + * Does the referencing. + * @param string $method + * @param array $params + * @return mixed + */ + public static function __callStatic($method, $params) + { + if (static::$instance === null) { + static::$instance = new Collection; + } + + return static::$instance->$method(...$params); + } +} diff --git a/app/Router.php b/app/Routerv1.php similarity index 99% rename from app/Router.php rename to app/Routerv1.php index 72a7372..11fcfb4 100644 --- a/app/Router.php +++ b/app/Routerv1.php @@ -16,7 +16,7 @@ use Phroute\Phroute\RouteCollector; * @package Sakura * @author Julian van de Groep */ -class Router +class Routerv1 { /** * Container for RouteCollector. diff --git a/public/index.php b/public/index.php index db13af1..c359f9e 100644 --- a/public/index.php +++ b/public/index.php @@ -10,4 +10,4 @@ namespace Sakura; require_once __DIR__ . '/../sakura.php'; // Handle requests -echo Router::handle($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); +echo Routerv1::handle($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); diff --git a/routes.php b/routes.php index 1f83052..8d67591 100644 --- a/routes.php +++ b/routes.php @@ -10,21 +10,21 @@ use Phroute\Phroute\Exception\HttpMethodNotAllowedException; use Phroute\Phroute\Exception\HttpRouteNotFoundException; // Check if logged out -Router::filter('logoutCheck', function () { +Routerv1::filter('logoutCheck', function () { if (CurrentSession::$user->isActive()) { throw new HttpRouteNotFoundException(); } }); // Check if logged in -Router::filter('loginCheck', function () { +Routerv1::filter('loginCheck', function () { if (!CurrentSession::$user->isActive()) { throw new HttpMethodNotAllowedException(); } }); // Maintenance check -Router::filter('maintenance', function () { +Routerv1::filter('maintenance', function () { if (config('general.maintenance')) { CurrentSession::stop(); http_response_code(503); @@ -32,33 +32,33 @@ Router::filter('maintenance', function () { } }); -Router::group(['before' => 'maintenance'], function () { +Routerv1::group(['before' => 'maintenance'], function () { // Meta pages - Router::get('/', 'MetaController@index', 'main.index'); - Router::get('/faq', 'MetaController@faq', 'main.faq'); - Router::get('/search', 'MetaController@search', 'main.search'); + Routerv1::get('/', 'MetaController@index', 'main.index'); + Routerv1::get('/faq', 'MetaController@faq', 'main.faq'); + Routerv1::get('/search', 'MetaController@search', 'main.search'); // Auth - Router::group(['before' => 'logoutCheck'], function () { - Router::get('/login', 'AuthController@login', 'auth.login'); - Router::post('/login', 'AuthController@login', 'auth.login'); - Router::get('/register', 'AuthController@register', 'auth.register'); - Router::post('/register', 'AuthController@register', 'auth.register'); - Router::get('/resetpassword', 'AuthController@resetPassword', 'auth.resetpassword'); - Router::post('/resetpassword', 'AuthController@resetPassword', 'auth.resetpassword'); - Router::get('/reactivate', 'AuthController@reactivate', 'auth.reactivate'); - Router::post('/reactivate', 'AuthController@reactivate', 'auth.reactivate'); - Router::get('/activate', 'AuthController@activate', 'auth.activate'); + Routerv1::group(['before' => 'logoutCheck'], function () { + Routerv1::get('/login', 'AuthController@login', 'auth.login'); + Routerv1::post('/login', 'AuthController@login', 'auth.login'); + Routerv1::get('/register', 'AuthController@register', 'auth.register'); + Routerv1::post('/register', 'AuthController@register', 'auth.register'); + Routerv1::get('/resetpassword', 'AuthController@resetPassword', 'auth.resetpassword'); + Routerv1::post('/resetpassword', 'AuthController@resetPassword', 'auth.resetpassword'); + Routerv1::get('/reactivate', 'AuthController@reactivate', 'auth.reactivate'); + Routerv1::post('/reactivate', 'AuthController@reactivate', 'auth.reactivate'); + Routerv1::get('/activate', 'AuthController@activate', 'auth.activate'); }); - Router::group(['before' => 'loginCheck'], function () { - Router::get('/logout', 'AuthController@logout', 'auth.logout'); + Routerv1::group(['before' => 'loginCheck'], function () { + Routerv1::get('/logout', 'AuthController@logout', 'auth.logout'); }); // Link compatibility layer, prolly remove this in like a year - Router::get('/r/{id}', function ($id) { + Routerv1::get('/r/{id}', function ($id) { header("Location: /p/{$id}"); }); - Router::get('/p/{id}', function ($id) { + Routerv1::get('/p/{id}', function ($id) { $resolve = [ 'terms' => 'info.terms', 'contact' => 'info.contact', @@ -86,217 +86,217 @@ Router::group(['before' => 'maintenance'], function () { }); // Info - Router::group(['prefix' => 'info'], function () { - Router::get('/terms', 'InfoController@terms', 'info.terms'); - Router::get('/privacy', 'InfoController@privacy', 'info.privacy'); - Router::get('/contact', 'InfoController@contact', 'info.contact'); - Router::get('/rules', 'InfoController@rules', 'info.rules'); - Router::get('/welcome', 'InfoController@welcome', 'info.welcome'); + Routerv1::group(['prefix' => 'info'], function () { + Routerv1::get('/terms', 'InfoController@terms', 'info.terms'); + Routerv1::get('/privacy', 'InfoController@privacy', 'info.privacy'); + Routerv1::get('/contact', 'InfoController@contact', 'info.contact'); + Routerv1::get('/rules', 'InfoController@rules', 'info.rules'); + Routerv1::get('/welcome', 'InfoController@welcome', 'info.welcome'); }); // Status - Router::group(['prefix' => 'status'], function () { - Router::get('/', 'StatusController@index', 'status.index'); + Routerv1::group(['prefix' => 'status'], function () { + Routerv1::get('/', 'StatusController@index', 'status.index'); }); // News - Router::group(['prefix' => 'news'], function () { - Router::get('/{category:c}?', 'NewsController@category', 'news.category'); - Router::get('/post/{id:i}', 'NewsController@post', 'news.post'); + Routerv1::group(['prefix' => 'news'], function () { + Routerv1::get('/{category:c}?', 'NewsController@category', 'news.category'); + Routerv1::get('/post/{id:i}', 'NewsController@post', 'news.post'); }); // Chat - Router::group(['prefix' => 'chat'], function () { - Router::get('/redirect', 'ChatController@redirect', 'chat.redirect'); - Router::get('/settings', 'ChatController@settings', 'chat.settings'); - Router::get('/auth', 'ChatController@auth', 'chat.auth'); - Router::get('/resolve', 'ChatController@resolve', 'chat.resolve'); - Router::get('/irc', 'ChatController@irc', 'chat.irc'); + Routerv1::group(['prefix' => 'chat'], function () { + Routerv1::get('/redirect', 'ChatController@redirect', 'chat.redirect'); + Routerv1::get('/settings', 'ChatController@settings', 'chat.settings'); + Routerv1::get('/auth', 'ChatController@auth', 'chat.auth'); + Routerv1::get('/resolve', 'ChatController@resolve', 'chat.resolve'); + Routerv1::get('/irc', 'ChatController@irc', 'chat.irc'); }); // Authentication for the "old" chat - Router::get('/web/sock-auth.php', 'ChatController@authLegacy'); + Routerv1::get('/web/sock-auth.php', 'ChatController@authLegacy'); // Forum - Router::group(['prefix' => 'forum'], function () { + Routerv1::group(['prefix' => 'forum'], function () { // Post - Router::group(['prefix' => 'post'], function () { - Router::get('/{id:i}', 'Forum.PostController@find', 'forums.post'); - Router::group(['before' => 'loginCheck'], function () { - Router::get('/{id:i}/raw', 'Forum.PostController@raw', 'forums.post.raw'); - Router::get('/{id:i}/delete', 'Forum.PostController@delete', 'forums.post.delete'); - Router::post('/{id:i}/delete', 'Forum.PostController@delete', 'forums.post.delete'); - Router::post('/{id:i}/edit', 'Forum.PostController@edit', 'forums.post.edit'); + Routerv1::group(['prefix' => 'post'], function () { + Routerv1::get('/{id:i}', 'Forum.PostController@find', 'forums.post'); + Routerv1::group(['before' => 'loginCheck'], function () { + Routerv1::get('/{id:i}/raw', 'Forum.PostController@raw', 'forums.post.raw'); + Routerv1::get('/{id:i}/delete', 'Forum.PostController@delete', 'forums.post.delete'); + Routerv1::post('/{id:i}/delete', 'Forum.PostController@delete', 'forums.post.delete'); + Routerv1::post('/{id:i}/edit', 'Forum.PostController@edit', 'forums.post.edit'); }); }); // Topic - Router::group(['prefix' => 'topic'], function () { - Router::get('/{id:i}', 'Forum.TopicController@view', 'forums.topic'); - Router::get('/{id:i}/sticky', 'Forum.TopicController@sticky', 'forums.topic.sticky'); - Router::get('/{id:i}/announce', 'Forum.TopicController@announce', 'forums.topic.announce'); - Router::get('/{id:i}/lock', 'Forum.TopicController@lock', 'forums.topic.lock'); - Router::get('/{id:i}/delete', 'Forum.TopicController@delete', 'forums.topic.delete'); - Router::get('/{id:i}/restore', 'Forum.TopicController@restore', 'forums.topic.restore'); - Router::get('/{id:i}/move', 'Forum.TopicController@move', 'forums.topic.move'); - Router::post('/{id:i}/reply', 'Forum.TopicController@reply', 'forums.topic.reply'); + Routerv1::group(['prefix' => 'topic'], function () { + Routerv1::get('/{id:i}', 'Forum.TopicController@view', 'forums.topic'); + Routerv1::get('/{id:i}/sticky', 'Forum.TopicController@sticky', 'forums.topic.sticky'); + Routerv1::get('/{id:i}/announce', 'Forum.TopicController@announce', 'forums.topic.announce'); + Routerv1::get('/{id:i}/lock', 'Forum.TopicController@lock', 'forums.topic.lock'); + Routerv1::get('/{id:i}/delete', 'Forum.TopicController@delete', 'forums.topic.delete'); + Routerv1::get('/{id:i}/restore', 'Forum.TopicController@restore', 'forums.topic.restore'); + Routerv1::get('/{id:i}/move', 'Forum.TopicController@move', 'forums.topic.move'); + Routerv1::post('/{id:i}/reply', 'Forum.TopicController@reply', 'forums.topic.reply'); }); // Forum - Router::get('/', 'Forum.ForumController@index', 'forums.index'); - Router::get('/{id:i}', 'Forum.ForumController@forum', 'forums.forum'); - Router::group(['before' => 'loginCheck'], function () { - Router::get('/{id:i}/mark', 'Forum.ForumController@markRead', 'forums.mark'); - Router::get('/{id:i}/new', 'Forum.TopicController@create', 'forums.new'); - Router::post('/{id:i}/new', 'Forum.TopicController@create', 'forums.new'); + Routerv1::get('/', 'Forum.ForumController@index', 'forums.index'); + Routerv1::get('/{id:i}', 'Forum.ForumController@forum', 'forums.forum'); + Routerv1::group(['before' => 'loginCheck'], function () { + Routerv1::get('/{id:i}/mark', 'Forum.ForumController@markRead', 'forums.mark'); + Routerv1::get('/{id:i}/new', 'Forum.TopicController@create', 'forums.new'); + Routerv1::post('/{id:i}/new', 'Forum.TopicController@create', 'forums.new'); }); }); // Members - Router::group(['prefix' => 'members', 'before' => 'loginCheck'], function () { - Router::get('/', 'UserController@members', 'members.index'); - Router::get('/{rank:i}', 'UserController@members', 'members.rank'); + Routerv1::group(['prefix' => 'members', 'before' => 'loginCheck'], function () { + Routerv1::get('/', 'UserController@members', 'members.index'); + Routerv1::get('/{rank:i}', 'UserController@members', 'members.rank'); }); // User - Router::group(['prefix' => 'u'], function () { - Router::get('/{id}', 'UserController@profile', 'user.profile'); - Router::get('/{id}/report', 'UserController@report', 'user.report'); - Router::get('/{id}/header', 'FileController@header', 'user.header'); + Routerv1::group(['prefix' => 'u'], function () { + Routerv1::get('/{id}', 'UserController@profile', 'user.profile'); + Routerv1::get('/{id}/report', 'UserController@report', 'user.report'); + Routerv1::get('/{id}/header', 'FileController@header', 'user.header'); }); // Notifications - Router::group(['prefix' => 'notifications'], function () { - Router::get('/', 'NotificationsController@notifications', 'notifications.get'); - Router::get('/{id}/mark', 'NotificationsController@mark', 'notifications.mark'); + Routerv1::group(['prefix' => 'notifications'], function () { + Routerv1::get('/', 'NotificationsController@notifications', 'notifications.get'); + Routerv1::get('/{id}/mark', 'NotificationsController@mark', 'notifications.mark'); }); // Comments - Router::group(['prefix' => 'comments', 'before' => 'loginCheck'], function () { - Router::post('/{category:c}/post/{reply:i}?', 'CommentsController@post', 'comments.category.post'); - Router::post('/{id:i}/delete', 'CommentsController@delete', 'comments.comment.delete'); - Router::post('/{id:i}/vote', 'CommentsController@vote', 'comments.comment.vote'); + Routerv1::group(['prefix' => 'comments', 'before' => 'loginCheck'], function () { + Routerv1::post('/{category:c}/post/{reply:i}?', 'CommentsController@post', 'comments.category.post'); + Routerv1::post('/{id:i}/delete', 'CommentsController@delete', 'comments.comment.delete'); + Routerv1::post('/{id:i}/vote', 'CommentsController@vote', 'comments.comment.vote'); }); // Comments - Router::group(['prefix' => 'friends', 'before' => 'loginCheck'], function () { - Router::post('/{id:i}/add', 'FriendsController@add', 'friends.add'); - Router::post('/{id:i}/remove', 'FriendsController@remove', 'friends.remove'); + Routerv1::group(['prefix' => 'friends', 'before' => 'loginCheck'], function () { + Routerv1::post('/{id:i}/add', 'FriendsController@add', 'friends.add'); + Routerv1::post('/{id:i}/remove', 'FriendsController@remove', 'friends.remove'); }); // Files - Router::get('/a/{id}', 'FileController@avatar', 'file.avatar'); - Router::get('/bg/{id}', 'FileController@background', 'file.background'); + Routerv1::get('/a/{id}', 'FileController@avatar', 'file.avatar'); + Routerv1::get('/bg/{id}', 'FileController@background', 'file.background'); // Premium - Router::group(['prefix' => 'support', 'before' => 'loginCheck'], function () { - Router::get('/', 'PremiumController@index', 'premium.index'); - Router::get('/error', 'PremiumController@error', 'premium.error'); - Router::get('/handle', 'PremiumController@handle', 'premium.handle'); - Router::get('/complete', 'PremiumController@complete', 'premium.complete'); - Router::post('/purchase', 'PremiumController@purchase', 'premium.purchase'); + Routerv1::group(['prefix' => 'support', 'before' => 'loginCheck'], function () { + Routerv1::get('/', 'PremiumController@index', 'premium.index'); + Routerv1::get('/error', 'PremiumController@error', 'premium.error'); + Routerv1::get('/handle', 'PremiumController@handle', 'premium.handle'); + Routerv1::get('/complete', 'PremiumController@complete', 'premium.complete'); + Routerv1::post('/purchase', 'PremiumController@purchase', 'premium.purchase'); }); // Helpers - Router::group(['prefix' => 'helper'], function () { + Routerv1::group(['prefix' => 'helper'], function () { // BBcode - Router::group(['prefix' => 'bbcode', 'before' => 'loginCheck'], function () { - Router::post('/parse', 'HelperController@bbcodeParse', 'helper.bbcode.parse'); + Routerv1::group(['prefix' => 'bbcode', 'before' => 'loginCheck'], function () { + Routerv1::post('/parse', 'HelperController@bbcodeParse', 'helper.bbcode.parse'); }); }); // Settings - Router::group(['prefix' => 'settings', 'before' => 'loginCheck'], function () { - Router::get('/', function () { - $route = Router::route('settings.account.profile'); + Routerv1::group(['prefix' => 'settings', 'before' => 'loginCheck'], function () { + Routerv1::get('/', function () { + $route = Routerv1::route('settings.account.profile'); return header("Location: {$route}"); }, 'settings.index'); // Account section - Router::group(['prefix' => 'account'], function () { - Router::get('/', function () { - $route = Router::route('settings.account.profile'); + Routerv1::group(['prefix' => 'account'], function () { + Routerv1::get('/', function () { + $route = Routerv1::route('settings.account.profile'); return header("Location: {$route}"); }); - Router::get('/profile', 'Settings.AccountController@profile', 'settings.account.profile'); - Router::post('/profile', 'Settings.AccountController@profile', 'settings.account.profile'); - Router::get('/details', 'Settings.AccountController@details', 'settings.account.details'); - Router::post('/details', 'Settings.AccountController@details', 'settings.account.details'); - Router::get('/ranks', 'Settings.AccountController@ranks', 'settings.account.ranks'); - Router::post('/ranks', 'Settings.AccountController@ranks', 'settings.account.ranks'); + Routerv1::get('/profile', 'Settings.AccountController@profile', 'settings.account.profile'); + Routerv1::post('/profile', 'Settings.AccountController@profile', 'settings.account.profile'); + Routerv1::get('/details', 'Settings.AccountController@details', 'settings.account.details'); + Routerv1::post('/details', 'Settings.AccountController@details', 'settings.account.details'); + Routerv1::get('/ranks', 'Settings.AccountController@ranks', 'settings.account.ranks'); + Routerv1::post('/ranks', 'Settings.AccountController@ranks', 'settings.account.ranks'); }); // Friends section - Router::group(['prefix' => 'friends'], function () { - Router::get('/', function () { - $route = Router::route('settings.friends.listing'); + Routerv1::group(['prefix' => 'friends'], function () { + Routerv1::get('/', function () { + $route = Routerv1::route('settings.friends.listing'); return header("Location: {$route}"); }); - Router::get('/listing', 'Settings.FriendsController@listing', 'settings.friends.listing'); - Router::get('/requests', 'Settings.FriendsController@requests', 'settings.friends.requests'); + Routerv1::get('/listing', 'Settings.FriendsController@listing', 'settings.friends.listing'); + Routerv1::get('/requests', 'Settings.FriendsController@requests', 'settings.friends.requests'); }); // Notifications section - Router::group(['prefix' => 'notifications'], function () { - Router::get('/', function () { - $route = Router::route('settings.notifications.history'); + Routerv1::group(['prefix' => 'notifications'], function () { + Routerv1::get('/', function () { + $route = Routerv1::route('settings.notifications.history'); return header("Location: {$route}"); }); - Router::get('/history', 'Settings.NotificationsController@history', 'settings.notifications.history'); + Routerv1::get('/history', 'Settings.NotificationsController@history', 'settings.notifications.history'); }); // Appearance section - Router::group(['prefix' => 'appearance'], function () { - Router::get('/', function () { - $route = Router::route('settings.appearance.avatar'); + Routerv1::group(['prefix' => 'appearance'], function () { + Routerv1::get('/', function () { + $route = Routerv1::route('settings.appearance.avatar'); return header("Location: {$route}"); }); - Router::get('/avatar', 'Settings.AppearanceController@avatar', 'settings.appearance.avatar'); - Router::post('/avatar', 'Settings.AppearanceController@avatar', 'settings.appearance.avatar'); - Router::get('/background', 'Settings.AppearanceController@background', 'settings.appearance.background'); - Router::post('/background', 'Settings.AppearanceController@background', 'settings.appearance.background'); - Router::get('/header', 'Settings.AppearanceController@header', 'settings.appearance.header'); - Router::post('/header', 'Settings.AppearanceController@header', 'settings.appearance.header'); - Router::get('/userpage', 'Settings.AppearanceController@userpage', 'settings.appearance.userpage'); - Router::post('/userpage', 'Settings.AppearanceController@userpage', 'settings.appearance.userpage'); - Router::get('/signature', 'Settings.AppearanceController@signature', 'settings.appearance.signature'); - Router::post('/signature', 'Settings.AppearanceController@signature', 'settings.appearance.signature'); + Routerv1::get('/avatar', 'Settings.AppearanceController@avatar', 'settings.appearance.avatar'); + Routerv1::post('/avatar', 'Settings.AppearanceController@avatar', 'settings.appearance.avatar'); + Routerv1::get('/background', 'Settings.AppearanceController@background', 'settings.appearance.background'); + Routerv1::post('/background', 'Settings.AppearanceController@background', 'settings.appearance.background'); + Routerv1::get('/header', 'Settings.AppearanceController@header', 'settings.appearance.header'); + Routerv1::post('/header', 'Settings.AppearanceController@header', 'settings.appearance.header'); + Routerv1::get('/userpage', 'Settings.AppearanceController@userpage', 'settings.appearance.userpage'); + Routerv1::post('/userpage', 'Settings.AppearanceController@userpage', 'settings.appearance.userpage'); + Routerv1::get('/signature', 'Settings.AppearanceController@signature', 'settings.appearance.signature'); + Routerv1::post('/signature', 'Settings.AppearanceController@signature', 'settings.appearance.signature'); }); // Advanced section - Router::group(['prefix' => 'advanced'], function () { - Router::get('/', function () { - $route = Router::route('settings.advanced.sessions'); + Routerv1::group(['prefix' => 'advanced'], function () { + Routerv1::get('/', function () { + $route = Routerv1::route('settings.advanced.sessions'); return header("Location: {$route}"); }); - Router::get('/sessions', 'Settings.AdvancedController@sessions', 'settings.advanced.sessions'); - Router::post('/sessions', 'Settings.AdvancedController@sessions', 'settings.advanced.sessions'); - Router::get('/deactivate', 'Settings.AdvancedController@deactivate', 'settings.advanced.deactivate'); - Router::post('/deactivate', 'Settings.AdvancedController@deactivate', 'settings.advanced.deactivate'); + Routerv1::get('/sessions', 'Settings.AdvancedController@sessions', 'settings.advanced.sessions'); + Routerv1::post('/sessions', 'Settings.AdvancedController@sessions', 'settings.advanced.sessions'); + Routerv1::get('/deactivate', 'Settings.AdvancedController@deactivate', 'settings.advanced.deactivate'); + Routerv1::post('/deactivate', 'Settings.AdvancedController@deactivate', 'settings.advanced.deactivate'); }); }); // Settings - Router::group(['prefix' => 'manage', 'before' => 'loginCheck'], function () { - Router::get('/', function () { - $route = Router::route('manage.overview.index'); + Routerv1::group(['prefix' => 'manage', 'before' => 'loginCheck'], function () { + Routerv1::get('/', function () { + $route = Routerv1::route('manage.overview.index'); return header("Location: {$route}"); }, 'manage.index'); // Overview section - Router::group(['prefix' => 'overview'], function () { - Router::get('/', function () { - $route = Router::route('manage.overview.index'); + Routerv1::group(['prefix' => 'overview'], function () { + Routerv1::get('/', function () { + $route = Routerv1::route('manage.overview.index'); return header("Location: {$route}"); }); - Router::get('/index', 'Manage.OverviewController@index', 'manage.overview.index'); - Router::get('/data', 'Manage.OverviewController@data', 'manage.overview.data'); + Routerv1::get('/index', 'Manage.OverviewController@index', 'manage.overview.index'); + Routerv1::get('/data', 'Manage.OverviewController@data', 'manage.overview.data'); }); }); // Management diff --git a/sakura.php b/sakura.php index 1e2ed67..f520003 100644 --- a/sakura.php +++ b/sakura.php @@ -50,12 +50,14 @@ $capsule = new DB; $capsule->addConnection(config('database')); $capsule->setAsGlobal(); +class_alias(Router\Collection::class, 'Router'); + if (!defined('IN_CLI')) { // Start output buffering ob_start(config('performance.compression') ? 'ob_gzhandler' : null); // Initialise the router and include the routes file - Router::init(); + Routerv1::init(); include_once ROOT . 'routes.php'; // Initialise the current session @@ -77,3 +79,31 @@ if (!defined('IN_CLI')) { 'session' => $_SESSION, ]); } + +// use Sakura\Router\Route; +// use Sakura\Router\Router; + +// Router::add( +// Route::path('/') +// ->methods('GET') +// ->controller(Controllers\MetaController::class) +// ->action('index') +// ->name('main.index'), +// Route::path('/test') +// ->controller(Controllers\MetaController::class) +// ->group( +// Route::path('faq') +// ->methods('GET') +// ->action('faq') +// ->group( +// Route::path('sub') +// ->methods(['GET', 'POST']) +// ->action('search') +// ->name('main.search') +// ) +// ) +// ); + +// echo Router::url('main.search'); +// header('Content-Type: text/plain'); +// exit; diff --git a/utility.php b/utility.php index 7c985bf..69d56da 100644 --- a/utility.php +++ b/utility.php @@ -5,7 +5,7 @@ use Sakura\Config; use Sakura\Net; -use Sakura\Router; +use Sakura\Routerv1; use Sakura\Template; // Sort of alias for Config::get @@ -22,10 +22,10 @@ function config($value) } } -// Alias for Router::route +// Alias for Routerv1::route function route($name, $args = null, $full = false) { - return ($full ? full_domain() : '') . Router::route($name, $args); + return ($full ? full_domain() : '') . Routerv1::route($name, $args); } // Getting the full domain (+protocol) of the current host, only works for http