autloading not working correct using autoloader.php in vendor directory
Asked Answered
I

3

7

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'),
Indemnification answered 19/9, 2014 at 15:20 Comment(0)
K
15

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.

Kurland answered 19/9, 2014 at 18:49 Comment(2)
whohaaaa, you're right... i've created a bootstrap.php in the tests directory and modify the phpunit.xml to bootstrap this fileIndemnification
And I simply use composer's autoload.php from vendor directory and it worked (I don't even use bootstrap.php): $loader = require_once __DIR__.'/vendor/autoload.php'; AnnotationRegistry::registerLoader(array($loader,'loadClass'));Pedestrianize
F
3

This is a bit old, but I created a composer plugin which registers composer ClassLoader into the AnnotationRegistry as a loader.

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

Faugh answered 3/5, 2015 at 12:54 Comment(0)
P
0

First run this command:

composer require indigophp/doctrine-annotation-autoload

After that finishes, run this:

composer dump-autoload
Phemia answered 29/1, 2020 at 12:20 Comment(1)
and this one after finishing the first : composer dump-autoloadPhemia

© 2022 - 2024 — McMap. All rights reserved.