0

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": "schmitz.chris@gmail.com"
        }
    ],
    "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?

Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137
  • 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 https://getcomposer.org/doc/05-repositories.md#using-private-repositories But I would expect composer to report an error that it couldn't find the package when you did `composer install`. – Michael Berkowski Aug 18 '15 at 19:59
  • 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 Aug 18 '15 at 20:03
  • 1
    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 Aug 18 '15 at 20:09
  • 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 difference](https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file) – Avalanche Aug 18 '15 at 20:12
  • 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 Aug 18 '15 at 21:51
  • Is there more information that would be helpful to show? – Chris Schmitz Aug 18 '15 at 22:14

1 Answers1

0

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.

Community
  • 1
  • 1
Alexandru Guzinschi
  • 5,675
  • 1
  • 29
  • 40
  • I believe this got me closer, but I'm still running into an issue. I'll update the initial question. – Chris Schmitz Aug 19 '15 at 14:10
  • @ChrisSchmitz If you commited your library to master branch, replace `"cschmitz/l5-simplefm": "dev-develop"` with `"cschmitz/l5-simplefm": "dev-master"` in your Laravel project. – Alexandru Guzinschi Aug 19 '15 at 14:44
  • Tried and I still get the "No valid composer.json file found" error from my update. Do I need to set something different in the library composer.json file as well? – Chris Schmitz Aug 19 '15 at 16:58
  • After looking over everything I noticed that for some reason my package's L5SimpleFM subfolder was not being tracked even though I saw the files in the git list. I trashed my .git folder, initialized it again, added everything (making sure the contents of the L5 folder were tracked), committed, composer updated and watched my package pull in from my local repository. Thanks for the help! – Chris Schmitz Aug 19 '15 at 17:57