} require '../vendor/pear/Universal/ClassLoader/BasePathClassLoader.php'; $loader = new \Universal\ClassLoader\BasePathClassLoader(array('../src', '../vendor/pear', '/opt/local/lib/php')); $loader->useIncludePath(true); $loader->register(); echo "init router\n"; $router = new Roller\Router(); $router->add('/', function () { return 'Hello World, please request /=/test for RESTful resource handler demo.'; }); foreach (range(1, 1000) as $i) { $router->add('/foo' . $i, function () { return 'bar'; }); } $router->routes->compile(); // var_dump( $router->routes->routes ); echo "dispatching\n"; $b = new SimpleBench(); $b->setN(10); $b->iterate('roller_ext', 'roller_ext', function () use($router) { $r = roller_dispatch($router->routes->routes, '/foo1000'); }); $router->enableExtension = false; $b->iterate('roller', 'roller', function () use($router) { $r = $router->dispatch('/foo1000'); }); // var_dump( $r , $regs ); // var_dump( hello_array_value( array( 'foo' => 'what' ) , 'foo' ) ); $result = $b->compare(); echo $result->output('Console');
public function dispatch($path) { if (!$this->hasCache) { foreach ($this->plugins as $p) { $p->beforeCompile($this); } $this->routes->compile(); foreach ($this->plugins as $p) { $p->afterCompile($this); } if ($this->cache) { // make cache $this->makeCache(); } // we are already in runtime, doesn't need to reload cache or // re-compile pattern. $this->hasCache = true; } $routeClass = $this->matchedRouteClass; if ($this->extensionSupport) { // function_exists('roller_dispatch') if ($route = roller_dispatch($this->routes->routes, $path)) { return new $routeClass($this, $route, $path); } else { return false; } } else { $server_req_method = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : null; foreach ($this->routes as $route) { if (preg_match($route['compiled'], $path, $regs)) { // if method is defined, we should check server request method if ($server_req_method && isset($route['method']) && ($m = $route['method'])) { /* * Which request method was used to access the page; * i.e. 'GET', 'HEAD', 'POST', 'PUT'. */ if ($server_req_method !== $m) { continue; } } // apply variables foreach ($route['variables'] as $k) { if (isset($regs[$k])) { $route['vars'][$k] = $regs[$k]; } elseif (isset($route['default'][$k])) { $route['vars'][$k] = $route['default'][$k]; } } // matched! return new $routeClass($this, $route, $path); } } } return false; }
<?php if (extension_loaded('roller')) { echo "extension loaded\n"; } # $i = 0; # foreach( range(1,1000) as $i ) { # $router->add('/foo' . $i , function() { return 'bar'; }); # } // var_dump( $router->routes->routes ); /* echo "before_dispatch: " . (memory_get_usage() / 1024 / 1024) . " MB\n"; $regs = null; $route = roller_dispatch( $router->routes->routes , "/foo$i" , $regs ); echo "after_dispatch: " . (memory_get_usage() / 1024 / 1024) . " MB\n"; var_dump( $route, $regs ); */ echo "===> testing variables\n"; $regs = null; $route = roller_dispatch($router->routes->routes, "/blog/2012", $regs); var_dump($route); var_dump(isset($route['vars']['year'])); var_dump($route['vars']['year'] == 2012); echo "===> testing default variables\n"; $regs = null; $route = roller_dispatch($router->routes->routes, "/blog"); var_dump($route); echo "===> testing default variables\n"; $route = roller_dispatch($router->routes->routes, "/blog"); var_dump($route);