コード例 #1
0
ファイル: Controller.php プロジェクト: saffyre/saffyre
 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();
 }
コード例 #2
0
function _rest($array, $index = 1, $guard = false)
{
    return Underscore::rest($array, $index, $guard);
}
コード例 #3
0
ファイル: Underscore.php プロジェクト: brombal/underscore.php
 /**
  * @dataProvider peopleDataProvider
  * @tags arrays
  */
 public function testRest($people, $type, $meta)
 {
     // it should return the rest list item
     $this->variable(_::rest($people, 4))->isEqualTo(_::get($people, 'rscott'));
     // it should return the rest N list items
     $this->array(_::rest($people, 3))->isEqualTo([_::get($people, 'nportman'), _::get($people, 'rscott')]);
     // it should always return an array when guard is true
     $this->array(_::rest($people, 4, true))->isEqualTo([_::get($people, 'rscott')]);
     // it should return null if provided an empty list
     $this->variable(_::rest([]))->isNull();
 }