beberlei/porpaginas 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

beberlei/porpaginas

Composer 安装命令:

composer require beberlei/porpaginas

包简介

Library that generically solves several pagination issues with DAO/repository abstractions.

README 文档

README

Build Status

This library solves a bunch of issues that comes up with APIs of Repository classes alot:

  • You need different methods for paginatable and non-paginatable finder methods.
  • You need to expose the underlying data-source and return query objects from your repository.
  • Serialization of paginators should be easily possible for REST APIs

Both Pagerfanta and KnpLabs Pager don't solve this issue and their APIs are really problematic in this regard. You need to return the query objects or adapters for paginators from your repositories to get it working and then use an API on the controller side to turn them into a paginated result set.

Porpaginas solves this by introducing a sane abstraction for paginated results. For rendering purposes you can integrate with either Pagerfanta or KnpLabs Pager, this library is not about reimplementating the view part of pagination.

Central part of this library is the interface Result:

<?php
namespace Porpaginas;

/**
 * @template-covariant T
 * @extends IteratorAggregate<array-key, T>
 */
interface Result extends Countable, IteratorAggregate
{
    /**
     * @param int $offset
     * @param int $limit
     * @return Page<T>
     */
    public function take($offset, $limit);

    /**
     * Return the number of all results in the paginatable.
     *
     * @return int
     */
    public function count();

    /**
     * Return an iterator over all results of the paginatable.
     *
     * @return Traversable<array-key, T>
     */
    public function getIterator();
}

This API offers you two ways to iterate over the paginatable result, either the full result or a paginated window of the result using take(). One drawback is that the query is always lazily executed inside the Result and not directly in the repository.

The Page interface returned from Result#take() looks like this:

<?php

namespace Porpaginas;

/**
 * @template-covariant T
 * @extends IteratorAggregate<array-key, T>
 */
interface Page extends Countable, IteratorAggregate
{
    /**
     * Return the number of results on the current page.

     * @return int
     */
    public function count();

    /**
     * Return the number of ALL results in the paginatable.

     * @return int
     */
    public function totalCount();

    /**
     * Return an iterator over selected windows of results of the paginatable.
     *
     * @return Traversable<array-key, T>
     */
    public function getIterator();
}

You can use the IteratorPage in your own implementations, if the underlying data-source is itself an iterator.

Supported Backends

  • Array
  • Doctrine ORM

Example

Take the following example using Doctrine ORM:

<?php
class UserRepository extends EntityRepository
{
    /**
     * @return \Porpaginas\Result<User>
     */
    public function findAllUsers()
    {
        $qb = $this->createQueryBuilder('u')->orderBy('u.username');

        return new ORMQueryResult($qb);
    }
}

class UserController
{
    /**
     * @var UserRepository
     */
    private $userRepository;

    public function listAction(Request $request)
    {
        $result = $this->userRepository->findAllUsers();
        // no filtering by page, iterate full result

        return array('users' => $result);
    }

    public function porpaginasListAction(Request $request)
    {
        $result = $this->userRepository->findAllUsers();

        $paginator = $result->take(($request->get('page')-1) * 20, 20);

        return array('users' => $paginator);
    }
}

Now in the template for porpaginasListAction using the porpaginas Twig extension for example:

We found a total of <strong>{{ porpaginas_total(users) }}</strong> users:

<ul>
{% for user in users %}
    <li>{{ user.name }}</li>
{% endfor %}
</ul>

{{ porpaginas_render(users) }}

Pager Library Support

  • For Pagerfanta use the Porpaginas\Pagerfanta\PorpaginasAdapter and pass it a result as first argument.

Embedded Pager

You can use the Porpaginas\Pager class to help you get a slice of previous and next pages to display:

$pager = Porpaginas\Pager::fromPage($page);

Passed to a twig template:

<nav class="pages">
    <ul>
        {% for page in pager.pages() %}
            <li class="{{ pager.isCurrent(page) ? 'active' }}">
                <a href="{{ page }}">{{ page }}</a>
            </li>
        {% endfor %}
    </ul>
</nav>

beberlei/porpaginas 适用场景与选型建议

beberlei/porpaginas 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 638.12k 次下载、GitHub Stars 达 163, 最近一次更新时间为 2014 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「page」 「paging」 「paginator」 「pagination」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 beberlei/porpaginas 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 beberlei/porpaginas 我们能提供哪些服务?
定制开发 / 二次开发

基于 beberlei/porpaginas 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 638.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 164
  • 点击次数: 28
  • 依赖项目数: 14
  • 推荐数: 2

GitHub 信息

  • Stars: 163
  • Watchers: 9
  • Forks: 19
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-01-04