コード例 #1
0
ファイル: Router.class.php プロジェクト: discophp/framework
 /**
  * Process the the router.
  *
  * Find if there is a match and take the appropriate action:
  *     - Execute an instance of a Closure
  *     - Resolve the requested Controller and method
  *     - Bind the passed data into the Closure function or class method
  *     - Filter routes to another Router or Closure
  *
  *
  * @return void
  */
 public function process()
 {
     static::$numberOfProcessedRoutes++;
     if ($this->requestMethod && $this->requestMethod != $_SERVER['REQUEST_METHOD']) {
         if ($this->children) {
             $this->buildRelativeChildren();
         }
         //if
         return;
     }
     //if
     if (!$this->uri) {
         return;
     }
     //if
     if ($this->secureRoute && empty($_SERVER['HTTPS'])) {
         return;
     }
     //if
     //no route match yet?
     if (!static::$routeMatch) {
         //this Router is a Filter?
         if ($this->isFilter) {
             //Filter matches?
             if ($this->filterMatch($this->uri, $this->auth)) {
                 if ($this->useRouter instanceof \Closure) {
                     call_user_func_array($this->useRouter, array($this->filterBase, $this->filteredOn));
                 } else {
                     if ($this->useRouter) {
                         static::useRouter($this->useRouter);
                     }
                 }
                 //el
                 //process the Routers that became available from calling the filter
                 static::processAvailableRoutes();
                 if (!static::$routeMatch && $this->children) {
                     $children = array();
                     foreach ($this->children as $uri => $route) {
                         $children[$this->filterBase . $uri] = $route;
                     }
                     //foreach
                     static::processRouterArray($children);
                 }
                 //if
             }
             //if
         } else {
             if ($this->paginate) {
                 //base uri of pagination matched without including pagination format
                 if ($this->match()) {
                     //default value of 1 for page variable (which is required in pagination format)
                     $this->variables['page'] = 1;
                     static::executeRoute();
                 } else {
                     $format = '/page/{page}';
                     if (\App::configKeyExists('paginate')) {
                         $format = \App::config('paginate');
                     }
                     //if
                     if (substr($format, 0, 1) == '/' && substr($this->uri, -1) == '/') {
                         $this->uri = rtrim($this->uri, '/');
                     }
                     //elif
                     $this->uri .= $format;
                     $this->variableRestrictions['page'] = 'integer_positive';
                     if ($this->match()) {
                         if ($this->variables['page'] == '0') {
                             $page0 = str_replace('{page}', '0', $format);
                             $page1 = str_replace('{page}', '1', $format);
                             $redirect = str_replace($page0, $page1, $this->uri);
                             header("Location: {$redirect}");
                             exit;
                         }
                         //if
                         static::$paginateCurrentPage = $this->variables['page'];
                         static::executeRoute();
                     }
                     //if
                 }
                 //el
             } else {
                 if ($this->match()) {
                     static::executeRoute();
                 } else {
                     if ($this->children) {
                         $this->buildRelativeChildren();
                     }
                 }
                 //elif
             }
         }
         //el
     }
     //if
 }
コード例 #2
0
ファイル: Paginate.class.php プロジェクト: discophp/framework
 /**
  * From the passed arguements all public variables of this class can be derived and will be set.
  *
  *
  * @param int $currentPage The current page being paginated.
  * @param int $totalItems The total items being paginated.
  * @param int $limit The limit used in the query for results to be paginated.
  */
 public function __construct($currentPage, $totalItems, $limit)
 {
     self::$paginateUsed = true;
     if (\App::configKeyExists('paginate')) {
         $this->format = \App::config('paginate');
     }
     //if
     if ($currentPage == 0) {
         $currentPage = 1;
     }
     //if
     $this->uri = explode('?', $_SERVER['REQUEST_URI'])[0];
     $currentPageFormat = $this->fromFormat($currentPage);
     if (strpos($this->uri, $currentPageFormat) === false) {
         if (substr($this->format, 0, 1) == '/' && substr($this->uri, -1) == '/') {
             $this->uri = rtrim($this->uri, '/');
         }
         //elif
         $this->uri .= $currentPageFormat;
     }
     //if
     //public vars
     $this->currentPage = $currentPage;
     $this->limit = $limit;
     $this->totalItems = $totalItems;
     $this->totalPages = ceil($this->totalItems / $this->limit);
     $this->perPage = $limit;
     $this->total = $this->totalItems;
     $this->first = ($this->currentPage - 1) * $this->limit + 1;
     if ($this->totalPages != $this->currentPage) {
         $this->totalDisplayed = $this->limit;
     } else {
         $this->totalDisplayed = $this->totalItems % $this->limit;
         if ($this->totalDisplayed == 0) {
             $this->totalDisplayed = $this->limit;
         }
         //if
     }
     //el
     $this->last = $this->first + $this->totalDisplayed - 1;
     if ($this->totalPages < $this->currentPage) {
         self::$pageDoesNotExist = true;
     }
     //if
     $this->firstUrl = $this->getPageUrl(1);
     $this->currentUrl = $this->getPageUrl($this->currentPage);
     $this->lastUrl = $this->getPageUrl($this->totalPages);
     if (!self::$pageDoesNotExist) {
         $domain = \App::domain();
         if ($currentPage != 1) {
             $this->prevUrl = $this->getPageUrl($currentPage - 1);
             \View::headExtra("<link rel='prev' href='{$domain}{$this->prevUrl}'>");
         }
         //if
         if ($this->currentPage != $this->totalPages && $this->totalPages != 0) {
             $this->nextUrl = $this->getPageUrl($currentPage + 1);
             \View::headExtra("<link rel='next' href='{$domain}{$this->nextUrl}'>");
         }
         //if
         if ($currentPage == 1 && $this->uri != $this->currentUrl) {
             \View::headExtra("<link rel='canonical' href='{$domain}{$this->currentUrl}'/>");
         }
         //if
     }
     //if
 }