Exemple #1
0
 /**
  * Test for Inflector::demodulize()
  *
  * @test
  */
 public function test_demodulize()
 {
     $output = Inflector::demodulize('Uri::main()');
     $expected = 'main()';
     $this->assertEquals($expected, $output);
 }
Exemple #2
0
 /**
 * Returns $class_name in underscored form, with "_id" tacked on at the end.
 * This is for use in dealing with the database.
 *
 * @param string $class_name
 * @return string
 */
 public static function foreignKey($class_name, $separate_class_name_and_id_with_underscore = true)
 {
     return Inflector::underscore(Inflector::demodulize($class_name)) . ($separate_class_name_and_id_with_underscore ? "_id" : "id");
 }
Exemple #3
0
 /**
 * Returns $class_name in underscored form, with "_id" tacked on at the end.
 * This is for use in dealing with the database.
 *
 * @param string $class_name
 * @return string
 */
 private static function _foreignKey($class_name, $suffix = 'id', $separate_class_name_and_id_with_underscore = true)
 {
     return Inflector::underscore(Inflector::demodulize($class_name)) . ($separate_class_name_and_id_with_underscore ? "_" . $suffix : $suffix);
 }
Exemple #4
0
 /**
  *  Convert URL to controller, action and id
  *
  *  Parse the URL in
  *  {@link
  *  http://www.php.net/manual/en/reserved.variables.php#reserved.variables.server $_SERVER}['REDIRECT_URL']
  *  into elements.
  *  Compute filesystem paths to the various components used by the
  *  URL and store the paths in object private variables.
  *  Verify that the controller exists.
  *
  *  @uses load_router()
  *  @uses $action
  *  @uses $application_controller_file
  *  @uses $controller
  *  @uses $controller_class
  *  @uses $controller_file
  *  @uses $controllers_path
  *  @uses $helper_file
  *  @uses $helpers_path
  *  @uses $id
  *  @uses $layouts_path
  *  @uses $loaded
  *  @uses $router
  *  @uses $router_loaded
  *  @uses set_paths()
  *  @uses $url_path
  *  @uses $views_path
  *  @return boolean
  *  <ul>
  *    <li>true =>  route recognized, controller found.</li>
  *    <li>false => failed, route not recognized.</li>
  *  </ul>
  */
 function recognize_route()
 {
     if (!$this->router_loaded) {
         $this->load_router();
     }
     # current url
     if (isset($_SERVER['REDIRECT_URL']) && !stristr($_SERVER['REDIRECT_URL'], 'dispatch.php')) {
         $browser_url = $_SERVER['REDIRECT_URL'];
     } elseif (isset($_SERVER['REQUEST_URI'])) {
         $browser_url = strstr($_SERVER['REQUEST_URI'], "?") ? substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], "?")) : $_SERVER['REQUEST_URI'];
     }
     //error_log('browser url='.$browser_url);
     # strip off url prefix, if any
     if (!is_null(Trax::$url_prefix)) {
         $browser_url = str_replace(Trax::$url_prefix, "", $browser_url);
     }
     # strip leading slash (if any)
     if (substr($browser_url, 0, 1) == "/") {
         $browser_url = substr($browser_url, 1);
     }
     # strip trailing slash (if any)
     if (substr($browser_url, -1) == "/") {
         $browser_url = substr($browser_url, 0, -1);
     }
     if ($browser_url) {
         $this->url_path = explode("/", !is_null(Trax::$url_word_seperator) ? str_replace(Trax::$url_word_seperator, "_", $browser_url) : $browser_url);
     } else {
         $this->url_path = array();
     }
     if ($this->router->routes_count > 0) {
         $this->controllers_path = Trax::$controllers_path;
         $this->helpers_path = $this->helpers_base_path = Trax::$helpers_path;
         $this->application_controller_file = $this->controllers_path . "/application.php";
         $this->application_helper_file = $this->helpers_path . "/application_helper.php";
         $this->layouts_path = Trax::$layouts_path;
         $this->views_path = Trax::$views_path;
         $route = $this->router->find_route($browser_url);
         #echo "browser_url:$browser_url Found Route:".print_r($route, true)."<br>";
         //  find_route() returns an array if it finds a path that
         //  matches the URL, null if no match found
         if (is_array($route)) {
             //  Matching route found.  Try to get
             //  controller and action from route and URL
             #print_r($this->router);
             #echo "<br>";
             if ($route['plugin']) {
                 $this->plugin = $route['plugin'];
                 $this->controllers_path = Trax::$plugins_path . "/{$route['plugin']}/app/controllers";
                 $this->helpers_path = $this->helpers_base_path = Trax::$plugins_path . "/{$route['plugin']}/app/helpers";
                 $this->layouts_path = Trax::$plugins_path . "/{$route['plugin']}/app/views/layouts";
                 $this->views_path = Trax::$plugins_path . "/{$route['plugin']}/app/views";
             }
             $this->set_paths();
             $route_path = explode("/", $route['path']);
             $route_params = $route['params'];
             //  Find the controller from the route and URL
             if (is_array($route_params) && array_key_exists(":controller", $route_params)) {
                 //  ':controller' in route params overrides URL
                 $this->controller = $route_params[":controller"];
                 if (stristr($route_params[":controller"], "/")) {
                 }
             } elseif (is_array($route_path) && in_array(":controller", $route_path) && count($this->url_path) > 0) {
                 //  Set controller from URL if that field exists
                 $this->controller = strtolower($this->url_path[array_search(":controller", $route_path)]);
             }
             //error_log('controller='.$this->controller);
             //  Find the action from the route and URL
             if (is_array($route_params) && array_key_exists(":action", $route_params)) {
                 //  ':action' in route params overrides URL
                 $this->action = $route_params[':action'];
             } elseif (is_array($route_path) && in_array(":action", $route_path) && array_key_exists(@array_search(":action", $route_path), $this->url_path)) {
                 //  Get action from URL if that field exists
                 $this->action = strtolower($this->url_path[@array_search(":action", $route_path)]);
             }
             //error_log('action='.$this->action);
             //  FIXME: RoR uses :name as a keyword parameter, id
             //  is not treated as a special case.
             //  Do we want to do the same?
             $id = null;
             if (is_array($route_params) && array_key_exists(":id", $route_params)) {
                 //  ':id' in route params overrides URL
                 $id = $route_params[':id'];
             } elseif (@in_array(":id", $route_path)) {
                 #print_r($this->url_path);
                 #print_r($route_path);
                 #if(count($this->extra_path)) {
                 #foreach(array_reverse($this->extra_path) as $extra_path) {
                 #array_unshift($this->url_path, $extra_path);
                 #}
                 #}
                 #print_r($this->url_path);
                 $idPathKey = @array_search(":id", $route_path);
                 if (isset($this->url_path[$idPathKey])) {
                     $id = strtolower($this->url_path[$idPathKey]);
                 } else {
                     $id = null;
                 }
             }
             //  For historical reasons, continue to pass id
             //  in $_REQUEST
             if ($id != "") {
                 $_REQUEST['id'] = $id;
             }
             //error_log('id='.$id);
             $this->views_path .= "/" . $this->controller;
             $this->controller_file = $this->controllers_path . "/" . $this->controller . "_controller.php";
             $this->controller_class = Inflector::camelize(Inflector::demodulize($this->controller)) . "Controller";
             $this->helper_file = $this->helpers_path . "/" . $this->controller . "_helper.php";
             # error_log("controller:{$this->controller} - controller_file:{$this->controller_file} - controller_class:{$this->controller_class} - views_path:{$this->views_path}");
         }
     }
     if (is_file($this->controller_file)) {
         $this->loaded = true;
         return true;
     } else {
         $this->loaded = false;
         return false;
     }
 }