美文网首页clean architecture程序员
The Clean Architecture in PHP 读书

The Clean Architecture in PHP 读书

作者: 小聪明李良才 | 来源:发表于2016-11-22 19:41 被阅读190次
    Clean Architecture

    这是clean architecture的第9篇,也是具体案例的第一篇,本篇开始将会运用之前学到的知识,来构建一个小型的php应用。

    本文为系列文章的第九篇,完成的目录请查看Clean Architecture

    计费系统

    应用的uml简图如下:

    uml

    应用的核心逻辑是:用户会有多个订单,然后固定周期对账单进行结算。逻辑非常简单,可以让我们更专注于系统的架构上,那就让我们开始系统的构建吧。

    应用功能的构建流程

    • 能够新增用户
    • 能够给用户新增订单
    • 当需要给用户出账的时候能够将订单转换为发票(invoices)

    构建我们的领域模型

    领域模型层只包含简单的php class,此处只有3个Customer,Order,Invoice。我们先来构建我们的项目。

    mkdir -p cleanphp-laravel/core/Domain/Entity

    在cleanphp-laravel下新建composer.json文件,内容是:

    {
      "autoload": {
            "psr-4": {
                "CleanPhp\\Invoicer\\":"core/"
            }
        }
    }
    

    运行composer dump-autoload产生vendor目录。

    创建Entity

    每个entity都会有唯一的标识,于是我们新建一个AbstractEntity,将$id放入里面。

    <?php namespace CleanPhp\Invoicer\Domain\Entity;
    
    abstract class AbstractEntity {
        protected $id;
    
        /**
         * @return mixed
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * @param mixed $id
         *
         * @return $this
         */
        public function setId( $id )
        {
            $this->id = $id;
            return $this;
        }
    }
    

    同时定义Customer,Order,Invoice都继承自该类,具体的可查看https://github.com/zhuanxuhit/php-clean-code

    领域服务

    领域服务包含了3个主要部分

    • Repository Interface
    • Factories
    • Services

    下面分别构建这3部分,先是Repository,对于每个Entity,都会有对应的Entity,而且会有一些公共的操作,于是提取出来,叫做RepositoryInterface,代码如下:

    <?php namespace CleanPhp\Invoicer\Domain\Repository;
    
    interface RepositoryInterface {
        public function getById( $id );
        public function getAll();
        public function persist( $entity );
        public function begin();
        public function commit();
    }
    

    其余的CustomerRepositoryInterface,OrderRepositoryInterface,InvoiceRepositoryInterface都扩展RepositoryInterface即可,其中OrderRepositoryInterface需要有个新的接口:getUninvoicedOrders来获取未出账单的订单。

    再来看Factory的构建,此处invoice的构建依赖于order,于是我们有下面的定义:

    public function createFromOrder(Order $order);
    

    至于怎么去实现这个方法,我们采用BDD(Behavior-Driven Development)的方式。

    首先通过composer来包含库

    $ composer require --dev peridot-php/peridot peridot-php/leo peridot-php/peridot-prophecy-plugin
    

    安装完成后,通过执行

    ./vendor/bin/peridot
    

    我们应该就能看到peridot本身的所有case执行成功。下一步让我们来构建符合我们要求的case。

    在根目录下新建specs/domain/service/invoice-factory.spec.php文件,

    describe( "InvoiceFactory", function () {
        describe( "->createFromOrder()", function () {
            it( "should return an order object", function () {
                $order   = new Order();
                $factory = new InvoiceFactory();
                $invoice = $factory->createFromOrder( $order );
                expect( $invoice )->to->be->instanceof( Invoice::class );
            } );
        } );
    } );
    

    基于这种方式我们就能通过测试不断去驱动完成我们的createFromOrder方法了,完整的测试case可以在github上查看。

    最后我们来构建Invoicing services,同样通过BDD的方式,先写出case,新建specs/domain/service/invoice-factory.spec.php文件,里面的内容是:

    describe( 'InvoicingService', function () {
        describe( '->generateInvoices()', function () {
            it( 'should query the repository for uninvoiced Orders');
            it('should return an Invoice for each uninvoiced Order');
        } );
    } );
    

    然后根据case再去实现generateInvoices()方法。

    完整的例子,我们可以通过下面的命令查看

    git clone https://github.com/zhuanxuhit/php-clean-code.git
    git checkout 02-domain-services
    

    总结

    以上我们就完成了领域模型层和领域服务层的基本设计,到目前为止,我们的目录如下:

    在下一讲中我们会引入Laravel,看怎么和具体的框架结合,尽请期待。

    这是The Clean Architecture in PHP的第九篇,你的鼓励是我继续写下去的动力,期待我们共同进步。

    相关文章

      网友评论

        本文标题:The Clean Architecture in PHP 读书

        本文链接:https://www.haomeiwen.com/subject/pahmpttx.html