public function testDecorate() { $collection = new RouteCollection(); $collection->get('no-tokens', '/no-tokens', ''); $collection->get('token-digit', '/token-digit/{token}', '')->addRule('token', 'digit'); $collection->compile(); $creator = new MyRouteCreator($collection); $this->assertEquals('/before/no-tokens/after', $creator->create('no-tokens')); $this->assertEquals('/before/token-digit/1/after', $creator->create('token-digit', ['token' => '1'])); }
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; }
/** * Create two collections, merge it and checks if base collection * has two routes. */ public function testMerge() { $this->collection->get('current', '/current', 'actionCurrent'); $newCollection = new RouteCollection(); $newCollection->get('new', '/', 'action'); $this->collection->mergeWith($newCollection); $routes = $this->collection->all(); $this->assertCount(2, $routes); $this->assertArrayHasKey('0', $routes); $this->assertArrayHasKey('1', $routes); $this->assertEquals('current', $routes[0]->getName()); $this->assertEquals('/current', $routes[0]->getSourceRoute()); $this->assertEquals('new', $routes[1]->getName()); $this->assertEquals('/', $routes[1]->getSourceRoute()); }
/** * Merges current collection with given collection. * @param RouteCollection $collection Collection object from we merge routes. * @return self */ public function mergeWith(RouteCollection $collection) { $this->collection = array_merge($this->collection, $collection->all()); return $this; }
<?php use Requtize\Routing\RouteCollection; use Requtize\Routing\Route; use Requtize\Routing\RouteDefinitionCompiler; use Requtize\Routing\RouteCreator; use Requtize\Routing\RouteMatcher; error_reporting(-1); include 'vendor/autoload.php'; $collection = new RouteCollection(); // Homepage => callback /*$collection->add(new Route('/', function($request, $response) { echo $response; })); // Homepage => controller definition $collection->add(new Route('/', 'Controller:action')); // Named route $collection->add(new Route('/', 'Controller:action')) ->setName('homepage'); // Argument in path $collection->add(new Route('/{argument}', 'Controller:action')); // Multiple arguments in path $collection->add(new Route('/{argument1}/{argument2}/{argument3}', 'Controller:action')); // Optional argument in path $collection->add(new Route('/{argument?}', 'Controller:action'));