From df185e75d4cbaf278d8dbffa9b46365bbd9887d9 Mon Sep 17 00:00:00 2001 From: Guilherme Nascimento Date: Fri, 28 Jan 2022 07:00:50 -0300 Subject: [PATCH] Improved PHP8.1 support Improved PHP8.1 support Improved server.bat --- README.html | 188 ++++++++++++++++++++++++----------------------- index.php | 4 +- server.bat | 45 +++++++----- vendor/Teeny.php | 2 +- 4 files changed, 126 insertions(+), 113 deletions(-) diff --git a/README.html b/README.html index be11964..52b19e8 100644 --- a/README.html +++ b/README.html @@ -118,23 +118,28 @@ padding: 0; } -.markdown-body .codehilitetable { +.markdown-body .codehilitetable, +.markdown-body .highlighttable { border: 0; border-spacing: 0; } -.markdown-body .codehilitetable tr { +.markdown-body .codehilitetable tr, +.markdown-body .highlighttable { border: 0; } .markdown-body .codehilitetable pre, -.markdown-body .codehilitetable div.codehilite { +.markdown-body .codehilitetable div.codehilite, +.markdown-body .highlighttable pre, +.markdown-body .highlighttable div.highlight { margin: 0; } .markdown-body .linenos, .markdown-body .code, -.markdown-body .codehilitetable td { +.markdown-body .codehilitetable td, +.markdown-body .highlighttable td { border: 0; padding: 0; } @@ -517,11 +522,13 @@ border: 0; } -.markdown-body .codehilite { +.markdown-body .codehilite, +.markdown-body .highlight { margin-bottom: 16px; } .markdown-body .codehilite pre, +.markdown-body .highlight pre, .markdown-body pre { padding: 16px; overflow: auto; @@ -531,7 +538,8 @@ border-radius: 3px; } -.markdown-body .codehilite pre { +.markdown-body .codehilite pre, +.markdown-body .highlight pre { margin-bottom: 0; word-break: normal; } @@ -993,80 +1001,80 @@ } } README
- Teeny route system for PHP + Teeny route system for PHP - Teeny route system for JavaScript (Node.js) + Teeny route system for JavaScript (Node.js) - Teeny route system for Golang + Teeny route system for Golang - Teeny route system for Python + Teeny route system for Python
@@ -1074,11 +1082,11 @@

Teeny for PHPTeeny is a micro-route system that is really micro, supports PHP 5.3 to PHP 8, is extremely simple and ready to use.

Install using composer

For create your project use:

-
composer create-project inphinit/teeny <project name>
+
composer create-project inphinit/teeny <project name>
 

Replace <project name> by your project name, for exemple, if want create your project with “blog” name (folder name), use:

-
composer create-project inphinit/teeny blog
+
composer create-project inphinit/teeny blog
 

Download without composer

@@ -1123,49 +1131,49 @@

API

Add and remove routes

For create a new route in index.php put like this:

-
$app->action('GET', '/myroute', function () {
+
$app->action('GET', '/myroute', function () {
     echo 'Test!';
 });
 

You can use return:

-
$app->action('GET', '/myroute', function () {
+
$app->action('GET', '/myroute', function () {
     return 'Test!';
 });
 

For remove a route use null value, like this:

-
$app->action('GET', '/myroute', null);
+
$app->action('GET', '/myroute', null);
 

Route include file

For include a file uses like this:

-
$app->action('GET', '/myroute', 'foo/bar/test.php');
+
$app->action('GET', '/myroute', 'foo/bar/test.php');
 

If foo/bar/test.php not found in project will display the following error:

-
Warning: require(foo/bar/test.php): failed to open stream: No such file or directory in /home/user/blog/vendor/teeny.php on line 156
+
Warning: require(foo/bar/test.php): failed to open stream: No such file or directory in /home/user/blog/vendor/teeny.php on line 156
 
 Fatal error: require(): Failed opening required 'foo/bar/test.php' (include_path='.') /home/user/blog/vendor/teeny.php on line 156
 

HTTP status

For retrieve HTTP status from SAPI (Apache, Ngnix, IIS) or previously defined in the script itself use like this:

-
$var = $app->status();
+
$var = $app->status();
 

For retrieve into a route use like this:

-
$app->action('GET', '/myroute', function () use ($app) {
+
$app->action('GET', '/myroute', function () use ($app) {
     echo 'HTTP status: ', $app->status();
 });
 

For set a new HTTP status use like this (eg.: emit 404 Not Found):

-
$app->status(404);
+
$app->status(404);
 

For set into route use like this (a example with condition/if):

-
$app->action('GET', '/report', function () use ($app) {
+
$app->action('GET', '/report', function () use ($app) {
     $file = 'data/foo.csv';
 
     if (is_file($file)) {
@@ -1181,20 +1189,20 @@ 

HTTP statusNamed params in route

You can use params like this:

-
$app->action('GET', '/user/<user>', function ($params) {
+
$app->action('GET', '/user/<user>', function ($params) {
     var_dump($params);
 });
 

If access a URL like this http://mywebsite/user/mary returns:

-
array(2) {
+
array(2) {
   ["user"]=>
   string(3) "mary"
 }
 

Another example:

-
$app->action('GET', '/article/<name>-<id>', function ($params) use ($app) {
+
$app->action('GET', '/article/<name>-<id>', function ($params) use ($app) {
     // Only ID numerics are valids
     if (ctype_digit($params['id'])) {
         echo 'Article ID: ', $params['id'], '<br>';
@@ -1208,13 +1216,13 @@ 

Named params in route
Article ID: mary
+
Article ID: mary
 Article name: 1000
 

Types of params named in routes

An example, only numeric id are valids:

-

+

If you need more features you can experience the Inphinit PHP framework: https://inphinit.github.io

\ No newline at end of file diff --git a/index.php b/index.php index 72ddb45..d9ccb4b 100644 --- a/index.php +++ b/index.php @@ -111,10 +111,10 @@ function testCallback($params) { $app->setPattern('example', '[A-Z]\d+'); -//Handle the HTTP response when the code is different than 200 +// Handle the HTTP response when the code is different than 200 $app->handlerCodes(array(403, 404, 405), function ($code) { echo 'Custom page error ', $code; }); //Remove true in argument if use Apache, Ngnix or IIS -return $app->exec(true); +return $app->exec(); diff --git a/server.bat b/server.bat index 66e721d..aa7a940 100644 --- a/server.bat +++ b/server.bat @@ -1,20 +1,25 @@ -@echo off - -:: Setup PHP and PORT -set PHP_BIN="C:\php\php.exe" -set PHP_INI="C:\php\php.ini" -set HOST_HOST=localhost -set HOST_PORT=9000 - -:: Current path -set CURRENT_PATH=%~dp0 -set CURRENT_PATH=%CURRENT_PATH:~0,-1% - -:: Router path -set ROUTER="%CURRENT_PATH%\index.php" - -:: Start built in server -%PHP_BIN% -S "%HOST_HOST%:%HOST_PORT%" -c %PHP_INI% -t "%CURRENT_PATH%" %ROUTER% - -:: Prevent close if PHP failed to start -pause +@echo off + +:: Setup PHP and PORT +set PHP_BIN="C:\php\php.exe" +set PHP_INI="C:\php\php.ini" +set HOST_HOST=localhost +set HOST_PORT=9000 + +:: Current path +set CURRENT_PATH=%~dp0 +set CURRENT_PATH=%CURRENT_PATH:~0,-1% + +:: Router path +set ROUTER="%CURRENT_PATH%\index.php" + +if not exist %PHP_BIN% ( + echo ERROR: %PHP_BIN% not found + pause +) else if not exist %PHP_INI% ( + echo ERROR: %PHP_INI% not found + pause +) else ( + :: Start built in server + %PHP_BIN% -S "%HOST_HOST%:%HOST_PORT%" -c %PHP_INI% -t "%CURRENT_PATH%" %ROUTER% || pause +) diff --git a/vendor/Teeny.php b/vendor/Teeny.php index 3e5fe68..2c660bf 100644 --- a/vendor/Teeny.php +++ b/vendor/Teeny.php @@ -66,7 +66,7 @@ public function path() public function status($code = null) { if (function_exists('http_response_code')) { - return http_response_code($code); + return $code ? http_response_code($code) : http_response_code(); } if ($code === null && preg_match('#/RESERVED\.TEENY-(\d{3})\.html#', $_SERVER['PHP_SELF'], $match)) {