예제 #1
0
 /**
  * @param \SplFileInfo $path
  * @param Request      $request
  * @param array        $env
  *
  * @return array
  */
 protected function createEnvVars(\SplFileInfo $path, Request $request, array $env = [])
 {
     $env = array_merge($env, ['REQUEST_URI' => $request->getUri() . ($request->getQueryString() ? '?' . $request->getQueryString() : ''), 'SERVER_NAME' => 'localhost', 'SERVER_PORT' => $request->getServerPort(), 'QUERY_STRING' => $request->getQueryString(), 'SCRIPT_NAME' => $request->getPath(), 'SCRIPT_FILENAME' => $path->getRealPath(), 'REQUEST_METHOD' => $request->getMethod(), 'REDIRECT_STATUS' => 200, 'SERVER_SOFTWARE' => 'Coupe/PHP ' . PHP_VERSION . ' Development Server', 'REMOTE_ADDR' => $request->getRemoteAddr(), 'REMOTE_PORT' => $request->getRemotePort()]);
     if ($request->getPathInfo()) {
         $env['PATH_INFO'] = $request->getPathInfo();
     }
     if ($request->getHeader('Content-Length')) {
         $env['CONTENT_LENGTH'] = $request->getHeader('Content-Length');
         $env['CONTENT_TYPE'] = $request->getHeader('Content-Type');
     }
     if ($request->getHeader('Https')) {
         $env['HTTPS'] = 1;
     }
     foreach ($request->getHeaders() as $name => $value) {
         $variable = 'HTTP_' . str_replace('-', '_', strtoupper($name));
         $env[$variable] = $value;
     }
     return $env;
 }
예제 #2
0
 /**
  * @param Request $request
  *
  * @return \SplFileInfo
  * @throws ResourceNotFoundException
  */
 protected function findFile(Request $request)
 {
     $path = $this->joinPath($this->options['docroot'], $request->getUri());
     $file = new \SplFileInfo($path);
     if ($file->isDir()) {
         // find indexes
         foreach ($this->options['indexes'] as $index) {
             $fixedPath = $this->joinPath($path, $index);
             $fixedFile = new \SplFileInfo($fixedPath);
             if ($fixedFile->isFile()) {
                 return $fixedFile;
             }
         }
     }
     if ($file->isFile()) {
         if (substr($path, -1) == '/') {
             $request->setPathInfo('/');
         }
         return $file;
     }
     if (preg_match('{(^[^\\.]*?\\.[^/]+)(/.*?$)}', $request->getUri(), $matches)) {
         $file = new \SplFileInfo($this->joinPath($this->options['docroot'], $matches[1]));
         if ($file->isFile()) {
             $request->setPathInfo($matches[2]);
             $request->setPath($matches[1]);
             return $file;
         }
     }
     // find fallback
     $path = $this->joinPath($this->options['docroot'], $this->options['fallback']);
     $file = new \SplFileInfo($path);
     if ($file->isFile()) {
         return $file;
     }
     throw new ResourceNotFoundException();
 }