使用供应商目录中的 autoloader.php 自动加载无法正常工作 [英] autloading not working correct using autoloader.php in vendor directory
问题描述
我对 Composer 的自动加载感到厌烦,因为自动加载器无法解析 DoctrineORMMappingTable.对于单元测试,我创建了带有典型注释的学说实体类:
i've troube with the autoloading of composer as the autoloader can't resolve DoctrineORMMappingTable. For the Unittests i have created doctrine entity classes with typical Annotations:
<?php
namespace OmniSearchTestEntity;
use DoctrineORMMapping as ORM;
/**
* Picture
*
* @ORMTable(name="picture")
* @ORMEntity
*/
class Picture
{
并使用此实体创建了一个新的实体管理器.但我收到了消息:
and created a new entity manager by using this entities. But im getting The Message:
DoctrineCommonAnnotationsAnnotationException: [Semantical Error] The annotation "@DoctrineORMMappingTable" in class OmniSearchTestEntityPicture does not exist, or could not be auto-loaded.
对于一些单元测试
首先,我有以下项目结构:
First, i have the following project structure:
/src
/OmniSearch
SomeClass.php
/tests
/OmniSearchTest
SomeClassTest.php
/composer.json
/phpunit.xml.dist
我的 composer.json 看起来像这样:
My composer.json looks like this:
{
/* ... */
"require": {
"php": ">=5.4",
"doctrine/orm": "2.*"
},
"require-dev": {
"phpunit/phpunit": "4.*"
},
"autoload": {
"psr-0": {
"OmniSearch\": "src/"
}
},
"autoload-dev": {
"psr-0": {
"OmniSearchTest\": "tests/"
}
}
}
虽然我的 phpunit 看起来完全像这样:
While my phpunit looks excactly like this:
<?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 包含映射:
I cutted off this project from another zf2 project of mine where the autoloading was working fine. Im not sure what exactly went wrong because the autogenerated autoload_namespaces.php contains the the mapping:
'Doctrine\ORM\' => array($vendorDir . '/doctrine/orm/lib'),
推荐答案
这有点摸不着头脑,但 Symfony 2 应用程序包含一个 autoload.php 文件,该文件显式加载了一个注解注册表.
This is kind of a shot in the dark but Symfony 2 applications include an autoload.php file which explicitly loads an annotation registry.
// autoload.php
use DoctrineCommonAnnotationsAnnotationRegistry;
use ComposerAutoloadClassLoader;
/**
* @var ClassLoader $loader
*/
$loader = require __DIR__.'/../vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
因为我不使用注释,所以我从未真正研究过任何细节.但试一试.伤不起.
I never really researched why in any detail since I don't use annotations. But give it a try. Can't hurt.
这篇关于使用供应商目录中的 autoloader.php 自动加载无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!