diff --git a/examples/basic/index.php b/examples/basic/index.php index a31239c..f5c9484 100644 --- a/examples/basic/index.php +++ b/examples/basic/index.php @@ -12,10 +12,10 @@ if (file_exists($_SERVER['SCRIPT_FILENAME']) && pathinfo($_SERVER['SCRIPT_FILENA $router = new AltoRouter(); $router->setBasePath('/AltoRouter/examples/basic'); -$router->map('GET|POST','/', 'home#index', 'home'); -$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction')); -$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('GET|POST', '/', 'home#index', 'home'); +$router->map('GET', '/users/', ['c' => 'UserController', 'a' => 'ListAction']); +$router->map('GET', '/users/[i:id]', 'users#show', 'users_show'); +$router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do'); // match current request $match = $router->match(); @@ -24,12 +24,12 @@ $match = $router->match();

Current request:

-	Target: 
-	Params: 
-	Name: 	
+    Target: 
+    Params: 
+    Name:   
 

Try these requests:

GET generate('home'); ?>

-

GET generate('users_show', array('id' => 5)); ?>

-

+

GET generate('users_show', ['id' => 5]); ?>

+

diff --git a/tests/benchmark.php b/tests/benchmark.php index 99b2584..ff469e9 100644 --- a/tests/benchmark.php +++ b/tests/benchmark.php @@ -14,10 +14,11 @@ require __DIR__ . '/../vendor/autoload.php'; global $argv; -$n = isset( $argv[1] ) ? intval( $argv[1] ) : 1000; +$n = isset($argv[1]) ? intval($argv[1]) : 1000; // generates a random request url -function random_request_url() { +function random_request_url() +{ $characters = 'abcdefghijklmnopqrstuvwxyz'; $charactersLength = strlen($characters); $randomString = '/'; @@ -26,45 +27,46 @@ function random_request_url() { for ($i = 0; $i < rand(5, 20); $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; - if( rand(1, 10) === 1 ) { - $randomString .= '/'; + if (rand(1, 10) === 1) { + $randomString .= '/'; } } // add dynamic route with 10% chance - if ( rand(1, 10) === 1 ) { - $randomString = rtrim( $randomString, '/' ) . '/[:part]'; + if (rand(1, 10) === 1) { + $randomString = rtrim($randomString, '/') . '/[:part]'; } return $randomString; } // generate a random request method -function random_request_method() { - static $methods = array( 'GET', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' ); - $random_key = array_rand( $methods ); +function random_request_method() +{ + static $methods = [ 'GET', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' ]; + $random_key = array_rand($methods); return $methods[ $random_key ]; } // prepare benchmark data -$requests = array(); -for($i=0; $i<$n; $i++) { - $requests[] = array( +$requests = []; +for ($i=0; $i<$n; $i++) { + $requests[] = [ 'method' => random_request_method(), 'url' => random_request_url(), - ); + ]; } $router = new AltoRouter(); // map requests $start = microtime(true); -foreach($requests as $r) { +foreach ($requests as $r) { $router->map($r['method'], $r['url'], ''); } $end = microtime(true); $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 @@ -75,19 +77,16 @@ $start = microtime(true); $router->match($r['url'], $r['method']); $end = microtime(true); $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 $start = microtime(true); $router->match('/55-foo-bar', 'GET'); $end = microtime(true); $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 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('Peak memory usage: %d KB', round( memory_get_peak_usage( true ) / 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;