使用供应商目录中的autoloader.php,autloading无法正常工作


autloading not working correct using autoloader.php in vendor directory

我对composer的自动加载感到困惑,因为自动加载程序无法解决Doctrine''ORM''Mapping''Table。对于Unittest,我创建了具有典型注释的条令实体类:

<?php
namespace OmniSearchTest'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
 * Picture
 *
 * @ORM'Table(name="picture")
 * @ORM'Entity
 */
class Picture
{

并通过使用这些实体创建了一个新的实体管理器。但我得到的信息:

Doctrine'Common'Annotations'AnnotationException: [Semantical Error] The annotation "@Doctrine'ORM'Mapping'Table" in class OmniSearchTest'Entity'Picture does not exist, or could not be auto-loaded.

对于某些Unittest

首先,我有以下项目结构:

/src
    /OmniSearch
        SomeClass.php
/tests
    /OmniSearchTest
        SomeClassTest.php
/composer.json
/phpunit.xml.dist

我的composer.json看起来像这样:

{
    /* ... */
    "require": {
        "php": ">=5.4",
        "doctrine/orm": "2.*"
    },
    "require-dev": {
        "phpunit/phpunit": "4.*"
    },
    "autoload": {
        "psr-0": {
            "OmniSearch''": "src/"
        }
    },
    "autoload-dev": {
        "psr-0": {
            "OmniSearchTest''": "tests/"
        }
    }
}

而我的phpunit实际上是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
     backupStaticAttributes="false"
     bootstrap="vendor/autoload.php"
     strict="true"
     verbose="true">
    <testsuites>
        <testsuite name="omnisearch">
            <directory>./tests/OmniSearchTest</directory>
        </testsuite>
    </testsuites>
</phpunit>

我把这个项目从我的另一个zf2项目中删掉了,在那里自动加载工作得很好。我不确定到底出了什么问题,因为自动生成的autoload_namespaces.php包含映射:

'Doctrine''ORM''' => array($vendorDir . '/doctrine/orm/lib'),

这有点像是在黑暗中拍摄的,但Symfony 2应用程序包含一个自动加载.php文件,该文件显式加载注释注册表。

// autoload.php
use Doctrine'Common'Annotations'AnnotationRegistry;
use Composer'Autoload'ClassLoader;
/**
 * @var ClassLoader $loader
 */
$loader = require __DIR__.'/../vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;

由于我不使用注释,所以我从未真正详细研究过原因。但请尝试一下。不会受伤。

这有点旧,但我创建了一个composer插件,它将composer ClassLoader作为加载器注册到AnnotationRegistry中。

https://github.com/indigophp/doctrine-annotation-autoload

首先运行此命令:

composer require indigophp/doctrine-annotation-autoload

完成后,运行此:

composer dump-autoload