alanmanderson
alanmanderson

Reputation: 8200

Composer package not showing up in autoload_namespaces

This should be an easy one. I developed a package call it MyVendor\MyPackage

inside MyVendor\MyPackage is:

The MyVendor\MyPackage\composer.json file contains:

{
    "name":"MyVendor/MyPackage",
    "description":"MyClass!!!",
    "keywords": ["MyKeyword"],
    "homepage": "http://MyPackage.com",
    "type":"library",
    "license": "MIT",
    "authors": [
        {
            "name": "ME",
            "email": "[email protected]",
            "homepage":"http://ME.com"
        }
    ],
    "require": {

    },
    "autoload":{
        "psr-4" : {
            "MyVendor\\MyPackage\\":""
        }
    }
}

Now I have another project called MyOtherPackage whose composer.json file looks like:

{
"require": {
    "monolog/monolog": "1.2.*",
    "MyVendor/MyPackage": "1.0.0"
},
"autoload": {
    "psr-4": {
        "MyVendor\\MyOtherPackage\\": "MyOtherPackage/",
        "MyVendor\\": "/"
    }
},
"repositories": [
    {
        "type": "package",
        "package": {
            "name": "MyVendor/MyPackage",
            "version": "1.0.0",
            "source": {
                "url": "https://ME.com/svn/MyVendor/MyPackage/",
                "type": "svn",
                "reference": "trunk"
            }
        }
    }
]
}

So MyOtherPackage depends on MyPackage. Everything downloads just fine, but if I open up autload_namespaces.php it only includes monolog. It looks like this:

return array(
    'Monolog' => array($vendorDir . '/monolog/monolog/src'),
);

Why isn't MyVendor/MyPackage appear in the namespaces.php or autoload_psr4.php file? Is the composer.json file wrong?

EDIT I added to the MyPackage composer.json file.

Upvotes: 1

Views: 687

Answers (2)

alanmanderson
alanmanderson

Reputation: 8200

I've figured it out. It seems as if defining the repository as a package, I am telling composer that it isn't a composer compatible class, which means composer doesn't look for a composer.json file.

To fix it I removed the package definition and made the dependent class's composer.json file to look like:

{
    "require": {
         "monolog/monolog": "1.2.*",
         "MyVendor/MyPackage": "1.0.0"
    },
    "autoload": {
        "psr-4": {
            "MyVendor\\MyOtherPackage\\": "MyOtherPackage/",
            "MyVendor\\": "/"
        }
    },
    "repositories": [
        {
            "type": "svn",
            "url": "https://ME.com/svn/MyVendor/MyPackage/",
            "reference": "tags"
        }
    ]
}

This tells composer to download the package from this repository and look for the composer.json file.

Upvotes: 1

Sven
Sven

Reputation: 70863

You did not define any autoload mechanism in your first package. If you don't, Composer cannot know how to autoload the classes, and does nothing (which is a valid option if your package does not contain any PHP at all, but for example only images and javascript).

Add something like this:

"autoload": {
    "psr-0": {
        "MyVendor\\Namespace":"src/path"
    }
}

Upvotes: 0

Related Questions