コード例 #1
0
ファイル: HTTPRequestCustom.php プロジェクト: tomaka17/niysu
 /**
  * Builds a copy of another HTTPRequestInterface.
  *
  * @param HTTPRequestInterface 	$source 	The request to clone
  * @return HTTPRequestCustom
  */
 public static function copyOf(HTTPRequestInterface $source)
 {
     return new HTTPRequestCustom($source->getURL(), $source->getMethod(), $source->getHeadersList(), $source->getRawData(), $source->isHTTPS());
 }
コード例 #2
0
ファイル: RoutesCollection.php プロジェクト: tomaka17/niysu
 /**
  * Tries to handle an HTTP request through a route of this collection or its children collections.
  *
  * Returns true if the request was handled, false if the route didn't match this request.
  * See Route::handle for details.
  *
  * @param Scope 	$scope 		Scope that will contain the variables accessible to the route
  * @return boolean
  */
 public function handle(HTTPRequestInterface &$request, HTTPResponseInterface &$response, Scope $scope = null)
 {
     $fullPrefix = $this->getFullPrefix();
     if (strlen(trim($fullPrefix, '/')) && strpos($request->getURL(), $fullPrefix) !== 0) {
         return false;
     }
     foreach ($this->routes as $route) {
         if ($route->handle($request, $response, $scope)) {
             return true;
         }
     }
     foreach ($this->children as $child) {
         if ($child->handle($request, $response, $scope)) {
             return true;
         }
     }
     return false;
 }
コード例 #3
0
ファイル: Route.php プロジェクト: tomaka17/niysu
 private function doHandle(HTTPRequestInterface &$request, HTTPResponseInterface &$response, $scope, $prefix, $noURLCheck)
 {
     if (!$scope) {
         $scope = new Scope();
     }
     // checking method
     if (!$noURLCheck && !preg_match_all('/' . $this->method . '/i', $request->getMethod())) {
         return false;
     }
     // checking prefix
     $url = $request->getURL();
     if (!$noURLCheck && $prefix && strpos($url, $prefix) !== 0) {
         return false;
     }
     $url = substr($url, strlen($prefix));
     if ($url === false) {
         $url = '/';
     }
     // checking whether the URL matches
     $result = null;
     foreach ($this->urlPatterns as $p) {
         $result = $p->testURL($url);
         if (isset($result)) {
             break;
         }
     }
     if (!$noURLCheck && !isset($result)) {
         return false;
     }
     // logging
     //$scope->log->debug('URL '.$request->getURL().' matching route '.$this->urlPattern->getOriginalPattern().' with prefix '.$prefix.' ; regex is: '.$this->urlPattern->getURLRegex());
     // checking that the handler was defined
     if (!$this->callback) {
         throw new \LogicException('The handler of the route ' . $this->originalPattern . ' has not been defined');
     }
     // creating the local scope
     $localScope = clone $scope;
     // adding parts of the URL inside scope
     if (isset($result)) {
         foreach ($result as $varName => $value) {
             $localScope->set($varName, $value);
         }
     }
     // adding controlling variables to scope.
     $localScope->set('request', $request);
     $localScope->passByRef('request', true);
     $localScope->set('response', $response);
     $localScope->passByRef('response', true);
     $localScope->set('isRightResource', true);
     $localScope->set('stopRoute', false);
     // list of function objects to call
     $toCall = array_merge($this->before, [$this->callback], $this->after);
     // calling functions
     foreach ($toCall as $f) {
         $localScope->call($f);
         // checking controlling variables
         if ($localScope->get('isRightResource') === false) {
             if (isset($scope->log)) {
                 $scope->log->debug('Route ignored by function');
             }
             // pushing back in variables
             $request = $localScope->request;
             $response = $localScope->response;
             return false;
         }
         if ($localScope->get('stopRoute') === true) {
             if (isset($scope->log)) {
                 $scope->log->debug('Route\'s handler has been stopped by function');
             }
             break;
         }
     }
     // pushing back in variables
     $request = $localScope->request;
     $response = $localScope->response;
     return true;
 }