tectonic/应用支持

此包已被废弃且不再维护。没有建议替代包。

一组用于支持Laravel 4应用开发的库和类。

dev-master 2015-03-10 20:11 UTC

This package is auto-updated.

Last update: 2020-09-19 02:28:31 UTC


README

此包将不再维护。我们在办公室使用Laravel,它现在有一个相当不错的命令总线管道实现。任何想接管此项目的人,请告诉我们。

应用支持

此仓库是一组旨在通过设置某些开发方法的默认标准或方法来辅助Laravel 4开发的类和实用工具。它是各种社区来源和启发式项目的融合,包括Jeffrey Way的领域驱动设计系列,以及Laravel.io项目。它还包括一些额外功能和功能,并将随着社区需求的扩展而继续增长。

领域驱动设计(DDD)

最近关于DDD及其对我们开发者意味着什么的炒作和讨论很多。我们中的每个人都不必重建各种组件(有一些)来支持这种应用开发的思想方法,为何不有一个库,在需要这些功能时可以调用呢?

DDD的一个核心原则是能够发送命令,这些命令将由命令处理器处理,执行所需代码,然后返回结果。结合事件溯源,你将拥有一个支持SOLID设计原则的强大开发工具套件。

让我们从设置用户注册的常规代码开始。我们将使用应用支持库,设置控制器,注入所需依赖项,编写命令,连接到一些事件,然后向我们的客户端返回响应。

用例

在这个用例中,我们将查看用户注册操作。用户将注册或尝试注册,通过提交包含他们请求的电子邮件地址、他们想要使用的密码以及密码确认字段的形式。让我们先看看我们的注册控制器中执行此操作的函数。

<?php

class RegistrationController extends \Tectonic\Application\Support\BaseController
{
    protected $registrationService;

    public function __construct(RegistrationService $registrationService)
    {
        $this->registrationService = $registrationService;
    }

    public function postIndex()
    {
        $this->registrationService->register(Input::get());
    }
}

相当简单。我们定义了一个注册控制器所依赖的依赖项,这是一个处理注册逻辑本身的服务。这样,控制器只是作为一个请求进入的通道,将请求引导到适当的服务,并以某种方式处理响应。让我们看看注册服务。

<?php

use Tectonic\Application\Validation\ValidationCommandBus;

class RegistrationService
{
	use EventGenerator;

	protected $commandBus;

	public function __construct(ValidationCommandBus $commandBus)
	{
		$this->commandBus = $commandBus;
	}

	/**
	 * Registers a new user account based on the array of information provided.
	 *
	 * @param array $input
	 * @return mixed
	 */
	public function register(array $input)
	{
		$command = new UserRegistersCommand($input['email'], $input['password'], $input['password_confirmation']);

		$user = $this->commandBus->execute($command);

		$this->raise(new UserHasRegistered($user));

		return $user;
	}
}

在此服务中,我们很好地使用了某些基础应用支持功能。其中之一是EventGenerator,我们在用户成功注册后使用它来触发事件。我们还使用了ValidationCommandBus。这个类充当DefaultCommandBus的装饰器,实际上是在执行方法中包装了一些验证调用。

你还会注意到我们使用了另外两个对象:UserRegistersCommand和UserHasRegistered。现在让我们来了解一下这些。

<?php
class UserRegistersCommand extends \Tectonic\Application\Commanding\Command
{
	public $email;
	public $password;
	public $password_confirmation;

	public function __construct($email, $password, $password_confirmation)
	{
		$this->email = $email;
		$this->password = $password;
		$this->password_confirmation = $password_confirmation;
	}
}

所发生的情况是我们创建了一个扩展Command类的新的类,本质上创建了一个充当数据传输对象(DTO)的对象。DefaultCommandBus随后查看命令的名称来发现命令处理器。每个命令都需要一个处理器。在应用支持的情况下,该处理器需要位于与命令本身相同的目录中。因此在这种情况下,与UserRegistersCommand一起的应该是UserRegistersCommandHandler。让我们看看它是什么样子。

<?php
use Tectonic\Application\Eventing\EventDispatcher;

class UserRegistersCommandHandler implements use Tectonic\Application\Commanding\CommandHandler
{
	protected $dispatcher;
	protected $userRepository;

	public function __construct(UserRepository $userRepository, EventDispatcher $dispatcher)
	{
		$this->userRepository = $userRepository;
		$this->dispatcher = $dispatcher;
	}

	/**
	 * Handles the user registration command.
	 *
	 * @param $command
	 */
	public function handle($command)
	{
		$user = $this->userRepository->register(
			$command->email,
			$command->password
		);

		$this->dispatcher->dispatch($user->releaseEvents());

		return $user;
	}
}

这里不会涵盖存储库,因为那是非常基础的内容,但你应该知道应用支持代码库包括一个基础级别的存储库,您可以在自己的项目中使用。请查看Tectonic\Application\Support\BaseRepository。

<?php
class UserHasRegistered extends use \Tectonic\Application\Eventing\Event
{
    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }
}