Пример #1
0
 public function start($generateRoutes, $passess)
 {
     $tests = [];
     for ($i = 0; $i < $passess; $i++) {
         $tests[$i] = ['time-create-collection' => 0, 'time-compilation' => 0, 'time-match-unexistent-route' => 0, 'time-total-with-compilation' => 0, 'time-import-from-array' => 0, 'time-match-unexistent-route-imported' => 0, 'time-total-without-compilation' => 0];
         $collection = new RouteCollection();
         $start = microtime(true);
         for ($j = 0; $j < $generateRoutes; $j++) {
             $method = rand(1, 20) == 1 ? $this->possibleMethods[rand(0, 6)] : 'get';
             $path = $this->possiblePaths[rand(0, count($this->possiblePaths) - 1)];
             $collection->{$method}(microtime(), $path['path'], microtime());
         }
         $tests[$i]['time-create-collection'] = number_format(microtime(true) - $start, 4);
         // ===================================================================================
         $start = microtime(true);
         $collection->compile();
         $tests[$i]['time-compilation'] = number_format(microtime(true) - $start, 4);
         // ===================================================================================
         $matcher = new RouteMatcher($collection);
         $start = microtime(true);
         try {
             $matcher->matchWith('/_____________________________unexistent-route_____________________________', 'GET');
         } catch (Exception $e) {
         }
         $tests[$i]['time-match-unexistent-route'] = number_format(microtime(true) - $start, 4);
         $tests[$i]['time-total-with-compilation'] = $tests[$i]['time-create-collection'] + $tests[$i]['time-compilation'] + $tests[$i]['time-match-unexistent-route'];
         // ===================================================================================
         $exported = $collection->exportToArray();
         $start = microtime(true);
         $newCollection = new RouteCollection($exported, $collection->isCompiled());
         $tests[$i]['time-import-from-array'] = number_format(microtime(true) - $start, 4);
         // ===================================================================================
         $matcher = new RouteMatcher($collection);
         $start = microtime(true);
         try {
             $matcher->matchWith('/_____________________________unexistent-route_____________________________', 'GET');
         } catch (Exception $e) {
         }
         $tests[$i]['time-match-unexistent-route-imported'] = number_format(microtime(true) - $start, 4);
         $tests[$i]['time-total-without-compilation'] = $tests[$i]['time-import-from-array'] + $tests[$i]['time-match-unexistent-route-imported'];
     }
     return $tests;
 }
Пример #2
0
// Defined arguments
$collection->add(new Route('/{argument}/{argument2}', 'Controller:action'))
    ->setRules(['argument1' => '\d', 'argument2' => '[a-z]+']);

// Defined argument and optional defined argument
$collection->add(new Route('/{argumentWith-MANY_chars12365dD}/{argument2?}', 'Controller:action'))
    ->setRules(['argument1' => '\d', 'argument2' => '[a-z]+']);*/
// Full options route
$collection->add(new Route('full-option', '/full/{argument1}/{argument2}', 'Controller:action'))->setRules(['argument2' => 'word', 'argument1' => 'digit'])->setMethods(['POST'])->addMethod('GET');
$collection->get('homepage', '/', 'Homepage:Homepage');
$collection->group('blog.', '/blog', function ($collection) {
    $collection->get('homepage', '', function ($request, $response) {
    });
    $collection->get('show', '/{id}', 'Blog:show');
    $collection->get('delete', '/delete/{id}', 'Blog:delete');
});
$collection->get('asd', '/path-to/{digit}/asd/{alnum}/{number}', 'Homepage:Homepage');
$compiler = new RouteDefinitionCompiler();
//$compiler->addPredefinedRule('alias', '[a-zA-Z0-9\-]+');
$collection->compileWithCompiler($compiler);
$creator = new RouteCreator($collection);
var_dump($creator->create('full-option', ['argument2' => 'alias', 'argument1' => 1]));
/*$exported = $collection->export();
$collection2 = new RouteCollection;
$collection2->importFromArray($exported);
var_dump($collection, $collection2);*/
//var_dump($collection->findByName('full-option'));
//var_dump($collection->findByName('asd'));
$matcher = new RouteMatcher($collection);
var_dump($matcher->matchWith(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 'GET'));