You are here

class CleanPager in Clean Pagination 8

Provides a HTTP middleware.

Hierarchy

  • class \Drupal\cleanpager\StackMiddleware\CleanPager implements \Symfony\Component\HttpKernel\HttpKernelInterface

Expanded class hierarchy of CleanPager

1 string reference to 'CleanPager'
cleanpager.services.yml in ./cleanpager.services.yml
cleanpager.services.yml
1 service uses CleanPager
http_middleware.cleanpager in ./cleanpager.services.yml
Drupal\cleanpager\StackMiddleware\CleanPager

File

src/StackMiddleware/CleanPager.php, line 14

Namespace

Drupal\cleanpager\StackMiddleware
View source
class CleanPager implements HttpKernelInterface {

  /**
   * The wrapped HTTP kernel.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $httpKernel;

  /**
   * Constructs a new TestMiddleware object.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $kernel
   *   The decorated kernel.
   * @param mixed $optional_argument
   *   (optional) An optional argument.
   */
  public function __construct(HttpKernelInterface $http_kernel) {
    $this->httpKernel = $http_kernel;
  }
  public function cleanPagerGetQ($request) {
    $q = isset($_GET['q']) ? $_GET['q'] : '';
    $current_path = $request
      ->getPathInfo();
    $path = explode('/', $current_path);
    if ($path[1] == 'views' && $path[2] == 'ajax' && !empty($_POST['view_path'])) {
      $q = rtrim($_POST['view_path'], '/');
    }
    return $q ? $q : $current_path;
  }
  public function rewriteUrl(Request $request, $q) {
    global $_cleanpager_rewritten;
    $q_array = explode('/', $q);
    if (\Drupal::configFactory()
      ->get('cleanpager.settings')
      ->get('cleanpager_add_trailing')) {
      array_pop($q_array);
    }
    if ($this
      ->cleanPagerIsPagerElement(end($q_array))) {
      $_cleanpager_rewritten = FALSE;
      $p = array_pop($q_array);
      if (end($q_array) == 'page') {
        array_pop($q_array);
        $_cleanpager_rewritten = TRUE;
        $q = implode('/', $q_array);
        $current_path = $request
          ->getPathInfo();
        $path_args = explode('/', $current_path);
        if ($path_args[1] == 'views' && $path_args[2] == 'ajax' && !empty($_POST['view_path'])) {
          $q = '/views/ajax';
        }
        else {
          $_REQUEST['q'] = $_GET['q'] = $q;
          $request->query
            ->set('q', $q);
        }
        $_REQUEST['page'] = $_GET['page'] = $p;
        $request->server
          ->set('REQUEST_URI', $q);
        $request->server
          ->set('REDIRECT_URL', $q);
        $query_string = $request->server
          ->get('QUERY_STRING', $q);
        $request->server
          ->set('QUERY_STRING', $query_string . '&page=' . $p);
        $request->query
          ->add([
          'page' => $p,
        ]);
        $request
          ->initialize($request->query
          ->all(), $request->request
          ->all(), $request->attributes
          ->all(), $request->cookies
          ->all(), $request->files
          ->all(), $request->server
          ->all(), $request
          ->getContent());
      }
    }
    return $request;
  }

  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
    global $_cleanpager_pagination;
    $path = $this
      ->cleanPagerGetQ($request);
    if ($path_length = strpos($path, '/page/')) {
      $path_test_part = substr($path, 0, $path_length);
    }
    else {
      $path_test_part = $path;
    }
    $pages = \Drupal::configFactory()
      ->get('cleanpager.settings')
      ->get('cleanpager_pages');
    if (\Drupal::service('path.matcher')
      ->matchPath($path_test_part, $pages)) {
      $_cleanpager_pagination = TRUE;
      $result = $this
        ->rewriteUrl($request, $path);
    }
    else {
      $_cleanpager_pagination = FALSE;
      $result = $request;
    }
    return $this->httpKernel
      ->handle($result, $type, $catch);
  }
  public function cleanPagerIsPagerElement($value) {
    if (is_numeric($value)) {
      return TRUE;
    }

    // Handle multiple pagers (i.e. 0,0,1,0);
    $parts = explode(',', $value);
    foreach ($parts as $p) {
      if (!is_numeric($p)) {
        return FALSE;
      }
    }
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CleanPager::$httpKernel protected property The wrapped HTTP kernel.
CleanPager::cleanPagerGetQ public function
CleanPager::cleanPagerIsPagerElement public function
CleanPager::handle public function Handles a Request to convert it to a Response.
CleanPager::rewriteUrl public function
CleanPager::__construct public function Constructs a new TestMiddleware object.