Composer autoload无法找到课程

时间:2014-08-01 12:40:20

标签: php composer-php autoload

不知怎的,我无法让自动加载工作正常。我正在为phpunit使用以下bootstrap文件。

<?php

$loader = require __DIR__ . "/../vendor/autoload.php";
$loader->add('myproj\\example\\', __DIR__ . "/../example/");
$loader->add('myproj\\', __DIR__ . "/../src/");

var_dump($loader->getPrefixes());

var_dump的输出显示,据我所知,注册应该没问题。

Running "phpunit:test" (phpunit) task
Starting phpunit (target: test) in test
array(40) {
  ["phpDocumentor\Composer"]=>
  array(2) {
    [0]=>
    string(70) "/data/myproj/vendor/phpdocumentor/unified-asset-installer/src"
    [1]=>
    string(76) "/data/myproj/vendor/phpdocumentor/unified-asset-installer/test/unit"
  }
  ["phpDocumentor"]=>
  array(10) {
    [0]=>
    string(66) "/data/myproj/vendor/phpdocumentor/reflection-docblock/src"
    [1]=>
    string(57) "/data/myproj/vendor/phpdocumentor/reflection/src"
    [2]=>
    string(64) "/data/myproj/vendor/phpdocumentor/reflection/tests/unit"
    [3]=>
    string(65) "/data/myproj/vendor/phpdocumentor/reflection/tests/mocks"
    [4]=>
    string(55) "/data/myproj/vendor/phpdocumentor/graphviz/src"
    [5]=>
    string(62) "/data/myproj/vendor/phpdocumentor/graphviz/tests/unit"
    [6]=>
    string(54) "/data/myproj/vendor/phpdocumentor/fileset/src"
    [7]=>
    string(61) "/data/myproj/vendor/phpdocumentor/fileset/tests/unit"
    [8]=>
    string(60) "/data/myproj/vendor/phpdocumentor/phpdocumentor/src"
    [9]=>
    string(67) "/data/myproj/vendor/phpdocumentor/phpdocumentor/tests/unit"
  }
  ["Zend\Stdlib\"]=>
  array(1) {
    [0]=>
    string(54) "/data/myproj/vendor/zendframework/zend-stdlib"
  }
  ["Zend\ServiceManager\"]=>
  array(1) {
    [0]=>
    string(62) "/data/myproj/vendor/zendframework/zend-servicemanager"
  }
  ["Zend\Serializer\"]=>
  array(1) {
    [0]=>
    string(58) "/data/myproj/vendor/zendframework/zend-serializer"
  }
  ["Zend\Math\"]=>
  array(1) {
    [0]=>
    string(52) "/data/myproj/vendor/zendframework/zend-math"
  }
  ["Zend\Json\"]=>
  array(1) {
    [0]=>
    string(52) "/data/myproj/vendor/zendframework/zend-json"
  }
  ["Zend\I18n\"]=>
  array(1) {
    [0]=>
    string(52) "/data/myproj/vendor/zendframework/zend-i18n"
  }
  ["Zend\Filter\"]=>
  array(1) {
    [0]=>
    string(54) "/data/myproj/vendor/zendframework/zend-filter"
  }
  ["Zend\EventManager\"]=>
  array(1) {
    [0]=>
    string(60) "/data/myproj/vendor/zendframework/zend-eventmanager"
  }
  ["Zend\Config\"]=>
  array(1) {
    [0]=>
    string(54) "/data/myproj/vendor/zendframework/zend-config"
  }
  ["Zend\Cache\"]=>
  array(1) {
    [0]=>
    string(53) "/data/myproj/vendor/zendframework/zend-cache"
  }
  ["Twig_"]=>
  array(1) {
    [0]=>
    string(42) "/data/myproj/vendor/twig/twig/lib"
  }
  ["Symfony\Component\Yaml\"]=>
  array(1) {
    [0]=>
    string(41) "/data/myproj/vendor/symfony/yaml"
  }
  ["Symfony\Component\Validator\"]=>
  array(1) {
    [0]=>
    string(46) "/data/myproj/vendor/symfony/validator"
  }
  ["Symfony\Component\Translation\"]=>
  array(1) {
    [0]=>
    string(48) "/data/myproj/vendor/symfony/translation"
  }
  ["Symfony\Component\Stopwatch\"]=>
  array(1) {
    [0]=>
    string(46) "/data/myproj/vendor/symfony/stopwatch"
  }
  ["Symfony\Component\Process\"]=>
  array(1) {
    [0]=>
    string(44) "/data/myproj/vendor/symfony/process"
  }
  ["Symfony\Component\Finder\"]=>
  array(1) {
    [0]=>
    string(43) "/data/myproj/vendor/symfony/finder"
  }
  ["Symfony\Component\Filesystem\"]=>
  array(1) {
    [0]=>
    string(47) "/data/myproj/vendor/symfony/filesystem"
  }
  ["Symfony\Component\EventDispatcher\"]=>
  array(1) {
    [0]=>
    string(53) "/data/myproj/vendor/symfony/event-dispatcher"
  }
  ["Symfony\Component\Console\"]=>
  array(1) {
    [0]=>
    string(44) "/data/myproj/vendor/symfony/console"
  }
  ["Symfony\Component\Config\"]=>
  array(1) {
    [0]=>
    string(43) "/data/myproj/vendor/symfony/config"
  }
  ["Psr\Log\"]=>
  array(1) {
    [0]=>
    string(36) "/data/myproj/vendor/psr/log"
  }
  ["Pimple"]=>
  array(1) {
    [0]=>
    string(46) "/data/myproj/vendor/pimple/pimple/lib"
  }
  ["PhpOption\"]=>
  array(1) {
    [0]=>
    string(52) "/data/myproj/vendor/phpoption/phpoption/src"
  }
  ["PhpCollection"]=>
  array(1) {
    [0]=>
    string(60) "/data/myproj/vendor/phpcollection/phpcollection/src"
  }
  ["Parsedown"]=>
  array(1) {
    [0]=>
    string(45) "/data/myproj/vendor/erusev/parsedown"
  }
  ["PHPParser"]=>
  array(1) {
    [0]=>
    string(49) "/data/myproj/vendor/nikic/php-parser/lib"
  }
  ["Metadata\"]=>
  array(1) {
    [0]=>
    string(45) "/data/myproj/vendor/jms/metadata/src"
  }
  ["Knp\Menu\"]=>
  array(1) {
    [0]=>
    string(49) "/data/myproj/vendor/knplabs/knp-menu/src"
  }
  ["JMS\Serializer"]=>
  array(1) {
    [0]=>
    string(47) "/data/myproj/vendor/jms/serializer/src"
  }
  ["JMS\"]=>
  array(1) {
    [0]=>
    string(47) "/data/myproj/vendor/jms/parser-lib/src"
  }
  ["Doctrine\Common\Lexer\"]=>
  array(1) {
    [0]=>
    string(47) "/data/myproj/vendor/doctrine/lexer/lib"
  }
  ["Doctrine\Common\Annotations\"]=>
  array(1) {
    [0]=>
    string(53) "/data/myproj/vendor/doctrine/annotations/lib"
  }
  ["Cilex\Provider\Console"]=>
  array(1) {
    [0]=>
    string(63) "/data/myproj/vendor/cilex/console-service-provider/src"
  }
  ["Cilex\Provider"]=>
  array(1) {
    [0]=>
    string(60) "/data/myproj/vendor/phpdocumentor/phpdocumentor/src"
  }
  ["Cilex"]=>
  array(1) {
    [0]=>
    string(44) "/data/myproj/vendor/cilex/cilex/src"
  }
  ["myproj\example\"]=>
  array(1) {
    [0]=>
    string(38) "/data/myproj/test/../example/"
  }
  ["myproj\"]=>
  array(1) {
    [0]=>
    string(34) "/data/myproj/test/../src/"
  }
}

不知何故,我遇到了以下错误。

PHPUnit 4.1.4 by Sebastian Bergmann.

Fatal error: Command failed: PHP Fatal error:  Class 'myproj\example\NotificationCountChangedEvent' not found in /data/myproj/test/unit/DefaultDomainEventTest.php on line 14

我的文件夹结构在myproj文件夹中看起来像这样     SRC /     测试/       单元/        - DefaultDomainEventTest.php      - bootstrap.php     例/      - NotificationCountChangedEvent.php

NotificationCountChangedEvent.php有名称空间myproj\examplesrc/中的所有类也以名称空间myproj开头,然后对于每个子文件夹,我使用foldername作为子名称空间。

我缺少什么/为什么这不起作用。

1 个答案:

答案 0 :(得分:0)

使用以下bootstrap解决了这个问题。请注意addPsr4方法,而不是add方法。

<?php

$loader = require realpath(__DIR__ . "/../vendor/autoload.php");
$loader->addPsr4('bkcqrs\\example\\', realpath(__DIR__ . "/../example/") . "/", true);
$loader->addPsr4('bkcqrs\\', realpath(__DIR__ . "/../src/") . "/", true);