定制 naokioouchi/laravel-circular-detector 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

naokioouchi/laravel-circular-detector

Composer 安装命令:

composer require naokioouchi/laravel-circular-detector

包简介

A Laravel package to detect and visualize circular dependencies in modular monolith architectures

README 文档

README

A powerful Laravel package for detecting and visualizing circular dependencies in modular monolith architectures.

Features

  • 🔍 Circular Dependency Detection: Automatically detect circular dependencies between modules using depth-first search algorithm
  • 📊 Multiple Output Formats: Console, JSON, DOT (Graphviz), and Mermaid diagram formats
  • 📈 Dependency Visualization: Generate interactive dependency graphs
  • Performance Optimized: Efficiently analyze large codebases with 100+ modules
  • 🎯 Laravel Integration: Seamless integration with Laravel's service container and console commands

Installation

Install the package via Composer:

composer require naokioouchi/laravel-circular-detector --dev

Publishing Configuration

After installation, publish the configuration file to customize it for your project:

php artisan vendor:publish --provider="LaravelCircularDependencyDetector\ServiceProvider" --tag="config"

This will create config/circular-dependency-detector.php in your Laravel application where you can customize all settings.

Alternative: Without Publishing Config

If you don't want to publish the config, you can also configure it via environment variables or directly in your config/app.php:

// In your AppServiceProvider or a custom ServiceProvider
public function register()
{
    $this->app->configure('circular-dependency-detector', function() {
        return [
            'modules_path' => base_path('src'), // Your custom path
            'detection_strategy' => 'namespace',
            'namespace_pattern' => 'App\\Domain\\*',
            // ... other settings
        ];
    });
}

Configuration

Edit config/circular-dependency-detector.php to customize:

return [
    'modules_path' => app_path('Modules'),
    
    // Namespace patterns configuration (NEW)
    // {MODULE} placeholder will be replaced with module name
    'namespace_patterns' => [
        'App\\Modules\\{MODULE}',     // app/Modules/ModuleName
        // 'Packages\\{MODULE}',       // packages/ModuleName
        // 'Domain\\{MODULE}',         // Custom domain structure
    ],
    
    'scan_patterns' => [
        'controllers' => 'Controllers',
        'services' => 'Services',
        'repositories' => 'Repositories',
        'providers' => 'Providers',
        'models' => 'Models',
        'jobs' => 'Jobs',
        'listeners' => 'Listeners',
        // For DDD patterns
        'domain' => 'Domain',
        'application' => 'Application',
        'infrastructure' => 'Infrastructure',
    ],
    
    'ignore_patterns' => [
        '*/Tests/*',
        '*/Migrations/*',
        '*/Database/*',
    ],
    
    'allowed_dependencies' => [
        'Contracts',
        'Events', 
        'Exceptions',
        'DTOs',
        'Enums',
    ],
];

Example for packages/ directory structure (DDD/Ports and Adapters)

return [
    'modules_path' => base_path('packages'),
    
    'namespace_patterns' => [
        'Packages\\{MODULE}',
    ],
    
    'scan_patterns' => [
        'application' => 'Application',
        'domain' => 'Domain',
        'infrastructure' => 'Infrastructure',
    ],
];

Usage

Detect Circular Dependencies

php artisan modules:detect-circular

Options:

  • --path=: Override modules path (e.g., app/Domain, src/Services)
  • --format={console|json|dot|mermaid}: Output format (default: console)
  • --output=path: Save output to file

Examples:

# Analyze a custom directory structure
php artisan modules:detect-circular --path=app/Domain

# Generate JSON report for DDD structure
php artisan modules:detect-circular --path=src --format=json --output=report.json

# Generate Graphviz DOT file
php artisan modules:detect-circular --format=dot --output=dependencies.dot

# Analyze different project structures without modifying config
php artisan modules:detect-circular --path=packages
php artisan modules:detect-circular --path=src/Bounded

Generate Dependency Graph

php artisan modules:graph

Options:

  • --format={dot|mermaid}: Graph format (default: dot)
  • --output=path: Save graph to file

Examples:

# Generate DOT graph and convert to PNG
php artisan modules:graph --format=dot --output=graph.dot
dot -Tpng graph.dot -o graph.png

# Generate Mermaid diagram
php artisan modules:graph --format=mermaid --output=graph.md

Output Examples

Console Output

=====================================
  Circular Dependency Analysis Report
=====================================

Summary:
--------
  Modules analyzed: 5
  Circular dependencies found: 1
  Status: ❌ Issues Found

Circular Dependencies:
---------------------

1. 🔴 CRITICAL (Length: 2)
   Cycle: ModuleA → ModuleB → ModuleA
   Affected dependencies:
   - ModuleA uses App\Modules\ModuleB\Services\ServiceB
   - ModuleB uses App\Modules\ModuleA\Services\ServiceA

JSON Output

{
  "analysis_timestamp": "2025-01-01T00:00:00Z",
  "summary": {
    "modules_count": 5,
    "cycles_count": 1,
    "has_issues": true
  },
  "cycles": [
    {
      "cycle": ["ModuleA", "ModuleB", "ModuleA"],
      "length": 2,
      "severity": "critical"
    }
  ],
  "dependencies": {}
}

DOT Graph Output

digraph ModuleDependencies {
    rankdir=LR;
    node [shape=box, style=rounded];
    edge [color=gray50];

    "ModuleA" [fillcolor=lightcoral, style="rounded,filled"];
    "ModuleB" [fillcolor=lightcoral, style="rounded,filled"];
    
    "ModuleA" -> "ModuleB" [color=red, penwidth=2, label="cycle"];
    "ModuleB" -> "ModuleA" [color=red, penwidth=2, label="cycle"];
}

Mermaid Diagram Output

graph TD
    M0["ModuleA"]
    M1["ModuleB"]
    
    M0 -->|cycle| M1
    M1 -->|cycle| M0
    
    classDef cycleNode fill:#ffcccc,stroke:#ff0000,stroke-width:2px
    class M0 cycleNode
    class M1 cycleNode
Loading

CI/CD Integration

GitHub Actions

name: Dependency Check
on: [push, pull_request]
jobs:
  dependency-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1'
      - name: Install dependencies
        run: composer install
      - name: Check circular dependencies
        run: php artisan modules:detect-circular --format=json --output=dependencies.json

GitLab CI

dependency-check:
  image: php:8.1
  script:
    - composer install
    - php artisan modules:detect-circular --format=json --output=dependencies.json
  artifacts:
    reports:
      junit: dependencies.json
    paths:
      - dependencies.json

Performance

The package is optimized for performance:

  • 100 modules: Analysis completes in under 30 seconds
  • Memory usage: Less than 512MB for large projects
  • File limit: Supports up to 10,000 PHP files

Requirements

  • PHP ^8.1
  • Laravel 10.x, 11.x, or 12.x
  • nikic/php-parser ^4.15

Testing

Run the test suite:

composer test

Run specific test suites:

# Unit tests only
./vendor/bin/phpunit --testsuite=Unit

# Feature tests only
./vendor/bin/phpunit --testsuite=Feature

Troubleshooting

No modules found

Check that your modules_path in the configuration points to the correct directory:

'modules_path' => app_path('Modules'), // Adjust this path

Missing dependencies

Some dependencies might not be detected if they use dynamic class resolution. Consider adding explicit use statements.

Performance issues

For large codebases, you can optimize by:

  • Adjusting ignore_patterns to skip unnecessary directories
  • Limiting scan_patterns to essential directories only

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter any issues or have feature requests, please create an issue on GitHub.

License

MIT

naokioouchi/laravel-circular-detector 适用场景与选型建议

naokioouchi/laravel-circular-detector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 naokioouchi/laravel-circular-detector 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-21