1
0
mirror of https://github.com/dannyvankooten/AltoRouter.git synced 2025-08-02 14:37:43 +02:00

phpcs formatting

This commit is contained in:
Félix Dorn
2019-07-03 21:44:06 +02:00
parent cd918de64b
commit a01166760c
2 changed files with 30 additions and 31 deletions

View File

@@ -12,10 +12,10 @@ if (file_exists($_SERVER['SCRIPT_FILENAME']) && pathinfo($_SERVER['SCRIPT_FILENA
$router = new AltoRouter(); $router = new AltoRouter();
$router->setBasePath('/AltoRouter/examples/basic'); $router->setBasePath('/AltoRouter/examples/basic');
$router->map('GET|POST','/', 'home#index', 'home'); $router->map('GET|POST', '/', 'home#index', 'home');
$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction')); $router->map('GET', '/users/', ['c' => 'UserController', 'a' => 'ListAction']);
$router->map('GET','/users/[i:id]', 'users#show', 'users_show'); $router->map('GET', '/users/[i:id]', 'users#show', 'users_show');
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do'); $router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
// match current request // match current request
$match = $router->match(); $match = $router->match();
@@ -24,12 +24,12 @@ $match = $router->match();
<h3>Current request: </h3> <h3>Current request: </h3>
<pre> <pre>
Target: <?php var_dump($match['target']); ?> Target: <?php var_dump($match['target']); ?>
Params: <?php var_dump($match['params']); ?> Params: <?php var_dump($match['params']); ?>
Name: <?php var_dump($match['name']); ?> Name: <?php var_dump($match['name']); ?>
</pre> </pre>
<h3>Try these requests: </h3> <h3>Try these requests: </h3>
<p><a href="<?php echo $router->generate('home'); ?>">GET <?php echo $router->generate('home'); ?></a></p> <p><a href="<?php echo $router->generate('home'); ?>">GET <?php echo $router->generate('home'); ?></a></p>
<p><a href="<?php echo $router->generate('users_show', array('id' => 5)); ?>">GET <?php echo $router->generate('users_show', array('id' => 5)); ?></a></p> <p><a href="<?php echo $router->generate('users_show', ['id' => 5]); ?>">GET <?php echo $router->generate('users_show', ['id' => 5]); ?></a></p>
<p><form action="<?php echo $router->generate('users_do', array('id' => 10, 'action' => 'update')); ?>" method="post"><button type="submit"><?php echo $router->generate('users_do', array('id' => 10, 'action' => 'update')); ?></button></form></p> <p><form action="<?php echo $router->generate('users_do', ['id' => 10, 'action' => 'update']); ?>" method="post"><button type="submit"><?php echo $router->generate('users_do', ['id' => 10, 'action' => 'update']); ?></button></form></p>

View File

@@ -14,10 +14,11 @@
require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/autoload.php';
global $argv; global $argv;
$n = isset( $argv[1] ) ? intval( $argv[1] ) : 1000; $n = isset($argv[1]) ? intval($argv[1]) : 1000;
// generates a random request url // generates a random request url
function random_request_url() { function random_request_url()
{
$characters = 'abcdefghijklmnopqrstuvwxyz'; $characters = 'abcdefghijklmnopqrstuvwxyz';
$charactersLength = strlen($characters); $charactersLength = strlen($characters);
$randomString = '/'; $randomString = '/';
@@ -26,45 +27,46 @@ function random_request_url() {
for ($i = 0; $i < rand(5, 20); $i++) { for ($i = 0; $i < rand(5, 20); $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)]; $randomString .= $characters[rand(0, $charactersLength - 1)];
if( rand(1, 10) === 1 ) { if (rand(1, 10) === 1) {
$randomString .= '/'; $randomString .= '/';
} }
} }
// add dynamic route with 10% chance // add dynamic route with 10% chance
if ( rand(1, 10) === 1 ) { if (rand(1, 10) === 1) {
$randomString = rtrim( $randomString, '/' ) . '/[:part]'; $randomString = rtrim($randomString, '/') . '/[:part]';
} }
return $randomString; return $randomString;
} }
// generate a random request method // generate a random request method
function random_request_method() { function random_request_method()
static $methods = array( 'GET', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' ); {
$random_key = array_rand( $methods ); static $methods = [ 'GET', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' ];
$random_key = array_rand($methods);
return $methods[ $random_key ]; return $methods[ $random_key ];
} }
// prepare benchmark data // prepare benchmark data
$requests = array(); $requests = [];
for($i=0; $i<$n; $i++) { for ($i=0; $i<$n; $i++) {
$requests[] = array( $requests[] = [
'method' => random_request_method(), 'method' => random_request_method(),
'url' => random_request_url(), 'url' => random_request_url(),
); ];
} }
$router = new AltoRouter(); $router = new AltoRouter();
// map requests // map requests
$start = microtime(true); $start = microtime(true);
foreach($requests as $r) { foreach ($requests as $r) {
$router->map($r['method'], $r['url'], ''); $router->map($r['method'], $r['url'], '');
} }
$end = microtime(true); $end = microtime(true);
$map_time = ($end - $start) * 1000; $map_time = ($end - $start) * 1000;
echo sprintf( 'Map time: %.2f ms', $map_time ) . PHP_EOL; echo sprintf('Map time: %.2f ms', $map_time) . PHP_EOL;
// pick random route to match // pick random route to match
@@ -75,19 +77,16 @@ $start = microtime(true);
$router->match($r['url'], $r['method']); $router->match($r['url'], $r['method']);
$end = microtime(true); $end = microtime(true);
$match_time_known_route = ($end - $start) * 1000; $match_time_known_route = ($end - $start) * 1000;
echo sprintf( 'Match time (known route): %.2f ms', $match_time_known_route ) . PHP_EOL; echo sprintf('Match time (known route): %.2f ms', $match_time_known_route) . PHP_EOL;
// match unexisting route // match unexisting route
$start = microtime(true); $start = microtime(true);
$router->match('/55-foo-bar', 'GET'); $router->match('/55-foo-bar', 'GET');
$end = microtime(true); $end = microtime(true);
$match_time_unknown_route = ($end - $start) * 1000; $match_time_unknown_route = ($end - $start) * 1000;
echo sprintf( 'Match time (unknown route): %.2f ms', $match_time_unknown_route ) . PHP_EOL; echo sprintf('Match time (unknown route): %.2f ms', $match_time_unknown_route) . PHP_EOL;
// print totals // print totals
echo sprintf('Total time: %.2f seconds', ($map_time + $match_time_known_route + $match_time_unknown_route)) . PHP_EOL; echo sprintf('Total time: %.2f seconds', ($map_time + $match_time_known_route + $match_time_unknown_route)) . PHP_EOL;
echo sprintf('Memory usage: %d KB', round( memory_get_usage() / 1024 )) . PHP_EOL; echo sprintf('Memory usage: %d KB', round(memory_get_usage() / 1024)) . PHP_EOL;
echo sprintf('Peak memory usage: %d KB', round( memory_get_peak_usage( true ) / 1024 )) . PHP_EOL; echo sprintf('Peak memory usage: %d KB', round(memory_get_peak_usage(true) / 1024)) . PHP_EOL;