示例#1
0
 public function __construct($method, $url)
 {
     if (!count(self::$directories)) {
         throw new \Exception('No controller directories have been registered! (Use Controller::registerDirectory(...))');
     }
     $this->method = strtoupper($method);
     $url = parse_url($url);
     $this->scheme = !empty($url['scheme']) ? $url['scheme'] : (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http');
     $this->host = !empty($url['host']) ? $url['host'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
     $this->port = !empty($url['port']) ? $url['port'] : (isset($_SERVER['HTTP_PORT']) ? $_SERVER['HTTP_PORT'] : '');
     $this->path = !empty($url['path']) ? trim($url['path'], '/') : '';
     $this->query = !empty($url['query']) ? $url['query'] : '';
     $sep = DIRECTORY_SEPARATOR;
     $this->segments = Controller::cleanPath($this->path);
     $max = null;
     foreach (self::$directories as $dir) {
         $info = ['dir' => $dir, 'args' => [], 'file' => $this->segments, 'extension' => ''];
         if ($dir['prefix']) {
             $prefix = Controller::cleanPath($dir['prefix']);
             if ($prefix == (array) _::first($this->segments, count($prefix))) {
                 $info['file'] = (array) _::rest($this->segments, count($prefix));
             }
         }
         if ($dir['extensions']) {
             if ($dir['extensions'] && count($info['file']) > 0) {
                 $lastIndex = count($info['file']) - 1;
                 $fileParts = explode('.', $info['file'][$lastIndex], 2);
                 if (count($fileParts) == 2) {
                     if (is_string($dir['extensions'])) {
                         $dir['extensions'] = [$dir['extensions']];
                     }
                     if (!is_array($dir['extensions']) || is_array($dir['extensions']) && count(array_filter($dir['extensions'], function ($prefix) {
                         return strpos($this->path, $prefix . '/') === 0 || strpos($this->path, $prefix . '.') === 0 || trim($prefix, '/') === $this->path;
                     }))) {
                         $info['extension'] = $fileParts[1];
                         $info['file'][$lastIndex] = $fileParts[0];
                     }
                 }
             }
         }
         do {
             $file = implode($sep, $info['file']);
             if (is_file("{$info['dir']['path']}{$sep}{$file}{$sep}_default.php") && ($info['file'][] = '_default')) {
                 break;
             }
             if (is_file("{$info['dir']['path']}{$sep}{$file}.php")) {
                 break;
             }
             array_unshift($info['args'], $slug = array_pop($info['file']));
         } while ($slug);
         if (!$max || count(_::reject($info['file'], function ($f) {
             return $f == '_default';
         })) > count(_::reject($max['file'], function ($f) {
             return $f == '_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->extension = $max['extension'];
     $this->responseHeaders = new HttpHeaders();
 }
function _reject($list, $iterator, $context = NULL)
{
    return Underscore::reject($list, $iterator, $context);
}
示例#3
0
 /**
  * @tags collections
  */
 public function testReject()
 {
     $even = function ($v) {
         return !($v & 1);
     };
     // it should work with arrays, objects and iterators
     $this->typeTolerant([1, 2, 3, 4], [0 => 1, 2 => 3], function ($in, $out) use($even) {
         $this->array(_::reject($in, $even))->isEqualTo($out);
     }, [0, -1]);
     // it should preserve associativity and order
     $this->variable(_::reject(['a' => 1, 9 => 2, null => 3, 0.2 => 4], $even))->isEqualTo(['a' => 1, null => 3]);
     // it should be possible to specify a context for the thruth test
     $this->variable(_::reject([1, 2, 3, 4], function ($v) {
         return $v % $this->mod == 0;
     }, (object) ['mod' => 2]))->isEqualTo([0 => 1, 2 => 3]);
     // it should return an empty array if everything passes the truth test
     $this->array(_::reject([2, 4, 6, 8], $even))->hasSize(0);
     // it should return an empty array if the list is empty
     $this->array(_::reject([], $even))->hasSize(0);
 }