Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load my own vendor folder with Composer?

I have this composer.json file

{
    "require": {
        "filp/whoops": "1.*"
    }
}

However, I have a folder for my own project called vendor/imaqtpie/framework/src. This is not hosted anywhere, so if I do composer update to update autoload files, it gives an error.

The requested package "imaqtpie/framework" could not be found in any version, there may be a typo in package name.

I had to add this myself to autoload file to make it work.

'Framework' => array($vendorDir . '/imaqtpie/framework/src')

Is there any way to solve this?

I want to tell Composer that this local vendor folder has to be autoloaded each time regardless of checking server/version, or looking for a more elegant solution since I'm new to composer.

like image 778
Aristona Avatar asked Jul 25 '13 02:07

Aristona


2 Answers

There are a few ways you can do this.

1. I'd say the most correct way, is to host it and use Satis to produce a private 'packagist'. Then composer will behave "normally" and get the latest version, do version checking etc. but you say you don't care for this.

If you want more details I can expand on this, I have set up many satis packages and it works really well. (Note there's also the new commercial Toran Proxy which I haven't trialled yet.)

2. If your 'imaqtpie' library is a fake vendor library (it sounds like you just have some files you've stored there, like you would an old-fashioned include library?), then you could simply use a classmap to point the autoloader to that folder from your top level application. This only makes sense if you are including that folder in your top-level app.

So your app's composer json could look like:

{
    "require": {
        "filp/whoops": "1.*"
    },
    "autoload": {
       "classmap":[
           "vendor/imaqtpie/framework/src"
       ]
    }
}

So this tells composer there's a bunch of classes in that folder. When you run composer dump-autoload it will scan the folder and generate vendor/composer/autoload_classmap.php with all your files listed.

This isn't how you're supposed to use composer, but you're not asking to use composer for package management you're asking how to use composer's autoloader, which I guess is fine! as long as you understand the risks.

3. If your package is either PSR0 or 4 (it sounds likely from the "src" folder) then you'd similarly do this in your top-level app:

{
    "require": {
        "filp/whoops": "1.*"
    },
    "autoload": {
       "psr-4": {
           "Imaqtpie\\Framework\\":"vendor/imaqtpie/framework/src"
       }
    }
}

Which again is a bit odd, but should work!

Normally, you'd specify this path in your package's composer.json and then when you do an update it gets merged into your composer.lock and then the vendor/composer/installed.json (which is the source used for the dump-autoload). But in theory you can load whatever you want from the top level app, and therefore you can 'hard code' a package into the vendor library and classpath to it.

I'd probably recommend not doing this though! The vendor folder is a tenuous location which most people and programs would assume can be destroyed and rebuilt at a whim. So it's a dangerous place to store anything which isn't in a package. It's also confusing for any other developers who will assume the same.

So I'd recommend moving your library to another location away from the vendor folder, e.g. 'lib', and then using the classpath approach above to include it in the autoloader.

like image 20
scipilot Avatar answered Sep 28 '22 08:09

scipilot


You have to create your own local git repository with your package code to achieve that. After that, put something like this into your composer.json file.

"repositories": [
    {
        "type":"vcs",
        "url":"/path/to/your/source"
    }
],

"require":{
    "filp/whoops":"dev-master"
}

Autoloading shouldn't be an issue if you implement it this way...

like image 100
OlliM Avatar answered Sep 28 '22 08:09

OlliM