Example #1
0
 public static function request($name = null)
 {
     if ($name === null) {
         return Q::fromRequest('post', 'get');
     }
     if (isset(self::$_POST[$name])) {
         return self::$_POST[$name];
     }
     if (isset(self::$_GET[$name])) {
         return self::$_GET[$name];
     }
     self::$_POST[$name] = isset($_POST[$name]) ? self::clean($_POST[$name]) : null;
     self::$_GET[$name] = isset($_GET[$name]) ? self::clean($_GET[$name]) : null;
     return self::$_POST[$name] ? self::$_POST[$name] : self::$_GET[$name];
 }
Example #2
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;
     }
 }