Composer autoload path not working - composer-php

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')
);

Related

Laravel Nova tool wouldn't update if symlink is set to true on composer.json

So I am running into a weird issue. I used Laravel Nova (2) command to generate a tool. It sits at ./nova-components/CustomNovaDashboard. In order for the deployment to work on Laravel Vapor, I had to add the below to my parent composer.json.
{
"type": "path",
"url": "./nova-components/CustomNovaDashboard",
"options": {
"symlink": false
}
}
This above allows the code to get deployed, because the absence of symlink in options would otherwise throw the following error:
include(/tmp/vendor/composer/../acme/custom-nova-dashboard/src/ToolServiceProvider.php): failed to open stream: No such file or directory
But the problem now is that when I run npm run watch inside ./nova-components/CustomNovaDashboard, the code in development never updates, because somehow there is a copy of the code that sits in vendor/acme/custom-nova-dashboard that doesn't pick up the changes.
How can I solve this?
I found a solution, it was quite simple.
In my vapor.yml, I had to add COMPOSER_MIRROR_PATH_REPOS=1 before composer install.
build:
- 'COMPOSER_MIRROR_PATH_REPOS=1 composer install'
- 'php artisan event:cache'
- 'npm ci && npm run dev && rm -rf node_modules'
This ensures the symbolic link generated by nova:tool works on dev and prod similarly.
Just don't forget to set "symlink": true in your composer.json. Or leave it as is originally generated by the nova:tool command.

Platform independent composer post-install-cmd script

Lets say my project has following structure
project\
templates\
web\assets\
composer.json
When running the composer update by default symlink to my project is created in vendor/ directory. I would like to create a custom post-install-cmd script that would create symlink to templates\ and web\assets\ folder in a different location.
Since my team is working on Windows/Mac/Linux I planned in running a simple php commands to make this happen. I can't seem to figure out where to start ...
The thing is, I can't figure out where is my current location when starting a php script? I've tried creating an empty file to see where it creates it, but it doesn't even create it.
This is what I wish to get working
"scripts": {
"post-install-cmd": [
"php -r \"symlink('/vendor/project/templates', '/templates/project');\"",
"php -r \"symlink('/vendor/project/web/assets', '/web/project/assets');\""
]
}
You should probably remove leading slashes from paths - at least on unix it will be interpreted as absolute path and definitely will not point to your project.
"scripts": {
"post-install-cmd": [
"php -r \"symlink('vendor/project/templates', 'templates/project');\"",
"php -r \"symlink('vendor/project/web/assets', 'web/project/assets');\""
]
}
If you want a bulletproof solution, you should probably create PHP helper class and use __DIR__ lub composer API to define correct paths. See examples in documentation.

Errors in the autoloaded HTTP/Request2 code, how to troubleshoot composer?

I have two different systems with a git repository cloned on them.
the project uses composer to install various dependencies.
One of these is pear/HTTP_Request2 which requires Net/URL2.
Both systems are Windows with a xampp development enviroment. So both run an Apache2 with a PHP 7.1.* installation.
On one of them everything works perfectly after installing via composer install. But the other one always Errors in the autoloaded HTTP/Request2 code:
<b>Warning</b>: require_once(Net/URL2.php): failed to open stream: No such file or directory in <b>C:\xampp\htdocs\XXX\vendor\pear\http_request2\HTTP\Request2.php</b> on line <b>25</b><br />
<br />
<b>Fatal error</b>: require_once(): Failed opening required 'Net/URL2.php' (include_path='C:\xampp\htdocs\XXX\vendor/pear/pear_exception;C:\xampp\htdocs\XXX\vendor/pear/http_request2;C:\xampp\php\PEAR') in <b>C:\xampp\htdocs\XXX\vendor\pear\http_request2\HTTP\Request2.php</b> on line <b>25</b><br />
Looking at the code in the Package we find:
if (!class_exists('Net_URL2', true)) {
require_once 'Net/URL2.php';
}
The dependency of NetURL2 was successfully installed, judging by composers output.
None the less I tried to fix it by also requiring the dependency from HTTP/Request2 "pear/net_url2" : "^2.2.0", in the Project after i heard from a bug (long closed but still) about problems with that (Issue#composer git), which didn't result in a change.
Judging by the inspected code I also assume this question is outdated.
Now I'm stuck not knowing what to do next... Help?
Edit:
My composer.json essentially looks like this if anyone was wondering:
{
"require":
{
"php":">=7.1.4",
"pear/http_request2": "v2.3.0",
"ext-json":"1.5.0",
"ext-PDO":"7.*",
"ext-pdo_mysql":"7.*",
"ext-mbstring":"7.*",
"ext-gd":"7.*"
},
"autoload": {
"files": [
"helper.php",
"settings.php"
],
"classmap": ["./"],
"exclude-from-classmap": ["vendor/"]
}
}
To troubleshoot Composer in general, you can add -vvv flags for verbose information.
Secondly, when you've XDebug extension for PHP installed, you will see a Call Stack each time when a Fatal error happens.
Note: Run phpenmod -s cli xdebug to enable XDebug in CLI mode.
The fatal error happens in pear/http_request2 package (as per vendor/pear/http_request2/HTTP/Request2.php) when it tries to include Net/URL2.php file (part of pear/net_url2, full path: vendor/pear/net_url2/Net/URL2.php).
This path should normally be added by Composer into include_paths.php like:
vendor/composer/include_paths.php: $vendorDir . '/pear/net_url2',
which failed to do so. To generate autoload files again, run this command:
composer dump-autoload -o
and check whether the autoload files under vendor/composer has been generated as expected.
To test that out, I've run this PHP command and it worked:
php -r 'require_once "vendor/autoload.php"; require_once "vendor/pear/http_request2/HTTP/Request2.php";'
Note: To see the failing scenario, remove include of autoload.php.

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

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.

Composer fails executing app/console cache:clear on symfony 3

I cant run composer install because it fails when executing the post-install-cmd-scripts.
The reason is Symfony 3 moved the console app from the app folder to a bin folder.
I have tried composer clear-cache and composer self-update, so I can verify that I'm running the latest version of composer without any luck... Can anyone help me?
Results from composer diagnose
composer diagnose
Checking composer.json: FAIL
Defining autoload.psr-4 with an empty namespace prefix is a bad idea for performance
require.symfony/symfony : unbound version constraints (dev-master) should be avoided
Checking platform settings: OK
Checking git settings: OK
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com rate limit: OK
Checking disk free space: OK
Checking composer version: OK
Edit:
Added my composer.json file: Pastie
Extra's part:
"extra": {
"symfony-app-dir": "app",
"symfony-bin-dir": "bin",
"symfony-var-dir": "var",
"symfony-web-dir": "web",
"symfony-tests-dir": "tests",
"symfony-assets-install": "relative",
"incenteev-parameters": {
"file": "app/config/parameters.yml"
}
}
Error from Composer:
Could not open input file: app/console
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command:
Could not open input file: app/console
.
Manuall test of bin/composer cache:clear:
bin/console cache:clear
// Clearing the cache for the dev environment with debug true
[OK] Cache for the "dev" environment (debug=true) was successfully cleared.
The problem repo: https://bitbucket.org/Richardh87/messenger
Had the same problem. Found that var directory was missing.
Adding var directory to project root dir solved this for me. It looks like symfony 3 tries to use old project structure if var folder is missing. Hope this can help someone.
try
sudo rm -rf vendor/*
sudo rm -rf composer.lock
then run
composer install
again
Check the extra config key in the composer.json files names as symfony-bin-dir.
So check that the composer contain the correct configuration key, as example:
composer.json
....
"extra": {
"symfony-app-dir": "app",
"symfony-bin-dir": "bin",
"symfony-var-dir": "var",
....
Changing the gitignore worked for me, like this:
# Cache and logs (Symfony3)
/var/cache/*
/var/logs/*
!var/cache/.gitkeep
!var/logs/.gitkeep
# Parameters
/app/config/parameters.yml
/app/config/parameters.ini
# Managed by Composer
/app/bootstrap.php.cache
/var/bootstrap.php.cache
/bin/*
!bin/console
!bin/symfony_requirements
/vendor/
# Assets and user uploads
/web/bundles/
/web/uploads/
# Assets managed by Bower
/web/assets/vendor/
# PHPUnit
/app/phpunit.xml
/phpunit.xml
# Build data
/build/
# Composer PHAR
/composer.phar

Resources