使用供应商目录中的autoloader.php进行的自动加载无法正常工作

问题描述 投票:6回答:3

由于自动加载器无法解析Doctrine \ ORM \ Mapping \ Table,因此自动加载Composer成为麻烦。对于单元测试,我创建了带有典型注释的学说实体类:

<?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.

对于某些单元测试

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

/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'),
php doctrine-orm phpunit composer-php autoload
3个回答
14
投票

这是在黑暗中的镜头,但是Symfony 2应用程序包含一个autoload.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;

我从来没有真正研究过为什么做任何细节,因为我不使用注释。但是尝试一下。不会受伤。


3
投票

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

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


0
投票

首先输入此命令:作曲家需要indigophp / doctrine-annotation-autoload

以及第一个完成后的这个

© www.soinside.com 2019 - 2024. All rights reserved.