定制 gobline/presenter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

gobline/presenter

Composer 安装命令:

composer require gobline/presenter

包简介

Presenter component

README 文档

README

The Presenter component allows you to decorate your model objects with methods used by your views. A typical example used to demonstrate the use for presenters, is a presenter method formatting a date from the model in a more readable format for the user.

Gobline\Presenter\Presenter

By decorating your model objects with a Gobline\Presenter\Presenter instance, you will be able to access private/protected fields as you would access a public field, as long as there is a getter defined.

class Property
{
    protected $price;
    protected $nbRooms;
    protected $livingArea;
    protected $publicationDate;

    public function __construct($price, $nbRooms, $livingArea)
    {
        $this->price = $price;
        $this->nbRooms = $nbRooms;
        $this->livingArea = $livingArea;
        $this->publicationDate = new \DateTime();
    }
    public function getPrice() { return $this->price; }
    public function getNbRooms() { return $this->nbRooms; }
    public function getLivingArea() { return $this->livingArea; }
    public function getPublicationDate() { return $this->publicationDate; }
}

$property = new Property(650, 1, 120);
$property = new Gobline\Presenter\Presenter($property);
<label>Living area:</label><?= $property->livingArea ?>

This is simply done by PHP's magic methods __get and __call.

In order to add behavior to your model objects for view formatting, you will have to create your own presenter:

class PropertyPresenter extends Gobline\Presenter\Presenter
{
    public function __construct(Property $subject)
    {
        parent::__construct($subject);
    }

    public function getPublicationDate()
    {
        return $this->subject->getPublicationDate()->format('d/m/Y');
    }

    public function getNbRooms()
    {
        $nbRooms = $this->subject->getNbRooms();

        if ($nbRooms === 0) {
            return 'Studio';
        }

        if ($nbRooms === 1) {
            return $nbRooms.' room';
        }

        return $nbRooms.' rooms';
    }
}

$property = new Property(650, 1, 120);
$property = new PropertyPresenter($property);
<label>Living area:</label><?= $property->livingArea ?>
<label>Number of rooms:</label><?= $property->nbRooms ?>
<label>Publication date:</label><?= $property->publicationDate ?>

Gobline\Presenter\PresenterFactoryInterface

A presenter factory is useful when your presenter requires dependencies. You inject the dependencies once in the factory, and the factory will create the presenter on demand with all its required dependencies.

use Gobline\Translator\Translator;

class PropertyPresenterFactory implements Gobline\Presenter\PresenterFactoryInterface
{
    protected $translator;

    public function __construct(Translator $translator)
    {
        $this->translator = $translator;
    }

    public function createPresenter($property)
    {
        return new PropertyPresenter($property, $this->translator);
    }
}

You would then typically inject this factory into the object that will return the decorated model instances to the view. That object can now return decorated model instances like the following:

$property = $this->propertyPresenterFactory->createPresenter($property);

Gobline\Presenter\CollectionPresenter

When the view needs to display data from an array of model instances, we then need to wrap this array in order to return our presenters (decorated model instances) for each array access. The collection wrapper requires a presenter factory in order to return the right presenters.

$results = $this->orm->findAll();
$results = new Gobline\Presenter\CollectionPresenter($results, $this->propertyPresenterFactory);

Gobline\Presenter\PresenterTrait

The presenter trait allows your private/protected properties to be accessed as public properties.

Installation

You can install the Presenter component using the dependency management tool Composer. Run the require command to resolve and download the dependencies:

composer require gobline/presenter

gobline/presenter 适用场景与选型建议

gobline/presenter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-12-03