deefour/presenter
Composer 安装命令:
composer require deefour/presenter
包简介
Presenters/Decorators for PHP Objects
README 文档
README
Object-oriented presentation logic.
Getting Started
Run the following to add Presenter to your project's composer.json. See Packagist for specific versions.
composer require deefour/presenter
>=PHP5.5.0 is required.
The Resolver
The Deefour\Presenter\Resolver determines the FQN of a presenter class associated with an object. The default behavior of the resolver is to append 'Presenter' to the Article FQN.
use Deefour\Presenter\Resolver; (new Resolver)->presenter(new Article); //=> 'ArticlePresenter'
This behavior can be customized by passing a callable to the resolver.
use Deefour\Presenter\Resolver; $resolver = new Resolver; $resolver->resolveWith(function ($instance) { return "App\Presenters\" . get_class($instance) . 'Presenter'; }); $resolver->presenter(new Article); //=> 'App\Presenters\ArticlePresenter'
The resolver will look for a modelClass() method on an object. If found, the returned FQN will be used instead of the object itself.
class BlogPost { static public function modelClass() { return Article::class; } } (new Resolver)->presenter(new BlogPost); //=> 'ArticlePresenter'
Instantiation
Instantiating an instance of a presenter is your responsibility.
use Deefour\Presenter\Resolver; $article = new Article; $presenterName = (new Resolver)->presenter($article); $presenter = new $presenterName($article);
use Deefour\Presenter\Resolver; (new Resolver)->presenter(new Article); //=> 'BlogPresenter'
If the resulting FQN from the resolver does not match an existing, valid class name, null will be returned or a NotDefinedException will be thrown.
use Deefour\Presenter\Resolver; (new Resolver)->presenter(new ObjectWithoutPresenter); //=> null (new Resolver)->presenterOrFail(new ObjectWithoutPresenter); //=> throws NotDefinedException
Presenters
The presenters themselves extend Deefour\Presenter\Presenter.
use Deefour\Presenter\Presenter; class ArticlePresenter extends Presenter { public function isDraft() { return $this->_model->isDraft() ? 'Yes' : 'No'; } }
The API
A quick overview of the API available.
use Deefour\Producer\Factory; $presenter = (new Factory)->make(new Article, 'presenter'); //=> ArticlePolicy $presenter->_model; //=> Article $presenter->_model->isDraft(); //=> false $presenter->isDraft(); //=> 'No' $presenter->is_draft; //=> 'No' $presenter->_model()->published; //=> true $presenter->published; //=> true
A few things to notice:
- The underlying object decorated by the presenter can be accessed via the
$_modelproperty ormodel()method. - Any property or method publicly accessible on the underlying object can also be accessed directly through the presenter.
- Any publicly accessible, camel-cased method on the presenter or underlying model can be accessed via snake-cased property access.
Automatic Presenter Resolution
When a property or method is resolved through the __get() or __call() methods on the presenter, an attempt will be made to resolve and wrap the return value in a presenter too.
namespace App; use Illuminate\Support\Collection; class Article { public function category() { return new Category; } public function tags() { $collection = new Collection; $collection->push(new Tag); $collection->push(new Tag); $collection->push(new Tag); return $collection; } }
Given the existence of ArticlePresenter, CategoryPresenter, and TagPresenter, the following will be returned
use Deefour\Presenter\Resolver; $presenter = (new Resolver)->presenter(new Article); //=> ArticlePresenter (new $presenter)->category; //=> CategoryPresenter (new $presenter)->tags->first(); //=> TagPresenter
Note: The collection resolution works by looking for an instance of
IteratorAggregate. The iterator is used to loop through the collection and generate presenters for each item. An attempt is then made to instantiate a new instance of the original object implementingIteratorAggregate. That is the return value.
If you want access to the raw association, simply request it from the underlying object.
$presenter->_model->tags()->first(); //=> Tag
Contribute
- Issue Tracker: https://github.com/deefour/presenter/issues
- Source Code: https://github.com/deefour/presenter
Changelog
3.0.1 - November 7, 2017
- Check for the existence of a method on the underlying modely before checking if it's a property. Fixes a conflict with Laravel's
__isset()implementation onIlluminate\Database\Eloquent\Model.
3.0.0 - July 20, 2017
- The resolver no longer accepts an object during instantiation. Instead, objects are passed directly to the
presenter()andpresenterOrFail()methods. - A new
resolveWith()method on the resolver accepts a callable to customize resolution. - Support for the
presenterClass()has been removed from the resolver in favor of the newresolveWith()method on the resolver. You can pass a callable with your existingpresenterClass()logic to the resolver instead. - Model access should only be done through the new
model()method. Access to_modelhas been disabled.
2.0.0 - February 12, 2017
- Replaced
Factorywith newResolverclass. - Removed dependency on
deefour\producer - Removed
Presentablecontract - Simplified
README.md
1.0.0 - October 7, 2015
- Release 1.0.0.
0.8.0 - August 8, 2015
- Compat changes for updates to
deefour/producer. - New
Factoryclass is available to prevent the need to interact directly with the factory indeefour/producer. - Abstracted presenter resolution out to new
deefour/producer. - Removed the Laravel service provider and facade. The
'producer'service indeefour/producershould be used instead.
0.6.2 - June 5, 2015
- Now following PSR-2.
0.6.0 - May 24, 2015
- Removed
model()method on base presenter. - Renamed
$modelproperty to$_modelto avoid conflicts with an actual model attribute with the name'model'. - Presenters now only provide property access to public properties on the presenter.
- Prefixed API methods/properties with
_on the base presenter to further avoid conflicts with attribute overrides. - Made
$_modelproperty public. - Updates to code formatting.
0.5.0 - April 27, 2015
- Snake-case to camel-case method conversions are now cached for performance
- Exceptions are no longer thrown for missing properties/methods. See
6f33ddafor an explanation.
0.4.0 - March 19, 2015
- Rename
presenter()helper topresent() - Remove
helpers.phpfrom Composer autoload. Developers should be able to choose whether these functions are included. - Cleaning docblocks.
- Type-hinting the presenter factory.
- Renaming
Presentabletrait toResolvesPresentersto avoid naming conflict with\Deefour\Presenter\Contracts\Presentable.
0.3.0 - March 16, 2015
- Allow presenters to be explicitly requested, bypassing the model default. For example
$article = new Article; echo get_class($article->presenter()); //=> 'ArticlePresenter' echo get_class($article->presenter(FeaturedArticlePresenter::class)); //=> 'FeaturedArticlePresenter'
0.2.3 - February 27, 2015
Illuminate\Support\Collectioninstances and native PHP arrays can now be passed directly into thepresenter()helper.
0.2.2 - February 20, 2015
- Updated support for Laravel's Eloquent relations. Relations are now fetched and converted to presenter-wrapped objects or collections when requested.
0.2.0 - February 5, 2015
- Fix service provider.
- Make global
presenter()work with Laravel IoC container if it's available. - Move trait.
0.1.0 - November 21, 2014
- Initial release.
License
Copyright (c) 2014 Jason Daly (deefour). Released under the MIT License.
deefour/presenter 适用场景与选型建议
deefour/presenter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.45k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2014 年 11 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「view」 「model」 「laravel」 「presenter」 「decorator」 「deefour」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 deefour/presenter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 deefour/presenter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 deefour/presenter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Illuminate View for Morningmedley.
Laravel 5 - Repositories to the database layer
Blade template for Kirby
统计信息
- 总下载量: 2.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-11-21