Composer and PSR-0

791 views Asked by At

I have composer.json structure like this:

"psr-0": {
        "DatabaseSeeder\\": "app/database/seeds/"
    },

I have files in app/database/seeds (Files here can be created dynamically so solution with "classmap": ["app/database/seeds"] doesn't work here bcs I have to always dump-autoload before seeding:

  • UserTableSeeder.php
  • DatabaseSeeder.php

with structure like this:

# DatabaseSeeder.php

namespace DatabaseSeeder;

use Doctrine\ORM\EntityManager;

 class DatabaseSeeder implements Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(EntityManager $em)
    {
            $this->call('DatabaseSeeder\UserTableSeeder', $em);
    }

    private function call($class, $em)
    {
            $reflectionMethod = new \ReflectionMethod($class, 'run');
            $reflectionMethod->invoke(new $class, $em);
    }

After php composer.phar install and php composer.phar dump-autoload I cannot use in application for example in index.php

 $object = new \DatabaseSeeder\DatabaseSeeder();

because I recieve error: Class DatabaseSeeder\DatabaseSeeder does not exist WHY ?? It should autoload class while initiating object.

1

There are 1 answers

0
Andy Librian On BEST ANSWER

First of all, you should make sure your index.php properly includes composer autoload.

<?php

include __DIR__ . '/vendor/autoload.php';

Then you need to validate your directory structure to be properly autoloaded.

If your PSR-0 configuration is

"psr-0": {
    "DatabaseSeeder\\": "app/database/seeds/"
},

then your directory structure should looks like:

app/
   database/
       seeds/
           DatabaseSeeder/
               DatabaseSeeder.php

For more information about composer PSR-0 autoload, you can read composer schema documentation. Hope this helps.