0
votes

I'm creating my first composer package. I'm testing it by pulling it into a vanilla Laravel project.

The issue I'm having is that when I require my composer package in the main Laravel composer.json file and then try to update the autoload.

My package's composer.json:

{
    "name": "cschmitz/l5-simplefm",
    "description": "A Laravel 5 wrapper for Soliant Consulting's SimpleFM package.",
    "require": {
        "soliantconsulting/simplefm": "3.0.*"
    },
    "license": "MIT",
    "authors": [
        {
            "name": "cschmitz",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "autoload": {
        "psr-4": {
            "L5SimpleFM\\": "src/"
        }
    }
}

My package's folder structure in the Laravel project's vendor folder:

vendor folder structure

My Laravel project's composer.json:

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "cschmitz/l5-simplefm": "dev" // Requiring my package here
    },
    ...

After this, I performed a composer dump-autoload. I don't get any errors from composer, but when I check my vendor/composer/autoload_psr4.php file, my namespace isn't listed in the array:

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

    return array(

        ...
        'App\\' => array($baseDir . '/app'),


        // I expected to see `'L5SimpleFM\\' => array($vendorDir . '/cschmitz/L5SimpleFM/src')` as the last key of this array, but nothing shows past the App key
    );

I looked around online and on stackoverflow, but the various answers and suggestions I found didn't resolve the issue.

Can anyone see what I'm missing?

Update

Per Alexandru Guzinschi's answer, I tried telling my Laravel project that there was a local composer repository to inspect by adding the following block into my Laravel project's composer.json file:

"repositories":[
    {
        "type": "vcs",
        "url": "../cschmitz/L5SimpleFM"
    }
],    

I then moved the package folder starting at the cschmitz directory out to the same level of my laravel project. The file structure looks like this now:

LaravelProjectFolder/
    composer.json
cschmitz/
    L5SimpleFM/
        composer.json

Initially I ran into the error "No driver found to handle VCS repository vendor/cschmitz". After reading a bit I found that to be able to use this kind of local repository, the repo itself needs to be under version control (i.e. git, svn, etc). Makes sense. I hadn't put it under version control yet because this was just a test project used to try to develop the package.

I created a git repository at the root of my Laravel project and ran composer update. Now I'm getting the error:

[Composer\Repository\InvalidRepositoryException] No valid composer.json was found in any branch or tag of ../cschmitz/L5SimpleFM, could not load a package from it.

This is confusing because I can ls ../cschmitz/L5SimpleFM/composer.json and see the file. I can also run git ls-tree -r master --name-only and see the composer.json file in the local repository version control:

composer.json in local repository's git repo

Is there something that would prevent my Laravel project from seeing the local repository's composer.json file?

1
It isn't in packagist yet, so you would need to tell Composer where to find it in source control before a composer install would find and install it getcomposer.org/doc/… But I would expect composer to report an error that it couldn't find the package when you did composer install.Michael Berkowski
Would that be necessary if I already moved the package into the vendor folder by hand and did a composer dump-autoload? I didn't think composer required packagist if it's just autoloading namespaces.Chris Schmitz
Not sure - I have always added a repositories key to point to the source repo. I have never tried just placing them into vendor/Michael Berkowski
This is probably because you already have a lock file which is being read by the install command. Add the repository location to your composer.json and then run composer update Documentation explains the differenceAvalanche
So I removed the lock file, ran composer update, and it still doesn't add my namespace to the autoload file. I read through the documentation and the lock file still sounds like it deals with downloading the package and not generating the autoload file. I read through the section on autoloading again and it still sounds like what I did would work. I'm still missing something.Chris Schmitz

1 Answers

0
votes

It is not enough to move your library inside vendor directory, because Composer doesn't scan that directory and doesn't know about your change.

You need to tell Composer about your package by publishing it to Packagist, or by loading it from local file system. In your Laravel project composer.json file, change:

"repositories": [
        {
            "type": "vcs",
            "url": "/path/to/cschmitz"
        }
],
"require": {
    "cschmitz/l5-simplefm": "dev-develop"
}

Next you need to run composer update in order for your library to be installed.


Not related to your current issue, but it will became an issue soon:

After you will do this and you will continue working on your cschmitz/l5-simplefm library, you will notice that you need to commit your changes made to your library and then run composer update again in your Laravel project before you can use those changes. You have a few solutions here on how to circumvent this issue.