Пример #1
0
 function rewrite($options = array())
 {
     list($params, $options) = $this->extractOptionsFromParameters($options);
     $this->rewriteParameters($params);
     $named_route = $this->extractNamedRoute($params);
     return (string) $this->Router->urlize($params, $named_route)->setOptions(array_merge($this->values_from_request, $options));
 }
Пример #2
0
 public function testRouterConnectAddsLangSegmentAutomatically()
 {
     $Router = new AkRouter();
     $Router->person('/person/:name');
     $routes = $Router->getRoutes();
     $segments = $routes['person']->getSegments();
     $this->assertArrayHasKey('lang', $segments);
 }
Пример #3
0
 /**
  * @param string $url
  * @return PHPUnit_Routing_TestCase
  */
 public function get($url)
 {
     $Request = $this->createRequest($url);
     try {
         $this->params = $this->Router->match($Request);
         if ($this->reciprocity) {
             $this->assertEqual($url, $this->Router->urlize($this->params)->path());
         }
     } catch (NoMatchingRouteException $e) {
         $this->errors = true;
     }
     return $this;
 }
Пример #4
0
 /**
  * @return AkRouter
  */
 function createRouter()
 {
     $Router = new AkRouter();
     $Router->generate_helper_functions = true;
     foreach ($this->Routes as $name => $args) {
         call_user_func_array(array($Router, $name), $args);
     }
     AkRouter::$singleton = $Router;
     return $this->Router = $Router;
 }
Пример #5
0
    function testInvestigateRequest()
    {
        $Request = new AkRequest();
        $Request->_request['ak'] = 'blog/show/1';
        $Request->_request['q']  = 'wer';
        
        $Router = new AkRouter();
        $Router->connect(':controller/:action/:id');
        $Request->checkForRoutedRequests($Router);

        $this->assertEquals(array(
            'controller'=>'blog',
            'action'=>'show',
            'id'=>1,
            'q'=>'wer',
            'ak'=>'blog/show/1'),$Request->getParameters());     # we don't need the 'ak'-key, do we?
        #var_dump($Request->getRequestUri());    # http://localhost/
        #var_dump($Request->getHost());          # localhost
        #var_dump($Request->getHostWithPort());  # localhost
        #var_dump($Request->getMethod());        # env->request_method
        #var_dump($Request->getLocaleFromUrl()); #
        #var_dump($Request->getPath());          # env->request_uri    
        #var_dump($Request->getPathParameters());# possibly orhpaned       
    }
Пример #6
0
 /**
  * Gets a route to an URL from the rules defined at config/routes.php
  */
 static function toUrl($options, $set_routes = false)
 {
     static $Router;
     if (empty($Router)) {
         if ($set_routes) {
             $Router = $options;
             return;
         } else {
             $Router = AkRouter::getInstance();
         }
     } else {
         if ($options instanceof AkRouter && $set_routes) {
             $Router = $options;
             return;
         }
     }
     return $Router->toUrl($options);
 }
Пример #7
0
 function createRouter()
 {
     $Router = new AkRouter();
     $Router->addRoute('default', new AkRoute('/:controller/:action/:id'));
     return $Router;
 }
Пример #8
0
 function toUrl($options, $set_routes = false)
 {
     static $Map;
     if(empty($Map)){
         if($set_routes){
             $Map = $options;
             return;
         }else{
             require_once(AK_LIB_DIR.DS.'AkRouter.php');
             $Map = new AkRouter();
             if(is_file(AK_ROUTES_MAPPING_FILE)){
                 include(AK_ROUTES_MAPPING_FILE);
             }
         }
     }
     return $Map->toUrl($options);
 }
Пример #9
0
 private function mapResourceRoutes(AkRouter $Map, $Resource, $action, $route_path, $route_name = null, $method = null, $resource_options = array())
 {
     if ($Resource->hasAction($action)) {
         $action_options = $this->actionOptionsFor($action, $Resource, $method, $resource_options);
         $formatted_route_path = $route_path . '.:format';
         $requirements = Ak::deleteAndGetValue($action_options, array('requirements'));
         $conditions = Ak::deleteAndGetValue($action_options, array('conditions'));
         if ($route_name && !in_array($route_name, $Map->getNamedRouteNames())) {
             $Map->connectNamed($route_name, $formatted_route_path, $action_options, (array) $requirements, (array) $conditions);
         } else {
             $Map->connectNamed(null, $formatted_route_path, $action_options, (array) $requirements, (array) $conditions);
         }
     }
 }
Пример #10
0
 /**
  * Asserts that the provided options can be used to generate the provided 
  * path.  This is the inverse of +assertRecognizes+.
  * The +extras+ parameter is used to tell the request the names and values of
  * additional request parameters that would be in a query string. 
  * The +message+ parameter allows you to specify a custom error message for 
  * assertion failures.
  *
  * The +defaults+ parameter is unused.
  *
  * ==== Examples
  *
  * Asserts that the default action is generated for a route with no action
  *
  *     $this->assertGenerates(
  *               '/items', 
  *               array('controller'=>'items', 'action'=>'index')
  *           );
  *
  * Tests that the list action is properly routed
  *
  *     $this->assertGenerates(
  *               '/items/list', 
  *               array('controller'=>'items', 'action'=>'list')
  *           );
  *
  *  Tests the generation of a route with a parameter
  * 
  *     $this->assertGenerates(
  *               '/items/list/1', 
  *               array('controller'=>'items', 'action'=>'list', 'id'=>'1')
  *           );
  *
  * Asserts that the generated route gives us our custom route
  * 
  *     $this->assertGenerates(
  *               'changesets/12', 
  *               array(
  *                   'controller'=>'scm', 'action'=>'show_diff', 
  *                   'revision' => 12)
  *           );
  */
 public function assertGenerates($expected_path, $options = array(), $defaults = array(), $extras = array(), $message = null)
 {
     $options = array_merge($options, $defaults);
     if (@$expected_path[0] != '/') {
         $expected_path = '/' . $expected_path;
     }
     $this->Router = empty($this->Router) ? AkRouter::getInstance() : $this->Router;
     $generated_path = (string) $this->Router->urlize($options);
     if (!empty($extras)) {
         $parsed = parse_url($generated_path);
         if (!empty($parsed['query'])) {
             $vars = array();
             parse_str($parsed['query'], $vars);
             krsort($vars);
             krsort($extras);
             $this->_UnitTester->assertEqual($vars, $extras, 'Extra parameters ' . json_encode($extras) . ' do not match ' . json_encode($vars) . ' found in path ' . $expected_path);
             $generated_path = str_replace('?' . $parsed['query'], '', $generated_path);
         } else {
             $this->_UnitTester->fail('Extra parameters ' . json_encode($extras) . ' not found in path ' . $expected_path);
         }
     }
     $this->_UnitTester->assertEqual($expected_path, $generated_path);
 }
Пример #11
0
 public function test_should_assert_recognizes()
 {
     $Router = new AkRouter();
     $Router->connect('/about', array('controller' => 'pages', 'action' => 'about'));
     $Router->resources('photos');
     $this->nextAssertionUsingRouter($Router);
     $this->assertRecognizes(array('controller' => 'photos', 'action' => 'show', 'id' => 1), "/photos/1");
     $this->assertRecognizes(array('controller' => 'pages', 'action' => 'about'), "/about");
 }
Пример #12
0
 public function mapRoutes($Router = null)
 {
     if (empty($Router)) {
         $Router = AkRouter::getInstance();
     }
     try {
         $this->checkForRoutedRequests($Router);
     } catch (Exception $e) {
         if (AK_TEST_MODE) {
             throw $e;
         } else {
             $ExceptionDispatcher = new AkExceptionDispatcher();
             $ExceptionDispatcher->renderException($e);
             return false;
         }
     }
     return true;
 }
Пример #13
0
 /**
  * @return AkRouter
  */
 static function getInstance()
 {
     if (!($Router = Ak::getStaticVar('AkRouterSingleton'))) {
         $Router = new AkRouter();
         $Router->loadMap();
         Ak::setStaticVar('AkRouterSingleton', $Router);
     }
     return $Router;
 }
Пример #14
0
 function _mapRoutes($Map = null)
 {
     require_once AK_LIB_DIR . DS . 'AkRouter.php';
     if (is_file(AK_ROUTES_MAPPING_FILE)) {
         if (empty($Map)) {
             $Map = AkRouter::getInstance();
         }
         #$Map->loadMap();
         $this->checkForRoutedRequests($Map);
     }
 }
Пример #15
0
 /**
  * @return AkRouter
  */
 static function getInstance()
 {
     if (!self::$singleton) {
         $Map = self::$singleton = new AkRouter();
         $Map->loadMap();
     }
     return self::$singleton;
 }
Пример #16
0
 /**
  * @param string $url
  * @return AkRouterSpec
  */
 function get($url)
 {
     $this->params = $this->Router->toParams($url);
     return $this;
 }