Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 Package Development - loading packages dependencies in development

Hi I'm trying to develop a package in Laravel 5.1. Thanks to help here I have the basics set up.

My current problem is how to load dependencies for the package while I'm developing it.

In the packages composer.json I have added dependencies and have these installed now in a vendor folder within my packages development folder. This is not the frameworks root vendor folder.

Here's my require section of the packages composer.json:

"require": {
"illuminate/support": "~5.1",
"php" : ">=5.3.0",
"google/apiclient": "dev-master"
},

Because they are not part of the main autoload process what is the best approach to ensuring the dependencies for my package are loaded correctly from within the development folder? How do I include the autoload? I'm concerned that if I reference them to their current location/namespace that it will break when later installed as a package in another app.

in my code I have the following:

$client = new \Google_Client($config);

which gives the error:

Class 'Google_Client' not found

I can get round this by adding this dependency to the main composer.json but don't think that is the correct approach to keep the package development independent (if that makes sense)

When I developed in L4.2 there was the workbench which took care of the loading which of course no longer features in L5.1

Any help and best practice appreciated

like image 310
Ray Avatar asked Jun 23 '15 07:06

Ray


Video Answer


1 Answers

Because they are not part of the main autoload process

I think you misunderstood how composer dependencies are managed. When in your main compose.json file you list a dependency, composer will add it to the main autoload process as well as all their dependencies, and the dependencies of their dependencies, and so on recursively.

You don't have to worry about where the dependencies are stored or how Composer will load them. Composer will automatically add them to the autoload file and all you have to do is make sure you require the composer autoload file. Once you require the composer autoload file, all the classes and functions loaded by composer will be available. Provided you required the composer autoload file all you have to do to use the classes from any of the installed packages is to make sure you address them using the proper namespace. Composer is smart enough to know where all classes are stored and how to load them (that is what psr-0, psr-4,... are for).

So if you are developing a Composer package, lets call it 'A', and you list the package 'C' as one of the dependencies of your package 'A', composer will add it to the autoload file for you. If you use another package, lets say, Laravel, which has a dependency of you package 'A', then also the package 'C' will be available within Laravel, since it is a dependency of 'A'.

I.e: If this is your composer.json file

{
    "name": "foo/bar",
    "require": {
        "google/apiclient": "1.0.*"
    }
}

This code will work

require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$youtube = new Google_Service_YouTube($client);

Note I've required the composer autoload file, which seems to be your problem. When you are using Laravel, it will add that file for you.

like image 125
Javi Stolz Avatar answered Nov 15 '22 00:11

Javi Stolz