【问题标题】:How to get composer autoload classmap via Script\Event?如何通过 Script\Event 获取作曲家自动加载类映射?
【发布时间】:2019-09-12 23:48:49
【问题描述】:

我目前通过明确要求文件来获取自动加载类映射

$map = require 'vendor/composer/autoload_classmap.php';

但是我的班级已经在导入 Composer\Script\Event 班级并使用 Composer 实例

<?php

namespace Acme;

use Composer\Script\Event;

class Foo
{
   public static function bar(Event $event)
   {
      $extra = $event->getComposer()->getPackage()->getExtra();
      $autoload = $event->getComposer()->getPackage()->getAutoload();
   }
}

如何像获取 $extra 和 $autoload 一样获取自动加载类映射? (不使用require)

【问题讨论】:

    标签: php composer-php


    【解决方案1】:

    $event-&gt;getComposer()-&gt;getPackage()-&gt;getAutoload() 是我所知道的从 Composer 对象获取自动加载信息的唯一方法。我使用以下函数来处理getAutoload() 的结果,希望这些信息足以回答您的问题。

    function parseAutoload(Event $event)
    {
        foreach ($event->getComposer()->getPackage()->getAutoload() as $std => $lookup) {
            switch ($std) {
                case "psr-0":
                case "psr-4":
                    foreach ($lookup as $namespace => $paths) {
                        if (substr($namespace, -1) != "\\") {
                            $namespace .= "\\";
                            // TODO Maybe produce a warning?
                        }
                        if (!is_array($paths)) {
                            $paths = array($paths);
                        }
                        foreach ($paths as $path) {
                            if (substr($path, 0, -1) != DIRECTORY_SEPARATOR) {
                                $path .= DIRECTORY_SEPARATOR;
                            }
                            if (is_dir($path)) {
                                $directory = new RecursiveDirectoryIterator($path);
                                $iterator = new RecursiveIteratorIterator($directory);
                                $regex = new RegexIterator($iterator,
                                    '/^' . preg_quote($path, DIRECTORY_SEPARATOR) . '(.+)\.php$/i',
                                    RegexIterator::REPLACE);
                                $regex->replacement = '$1';
                                foreach ($regex as $file => $class) {
                                    $class = "{$namespace}" . str_replace(DIRECTORY_SEPARATOR, "\\", $class);
                                    echo "Found class '{$class}' at '{$file}'";
                                }
                            }
                        }
                    }
                    break;
                case "classmap":
                    foreach ($lookup as $class => $path) {
                        echo "Found class '{$class}' at '{$path}'";
                    }
                    break;
                case "files":
                    foreach ($lookup as $i => $path) {
                        if (is_readable($path)) {
                            $code = file_get_contents($path);
                            $tokens = token_get_all($code);
                            // File all the classes in the file.
                            for ($i = 2; $i < count($tokens); $i++) {
                                if ($tokens[$i - 2][0] == T_CLASS &&
                                    $tokens[$i - 1][0] == T_WHITESPACE &&
                                    $tokens[$i][0] == T_STRING
                                ) {
                                    $class = $tokens[$i][1];
                                    echo "Found class '{$class}' at '{$path}'";
                                }
                            }
                        }
                    }
                    break;
                default:
                    // TODO Maybe produce a warning?
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-30
      • 2021-02-23
      • 2014-08-15
      • 2013-12-09
      • 2021-12-18
      • 2018-10-01
      • 2016-03-06
      相关资源
      最近更新 更多