承接 robclancy/presenter 相关项目开发

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

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

robclancy/presenter

Composer 安装命令:

composer require robclancy/presenter

包简介

Decorate your objects using presenters. Primarily to keep presentation logic out of your models.

README 文档

README

Decorate your objects using presenters. Primarily to keep presentation logic out of your models.

Ping me @robboclancy for any urgent issues, github isn't always correctly notifying me.

This library provides a simple class to help make a Presenter for your objects or arrays. It also has little extras for use within Laravel with minimal extra code in your controllers (in most cases no extra code).

Tests

Table of Contents

Installation

composer require robclancy/presenter:^2.0

or

Add robclancy/presenter to the "require" section of your composer.json file.

"robclancy/presenter": "^2.0"

Run composer update to get the latest version of the package.

Laravel

This package comes with an optional service provider for Laravel 5.8+ < Laravel 10 so that you can automate some extra steps. You will need to have installed using the composer method above, then register the service provider with your application.

Open app/config/app.php and find the providers key. Add

'Robbo\Presenter\PresenterServiceProvider',

to the array at some point after

'Illuminate\View\ViewServiceProvider',

Now presenters will automatically be created if using the laravel method described below.

Usage

Presenter is a very simple class that overloads methods and variables so that you can add extra logic to your objects or arrays without adding view logic to areas like your models or controllers and also keeps any extra logic out of your views.

General Usage

Let's say you have a list of users and you want to generate a link to the profile of each user. Many people would just build the URL in the view, or worse, in the controller. To separate this logic we instead use a presenter. Let's assume we have a User class which simply has an id and username property. The presenter might look like this.

class UserPresenter extends Robbo\Presenter\Presenter {

	public function url()
	{
		return $this->id.'-'.$this->username;
	}
}

Now our view should receive an instance of this presenter which would be created with something like $user = new UserPresenter(new User);. If we want to link to the users page all we have to do is call $user->url(). Now you have good separation of logic and an easy little class you can modify to add properties to your User in all areas. However, you might not want to be calling methods like this, it could be inconsistent with what you are doing, or you might want the code to look a little cleaner. That is where methods with the present prefix come in. All we do is update the presenter to the following.

class UserPresenter extends Robbo\Presenter\Presenter {

	public function presentUrl()
	{
		return $this->id.'-'.$this->username;
	}
}

Now the presenter will call this new method when you execute $user->url. Further more you can access this method via ArrayAccess by calling $user['url']. More on ArrayAccess support below.

Manually Initiate

As mentioned in the above section to create a presenter you simply initiate with the new keyword and inject your object or array.

class User {
	// ...
}

class UserPresenter extends Robbo\Presenter\Presenter {

	// ...
}

$user = new User;

// handle stuff here

// make sure to "convert" to a presenter before the object gets to your views
$user = new UserPresenter($user);


// Can also create a presenter for arrays
$user = [
	'id' => 1,
	'username' => 'Robbo',
];

// same as before
$user = new UserPresenter($user);

Laravel Usage

If you are using laravel and have followed the above installation instructions you can use the provided interface Robbo\Presenter\PresentableInterface to automate turning your model instances into a Presenter from both collections and when a model is sent directly to the view.

What the service provider does is extend Laravel's view component with a step before the view object is created. This step turns anything that implements the PresentableInterface into a Presenter by calling ->getPresenter(). What this means is you don't need to add anything extra to your controllers to have your views using presenters for your objects.

For Example.

class UserPresenter extends Robbo\Presenter\Presenter {

	// ...
}

class User implements Robbo\Presenter\PresentableInterface {

	/**
	 * Return a created presenter.
	 *
	 * @return Robbo\Presenter\Presenter
	 */
	public function getPresenter()
	{
		return new UserPresenter($this);
	}
}

Now whenever your User model is sent to a view, in a collection, array or by itself it will be turned into a presenter using the provided getPresenter() method. So your controller will work with User and when you get to your view it will be working with UserPresenter with the internal object being User.

Array Usage

1.1.x introduces support for arrays. The Presenter will implement ArrayAccess so in your views you can access your variables with $presenter['variable'] if you want. But more importantly you can give the Presenter an array instead of an object. So you can use presenters to work with array data as well as objects.

For example.

$user = [
	'id' => 1,
	'username' => 'Robbo',
];

class UserPresenter extends Robbo\Presenter\Presenter {

	public function presentUrl()
	{
		// This will work exactly the same as previous examples
		return $this->id.'-'.$this->username;

		// You can also do this...
		return $this['id'].'-'.$this['username'];
	}
}

// Now we create a presenter much the same as before
$user = new UserPresenter($user);


// In our views we can use the $user as if it were still an array
echo 'Hello, ', $user['username'];

// Or even treat it like the object that it is
echo 'Hello, ', $user->username;

// And like other examples, we can present the url in the same way
echo 'The URL: ', $user->url;
echo 'And again: ', $user['url'];

Extending the Decorator

As of 1.2.x I have added in a decorator object. This object takes care of turning an object that has PresentableInterface into a Presenter. By default, this is done with Laravel's View objects. The reasoning behind a new class instead of the previous implementation is so it can be better tested and also to allow you to extend it. Here is an example of extending the Decorator so that instead of using the PresentableInterface and getPresenter() method you can use a public variable on the object called $presenter.

Note: these instructions are for Laravel usage.

First extend the decorator...

use Robbo\Presenter\Decorator as BaseDecorator;

class Decorator extends BaseDecorator {

	/*
     * If this variable implements Robbo\Presenter\PresentableInterface then turn it into a presenter.
     *
     * @param  mixed $value
     * @return mixed $value
    */
    public function decorate($value)
    {
    	if (is_object($value) and isset($value->presenter))
    	{
    		$presenter = $value->presenter;
    		return new $presenter;
    	}

    	return parent::decorate($value);
    }
}

To use your new decorator either add the following to start/global.php or into your own service provider.

// In start/global.php

App::make('presenter.decorator', App::share(function($app)
{
	$decorator = new Decorator;

	Robbo\Presenter\Presenter::setExtendedDecorator($decorator);
	return $decorator;
}));

// In a service provider's 'register' method

$this->app['presenter.decorator'] = $this->app->share(function($app)
{
	$decorator = new Decorator;

	Robbo\Presenter\Presenter::setExtendedDecorator($decorator);
	return $decorator;
});

And that is all there is to it. You can easily automate the creation of presenters to suit your workflow using this method.

Change Log

3.0

  • minimum PHP version is now 8.1
  • fixed PHP 8.1 ArrayAccess deprecations

2.0.1

  • branched off 2.x to support < PHP 8.1
  • modernized the repository somewhat
  • updated documentation

2.0.0

  • minimum Laravel version is now 5.8
  • minimum PHP version is now 7.1
  • improved the view factory integration which should be future proof

1.4.0

1.3.3

  • updated tests to better cover Laravel versions after 5.4 and changed the license to MIT

1.3.2

  • updated to work with laravel 5.4.x, only actual changes that aren't style or tests
  • added php-cs-fixer to update the code style
  • added examples to have tests for providers in a full app situation

1.3.1

  • updated to work with laravel 5.x

1.3.0

  • updated to work with laravel 4.2.x, to use in 4.1.x stay on version 1.2.*
  • moved to PSR-4 and now PHP 5.4+
  • small refactor and check isset against 'present' methods, thanks BenConstable

1.2.0

1.1.0

  • the Presenter class now implements ArrayAccess
  • added ability to use an array as your internal data

1.0.2

  • fixed bug caused by laravel update
  • added smarter converting of presenters from PresentableInterface'd objects
  • added object getter getObject to retrieve the internal object

1.0.1

  • fixed bug caused by laravel update

1.0.0

  • Initial Release

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.11M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 353
  • 点击次数: 38
  • 依赖项目数: 10
  • 推荐数: 50

GitHub 信息

  • Stars: 345
  • Watchers: 15
  • Forks: 38
  • 开发语言: PHP

其他信息

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