psr-autoloading-knowledge

PSR-4 自动加载知识库(适用于 PHP 8.4 项目):说明命名空间到路径的映射规则、composer.json 的相关配置、建议目录结构以及常见错误的快速参考,供自动加载审计和项目结构审查使用。

仓库最近推送
GitHub Stars
109
Forks
26
Issues / PRs
1

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "psr-autoloading-knowledge":https://skillpi.com/zh/skills/deepskill-fe74faad8a09c825bfddfb990886032e1ecd1e9353ac090c9e0fb39e6b6e531a。安装前核对上游来源和版本:https://github.com/dykyi-roman/awesome-claude-code。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

PSR-4 自动加载知识库(适用于 PHP 8.4 项目):说明命名空间到路径的映射规则、composer.json 的相关配置、建议目录结构以及常见错误的快速参考,供自动加载审计和项目结构审查使用。

0 下载

介绍来源: third_party · dykyi-roman · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: psr-autoloading-knowledge
description: PSR-4 autoloading standard knowledge base for PHP 8.4 projects. Provides quick reference for namespace-to-path mapping, composer.json configuration, directory structure, and common mistakes. Use for autoloading audits and project structure reviews.

PSR-4 Autoloading Knowledge

Quick Reference

Concept Description
Namespace Prefix Maps to a base directory
FQCN Fully Qualified Class Name
Path Mapping Namespace → Directory → File

Core Concept

PSR-4 defines how to autoload classes from file paths based on their fully qualified class name.

Namespace Prefix    → Base Directory
Acme\Log\           → ./src/

FQCN                → File Path
Acme\Log\Writer\FileWriter → ./src/Writer/FileWriter.php

Namespace to Path Mapping

The Formula

File Path = Base Directory + (FQCN - Namespace Prefix) + .php

Where:
- Namespace separators (\) → Directory separators (/)
- Class name → File name with .php extension

Examples

FQCN Namespace Prefix Base Directory File Path
App\Entity\User App\ src/ src/Entity/User.php
App\Domain\User\Entity\User App\ src/ src/Domain/User/Entity/User.php
Vendor\Package\SubNs\ClassName Vendor\Package\ lib/ lib/SubNs/ClassName.php

Composer Configuration

Basic Setup

{
    "autoload": {
        "psr-4": {
            "App\\": "src/",
            "App\\Tests\\": "tests/"
        }
    }
}

Multiple Directories

{
    "autoload": {
        "psr-4": {
            "App\\": ["src/", "lib/"]
        }
    }
}

DDD Project Structure

{
    "autoload": {
        "psr-4": {
            "App\\": "src/",
            "App\\Domain\\": "src/Domain/",
            "App\\Application\\": "src/Application/",
            "App\\Infrastructure\\": "src/Infrastructure/",
            "App\\Presentation\\": "src/Presentation/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    }
}

Symfony Bundle Structure

{
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

Laravel Package Structure

{
    "autoload": {
        "psr-4": {
            "Vendor\\Package\\": "src/"
        }
    }
}

Directory Structure Examples

Standard DDD Layout

project/
├── composer.json
├── src/
│   ├── Domain/
│   │   └── User/
│   │       ├── Entity/
│   │       │   └── User.php           # App\Domain\User\Entity\User
│   │       ├── ValueObject/
│   │       │   └── Email.php          # App\Domain\User\ValueObject\Email
│   │       ├── Repository/
│   │       │   └── UserRepositoryInterface.php
│   │       └── Event/
│   │           └── UserCreated.php
│   ├── Application/
│   │   └── User/
│   │       ├── Command/
│   │       │   └── CreateUserCommand.php
│   │       └── Handler/
│   │           └── CreateUserHandler.php
│   ├── Infrastructure/
│   │   └── Persistence/
│   │       └── Doctrine/
│   │           └── UserRepository.php
│   └── Presentation/
│       └── Api/
│           └── Controller/
│               └── UserController.php
└── tests/
    └── Unit/
        └── Domain/
            └── User/
                └── Entity/
                    └── UserTest.php   # App\Tests\Unit\Domain\User\Entity\UserTest

Class Examples

// src/Domain/User/Entity/User.php
<?php

declare(strict_types=1);

namespace App\Domain\User\Entity;

use App\Domain\User\ValueObject\Email;
use App\Domain\User\ValueObject\UserId;

final class User
{
    // ...
}
// src/Domain/User/ValueObject/Email.php
<?php

declare(strict_types=1);

namespace App\Domain\User\ValueObject;

final readonly class Email
{
    // ...
}

Detection Patterns

Find Mismatched Namespaces

# Extract namespace from files and compare with path
for file in $(find src -name "*.php"); do
    namespace=$(grep -m1 "^namespace" "$file" | sed 's/namespace //;s/;//')
    expected_ns="App\\$(dirname ${file#src/} | tr '/' '\\')"
    if [ "$namespace" != "$expected_ns" ]; then
        echo "Mismatch: $file"
        echo "  Found:    $namespace"
        echo "  Expected: $expected_ns"
    fi
done

Find Classes Without Namespace

# Files with class but no namespace
grep -rL "^namespace" --include="*.php" src/ | \
    xargs grep -l "^class\|^interface\|^trait\|^enum"

Find Incorrect Class Names

# Class name doesn't match filename
for file in $(find src -name "*.php"); do
    filename=$(basename "$file" .php)
    if ! grep -q "^\(class\|interface\|trait\|enum\) $filename" "$file"; then
        echo "Mismatch: $file (expected class/interface/trait/enum $filename)"
    fi
done

Validate Composer Autoload

# Dump autoload and check for errors
composer dump-autoload --strict 2>&1 | grep -i "warning\|error"

# Validate composer.json
composer validate --strict

Common Mistakes

1. Namespace vs Path Mismatch

// File: src/Domain/User/Entity/User.php
// BAD: Namespace doesn't match path
namespace App\User\Entity;  // Missing "Domain"

// GOOD: Namespace matches path
namespace App\Domain\User\Entity;

2. Class Name vs Filename Mismatch

// File: src/Domain/UserEntity.php
// BAD: Class name doesn't match filename
class User { }

// GOOD: Class name matches filename
class UserEntity { }

3. Case Sensitivity Issues

// File: src/Domain/User/Entity/user.php (lowercase)
// BAD: Will fail on case-sensitive filesystems (Linux)
namespace App\Domain\User\Entity;
class User { }

// GOOD: Filename should be User.php (PascalCase)

4. Missing Trailing Backslash

// BAD: Missing trailing backslash
{
    "autoload": {
        "psr-4": {
            "App": "src/"
        }
    }
}

// GOOD: Include trailing backslash
{
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

5. Incorrect Directory Separator

// BAD: Using backslash in path (Windows-only)
{
    "autoload": {
        "psr-4": {
            "App\\": "src\\"
        }
    }
}

// GOOD: Use forward slash (cross-platform)
{
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

Verification Commands

# Regenerate autoload files
composer dump-autoload

# Optimize autoload (production)
composer dump-autoload --optimize

# Validate and show issues
composer dump-autoload --strict

# Test class loading
php -r "require 'vendor/autoload.php'; new App\Domain\User\Entity\User();"

PHP-CS-Fixer Rules

<?php

return (new PhpCsFixer\Config())
    ->setRules([
        // PSR-4 related rules
        'single_class_element_per_statement' => true,
        'single_import_per_statement' => true,
        'ordered_imports' => [
            'sort_algorithm' => 'alpha',
        ],
        'no_unused_imports' => true,
        'fully_qualified_strict_types' => true,
    ]);

Integration with DDD Layers

Layer Mapping

Layer Namespace Directory
Domain App\Domain\{Context}\ src/Domain/{Context}/
Application App\Application\{Context}\ src/Application/{Context}/
Infrastructure App\Infrastructure\ src/Infrastructure/
Presentation App\Presentation\ src/Presentation/

Component Mapping

Component Namespace Pattern Directory Pattern
Entity App\Domain\{Context}\Entity\ src/Domain/{Context}/Entity/
Value Object App\Domain\{Context}\ValueObject\ src/Domain/{Context}/ValueObject/
Repository App\Domain\{Context}\Repository\ src/Domain/{Context}/Repository/
Service App\Domain\{Context}\Service\ src/Domain/{Context}/Service/
Command App\Application\{Context}\Command\ src/Application/{Context}/Command/
Query App\Application\{Context}\Query\ src/Application/{Context}/Query/
Handler App\Application\{Context}\Handler\ src/Application/{Context}/Handler/

See Also

  • references/psr-4-standard.md - Full PSR-4 specification
  • references/composer-integration.md - Composer autoload configuration
  • references/directory-structure.md - Project structure examples
  • references/antipatterns.md - Common mistakes and fixes
  • assets/report-template.md - Audit report template

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值85/100

亮点

  • 提供PSR-4自动加载标准的完整知识库,覆盖命名空间映射、composer配置、目录结构等核心内容
  • 包含实用的检测脚本和常见错误排查指南,可直接用于项目审计
  • 适用于PHP 8.4项目,具有明确的版本针对性
触发精准90/100

亮点

  • 触发词精准完整,覆盖PSR-4、自动加载、composer配置等多种变体
  • 边界清晰,不会与其他PHP相关Skill混淆
结构规范85/100

亮点

  • 结构清晰,包含快速参考、核心概念、配置示例、检测模式等完整章节
  • 提供端到端示例和实际项目结构,便于理解和应用
  • frontmatter完整,文档组织良好
执行可靠80/100

亮点

  • 提供的bash脚本和composer命令参数正确,可直接执行
  • 覆盖主要异常情况,如命名空间不匹配、类名不匹配等
  • 包含验证命令和PHP-CS-Fixer规则,确保代码质量
证据可验 (前四项均值)85/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。