Autoload does not work correctly using autoloader.php in the vendor directory

I have a problem with composer autoload because autoloader cannot resolve Doctrine \ ORM \ Mapping \ Table. For Unittests, I 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 using these entities. But I get 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

Firstly, 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/" } } } 

So far my phpunit looks 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 disconnected this project from another zf2 project where autoload worked fine. I'm not sure what exactly went wrong, because autoload_namespaces.php autogeneration contains a mapping:

 'Doctrine\\ORM\\' => array($vendorDir . '/doctrine/orm/lib'), 
+6
source share
2 answers

This is a kind of shot in the dark, but Symfony 2 applications include an autoload.php file that explicitly loads the 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 have never explored why in detail since I do not use annotations. But give it a try. It can't hurt.

+13
source

This is a bit dated, but I created a composer plugin that registers the ClassLoader composer in AnnotationRegistry as a loader.

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

+3
source

Source: https://habr.com/ru/post/975542/


All Articles