Search code examples
phpsymfonycomposer-phpsymfony-3.4autoloader

Excluding Data Fixtures for being autloaded by composer in a Symfony 3.4 application


I am using doctrine/data-fixtures in dev environments and require it as follows:

composer.json:

"autoload": {
    "psr-0": {
        "": "src/",
        "SymfonyStandard": "app/"
    },
    "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
"require-dev": {
    "symfony/maker-bundle": "^1.8",
    "phpunit/phpunit": "^7.0",
    "doctrine/data-fixtures": "dev-rfc1872 as v1.2.1",
    "doctrine/doctrine-fixtures-bundle": "^3.0"
},

In my Symfony 3.4 application my data fixtures live in /src/AppBundle/DataFixtures/ORM/*.php.

When I run composer, which in turn runs a Symfony cache:clear I get the following error:

[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command:
In DefinitionErrorExceptionPass.php line 37:

While discovering services from namespace "AppBundle\", an error was thrown    
 when processing the class "AppBundle\DataFixtures\ORM\LoadCourseData": "Cl    
ass Doctrine\Common\DataFixtures\AbstractFixture not found".

I would like to exclude the DataFixtures namespace from being autoloaded but I cannot find a way to do that.


Solution

  • I think you don't want to exclude data fixtures from being autoloaded, because they are valid PHP classes.

    This error comes from Symfony's autowiring feature. So I assume you're indeed using autowiring. You should data fixtures from autowiring discovery, which can be done in services.yml with exclude option.

    Assuming you have something like (which should be there by default):

    AppBundle\:
        resource: '../../src/AppBundle/*'
        exclude: '../../src/AppBundle/{Entity,Repository}'
    

    All you need to do is to add DataFixtures namespace to exclude:

    AppBundle\:
        resource: '../../src/AppBundle/*'
        exclude: '../../src/AppBundle/{Entity,Repository,DataFixtures}'
    

    You can read more about it in here nad here