composer.json: change the vendor-dir dynamically through a script - composer-php

In a project using Composer, shared by many developers and environments, I need to have the vendor-dir config parameter of composer.json to be set dynamically. That is, a script that runs when composer install/update is launched, must be able to change the value of this entry:
//composer.json
"config": {
"vendor-dir": "/var/www/html/......",
I tried the following:
//composer.json
"scripts": {
"pre-install-cmd": "MyBundle\\Composer\\Hook::setVendorDir",
The class Hook has this method:
//MyBundle/Composer/Hook.php
public static function setVendorDir(Event $event)
{
// ... some code set the $vendorDir variable here depending on many thing
$event->getComposer()->getConfig()->merge([
'config' => [
'vendor-dir' => $vendorDir
]
]);
// ...
}
The result is that the file autoloader.php and a composer folder are created in the right vendor directory, but all other packages are still installed in the default vendor directory!
The composer folder I mentioned only contains some PHP files (ClassLoader.php, _autoload\_*.php_, and LICENSE)
Notice: When I change the vendor-dir parameter in composer.json, it works flawlessly.
How may I set vendor-dir dynamically and have it taken into account for every package installations?

You can e.g. write a batch script (Windows) or bash script (Linux) or even a PHP script which you run instead of composer install. The script sets the correct vendor-dir in the composer.json and then runs composer install or whatever.
To set vendor dir just run in your script:
composer config vendor-dir /your/path/to/your/vendor/dir
For more info about composer config see the Composer documentation.

Related

Laravel package class not found

I have installed laravel packages on staging server and packages working fine. But when i take pull on staging server, it is showing me error that package class not found.
Steps I have followed to resolve issue
I have check in vendor folder as well as in config/app.php, but I got class declaration and package folder is there.
After this when I update composer, my issue get resolved.
Is there any other file which should i look for class defination?
Perform a composer update, then composer dump-autoload.
If the above doesn't solve the problem, change the classmap in your composer.json file such that it contains the project-relative path to your php files:
"autoload-dev": {
"classmap": [
"tests/TestCase.php",
"database/seeds/UserTableSeeder.php" //include the file with its path here
]},
and soon after, perform a composer dump-autoload, and it should work now

Create own laravel package

I try to create own laravel package.
I created package folder in my project in root directory.
Than created simplexi/greetr/src folders in package.
I added to autoload "Simplexi\Greetr\": "package/simplexi/greetr/src" in composer.json in main project, and used command composer dump-autoload.
Than in src folder I created RiakServiceProvider, added this provider to config=>app.php to providers array.
Then in boot method I added next code:
$this->publishes([__DIR__ . '../config/myconf.php' => config_path() . '/']);
And executed next command:
php artisan vendor:publish --provider="\Simplexi\Greetr\RiakServiceProvider"
Than I got Publishing complete.
But file myconf.php didn't copy to app/config.
Also I checked file myconf.php in my package/simplexi/greetr/config folder and it exists.
Can anyone tell me what the problem might be?
publishes() expect two parameters. Try something like this:
$this->publishes([
__DIR__.'/../config/myconf.php' => config_path('myconf.php'),
], 'config');

Composer autoload path not working

I have this script that use composer to setup the project but for some reasons does not work
<?php
include_once 'vendor/autoload.php';
use \LeagueWrap\Api;
$api = new Api($key = "somekey"); // Load up the API
$summoner = $api->summoner(); // Load up the summoner request object.
$bakasan = $summoner->info('bakasan'); // Get the information about this user.
$bakasan = $summoner->info(74602); // same thing as above, just to show that an id will wo$
echo $bakasan->summonerLevel; // 30
echo $bakasan->id; // 74602
echo $bakasan->name; // "bakasan"
echo $bakasan->profileIconId; // 24
echo $bakasan->revisionDate; // 1387391523000
echo $bakasan->revisionDateStr; // "12/18/2013 06:32 PM UTC"
?>
you can check here http://70.37.98.151/leaguewrap/tests/test2.php the error I get
Warning: include_once(vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/leaguewrap/tests/test2.php on line 3
my composer.json is
{
"name": "paquettg/leaguewrap",
"type": "library",
"description": "A wrapper for the League of Legends API.",
"version": "0.6.2",
"keywords": ["League", "legends", "wrap", "api", "facade", "proxy"],
"homepage": "https://github.com/paquettg/leaguewrap",
"license": "MIT",
"authors": [
{
"name": "Gilles Paquette",
"email": "paquettg#gmail.com",
"homepage": "http://gillespaquette.ca"
}
],
"require": {
"php": ">=5.4",
"guzzlehttp/guzzle": "4.0.*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"mockery/mockery": "0.8.*",
"satooshi/php-coveralls": "0.6.*"
},
"autoload": {
"psr-0": {
"LeagueWrap": "src/"
}
},
"minimum-stability": "dev"
}
and here folders tree
Your script cannot find the autoload file.
Including that file is like any other file: The path must be right, and which path is right depends on whether you are using an absolute path (unusual) or a relative path. Currently you are using a relative path.
Assuming your script is somewhere inside that src folder you showed, the path to the vendor/autoload.php must at least contain one ../ to go one directory level up - maybe more.
Seems like its the problem with your Composer Installation.
Follow these steps -
First make sure you have the Composer.json file created with all the dependencies.
Let's say you are creating a project, and you need a library that does logging. You decide to use monolog. In order to add it to your project, all you need to do is create a composer.json file which describes the project's dependencies.
{
"require": {
"monolog/monolog": "1.2.*"
}
}
Installation - Linux / Unix / OSX
Downloading the Composer Executable
There are in short, two ways to install Composer. Locally as part of your project, or globally as a system wide executable.
Locally
Installing Composer locally is a matter of just running the installer in your project directory:
curl -sS https://getcomposer.org/installer | php
Note: If the above fails for some reason, you can download the installer with php instead:
php -r "readfile('https://getcomposer.org/installer');" | php
The installer will just check a few PHP settings and then download composer.phar to your working directory. This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP which can be run on the command line, amongst other things.
You can install Composer to a specific directory by using the --install-dir option and providing a target directory (it can be an absolute or relative path):
curl -sS https://getcomposer.org/installer | php -- --install-dir=bin
Globally
You can place this file anywhere you wish. If you put it in your PATH, you can access it globally. On unixy systems you can even make it executable and invoke it without php.
You can run these commands to easily access composer from anywhere on your system:
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
Note: If the above fails due to permissions, run the mv line again with sudo.
Note: In OSX Yosemite the /usr directory does not exist by default. If you receive the error "/usr/local/bin/composer: No such file or directory" then you must create /usr/local/bin/ manually before proceeding.
Then, just run composer in order to run Composer instead of php composer.phar.
Installation - Windows
Using the Installer
This is the easiest way to get Composer set up on your machine.
Download and run Composer-Setup.exe, it will install the latest Composer version and set up your PATH so that you can just call composer from any directory in your command line.
Note: Close your current terminal. Test usage with a new terminal: That is
important since the PATH only gets loaded when the terminal starts.
Manual Installation#
Change to a directory on your PATH and run the install snippet to download composer.phar:
C:\Users\username>cd C:\bin
C:\bin>php -r "readfile('https://getcomposer.org/installer');" | php
Note: If the above fails due to readfile, use the http url or enable php_openssl.dll in php.ini
Create a new composer.bat file alongside composer.phar:
C:\bin>echo #php "%~dp0composer.phar" %*>composer.bat
Close your current terminal. Test usage with a new terminal:
C:\Users\username>composer -V
Composer version 27d8904
Using Composer
We will now use Composer to install the dependencies of the project.
To resolve and download dependencies, run the install command:
php composer.phar install
If you did a global install and do not have the phar in that directory run this instead:
composer install
Following the example above, this will download monolog into
the vendor/monolog/monolog directory.
Autoloading
Besides downloading the library, Composer also prepares an autoload file that's capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code's bootstrap process:
require 'vendor/autoload.php';
You can also try with below require form -
defined('__ROOT__') or define('__ROOT__', dirname(dirname(__FILE__)));
require_once implode(
    DIRECTORY_SEPARATOR, array(__ROOT__, 'vendor', 'autoload.php')
);

Composer classmap autoload does not load new files in folder

The following problem: I have defined a classmap in my composer.json:
"autoload": {
"classmap": [
"app/controllers",
"app/models",
"app/helper.php"
]
}
However, when I create a new file in the "controllers" or "models" folder, it will not load them and I always have to make a composer dump-autoload.
Is this the correct behavior? I thought the autoloader from composer monitors the folder for new files then?
Yes, this is correct behaviour. If you want new classes to be loaded automatically, you have to use either PSR-0 or PSR-4 autoloading.
Generating the classmap requires Composer to know the filename that contains a certain class. This can only be done by parsing the whole source code in the directory and scanning for classes, interfaces and trait definitions.
This usually is a CPU and I/O intensive task, so it is only done when Composer does install/update or (on demand) dumps the autoloader, it is not done with every require "vendor/autoload.php";.
Note that the classmap autoloading is simply there for old legacy codebases that didn't implement at least PSR-0. It is not intended for new code - unless you want to pay the price to dump the autoloader again and again during development.
Go to the root of your server by SSH. Now do the following:
Run ls to list all the files.
You will see composer.lock file; remove the file with rm composer.lock command.
Now run php composer update command.
Depending on your linux type you may have to run php-cli composer update.
Step 3 will create a new composer.lock file and all your classes will be loaded again. Do this anytime you add new classes.
or:
Run composer dump-autoload command.
As already pointed out this is correct behavior. If you want new classes to be loaded automatically, you have to use either PSR-0 or PSR-4 autoloading.
The classmap autoload type specified is composer.json is mainly used by legacy projects that do not follow PSR-0 or PSR-4. I have recently started working on such a project and wanted to try to automatically run the composer dump-autoload command when a new class is created. This is actually tricky without including all of the composer source inside the project. I came up with this just to remind the developer they need to dump the classmap:
$loader = include_once 'vendor/autoload.php';
if ( ! $loader ) {
throw new Exception( 'vendor/autoload.php missing please run `composer install`' );
}
spl_autoload_register(
function ( $class ) {
if ( 'A_Common_Class_Prefix' === substr( $class, 0, 10 ) ) {
throw new Error( 'Class "' . $class . '"" not found please run `composer dump-autoload`' );
}
},
true
);
This registers another autoloader which is run after composer's autoloader so any classes composer did not find would be passed to it. If the class matches a prefix an exception is throw reminding the developer to re-dump the autoloader and update the classmap.
For me, it somehow did not work too with Yii 1 class-map, when I added - required it along with many other libraries present - I don't remember exactly perhaps I manually edited the file or file permissions were to blame, it was not regenerated for some reason, even when I removed the composer.lock and erased completely the vendor folder - perhaps some cache, as far as I remember, but effectively what helped was to install firstly isolatedly only this single library, it generated class-map, then I added all the other remaining libraries separately at once at second step, viola, everything loadable.

Laravel config subfolder not recognized on new server

I have some config files of US state counties, for example:
config/
state/
alabama/
counties.php
alaska/
counties.php
...
And on my development localhost server, I call them with:
Config::get('state/alabama/counties');
and it works, it loads an array of counties which I display somewhere.
But when I deploy this app on the Heroku, they won't show up.
I was thinking that it maybe has a problem with treating the config/state/subfolder as an environment config?
I do have a config/staging subfolder, in which I have new DB config stuff, and it works with no problem,
but on the Heroku app, the states just won't show up.
Have you added this directory to your autoload mapping in composer.json and then run composer dump-autoload? Without this, Laravel doesn't know to load your new files in to consideration.
Composer is a CLI-run file that exists in the Root of each laravel installation. composer.json is the config file for Composer - it uses this to manage your dependencies.
There's a section in composer.json called "autoload." These are the files that will automatically be loaded each time the app boots. Mine looks like this:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/core"
]
For each folder that didn't exist before and that I wanted Laravel to understand, I added an entry here. Then, I ran composer dump-autoload and Laravel "understood" where the files I wanted to use were.
Each time you add a file, class, repository - anything that you want Laravel to automatically use, you'll have to run composer dump-autoload.
P.S: If composer dump-autoload doesn't work, try composer.phar dump-autoload.

Resources