Example #1
0
File: Flow.php Project: hisaboh/w2t
 /**
  * URLのパターンからTemplateを切り替える
  * @param array $urlconf
  */
 public function handler(array $urlconf = array())
 {
     $params = array();
     foreach ($urlconf as $pattern => $conf) {
         if (is_int($pattern)) {
             $pattern = $conf;
             $conf = null;
         }
         if (preg_match("/" . str_replace(array("\\/", "/", "__SLASH__"), array("__SLASH__", "\\/", "\\/"), $pattern) . "/", $this->args(), $params)) {
             if ($conf !== null) {
                 if (is_array($conf)) {
                     if (isset($conf["class"])) {
                         $this->class = $conf["class"];
                     }
                     if (isset($conf["method"])) {
                         $this->method = $conf["method"];
                     }
                     if (isset($conf["template"])) {
                         $this->template = $conf["template"];
                     }
                     if (isset($conf["name"])) {
                         $this->name = $conf["name"];
                     }
                 } else {
                     $this->dict($conf);
                 }
             }
             self::$match_pattern = empty($this->name) ? $params[0] : $this->name;
             if (!empty($this->class)) {
                 if (false !== strrpos($this->class, ".") || !class_exists($this->class)) {
                     $this->class = Rhaco::import($this->class);
                 }
                 if (empty($this->method) && !empty($pattern)) {
                     $method_patterns = array();
                     $patterns = explode("/", $pattern);
                     if ($patterns[0] == "^") {
                         array_shift($patterns);
                     }
                     foreach ($patterns as $p) {
                         if (!preg_match("/[\\w_]/", $p)) {
                             break;
                         }
                         $method_patterns[] = $p;
                     }
                     if (!empty($method_patterns)) {
                         $this->method = implode("_", $method_patterns);
                     }
                 }
             }
             if (empty($this->method) && !empty($this->template)) {
                 $obj = new self();
                 $obj->copy_module($this, true);
                 $obj->template($this->template);
             } else {
                 $method = empty($this->method) ? "index" : $this->method;
                 if (!method_exists($this->class, $method)) {
                     throw new Exception("Not found " . $this->class . "::" . $method);
                 }
                 array_shift($params);
                 try {
                     $class = $this->class;
                     $action = new $class();
                     $action->copy_module($this, true);
                     if ($action instanceof self) {
                         $action->handled();
                     }
                     $obj = call_user_func_array(array($action, $method), $params);
                 } catch (Exception $e) {
                     Log::debug($e);
                     $on_error = Rhaco::def("core.Flow@on_error");
                     if ($on_error === null) {
                         throw $e;
                     }
                     if (isset($on_error[0])) {
                         Http::status_header((int) $on_error[0]);
                     }
                     if (isset($on_error[2])) {
                         Http::redirect($on_error[2]);
                     }
                     if (isset($on_error[1])) {
                         $template = new Template();
                         $template->output($on_error[1]);
                     }
                     exit;
                 }
             }
             if ($obj instanceof self) {
                 $obj = $obj->templ();
             }
             if (!$obj instanceof Template) {
                 throw new Exception("Forbidden " . $this->args());
             }
             $obj->path($this->path());
             $obj->url($this->url());
             $this->templ = $obj;
             if (!$this->isTemplate()) {
                 $this->template($obj->filename());
             }
             if (!$this->isTemplate()) {
                 $cs = explode(".", $this->class);
                 $class = array_pop($cs);
                 $class = implode("/", $cs) . (!empty($cs) ? "/" : "") . strtolower($class[0]) . substr($class, 1);
                 $this->template($class . "/" . $method . ".html");
             }
             return $this;
         }
     }
     throw new Exception("no match pattern");
 }