【问题标题】:autloading not working correct using autoloader.php in vendor directory使用供应商目录中的 autoloader.php 自动加载无法正常工作
【发布时间】:2014-11-14 05:53:59
【问题描述】:

因为自动加载器无法解析 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


    【解决方案1】:

    这有点像在黑暗中拍摄,但 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;
    

    由于我不使用注释,因此我从未真正详细研究过原因。但是试一试。伤不起。

    【讨论】:

    • whohaaaa,你是对的......我在测试目录中创建了一个 bootstrap.php 并修改了 phpunit.xml 以引导该文件
    • 而且我只是使用来自vendor 目录的composer 的autoload.php 并且它工作(我什至不使用bootstrap.php):$loader = require_once __DIR__.'/vendor/autoload.php'; AnnotationRegistry::registerLoader(array($loader,'loadClass'));
    【解决方案2】:

    这有点老了,但是我创建了一个作曲家插件,它将作曲家类加载器注册到 AnnotationRegistry 作为加载器。

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

    【讨论】:

      【解决方案3】:

      首先运行这个命令:

      composer require indigophp/doctrine-annotation-autoload
      

      完成后,运行:

      composer dump-autoload
      

      【讨论】:

      • 完成第一个之后的这个:composer dump-autoload
      猜你喜欢
      • 2018-04-15
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 2018-11-27
      • 1970-01-01
      • 2015-11-20
      相关资源
      最近更新 更多