如何调试 PHP composer autoloader

How to debug PHP composer autoloader

我正在尝试使用 HiPay php 库。我安装了库:

composer require hipay/hipay-fullservice-sdk-php

生成一个包含 HiPay 库的供应商目录。在我的 order.php 页面中,我使用

<?php
namespace TokenizationExample;

require __DIR__ . '/config/credentials.php';
require __DIR__ . '/vendor/autoload.php';

$config = new \HiPay\Fullservice\HTTP\Configuration\Configuration($credentials['private']['username'], $credentials['private']['password']);

但我在

上收到 Class 未找到错误
$config = new \HiPay\Fullservice\HTTP\Configuration\Configuration($credentials['private']['username'], $credentials['private']['password']);

它在我本地的 win 10 计算机上工作 运行 XAMPP 但是当我将代码上传到我的主机时它不工作。我检查了 filnames 中的不匹配,但我无法发现错误。

这是错误:

Fatal error: Uncaught Error: Class 'HiPay\Fullservice\HTTP\Configuration\Configuration' not found in /home/XXXXX/order.php:8 Stack trace: #0 {main} thrown in /home/XXXXX/order.php on line 8

和 composer 创建的 vendor 目录

但是我怎样才能找出问题所在呢?并调试 autoload.php?抱歉新手问题。我是 PHP

的新手

更新 1:

我做了一个 "print_r" 的 aulotloader

$autoloader = require __DIR__ . '/vendor/autoload.php';
print_r($autoloader,true)

并从托管网站得到了这个结果,如果它不起作用

Composer\Autoload\ClassLoader Object
(
    [prefixLengthsPsr4:Composer\Autoload\ClassLoader:private] =&gt; Array
        (
            [H] =&gt; Array
                (
                    [HiPay\Fullservice\] =&gt; 18
                )

        )

    [prefixDirsPsr4:Composer\Autoload\ClassLoader:private] =&gt; Array
        (
            [HiPay\Fullservice\] =&gt; Array
                (
                    [0] =&gt; /home/XXXXX/vendor/composer/../hipay/hipay-fullservice-sdk-php/lib/HiPay/Fullservice
                )

        )

    [fallbackDirsPsr4:Composer\Autoload\ClassLoader:private] =&gt; Array
        (
        )

    [prefixesPsr0:Composer\Autoload\ClassLoader:private] =&gt; Array
        (
        )

    [fallbackDirsPsr0:Composer\Autoload\ClassLoader:private] =&gt; Array
        (
        )

    [useIncludePath:Composer\Autoload\ClassLoader:private] =&gt; 
    [classMap:Composer\Autoload\ClassLoader:private] =&gt; Array
        (
        )

    [classMapAuthoritative:Composer\Autoload\ClassLoader:private] =&gt; 
    [missingClasses:Composer\Autoload\ClassLoader:private] =&gt; Array
        (
        )

    [apcuPrefix:Composer\Autoload\ClassLoader:private] =&gt; 
)

不工作和工作本地 XAMPP 站点 print_r 输出之间的唯一区别是

[prefixDirsPsr4:Composer\Autoload\ClassLoader:private] =&gt; Array
        (
            [HiPay\Fullservice\] =&gt; Array
                (
                    [0] =&gt; C:\xampp\htdocs\hipay-example\vendor\composer/../hipay/hipay-fullservice-sdk-php/lib/HiPay/Fullservice
                )

        )

所以我将 HiPay 文件夹重命名为 hipay,一切正常。吸取教训,使用 "print_r" 调试自动装带器 fckps

您上传到服务器的代码在 Linux 上将是 运行,这是区分大小写的,但在 Windows 操作系统中这无关紧要。

  1. 确保软件包安装成功
    composer show | grep thepackageiwant
    如有疑问,请在不使用缓存和日志级别调试的情况下重新安装
    composer --no-cache -vvv require thepackageiwant

  2. 为了确保捕获所有可能发生 require(autoload.php) 的不同位置,从 vendor/autoload.php
    开始 在 vendor/autoload.php 中创建日志,将它们添加到日志文件中 - 根据您的代码,print_r 或 var_dump 会干扰其他事情

<?php
// autoload.php @generated by Composer

$timestamp = system('date +%s%N');
# to make sure you reach that far (you did require the autoload.php, right?!), add to first line in function getLoader, create/append to logfile
file_put_contents(__DIR__.'/_composer.log',print_r([$timestamp,'Autoloader Required by', debug_backtrace()],true),FILE_APPEND);

require_once __DIR__ . '/composer/autoload_real.php';
$loader = ComposerAutoloaderInit1fea3bd4e26e1ee7a171c20cea87d467::getLoader();

# The returned loader should include the classes and their path in case sensitive way corresponding to the actual path name in your file system
# If the classes aren't included, go back to Start, if they are, check if you can use the package path  when pasting it in your terminal/system explorer
file_put_contents(__DIR__.'/_composer.log',print_r([$timestamp,'Composer Autoload returns', $loader],true),FILE_APPEND);

return $loader;