使用autoloader.php在供应商目录中自动加载不能正常工作 [英] autloading not working correct using autoloader.php in vendor directory
问题描述
由于自动加载器无法解析Doctrine \ORM\Mapping\Table,我自动加载了作曲家,因此我遇到了麻烦。
对于Unittests,我创建了带有典型注释的doctrine实体类:
<?php
命名空间OmniSearchTest\Entity;
使用Doctrine\ORM\Mapping作为ORM;
/ **
*图片
*
* @ ORM\Table(name =picture)
* @ ORM\Entity
* /
class Picture
{
并创建了一个新实体经理使用这些实体。但是得到的消息:
Doctrine\Common \Annotations\AnnotationException:[语义错误]注释@Doctrine OmniSearchTest\Entity\Picture类中的\ORM\Mapping\Table不存在,或无法自动加载。
对于某些单元测试
具有以下项目结构:
/ 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.0encoding =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应用程序包括一个autoload.php文件,显式加载注释注册表。
// autoload.php
use Doctrine\Common\Annotations\AnnotationRegistry;
使用Composer\Autoload\ClassLoader;
/ **
* @var ClassLoader $ loader
* /
$ loader = require __DIR __。'/ .. / vendor / autoload.php';
AnnotationRegistry :: registerLoader(array($ loader,'loadClass'));
return $ loader;
我从来没有真正研究过为什么,因为我不使用注释。但是试试。不能伤害。
i've troube with the autoloading of composer as the autoloader can't resolve Doctrine\ORM\Mapping\Table. For the Unittests i have created doctrine entity classes with typical Annotations:
<?php
namespace OmniSearchTest\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Picture
*
* @ORM\Table(name="picture")
* @ORM\Entity
*/
class Picture
{
and created a new entity manager by using this entities. But im getting The Message:
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.
For some Unittests
First, i have the following project structure:
/src
/OmniSearch
SomeClass.php
/tests
/OmniSearchTest
SomeClassTest.php
/composer.json
/phpunit.xml.dist
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/"
}
}
}
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>
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'),
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 Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;
/**
* @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屋!