承接 koriit/phpdeps 相关项目开发

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

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

koriit/phpdeps

Composer 安装命令:

composer require koriit/phpdeps

包简介

Library for finding circular dependencies between modules

README 文档

README

Table of Contents

Build Status

Coverage Status Scrutinizer Code Quality StyleCI SensioLabsInsight

Latest Stable Version License

About

PHPDeps is a simple tool to detect circular dependencies in your modules, whatever you consider as a module or package.

Module is an organized unit of code, which is supposed to be treated as single entity. By grouping classes and functions into modules we can reason about the design at a higher level of abstraction.

Install

PHPDeps is available via composer:

composer require --dev koriit/phpdeps

Please, note that PHPDeps is used during the development. It’s not a part of your application, hence --dev.

Why care?

This is a general programming problem. If this subject is new to you, or you just need a refresher here are some quick references:

  1. Why Cyclic Dependencies are Bad

  2. What is circular dependency and why is it bad?

  3. Circular dependency

  4. Acyclic dependencies principle

  5. The Principles of OOD (The Acyclic Dependencies Principle part; in the end, most of the writers refer to this piece of literature)

Approach

TL;DR

If you are using PSR-4 and importing all classes and function via use then you are fine.

PHPDeps detects module usage by reading use section of your PHP files. This means that PHPDeps requires namespaces to be present.

PHPDeps assumes that all classes and functions belonging to a single module have the same namespace prefix in their fully qualified name.

For example, if we tell PHPDeps that SomeModule has namespace of ACME\Lib\Modules\SomeModule and PHPDeps analyzes a following file which belongs to OtherModule:

<?php
namespace ACME\Lib\Modules\OtherModule;

use ACME\Lib\Modules\SomeModule\Exceptions\ObjectNotFound;
// ...

Then PHPDeps notices that OtherModule depends on SomeModule.

Once all modules are analyzed, PHPDeps creates a dependency graph and looks for any cycles in it.

Basic usage

If you are all configured and set then just execute:

vendor/bin/phpdeps

If your configuration file is not named phpdeps.xml then you can pass it as --config option:

vendor/bin/phpdeps --config=myPHPDepsConfig.xml

It’s suggested to add phpdeps call to scripts section in your composer.json:

  "scripts": {
    "dependencies": "phpdeps"
  }

You can also run it alongside your tests:

  "scripts": {
    "test": ["phpunit", "phpdeps"]
  }

Example

Let’s consider a simplified structure of PHPDeps itself:

/src/PHPDeps
|-- ExtCodes.php
|-- PHPDepsApplication.php
|-- /Commands/
|-- /Config/
|-- /Modules/
|-- /Helpers/

First, you need to prepare a configuration file that will allow PHPDeps to find your modules:

phpdeps.xml
<?xml version="1.0" encoding="UTF-8"?>
<PHPDeps xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/koriit/phpdeps/phpdeps.xsd">

    <Modules> <!--(1)-->
        <Module>
            <Name>PHPDepsApplication</Name> <!--(2)-->
            <Namespace>Koriit\PHPDeps\PHPDepsApplication</Namespace> <!--(3)-->
            <Path>./src/PHPDeps/PHPDepsApplication.php</Path> <!--(4)-->
        </Module>
    </Modules>

    <Detectors> <!--(5)-->
        <ModuleDetector>
            <Namespace>Koriit\PHPDeps</Namespace> <!--(6)-->
            <Path>./src/PHPDeps</Path> <!--(7)-->
        </ModuleDetector>
    </Detectors>

</PHPDeps>
  1. First option is to directly define your modules.

  2. Each module needs a name, but currently this is only used for displaying purposes.

  3. Each module also needs a namespace prefix, this is used to check whether any other module depends on it. If module is a file then this needs to be fully qualified name of that module.

  4. Module path allows PHPDeps to find and analyze your module, this can be either filepath or dirpath.

  5. Second option is to define a module detector, right now PHPDeps supports detection of only dir based modules.

  6. Namespace prefix, directory name of found modules are appended to this to create actual module namespaces.

  7. Directory where modules are to be searched for.

Once you have a configuration ready, you can execute:

vendor/bin/phpdeps

If everything is all right you get nice OK message:

[OK] There are no circular dependencies in your modules!

If something is amiss, you get:

[WARNING] There are circular dependencies in your modules!

In total there are 4 dependency cycles in your modules.

1. Commands -> Modules -> Commands
----------------------------------

2. Commands -> Config -> Modules -> Commands
--------------------------------------------

3. Commands -> Helpers -> Modules -> Commands
---------------------------------------------

4. Commands -> Helpers -> Config -> Modules -> Commands
-------------------------------------------------------

Please, note that this example was generated by adding just one dependency to Commands module in Modules module.

At the moment PHPDeps does not provide any additional help in resolving the circular dependencies problem.

Exit codes

Code Name Description

0

OK

Application finished successfully and no issues detected

1

UNEXPECTED_ERROR

Application was aborted because of an error

3

CIRCULAR_DEPENDENCIES_EXIST

Application finished successfully but dependency cycle has been detected

255

STATUS_OUT_OF_RANGE

Returned status code was out of range

Config

Configuration is a simple XML file. Provided XSD allows for code completion and easy validation. Currently, configuration uses simple format consisting of only XML tags and no attributes

  • <PHPDeps> - Configuration root element.

    • <Modules> - Grouping tag for all kinds of module definitions.

      • <Module> - Defines and describes a single module.

        • <Name> - Module name, used for display purposes.

        • <Namespace> - Namespace prefix, to check whether any other module depends on it. If module is a file then this needs to be fully qualified name of that module.

        • <Path> - Module path allows PHPDeps to find and analyze your module, this can be either filepath or dirpath.

    • <Detectors> - Grouping tag for all kinds of detector definitions.

      • <ModuleDetector> - Defines and describes a single basic module detector.

        • <Namespace> - Namespace prefix, directory name of found modules are appended to this to create actual module namespaces.

        • <Path> - Directory where modules are to be searched for.

Config example

phpdeps.xml
<?xml version="1.0" encoding="UTF-8"?>
<PHPDeps xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/koriit/phpdeps/phpdeps.xsd">

    <Modules>
        <Module>
            <Name>PHPDepsApplication</Name>
            <Namespace>Koriit\PHPDeps\PHPDepsApplication</Namespace>
            <Path>./src/PHPDeps/PHPDepsApplication.php</Path>
        </Module>
    </Modules>

    <Detectors>
        <ModuleDetector>
            <Namespace>Koriit\PHPDeps</Namespace>
            <Path>./src/PHPDeps</Path>
        </ModuleDetector>
    </Detectors>

</PHPDeps>

Commands and options

Please, refer to built-in help command.

koriit/phpdeps 适用场景与选型建议

koriit/phpdeps 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 21
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-04