Пример #1
0
 public function __construct($fromRequest, $path = null, $method = null, $get = array(), $post = array(), $cookies = array(), $headers = array(), $body = null)
 {
     if (!count(self::$dirs)) {
         throw new Exception('No controller directories have been registered! (Use Controller::registerDirectory(...))');
     }
     $sep = DIRECTORY_SEPARATOR;
     if (is_array($path)) {
         $path = $sep . Saffyre::cleanPath($path, true);
     }
     $this->path = Saffyre::cleanPath($path);
     $max = null;
     foreach (self::$dirs as $dir) {
         $info = ['dir' => $dir['dir'] . $sep, 'args' => [], 'file' => $this->path];
         do {
             $file = implode($sep, $info['file']);
             if (is_file("{$info['dir']}{$sep}{$file}{$sep}_default.php") && ($info['file'][] = '_default')) {
                 break;
             }
             if (is_file("{$info['dir']}{$sep}{$file}.php")) {
                 break;
             }
             array_unshift($info['args'], $slug = array_pop($info['file']));
         } while ($slug);
         if (count(Util::array_clean($info['file'], '_default')) > count(Util::array_clean($max['file'], '_default'))) {
             $max = $info;
         }
     }
     if (!$max || !$max['file']) {
         throw new Exception('Invalid controller path. Maybe you don\'t have a _default.php file.');
     }
     $this->dir = $max['dir'];
     $this->args = $max['args'];
     $this->file = $max['file'];
     $this->get = $fromRequest ? Q::fromRequest('get') : new Q();
     $this->post = $fromRequest ? Q::fromRequest('post') : new Q();
     $this->cookie = $fromRequest ? Q::fromRequest('cookie') : new Q();
     $this->headers = new Q();
     if ($fromRequest) {
         $this->uri = $_SERVER['REQUEST_URI'];
         $this->method = strtoupper($_SERVER['REQUEST_METHOD']);
         foreach ($_SERVER as $key => $value) {
             if (strpos($key, 'HTTP_') === 0) {
                 $this->headers->{strtolower(str_replace(array('HTTP_', '_'), array('', '-'), $key))} = $value;
             }
         }
     }
     if ($path !== null) {
         $this->uri = $path;
     }
     if ($method !== null) {
         $this->method = strtoupper($method);
     }
     $this->get->__import($get);
     $this->post->__import($post);
     $this->cookie->__import($cookies);
     $this->headers->__import($headers);
     if ($body !== null) {
         if (is_object($body)) {
             $body = json_encode($body);
         }
         $this->body = $body;
     }
 }
Пример #2
0
 /**
  * The main execution method for the Saffyre framework. The method should only be invoked ONCE and there is no guarantee that
  * it will return execution (many methods and controllers will die or exit). This method starts an output buffer.
  *
  * @param string $path The path to execute, or the request URI if null
  * @param boolean $return True to return the response, or false to flush it.
  * @uses Controller
  */
 public static function execute($path = null, $return = false)
 {
     ob_start();
     if (!$path) {
         $path = strtok($_SERVER['REQUEST_URI'], '?');
     }
     self::$path = self::cleanPath($path);
     $required = array("URL_BASE", "URL_PATH");
     $undefined = array();
     foreach ($required as $const) {
         if (!defined($const)) {
             $undefined[] = $const;
         }
     }
     if ($undefined) {
         throw new Exception('The following constants are not defined: ' . join(', ', $undefined));
     }
     if (!defined('ENCODING')) {
         define('ENCODING', 'UTF-8');
     }
     header('Content-type: text/html; charset=' . ENCODING);
     header("X-Powered-By: Saffyre Framework 2.0", true);
     //include_once dirname(__FILE__) . '/Controller.php';
     $controller = new Controller(true, self::$path);
     $controller->isMainRequest = true;
     $response = $controller->execute(true);
     if (!$controller->resultWasOutput) {
         if (array_filter(headers_list(), function ($c) {
             return strpos(strtolower($c), 'content-type: application/json') === 0;
         })) {
             if ($response instanceof JsonSeriazable) {
                 echo json_encode($response);
             } else {
                 if (method_exists($response, '__toJson')) {
                     echo $response->__toJson();
                 } else {
                     echo json_encode($response);
                 }
             }
         } else {
             echo $response;
         }
     }
     if ($controller->status != null) {
         Saffyre::responseStatus($controller->status);
     }
     if ($return) {
         return ob_get_clean();
     } else {
         ob_flush();
     }
 }