承接 borisguery/paginated-resource 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

borisguery/paginated-resource

Composer 安装命令:

composer require borisguery/paginated-resource

包简介

A resource wrapper to encapsulate paginated resource into REST-like API.

README 文档

README

Table of contents

  1. Description
  2. Installation
  3. Usage
  4. Basic usage
  5. Using the ResourceFactory
  6. Using ResourceFactory and JMSSerializerBundle
    1. JMSSerializerBundle config to the rescue
  7. Custom Resources
    1. Available Resources
    2. Adding custom Resource
  8. Run the test
  9. Contributing
  10. Requirements
  11. Authors
  12. License

Description

A resource wrapper to encapsulate paginated resource into REST-like API.

It is designed to provide paging data along with a resource collection.

It is mostly supposed to be used with JMSSerializerBundle but there are no dependencies against it and it should work with/without any other Serializer component.

This library is mostly a draft but has been heavily tested and it should be production ready in most case.

Installation

Using Composer, just require the borisguery/paginated-resource package:

{
  "require": {
    "borisguery/paginated-resource": "dev-master"
  }
}

If you wish to use this library with Pagerfanta or Doctrine 2 ArrayCollection, install the according dependencies:

{
  "require": {
    "pagerfanta/pagerfanta": "*",
    "doctrine/common": ">2.0",
  }
}

It may not be needed if you already use Doctrine2 ORM and/or any of the Pagerfanta Bundle.

Usage

Basic usage

namespace Acme;

use Bgy\PaginatedResource\Resource\ArrayResource;

class ArticlesController {

    public function getArticles()
    {
        $articles = array(
            array(
                'title' => 'Lorem ipsum',
                'body'  => 'Not worth reading',
            ),
            array(
                'title' => 'Dolor sid amet',
                'body'  => 'Awesome blog post',
            ),
        );

        $resource = new ArrayResource($articles, 'articles');

        echo json_encode(
            array(
                $key     => $resource->getData(),
                'paging' => $resource->getPaging(),
            )
        );
    }
}

The result will looks like:

{
    "articles": [
       {
           "title": "Lorem ipsum",
           "body" : "Not worth reading"
       },
       {
           "title": "Dolor sid amet",
           "body" : "Awesome blog post"
       }
    ],
    "paging": {
        "total_item_count":    2,
        "total_page_count":    1,
        "item_count_per_page": 2,
        "current_page":        1,
        "current_item_count":  2
    }
}

Obviously, this isn't really usefull since we have to know both the initial resource type and we need to serialize the data ourselve.

Thanks to the ResourceFactory you can dynamically create resource according to their initial type.

Using the ResourceFactory

namespace Acme;

use Bgy\PaginatedResource\ResourceFactory;

class ArticlesController {

    public function getArticles()
    {
        $articles = array(
            array(
                'title' => 'Lorem ipsum',
                'body'  => 'Not worth reading',
            ),
            array(
                'title' => 'Dolor sid amet',
                'body'  => 'Awesome blog post',
            ),
        );

        $resource = ResourceFactory::create($articles, 'articles');

        echo $this->dependencyInjectionContainer->get('serializer')
            ->serialize($resource, 'json');
    }
}

Will result the same as above.

Using ResourceFactory and JMSSerializerBundle

Taking the previous example, you may need to manually configure how the serialized result will look like. Let's try with the JMSSerializerBundle.

If we assume the 'serializer' service from the above example is an instance of JMS\SerializerBundle\Serializer\Serializer the result will looks like this:

{
    {
        "total_item_count":    2,
        "total_page_count":    1,
        "item_count_per_page": 2,
        "current_page":        1,
        "current_item_count":  2
    },
    "data_key": "articles",
    "articles": [
       {
           "title": "Lorem ipsum",
           "body" : "Not worth reading"
       },
       {
           "title": "Dolor sid amet",
           "body" : "Awesome blog post"
       }
    ],
    "paging": {
        "total_item_count":    2,
        "total_page_count":    1,
        "item_count_per_page": 2,
        "current_page":        1,
        "current_item_count":  2
    }
}

Not really sexy.

JMSSerializerBundle config to the rescue

In the contrib/ folder, you will find a basic configuration which will make the Serializer acts as we want to. In order to configure it you need to specify to the Serializer where are the associated configuration for the base class.

This may be found in contrib/jms-serializer/PaginatedResource/Resource/AbstractResource.yml

Just add:

      jms_serializer:
          metadata:
              directories:
                  BgyPaginatedResource:
                    namespace_prefix: 'Bgy\PaginatedResource\Resource'
                    path: "%kernel.root_dir%/../vendor/borisguery/paginated-resource/contrib/jms-serializer/Bgy/PaginatedResource/Resource"

To your config.yml.

This will results in:

{
    "articles": [
       {
           "title": "Lorem ipsum",
           "body" : "Not worth reading"
       },
       {
           "title": "Dolor sid amet",
           "body" : "Awesome blog post"
       }
    ],
    "paging": {
        "total_item_count":    2,
        "total_page_count":    1,
        "item_count_per_page": 2,
        "current_page":        1,
        "current_item_count":  2
    }
}

Custom Resources

Available Resources

There are currently 4 allowed Resources.

  • NullResource, usefull when your collection is empty and returns NULL
  • ArrayResource, which takes a native PHP array
  • ArrayCollectionResource, intended to work with Doctrine\Common\Collection\ArrayCollection
  • PagerfantaResource, works with the Pagerfanta paginator

Adding custom Resource

If you want to make it work with your own type, you can add Custom Resource, just implement the ResourceInterface.

It may be tricky to implement it yourself, and you likely want to extend the AbstractResource because the properties need to exist to make the serializer works correctly.

Take a look at the existing Resource to know what to do with it, it is really simple, I promise.

Run the test

First make sure you have installed all the dependencies, run:

$ composer install --dev

then, run the test from within the root directory:

$ phpunit

Contributing

If you have some time to spare on an useless project and would like to help take a look at the list of issues.

Requirements

  • PHP 5.3+
  • Internet connection

Authors

Boris Guéry - guery.b@gmail.com - http://twitter.com/borisguery - http://borisguery.com

License

Bgy\PaginatedResource is licensed under the WTFPL License - see the LICENSE file for details

borisguery/paginated-resource 适用场景与选型建议

borisguery/paginated-resource 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.88k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2012 年 10 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 borisguery/paginated-resource 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 6.88k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: WTFPL
  • 更新时间: 2012-10-03