mirror of https://github.com/flightphp/core
Merge pull request #545 from flightphp/output-buffering-correction
Output buffering correctionpull/551/head v3.5.0
commit
5073758cfa
@ -0,0 +1,179 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is the test file where we can open up a quick test server and make
|
||||||
|
* sure that the UI is really working the way we would expect it to.
|
||||||
|
*
|
||||||
|
* @author Kristaps Muižnieks https://github.com/krmu
|
||||||
|
*/
|
||||||
|
|
||||||
|
require file_exists(__DIR__ . '/../../vendor/autoload.php') ? __DIR__ . '/../../vendor/autoload.php' : __DIR__ . '/../../flight/autoload.php';
|
||||||
|
|
||||||
|
Flight::set('flight.content_length', false);
|
||||||
|
Flight::set('flight.views.path', './');
|
||||||
|
Flight::set('flight.views.extension', '.phtml');
|
||||||
|
// This enables the old functionality of Flight output buffering
|
||||||
|
Flight::set('flight.v2.output_buffering', true);
|
||||||
|
|
||||||
|
// Test 1: Root route
|
||||||
|
Flight::route('/', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> Root route works!';
|
||||||
|
});
|
||||||
|
Flight::route('/querytestpath', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> This ir query route<br>';
|
||||||
|
echo "I got such query parameters:<pre>";
|
||||||
|
print_r(Flight::request()->query);
|
||||||
|
echo "</pre>";
|
||||||
|
}, false, "querytestpath");
|
||||||
|
|
||||||
|
// Test 2: Simple route
|
||||||
|
Flight::route('/test', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> Test route works!';
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test 3: Route with parameter
|
||||||
|
Flight::route('/user/@name', function ($name) {
|
||||||
|
echo "<span id='infotext'>Route text:</span> Hello, $name!";
|
||||||
|
});
|
||||||
|
Flight::route('POST /postpage', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> THIS IS POST METHOD PAGE';
|
||||||
|
}, false, "postpage");
|
||||||
|
|
||||||
|
// Test 4: Grouped routes
|
||||||
|
Flight::group('/group', function () {
|
||||||
|
Flight::route('/test', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> Group test route works!';
|
||||||
|
});
|
||||||
|
Flight::route('/user/@name', function ($name) {
|
||||||
|
echo "<span id='infotext'>Route text:</span> There is variable called name and it is $name";
|
||||||
|
});
|
||||||
|
Flight::group('/group1', function () {
|
||||||
|
Flight::group('/group2', function () {
|
||||||
|
Flight::group('/group3', function () {
|
||||||
|
Flight::group('/group4', function () {
|
||||||
|
Flight::group('/group5', function () {
|
||||||
|
Flight::group('/group6', function () {
|
||||||
|
Flight::group('/group7', function () {
|
||||||
|
Flight::group('/group8', function () {
|
||||||
|
Flight::route('/final_group', function () {
|
||||||
|
echo 'Mega Group test route works!';
|
||||||
|
}, false, "final_group");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test 5: Route alias
|
||||||
|
Flight::route('/alias', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> Alias route works!';
|
||||||
|
}, false, 'aliasroute');
|
||||||
|
class AuthCheck
|
||||||
|
{
|
||||||
|
public function before()
|
||||||
|
{
|
||||||
|
if (!isset($_COOKIE['user'])) {
|
||||||
|
echo '<span id="infotext">Middleware text:</span> You are not authorized to access this route!';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$middle = new AuthCheck();
|
||||||
|
// Test 6: Route with middleware
|
||||||
|
Flight::route('/protected', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> Protected route works!';
|
||||||
|
})->addMiddleware([$middle]);
|
||||||
|
|
||||||
|
// Test 7: Route with template
|
||||||
|
Flight::route('/template/@name', function ($name) {
|
||||||
|
Flight::render('template.phtml', ['name' => $name]);
|
||||||
|
});
|
||||||
|
Flight::set('flight.views.path', './');
|
||||||
|
Flight::map('error', function (Throwable $error) {
|
||||||
|
echo "<h1> An error occurred, mapped error method worked, error bellow </h1>";
|
||||||
|
echo '<pre style="border: 2px solid red; padding: 21px; background: lightgray; font-weight: bold;">';
|
||||||
|
echo str_replace(getenv('PWD'), "***CLASSIFIED*****", $error->getTraceAsString());
|
||||||
|
echo "</pre>";
|
||||||
|
echo "<a href='/'>Go back</a>";
|
||||||
|
});
|
||||||
|
Flight::map('notFound', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> The requested URL was not found';
|
||||||
|
echo "<a href='/'>Go back</a>";
|
||||||
|
});
|
||||||
|
echo '
|
||||||
|
<style>
|
||||||
|
ul {
|
||||||
|
list-style-type: none;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
overflow: hidden;
|
||||||
|
background-color: #333;
|
||||||
|
}
|
||||||
|
|
||||||
|
li {
|
||||||
|
float: left;
|
||||||
|
}
|
||||||
|
#infotext {
|
||||||
|
font-weight: bold;
|
||||||
|
color: blueviolet;
|
||||||
|
}
|
||||||
|
li a {
|
||||||
|
display: block;
|
||||||
|
color: white;
|
||||||
|
text-align: center;
|
||||||
|
padding: 14px 16px;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
li a:hover {
|
||||||
|
background-color: #111;
|
||||||
|
}
|
||||||
|
#container {
|
||||||
|
color: #333;
|
||||||
|
font-size: 16px;
|
||||||
|
line-height: 1.5;
|
||||||
|
margin: 20px 0;
|
||||||
|
padding: 10px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
}
|
||||||
|
#debugrequest {
|
||||||
|
color: #333;
|
||||||
|
font-size: 16px;
|
||||||
|
line-height: 1.5;
|
||||||
|
margin: 20px 0;
|
||||||
|
padding: 10px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<ul>
|
||||||
|
<li><a href="/">Root Route</a></li>
|
||||||
|
<li><a href="/test">Test Route</a></li>
|
||||||
|
<li><a href="/user/John">User Route with Parameter (John)</a></li>
|
||||||
|
<li><a href="/group/test">Grouped Test Route</a></li>
|
||||||
|
<li><a href="/group/user/Jane">Grouped User Route with Parameter (Jane)</a></li>
|
||||||
|
<li><a href="/alias">Alias Route</a></li>
|
||||||
|
<li><a href="/protected">Protected path</a></li>
|
||||||
|
<li><a href="/template/templatevariable">Template path</a></li>
|
||||||
|
<li><a href="/querytestpath?test=1&variable2=uuid&variable3=tester">Query path</a></li>
|
||||||
|
<li><a href="/postpage">Post method test page - should be 404</a></li>
|
||||||
|
<li><a href="' . Flight::getUrl('final_group') . '">Mega group</a></li>
|
||||||
|
</ul>';
|
||||||
|
Flight::before('start', function ($params) {
|
||||||
|
echo '<div id="container">';
|
||||||
|
});
|
||||||
|
Flight::after('start', function ($params) {
|
||||||
|
echo '</div>';
|
||||||
|
echo '<div id="debugrequest">';
|
||||||
|
echo "Request information<pre>";
|
||||||
|
print_r(Flight::request());
|
||||||
|
echo "</pre>";
|
||||||
|
echo "</div>";
|
||||||
|
});
|
||||||
|
Flight::start();
|
@ -0,0 +1 @@
|
|||||||
|
<span id="infotext">Route text:</span> Template <?=$name?> works!
|
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
class AuthCheck
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Before
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function before()
|
||||||
|
{
|
||||||
|
if (!isset($_COOKIE['user'])) {
|
||||||
|
echo '<span id="infotext">Middleware text:</span> You are not authorized to access this route!';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
class LayoutMiddleware
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Before
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function before()
|
||||||
|
{
|
||||||
|
$final_route = Flight::getUrl('final_group');
|
||||||
|
echo <<<HTML
|
||||||
|
<style>
|
||||||
|
ul {
|
||||||
|
list-style-type: none;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
overflow: hidden;
|
||||||
|
background-color: #333;
|
||||||
|
}
|
||||||
|
|
||||||
|
li {
|
||||||
|
float: left;
|
||||||
|
}
|
||||||
|
#infotext {
|
||||||
|
font-weight: bold;
|
||||||
|
color: blueviolet;
|
||||||
|
}
|
||||||
|
li a {
|
||||||
|
display: block;
|
||||||
|
color: white;
|
||||||
|
text-align: center;
|
||||||
|
padding: 14px 16px;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
li a:hover {
|
||||||
|
background-color: #111;
|
||||||
|
}
|
||||||
|
#container {
|
||||||
|
color: #333;
|
||||||
|
font-size: 16px;
|
||||||
|
line-height: 1.5;
|
||||||
|
margin: 20px 0;
|
||||||
|
padding: 10px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
}
|
||||||
|
#debugrequest {
|
||||||
|
color: #333;
|
||||||
|
font-size: 16px;
|
||||||
|
line-height: 1.5;
|
||||||
|
margin: 20px 0;
|
||||||
|
padding: 10px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<ul>
|
||||||
|
<li><a href="/">Root Route</a></li>
|
||||||
|
<li><a href="/test">Test Route</a></li>
|
||||||
|
<li><a href="/user/John">User Route with Parameter (John)</a></li>
|
||||||
|
<li><a href="/group/test">Grouped Test Route</a></li>
|
||||||
|
<li><a href="/group/user/Jane">Grouped User Route with Parameter (Jane)</a></li>
|
||||||
|
<li><a href="/alias">Alias Route</a></li>
|
||||||
|
<li><a href="/protected">Protected path</a></li>
|
||||||
|
<li><a href="/template/templatevariable">Template path</a></li>
|
||||||
|
<li><a href="/querytestpath?test=1&variable2=uuid&variable3=tester">Query path</a></li>
|
||||||
|
<li><a href="/postpage">404 Not Found</a></li>
|
||||||
|
<li><a href="{$final_route}">Mega group</a></li>
|
||||||
|
<li><a href="/error">Error</a></li>
|
||||||
|
</ul>
|
||||||
|
HTML;
|
||||||
|
echo '<div id="container">';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function after()
|
||||||
|
{
|
||||||
|
echo '</div>';
|
||||||
|
echo '<div id="debugrequest">';
|
||||||
|
echo "<h2>Request Information</h2><pre>";
|
||||||
|
print_r(Flight::request());
|
||||||
|
echo '<h3>Raw Request Information</h3>';
|
||||||
|
print_r($_SERVER);
|
||||||
|
echo "</pre><h2>Response Information</h2><pre>";
|
||||||
|
print_r(Flight::response());
|
||||||
|
echo "</pre>";
|
||||||
|
echo "</div>";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,116 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is the test file where we can open up a quick test server and make
|
||||||
|
* sure that the UI is really working the way we would expect it to.
|
||||||
|
*
|
||||||
|
* @author Kristaps Muižnieks https://github.com/krmu
|
||||||
|
*/
|
||||||
|
|
||||||
|
require file_exists(__DIR__ . '/../../vendor/autoload.php') ? __DIR__ . '/../../vendor/autoload.php' : __DIR__ . '/../../flight/autoload.php';
|
||||||
|
|
||||||
|
Flight::set('flight.content_length', false);
|
||||||
|
Flight::set('flight.views.path', './');
|
||||||
|
Flight::set('flight.views.extension', '.phtml');
|
||||||
|
//Flight::set('flight.v2.output_buffering', true);
|
||||||
|
|
||||||
|
require_once 'LayoutMiddleware.php';
|
||||||
|
|
||||||
|
Flight::group('', function () {
|
||||||
|
|
||||||
|
// Test 1: Root route
|
||||||
|
Flight::route('/', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> Root route works!';
|
||||||
|
});
|
||||||
|
Flight::route('/querytestpath', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> This ir query route<br>';
|
||||||
|
echo "I got such query parameters:<pre>";
|
||||||
|
print_r(Flight::request()->query);
|
||||||
|
echo "</pre>";
|
||||||
|
}, false, "querytestpath");
|
||||||
|
|
||||||
|
// Test 2: Simple route
|
||||||
|
Flight::route('/test', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> Test route works!';
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test 3: Route with parameter
|
||||||
|
Flight::route('/user/@name', function ($name) {
|
||||||
|
echo "<span id='infotext'>Route text:</span> Hello, $name!";
|
||||||
|
});
|
||||||
|
Flight::route('POST /postpage', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> THIS IS POST METHOD PAGE';
|
||||||
|
}, false, "postpage");
|
||||||
|
|
||||||
|
// Test 4: Grouped routes
|
||||||
|
Flight::group('/group', function () {
|
||||||
|
Flight::route('/test', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> Group test route works!';
|
||||||
|
});
|
||||||
|
Flight::route('/user/@name', function ($name) {
|
||||||
|
echo "<span id='infotext'>Route text:</span> There is variable called name and it is $name";
|
||||||
|
});
|
||||||
|
Flight::group('/group1', function () {
|
||||||
|
Flight::group('/group2', function () {
|
||||||
|
Flight::group('/group3', function () {
|
||||||
|
Flight::group('/group4', function () {
|
||||||
|
Flight::group('/group5', function () {
|
||||||
|
Flight::group('/group6', function () {
|
||||||
|
Flight::group('/group7', function () {
|
||||||
|
Flight::group('/group8', function () {
|
||||||
|
Flight::route('/final_group', function () {
|
||||||
|
echo 'Mega Group test route works!';
|
||||||
|
}, false, "final_group");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test 5: Route alias
|
||||||
|
Flight::route('/alias', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> Alias route works!';
|
||||||
|
}, false, 'aliasroute');
|
||||||
|
|
||||||
|
/** Middleware test */
|
||||||
|
include_once 'AuthCheck.php';
|
||||||
|
$middle = new AuthCheck();
|
||||||
|
// Test 6: Route with middleware
|
||||||
|
Flight::route('/protected', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> Protected route works!';
|
||||||
|
})->addMiddleware([$middle]);
|
||||||
|
|
||||||
|
// Test 7: Route with template
|
||||||
|
Flight::route('/template/@name', function ($name) {
|
||||||
|
Flight::render('template.phtml', ['name' => $name]);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test 8: Throw an error
|
||||||
|
Flight::route('/error', function () {
|
||||||
|
trigger_error('This is a successful error');
|
||||||
|
});
|
||||||
|
}, [ new LayoutMiddleware() ]);
|
||||||
|
|
||||||
|
Flight::map('error', function (Throwable $e) {
|
||||||
|
echo sprintf(
|
||||||
|
'<h1>500 Internal Server Error</h1>' .
|
||||||
|
'<h3>%s (%s)</h3>' .
|
||||||
|
'<pre style="border: 2px solid red; padding: 21px; background: lightgray; font-weight: bold;">%s</pre>',
|
||||||
|
$e->getMessage(),
|
||||||
|
$e->getCode(),
|
||||||
|
str_replace(getenv('PWD'), '***CONFIDENTIAL***', $e->getTraceAsString())
|
||||||
|
);
|
||||||
|
echo "<br><a href='/'>Go back</a>";
|
||||||
|
});
|
||||||
|
Flight::map('notFound', function () {
|
||||||
|
echo '<span id="infotext">Route text:</span> The requested URL was not found<br>';
|
||||||
|
echo "<a href='/'>Go back</a>";
|
||||||
|
});
|
||||||
|
|
||||||
|
Flight::start();
|
@ -0,0 +1 @@
|
|||||||
|
<span id="infotext">Route text:</span> Template <?=$name?> works!
|
Loading…
Reference in new issue