Lots and lots of code changes to make things work
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
# AUTOGENERATED COPYRIGHT HEADER START
|
||||
# Copyright (C) 2017-2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
|
||||
# AUTOGENERATED COPYRIGHT HEADER END
|
||||
|
||||
# top-most EditorConfig file
|
||||
root = true
|
||||
|
||||
# Unix-style newlines with a newline ending every file.
|
||||
[*]
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
charset = utf-8
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
|
||||
[*.yml]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
+14
-8
@@ -1,7 +1,19 @@
|
||||
# Peninsula - Backend
|
||||
The Pensinsula Backend provides the necessary APIs for the frontend, bridge and clients to work correctly.
|
||||
The backend is in charge of the functional APIs which allow the frontend, bridge and client to work properly.
|
||||
|
||||
## Requirements
|
||||
## Design Considerations
|
||||
- Write sane and safe code: Enable as much strictness as possible without cutting into developer sanity.
|
||||
- Keep it stupid & simple: We don't need a lot of complexity where we're going, the simple stuff works just fine.
|
||||
- Widely supported: Don't rely on functionality, features, or languages that aren't available widely. PHP fits this perfectly, it's already everywhere.
|
||||
|
||||
## Installation
|
||||
1. Copy the content of this directory to your web server.
|
||||
2. Point the webroot into the `public/` directory.
|
||||
3. Ensure that all requests either resolve to an existing file or are internally redirected to `index.php`.
|
||||
4. Done.
|
||||
|
||||
### Requirements
|
||||
- A functional Web server with internal redirects.
|
||||
- PHP 8.1 or newer
|
||||
- For ideal performance enable opcache, JIT and other common PHP performance improvements.
|
||||
- Additional Modules
|
||||
@@ -12,12 +24,6 @@ The Pensinsula Backend provides the necessary APIs for the frontend, bridge and
|
||||
- [mysqlnd](http://www.php.net/manual/en/mysqlnd.install.php) (MySQL, MariaDB)
|
||||
- [pgsql](https://www.php.net/manual/en/pgsql.installation.php) (PostgreSQL)
|
||||
|
||||
## Installation
|
||||
1. Copy the content of this directory to your web server.
|
||||
2. Point the webroot into the `public/` directory.
|
||||
3. Ensure that all requests either resolve to an existing file or are internally redirected to `index.php`.
|
||||
4. Done.
|
||||
|
||||
### Example Server Configurations
|
||||
These examples are provided without warranty or guarantee of functionality. They're references to look at and configure your own server with.
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
spl_autoload_register(function(string $class_name) {
|
||||
$parts = explode("\\", mb_strtolower($class_name));
|
||||
if ($parts[0] === "app") {
|
||||
array_shift($parts); // Remove the prefix.
|
||||
$path = join("/", $parts);
|
||||
require(__DIR__."/{$path}.php");
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php declare(strict_types=1);
|
||||
/** License
|
||||
* Copyright 2025-2026 Narta Xaymar Dirks <info@xaymar.com>
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**/
|
||||
|
||||
$config = Peninsula\Config::instance();
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
spl_autoload_register(function(string $class_name) {
|
||||
$parts = explode("\\", mb_strtolower($class_name));
|
||||
if ($parts[0] === "peninsula") {
|
||||
array_shift($parts); // Remove the prefix.
|
||||
$path = join("/", $parts);
|
||||
require(__DIR__."/{$path}.php");
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
/** License
|
||||
* Copyright 2025-2026 Narta Xaymar Dirks <info@xaymar.com>
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**/
|
||||
|
||||
namespace Peninsula;
|
||||
|
||||
class Config {
|
||||
use \Peninsula\Traits\Singleton;
|
||||
|
||||
public function router() { return \Peninsula\Router\Config::instance(); }
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php declare(strict_types=1);
|
||||
/** License
|
||||
* Copyright 2025-2026 Narta Xaymar Dirks <info@xaymar.com>
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**/
|
||||
|
||||
namespace Peninsula\HTTP;
|
||||
|
||||
enum Method: string {
|
||||
case GET = 'GET';
|
||||
case POST = 'POST';
|
||||
case HEAD = 'HEAD';
|
||||
case PUT = 'PUT';
|
||||
case DELETE = 'DELETE';
|
||||
case PATCH = 'PATCH';
|
||||
|
||||
public static function fromString(string $value): \Peninsula\HTTP\Method {
|
||||
$value = mb_strtoupper($value);
|
||||
foreach(\Peninsula\HTTP\Method::cases() as $case) {
|
||||
if ($case->value === $value) {
|
||||
return $case;
|
||||
}
|
||||
}
|
||||
throw new Exception('Expected any of \Peninsula\HTTP\Method, got {$value} instead.');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php declare(strict_types=1);
|
||||
/** License
|
||||
* Copyright 2025-2026 Narta Xaymar Dirks <info@xaymar.com>
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**/
|
||||
|
||||
namespace Peninsula\HTTP;
|
||||
|
||||
class Request {
|
||||
// There can't be more than a single request anyway.
|
||||
use \Peninsula\Traits\Singleton;
|
||||
|
||||
private function __construct() {}
|
||||
|
||||
public function method(): \Peninsula\HTTP\Method {
|
||||
return \Peninsula\HTTP\Method::fromString($_SERVER['REQUEST_METHOD']);
|
||||
}
|
||||
|
||||
public function header(string $header): string {
|
||||
// Sanitize input.
|
||||
$header = mb_strtoupper(mb_ereg_replace('\\-', '_', $header, 'z'));
|
||||
return $_SERVER['HTTP_{$header}'];
|
||||
}
|
||||
|
||||
public function path(): string {
|
||||
return $_SERVER['REQUEST_URI'];
|
||||
}
|
||||
|
||||
public function pathInfo(): string {
|
||||
return $_SERVER['PATH_INFO'];
|
||||
}
|
||||
|
||||
public function queryString(): string {
|
||||
return $_SERVER['QUERY_STRING'];
|
||||
}
|
||||
|
||||
public function query(string $key): string {
|
||||
return $_GET[$key];
|
||||
}
|
||||
|
||||
public function data(string $key): string {
|
||||
return $_POST[$key];
|
||||
}
|
||||
|
||||
public function file(string $key): string {
|
||||
return $_FILES[$key];
|
||||
}
|
||||
|
||||
public function cookie(string $key): string {
|
||||
return $_COOKIE[$key];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php declare(strict_types=1);
|
||||
/** License
|
||||
* Copyright 2025-2026 Narta Xaymar Dirks <info@xaymar.com>
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**/
|
||||
|
||||
namespace Peninsula\HTTP;
|
||||
|
||||
class Response {
|
||||
// There can't be more than a single response to a request anyway.
|
||||
use \Peninsula\Traits\Singleton;
|
||||
|
||||
private int $status;
|
||||
private array $headers;
|
||||
|
||||
private bool $haveSentHeader;
|
||||
private bool $haveBegunStreaming;
|
||||
|
||||
private function __construct() {
|
||||
$this->$status = http_response_code();
|
||||
$this->$headers = array();
|
||||
$this->$haveSentHeader = false;
|
||||
$this->$haveBeginStreaming = false;
|
||||
|
||||
// Default to shifting any output into the void for now.
|
||||
mb_http_output("UTF-8");
|
||||
ob_start(self::yellIntoTheVoid, 1024, PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_REMOVABLE);
|
||||
}
|
||||
|
||||
public function __destruct(): void {
|
||||
// Ensure we cleanly stop everything.
|
||||
$this->stop();
|
||||
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
/** Retrieve the value of a header, if set.
|
||||
*
|
||||
*/
|
||||
public function header(string $header): string|array|null {
|
||||
return $headers[$header];
|
||||
}
|
||||
|
||||
/** Replace, Set or Clear the value of a header.
|
||||
*
|
||||
*/
|
||||
public function header(string $header, string|array $value): void {
|
||||
$headers[$header] = $value;
|
||||
}
|
||||
|
||||
//----------------------------------------//
|
||||
// Transmission Status
|
||||
//----------------------------------------//
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function sendHeader(): void {
|
||||
// Ignore the call if we've already sent a header.
|
||||
if ($this->$haveSentHeader) return;
|
||||
|
||||
http_response_code($this->$status);
|
||||
foreach($headers as $key => &$value) {
|
||||
if (is_string($value)) {
|
||||
header("{$key}: {$value}");
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Flush the headers to the output.
|
||||
ob_flush();
|
||||
|
||||
$this->$haveSentHeader = true;
|
||||
}
|
||||
|
||||
public function start(): void {
|
||||
// Ignore the call if we've already begin streaming output.
|
||||
if ($this->$haveBegunStreaming) return;
|
||||
|
||||
// Forcefully send headers.
|
||||
$this->sendHeaders();
|
||||
|
||||
// Set the flag for streaming
|
||||
$this->$haveBegunStreaming = true;
|
||||
|
||||
// Actually set the output buffering to on so we can perform streamed output.
|
||||
ob_start(\Peninsula)
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function stop(): void {
|
||||
if (!$this->$haveBegunStreaming) {
|
||||
|
||||
ob_end_flush();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function toProperEncoding(string $buffer, int $phase): string {
|
||||
$buffer = mb_convert_encoding()
|
||||
}
|
||||
|
||||
public static function yellIntoTheVoid(string $buffer, int $phase): string {
|
||||
// Literally just ignore all output while output buffering is on.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php declare(strict_types=1);
|
||||
/** License
|
||||
* Copyright 2025-2026 Narta Xaymar Dirks <info@xaymar.com>
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**/
|
||||
|
||||
namespace Peninsula\Router;
|
||||
|
||||
class Config {
|
||||
use \Peninsula\Traits\Singleton;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php declare(strict_types=1);
|
||||
/** License
|
||||
* Copyright 2025-2026 Narta Xaymar Dirks <info@xaymar.com>
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**/
|
||||
|
||||
namespace Peninsula\Router;
|
||||
|
||||
/** Attach a class, function or method to a route.
|
||||
* Note that you still need to register the class, function or method manually using Peninsula\Router::instance()->register().
|
||||
* This may be specified multiple times in order to register the same class, function or method to multiple paths or Methods.
|
||||
*
|
||||
* @param $path The URI path to which we respond to. The value NULL means that this is the default route for anything that does not match a specific path.
|
||||
* @param $method Either an array or a single value of Peninsula\HTTP\Methods.
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION | \Attribute::IS_REPEATABLE)]
|
||||
class RouteAttribute {
|
||||
private string|null $path;
|
||||
private \Peninsula\HTTP\Method $method;
|
||||
|
||||
public function __construct(string|null $path, \Peninsula\HTTP\Method $method = \Peninsula\HTTP\Method::GET) {
|
||||
// Most validation is handled by PHP, nothing much to do here really.
|
||||
|
||||
vardump($this);
|
||||
|
||||
$this->$path = $path;
|
||||
$this->$method = $method;
|
||||
}
|
||||
|
||||
public function path() { return $this->$path; }
|
||||
public function method() { return $this->$method; }
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php declare(strict_types=1);
|
||||
/** License
|
||||
* Copyright 2025-2026 Narta Xaymar Dirks <info@xaymar.com>
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**/
|
||||
|
||||
namespace Peninsula\Router;
|
||||
|
||||
/** Router provides path and method based forwarding of requests to their handlers.
|
||||
*
|
||||
*/
|
||||
class Router {
|
||||
use \Peninsula\Traits\Singleton;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private array $statics;
|
||||
|
||||
/** Dynamic routes support arguments in their path, but are significantly slower to evaluate.
|
||||
*
|
||||
*
|
||||
*/
|
||||
private array $dynamics;
|
||||
|
||||
private function __construct() {
|
||||
// Initialize our routes on a per-method basis.
|
||||
$this->$statics = array();
|
||||
foreach(\Peninsula\HTTP\Method::cases() as $case) {
|
||||
$this->$statics[$case->value] = array();
|
||||
// Set the default handler for all routes that weren't found.
|
||||
$this->$statics[$case->value][NULL] = '\Peninsula\Router\Router::defaultHandler';
|
||||
}
|
||||
}
|
||||
|
||||
public function register(object $handler): null {}
|
||||
|
||||
public function resolve(\Peninsula\HTTP\Method $method, string|null $path): object {
|
||||
// Most validation is done by PHP with strict_types=1.
|
||||
|
||||
// Retrieve the bucket the route may be in.
|
||||
$bucket = $this->$statics[$method->value];
|
||||
|
||||
// Strip anything that isn't a path from the path.
|
||||
$path = parse_url($path, PHP_URL_PATH);
|
||||
|
||||
// Try and find a static route match
|
||||
|
||||
|
||||
return '\Peninsula\Router\Router::defaultHandler';
|
||||
}
|
||||
|
||||
static function defaultHandler(\Peninsula\HTTP\Request $request, \Peninsula\HTTP\Response $response, array $routeArguments): null {
|
||||
http_response_code(404);
|
||||
echo("404 Not Found");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php declare(strict_types=1);
|
||||
/** License
|
||||
* Copyright 2025-2026 Narta Xaymar Dirks <info@xaymar.com>
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**/
|
||||
|
||||
namespace Peninsula\Traits;
|
||||
|
||||
/** Provides a generalized Singleton trait for classes to use.
|
||||
*
|
||||
* "Singletons don't have a use in PHP" - some person that has watched a single tutorial on StackOverflow.
|
||||
*/
|
||||
trait Singleton {
|
||||
final public static function instance() {
|
||||
static $instance = null;
|
||||
|
||||
if (is_null($instance)) {
|
||||
$instance = new self();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
|
||||
private function __construct() {}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php declare(strict_types=1);
|
||||
/** License
|
||||
* Copyright 2025-2026 Narta Xaymar Dirks <info@xaymar.com>
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**/
|
||||
|
||||
/**
|
||||
* Hack to dynamically process things inside strings.
|
||||
*
|
||||
* @param mixed $v Value
|
||||
* return mixed Same value
|
||||
*/
|
||||
$GLOBALS['_'] = function ($v) {
|
||||
return $v;
|
||||
};
|
||||
|
||||
require(__DIR__."/../framework/autoload.php");
|
||||
require(__DIR__."/../app/autoload.php");
|
||||
require(__DIR__."/../config.php");
|
||||
|
||||
Peninsula\Router\Router::instance()->resolve(\Peninsula\HTTP\Method::fromString($_SERVER['REQUEST_METHOD']), $_SERVER['REQUEST_URI'])();
|
||||
Reference in New Issue
Block a user