team-reflex/presenter

此包已被废弃,不再维护。作者建议使用robclancy/presenter包。

这是一个简单的库,帮助您为对象创建展示者。

v1.4.1 2017-01-27 00:49 UTC

This package is auto-updated.

Last update: 2020-01-29 07:00:46 UTC


README

此库提供了一个简单的类,帮助您为对象或数组创建展示者。它还在控制器中添加了少量额外的代码(在大多数情况下无需额外代码)以用于Laravel。

已弃用:这只是为了维护公司项目的兼容性而进行的分叉。只需使用robclancy/presenter。

Build Status

目录

安装

Composer

robclancy/presenter添加到您的composer.json文件的"require"部分。

	"robclancy/presenter": "1.3.*"

运行composer update以获取该包的最新版本。

手动安装

虽然推荐使用Composer,但您也可以从本仓库下载并安装。

Laravel 4

此包附带了一个可选的服务提供程序,以便您可以使用Laravel 4自动化一些额外步骤。您需要先使用上述Composer方法安装,然后注册服务提供程序到您的应用程序。

打开app/config/app.php,找到providers键。在以下数组中添加以下内容:

'Robbo\Presenter\PresenterServiceProvider',

到某个位置后:

'Illuminate\View\ViewServiceProvider',

现在,如果您使用以下描述的Laravel方法,展示者将自动创建。

使用方法

展示者是一个非常简单的类,它覆盖了方法和变量,以便您可以在不向模型或控制器等区域添加视图逻辑的情况下向对象或数组添加额外逻辑,并且还把任何额外逻辑保持在视图之外。

通用使用

假设您有一份用户列表,并想为每个用户生成一个链接到其个人资料。许多人会直接在视图中构建URL,或者在更糟糕的情况下,在控制器中构建。为了分离这种逻辑,我们使用展示者。假设我们有一个拥有idusername属性的User类。展示者可能看起来像这样。


class UserPresenter extends Robbo\Presenter\Presenter {
	
	public function url()
	{
		return $this->id.'-'.$this->username;
	}
}

现在我们的视图应该接收一个此演示者实例,它将使用类似 $user = new UserPresenter(new User); 的方式创建。如果我们想链接到用户页面,我们只需要调用 $user->url()。现在你有了逻辑和易于修改以向所有区域添加属性的小类。然而,你可能不想调用这样的方法,这可能与你的操作不一致,或者你可能想让代码看起来更整洁。这就是带有 present 前缀的方法的作用。我们只需更新演示者如下。


class UserPresenter extends Robbo\Presenter\Presenter {
	
	public function presentUrl()
	{
		return $this->id.'-'.$this->username;
	}
}

现在,当您执行 $user->url 时,演示者将调用这个新方法。此外,您还可以通过 ArrayAccess 访问此方法,通过调用 $user['url']。有关 ArrayAccess 支持的更多信息见下文。

手动启动

如上节所述,要创建演示者,您只需使用 new 关键字初始化,并注入您的对象或数组。


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 使用

如果您正在使用 Laravel 并遵循上述安装说明,则可以使用提供的接口 Robbo\Presenter\PresentableInterface 自动将模型实例转换为从集合和当模型直接发送到视图时的 Presenter

服务提供者所做的是在创建视图对象之前扩展 Laravel 的视图组件。此步骤通过调用 ->getPresenter() 将实现 PresentableInterface 的任何内容转换为 Presenter。这意味着您不需要在控制器中添加任何额外内容,就可以让您的视图使用演示者来处理对象。

例如。


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);
	}
}

现在,每当您的 User 模型被发送到视图、在集合、数组或单独发送时,它都会使用提供的 getPresenter() 方法转换为演示者。所以您的控制器将处理 User,当您到达视图时,它将使用带有内部对象 UserUserPresenter

数组使用

1.1.x 引入了对数组的支持。演示者将实现 ArrayAccess,因此在您的视图中,如果您想的话,可以使用 $presenter['variable'] 访问变量。但更重要的是,您可以用数组而不是对象提供演示者。因此,您可以使用演示者来处理数组数据以及对象。

例如。


$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'];

扩展装饰器

从 1.2.x 开始,我添加了一个装饰器对象。此对象负责将具有 PresentableInterface 的对象转换为 Presenter。默认情况下,这是通过 Laravel 的 View 对象完成的。使用新类而不是以前实现的理由是它可以更好地测试,并且还可以让您扩展它。以下是如何扩展 Decorator 的示例,这样您就可以使用对象上的公共变量 $presenter 而不是使用 PresentableInterfacegetPresenter() 方法。

注意:这些说明适用于 Laravel 使用。

首先扩展装饰器...


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);
    }
}

要使用您的新装饰器,请将以下内容添加到 start/global.php 或添加到您自己的服务提供者中。


// 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;
});

这就是全部内容。您可以使用这种方法轻松自动化创建演示者,以适应您的工作流程。

变更日志

1.3.0

  • 已更新以兼容 laravel 4.2.x,若要在 4.1.x 使用,请保留版本 1.2.*
  • 迁移到 PSR-4,现在支持 PHP 5.4+
  • 进行了小的重构和检查,再次确认 'present' 方法中的 isset,感谢 BenConstable

1.2.0

  • 现在可以嵌套使用 presenter,感谢 alexwhitman
  • 增加了使用 Laravel 的 View::with(array here) 的支持,感谢 skovachev
  • 增加了在 presenter 变量中使用 isset(...)unset(...) 的功能,感谢 nsbucky
  • 增加了一个新的装饰器来创建 presenter 对象。这使得您可以轻松地 扩展对象装饰时发生的操作

1.1.0

  • Presenter 类现在实现了 ArrayAccess 接口
  • 增加了使用数组作为内部数据的能力

1.0.2

  • 修复了由 Laravel 更新引起的错误
  • 增加了从 PresentableInterface'd 对象智能转换 presenter 的功能
  • 增加了一个对象获取器 getObject 来检索内部对象

1.0.1

  • 修复了由 Laravel 更新引起的错误

1.0.0

  • 初始发布

许可证

Presenter 在 DBAD 许可证下发布。做你想做的事,只要成个讨厌的家伙。