kodzila / architecture-validator
用于验证PHP项目架构的库。
Requires
- php: ^7.4 | ^8.0
- symfony/finder: 4.* || 5.*
Requires (Dev)
- kodzila/sniffer: ^1.0
- phpunit/phpunit: 9.*
- vimeo/psalm: 4.*
This package is auto-updated.
Last update: 2024-09-24 02:47:31 UTC
README
动机
大多数项目都是单体应用,并维护一些规则以允许项目在需要时进行扩展。这些规则通常需要在代码审查期间强制执行,每次检查都令人筋疲力尽。
这个库的目的是通过提供一个工具来自动检查这些规则来明确指定这些规则。
灵感来自Java的ArchUnit和不再维护的PhpArch。
安装
通过composer
composer require kodzila/architecture-validator --dev
运行
库本身不提供任何运行器 - 它只是一组验证规则的函数。您需要编写自己的运行器。
运行器:PHPUnit
我个人推荐使用这个运行器,因为它配置起来相当容易,而且几乎每个项目都附带了这个库。
- (如果尚未安装) 安装PHPUnit
composer require phpunit/phpunit --dev
- 在您选择的目录中创建一个测试用例。我更喜欢在
tests/Architecture/ArchitectureTest.php
中配置它
use Kodzila\ArchValidator\Architecture; use Kodzila\ArchValidator\Rule\CoreModuleRule; use PHPUnit\Framework\TestCase; final class ArchitectureTest extends TestCase { public function test(): void { $architecture = Architecture::build() ->defineModule( 'Core', 'Isav\\Core\\', 'src/Core' ) ->defineModule( 'Armap', 'Isav\\Armap\\', 'src/Armap' ) ->defineModule( 'Import', 'Isav\\Import\\', 'src/Import' ) ; $architecture->checkRule(new CoreModuleRule('Core')); $this->assertTrue(true); } }
- 在
phpunit.xml
中创建一个新的测试套件
<testsuites> <testsuite name="Architecture"> <directory>tests/Architecture</directory> </testsuite> </testsuites>
- 将脚本添加到
composer.json
"scripts": { "test:arch": [ "bin/phpunit --testsuite Architecture" ] }
- 现在您可以使用以下命令运行验证器
composer test:arch
概念
与Java和C#等其他现代面向对象语言不同,PHP在单个项目中没有包装的概念。唯一的东西就是文件夹,但它们不能通过作用域修饰符进行限制。
如前所述,项目可以是单体应用程序,但没有结构的项目很快就会变成意大利面代码。
模块化方法
想法是将项目源代码捆绑在模块中。每个模块都有一个路径和相应的PHP 命名空间。
考虑以下项目
.
+-- config
| +-- services.yaml
| +-- routing.yaml
+-- src
| +-- Armap
| +-- SyncService.php
| +-- Core
| +-- CoreEntity.php
| +-- DoctrineMigrations
| +-- Migration214312412.php
+-- tests
| +-- E2E.php
您可以看到项目已经被划分为模块:Armap
和Core
。架构验证器可以反映这种情况
$architecture = Architecture::build() ->defineModule( 'Core', 'Isav\\Core\\', 'src/Core' ) ->defineModule( 'Armap', 'Isav\\Armap\\', 'src/Armap' ) ;
每个模块都可以包含一组将被分析的PHP 类
、接口
和特质
。
规则
架构验证器提供了一组验证器来检查常用方法。它们被称为规则
,可以在src/Rule/Extension
中找到
CoreModuleRule
规则将一个模块指定为Core
模块。其余注册的模块被视为子模块
。
检查
Core
模块不能依赖于任何子模块
。子模块
可以依赖于Core
模块,但不能依赖于任何其他子模块
。
这种方法允许创建易于维护的包。依赖关系只有一个方向。
DomainDrivenDesignRule
该规则强制执行这种模块架构典型的结构。典型的结构如下
.
+-- Core
| +-- Domain
| +-- Application
| +-- Infrastructure
| +-- Presentation
您可以在这里了解更多关于结构的信息。
此外,{域、应用程序、基础设施、表示}应称为层
。
检查
- 域层不能依赖于任何其他层。
- 应用程序层只能依赖于域层。
DomainForbiddenDependenciesRule
域是系统的核心,不应受到不稳定库的污染。它应该是一个深思熟虑的过程。该规则确保开发者在将依赖项添加到域层之前已经考虑了其影响。
Domain is the heart of the business application and should be well isolated from the other layers of the application.
Also, it should not be dependent on the application frameworks used in the other layers (JSP/JSF, Struts, EJB, Hibernate,
XMLBeans and so-on).
您需要添加白名单中的依赖项以忽略错误。
$architecture->checkRules([ new DomainForbiddenDependenciesRule(['Core'], [ 'Doctrine\ORM\Mapping', 'Doctrine\Common\Collections', 'Ramsey\Uuid\UuidInterface', ]) ]);
开发
-
克隆仓库
-
安装依赖项
composer install
发布新版本
- 运行
composer mr
(静态代码分析)以确保库代码符合标准。 - 添加更改
- 提交更改
- 标记更改
- 推送到master分支