David
David

Reputation: 4117

composer autoload error

I'm trying to work with composer and will create own packages. My package project has the following file structure:

scr/Scheduler.php
.gitignore
composer.json

The test class:

namespace david\Scheduler;

use Symfony\Component\Yaml\Parser;

class Scheduler
{
    private $yamlParser;

    public function __construct()
    {
        $this->yamlParser = new Parser();
    }
}

and the composer.json file

...
"require": {
    "symfony/yaml": "~3.0.3"
},
"autoload": {
    "psr-4": {
        "david\\Scheduler\\": "src/"
    }
}

That all I've committed to a private github repository. Currently I've only the master branch and no tags. My second step was to create a client project with the following composer.json file:

"repositories": [
    {
        "type": "package",
        "package": {
            "name": "david/scheduler",
            "type": "package",
            "version": "dev-master",
            "source": {
                "url": "[email protected]:david/scheduler.git",
                "type": "git",
                "reference": "master"
            }
        }
    }
],
"require": {
    "david/scheduler": "dev-master"
}

When I'm running composer update the github project will be cloned and stored in the vendor directory. But only my package. The dependency to the symfony/yaml will not loaded.

vendor
    composer
    david/scheduler

Also composer create a empty autoload_psr4 file.

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
);

I think maybe there are some issues in my architecture...

Upvotes: 0

Views: 1841

Answers (1)

Christophe Coevoet
Christophe Coevoet

Reputation: 999

Your issue is that you are registering your custom repository using a package type, which means that the root package is defining the metadata of david/scheduler and the composer.json file in the git repo is never used by Composer. and in this package definition, there is no dependency on the symfony/yaml component and no autoloading for your Scheduler class.

As a general rule, the package repository should always be considered as a last resort (it has many drawbacks). when you control the git repository, putting a composer.json file in the repository and using a vcs repository works much better (as composer can then rely on metadata coming from git itself, and the package metadata are provided in the package git repo).

The root package should look like this:

{
    "repositories": [
        {
            "type": "vcs",
            "url": "[email protected]:david/scheduler.git"
        }
    ],
    "require": {
        "david/scheduler": "dev-master"
    }
}

Upvotes: 1

Related Questions