/** * CLI 模式下的 DI 配置 * * @param CLI $di */ protected function cliDI(CLI $di) { global $argv; $di->set('router', function () use($di, $argv) { $router = new CLIRouter(); $router->setDI($di); return $router; }); $di->set('output', function () { return new ConsoleOutput(); }); $di->set('dispatcher', function () use($di, $argv) { $dispatcher = new CLIDispatcher(); $dispatcher->setDI($di); $moduleName = array_shift($argv); $taskName = array_shift($argv); $actionName = 'main'; if (strpos($taskName, ':') > 0) { @(list($taskName, $actionName) = preg_split("/:/", $taskName)); } if ($moduleName) { $dispatcher->setTaskName(ucwords($taskName)); $dispatcher->setActionName($actionName); $dispatcher->setParams($argv); if ($moduleName == '_current') { $_appName = ucwords($this->getAppName()); $dispatcher->setNamespaceName("{$_appName}\\Tasks"); } else { $dispatcher->setNamespaceName("Eva\\{$moduleName}\\Tasks"); } } else { $dispatcher->setTaskName('Main'); $dispatcher->setParams($argv); $dispatcher->setNamespaceName("Eva\\EvaEngine\\Tasks"); } return $dispatcher; }); }
public function testDelimiter() { $this->specify("CLI Router doesn't work with custom delimiters", function ($test) { Route::reset(); Route::delimiter('/'); $router = new Router(); $router->add('/', array('module' => 'devtools', 'task' => 'main', 'action' => 'hello')); $router->add('/system/:task/a/:action/:params', array('task' => 1, 'action' => 2, 'params' => 3)); $router->add('/([a-z]{2})/:task', array('task' => 2, 'action' => 'index', 'language' => 1)); $router->add('/admin/:task/:action/:int', array('module' => 'admin', 'task' => 1, 'action' => 2, 'id' => 3)); $router->add('/posts/([0-9]{4})/([0-9]{2})/([0-9]{2})/:params', array('task' => 'posts', 'action' => 'show', 'year' => 1, 'month' => 2, 'day' => 3, 'params' => 4)); $router->add('/manual/([a-z]{2})/([a-z\\.]+)\\.txt', array('task' => 'manual', 'action' => 'show', 'language' => 1, 'file' => 2)); $router->add('/named-manual/{language:([a-z]{2})}/{file:[a-z\\.]+}\\.txt', array('task' => 'manual', 'action' => 'show')); $router->add('/very/static/route', array('task' => 'static', 'action' => 'route')); $router->add("/feed/{lang:[a-z]+}/blog/{blog:[a-z\\-]+}\\.{type:[a-z\\-]+}", "Feed::get"); $router->add("/posts/{year:[0-9]+}/s/{title:[a-z\\-]+}", "Posts::show"); $router->add("/posts/delete/{id}", "Posts::delete"); $router->add("/show/{id:video([0-9]+)}/{title:[a-z\\-]+}", "Videos::show"); $this->_runTest($router, $test); }, ["examples" => array([array('uri' => '/', 'module' => 'devtools', 'task' => 'main', 'action' => 'hello', 'params' => array())], [array('uri' => '/documentation/index/hellao/aaadpqñda/bbbAdld/cc-ccc', 'module' => null, 'task' => 'documentation', 'action' => 'index', 'params' => array('hellao', 'aaadpqñda', 'bbbAdld', 'cc-ccc'))], [array('uri' => '/documentation/index/', 'module' => null, 'task' => 'documentation', 'action' => 'index', 'params' => array())], [array('uri' => '/documentation/index', 'module' => null, 'task' => 'documentation', 'action' => 'index', 'params' => array())], [array('uri' => '/documentation/', 'module' => null, 'task' => 'documentation', 'action' => null, 'params' => array())], [array('uri' => '/system/admin/a/edit/hellao/aaadp', 'module' => null, 'task' => 'admin', 'action' => 'edit', 'params' => array('hellao', 'aaadp'))], [array('uri' => '/es/news', 'module' => null, 'task' => 'news', 'action' => 'index', 'params' => array('language' => 'es'))], [array('uri' => '/admin/posts/edit/100', 'module' => 'admin', 'task' => 'posts', 'action' => 'edit', 'params' => array('id' => 100))], [array('uri' => '/posts/2010/02/10/title/content', 'module' => null, 'task' => 'posts', 'action' => 'show', 'params' => array('year' => '2010', 'month' => '02', 'day' => '10', 0 => 'title', 1 => 'content'))], [array('uri' => '/manual/en/translate.adapter.txt', 'module' => null, 'task' => 'manual', 'action' => 'show', 'params' => array('language' => 'en', 'file' => 'translate.adapter'))], [array('uri' => '/named-manual/en/translate.adapter.txt', 'module' => null, 'task' => 'manual', 'action' => 'show', 'params' => array('language' => 'en', 'file' => 'translate.adapter'))], [array('uri' => '/posts/1999/s/le-nice-title', 'module' => null, 'task' => 'posts', 'action' => 'show', 'params' => array('year' => '1999', 'title' => 'le-nice-title'))], [array('uri' => '/feed/fr/blog/diaporema.json', 'module' => null, 'task' => 'feed', 'action' => 'get', 'params' => array('lang' => 'fr', 'blog' => 'diaporema', 'type' => 'json'))], [array('uri' => '/posts/delete/150', 'module' => null, 'task' => 'posts', 'action' => 'delete', 'params' => array('id' => '150'))], [array('uri' => '/very/static/route', 'module' => null, 'task' => 'static', 'action' => 'route', 'params' => array())])]); }