Instruct composer to use different class when ambiguous - php

When performing a composer update, I received the following warning:
Warning: Ambiguous class resolution, "Normalizer" was found in both "/var/www/concrete5/vendor/patchwork/utf8/src/Normalizer.php" and "/var/www/concrete5/vendor/voku/portable-utf8/src/Normalizer.php", the first will be used.
The site is now experiencing errors which I think might be related.
How can I instruct composer to use the second file (i.e. /var/www/concrete5/vendor/voku/portable-utf8/src/Normalizer.php) instead of the first?
Note that I have tried adding the following to exclude-from-classmap to composer.json and while it suppresses the warning, doesn't appear to have any impact.
"autoload-dev": {
"psr-4": {
"ConcreteComposer\\" : "./tests"
},
"exclude-from-classmap": [
"vendor/patchwork/utf8/src/Normalizer.php"
]
},

exclude-from-classmap has effect only when classmap is used for autoloading specified class. In your case class is loaded using PSR rules, but you may use optimized autolader, which generates classmap for all classes:
composer install -o
Also, since you placed this rule inside of autoload-dev, it will not have effect when you run composer install with --no-dev flag.

Related

Changing vendor directory can't find files anymore using Symfony 3.4

I'm trying to change the directory of my dependencies on a Symfony 3.4 application.
I need that because I'm working on macOS with Docker and I'd rather have them not shared with the host since the file synchronization is too slow.
The related documentation, says:
The change in the composer.json will look like this:
{
"config": {
"bin-dir": "bin",
"vendor-dir": "/some/dir/vendor"
},
}
That I did
Then, update the path to the autoload.php file in app/autoload.php:
// app/autoload.php
// ...
$loader = require '/some/dir/vendor/autoload.php';
I don't have any autoload.php file in my app directory.
Am I missing something in the doc ?
The application generates the following fatal error:
Warning: require(/some/dir/vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php): failed to open stream: No such file or directory in /vendor/composer/autoload_real.php on line 66
Fatal error: require(): Failed opening required '/some/dir/vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php' (include_path='.:/usr/local/lib/php') in /vendor/composer/autoload_real.php on line 66
I originally created the application with:
$ composer create-project symfony/framework-standard-edition test "3.*"
Open your composer.json file in editor.
Look for "autoload-dev" section
Remove whole "files" part (if exist)
Save file
Run composer install once again
Enjoy the party.
Sample code:
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
},
"files": [
"vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
]
},
In Symfony 3.4, the file app/autoload.php is removed so you should:
replace old vendor path by the new vendor path directly in web/app.php, web/app_dev.php, bin/console and var/SymfonyRequirements.php files
Rerun the command $ composer install
I had the same issue and it was resolved doing the next.
Follow these 3 steps
1. First of all, modify composer.json to use the new vendor path:
"config": {
...,
"vendor-dir": "/app-vendor"
},
And remove the next line:
"files": ["vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"]
2. Secondly, if you are using docker-compose add a new volume where you'll put your vendors.
volumes:
...
- /app-vendor
PD: /app-vendor is a mounted volume which is now empty directory.
3. Lastly, write require '/app-vendor/autoload.php'; to:
my_project_name/bin/console
my_project_name/web/app.php
my_project_name/web/app_dev.php
PD1: Simply, this line is pointing to the new vendor path.
PD2: It's not necessary to modify any other file (like var/SymfonyRequirements.php as I could read).
Check your changes
Once the changes are ready, remove vendor/ and also remove the containers to avoid future problems.
Start your new containers and execute composer install. Now, /vendor will be /app-vendor, it won't be in the root folder of the project anymore.
For more details, I'd recommend you to go to my docker-symfony repository and check the commits. You'll see a benchmark progression and another tips like cached volumes and non-shared /cache && /logs folders.
All for Symfony 3.4.

Composer won't update due to changes on the current branch

We have composer as our dependency injection framework which will pull in a library we created, foobar, which works fine. The library foobar has 14 version v1.1.1 -> v1.1.14. All the way up to .12 composer updated the app fine. But now we are getting this error:
Update failed (Source directory /home/username/dev/git/appname/vendor/foorbar/library has unpushed changes on the current branch:
Branch v1.1.14 could not be found on the origin remote and appears to be unpushed)
The composer.json:
{
"name": "App",
"description": "Foo Bar",
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.2.",
"foobarzf2lib/library": "v1.1."
},
"minimum-stability": "stable",
"repositories": [
{
"type": "package",
"package": {
"name": "foobarzf2lib/library",
"version": "v1.1.14",
"source": {
"url": "https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/foobarzf2lib",
"type": "git",
"reference": "test"
},
"autoload": {
"psr-4": {
"FooBar\\" : "FooBar/"
}
}
}
}
]
}
More things to know:
Vendor is ignored in git.
Tried doing $ composer.phar clearcache.
Would be ideal to not have to delete the library every time.
Remove vendor folder and reinstall packages.
Branch v1.1.14 could not be found on the origin remote and appears to be unpushed)
This means that wherever you're pulling the package from does not have a v1.1.14.
Make sure you push v1.1.14 to the package provider and everything should be fine.
Composer treats the local repository (the one inside the vendor folder) as leading. Whenever composer thinks it is not connected any longer (or diverged) for the checkout in place, it refuses to update (which I think is good and sane).
This may seem cumbersome but that is perhaps the price to pay for the ease of use. Composer is actually managing that git checkout while taking care of updates (it has various remotes configured and then a cache, all without additional setup).
In my case I can see similar errors (not the exact error message as asked about, I get a notification one or some files were changed without any direct changes when the remote did change non-fast-forward) and I resolved it the following way (perhaps it works in case of a missing branch, too? Nevertheless reviewing the issue with a git command similar to the following might shed better insights):
git -C <path> pull && composer update
Could work then, where <path> is the directory path given in composers' error message. If it does not, a more specific error message from git hopefully says more.
Point in case is that you need to resolve the version conflict before composer continues. That is similar when you have a merge conflict in git, you can't do the next commit without resolving it as otherwise you'd have files in the repository with merge conflict markers (and won't compile).
In case of interest, one aspect of my git configuration is pull.rebase=merges. That is in principle to do a rebase on pull (IIRC perhaps preserving some merges).
As an alternative - and similar to removing the whole vendor folder - removing just the reported path should work. As this is git, that repository is gone and as composer was concerned of it, it is not any longer:
rm -rf -- <path> && composer update
where <path> is (must be) the path in the composer error message. Take care with rm -rf, it can be easily over-reaching - just fyi.
(this is similar to the reported resolution in composers issue tracker)

First Composer Package - autoloader not working

I did my best to find a question/answer that applied, but I don't think I understand enough about the autoloader to recognize a suitable answer.
I have a package with the following composer.json:
{
"name": "Pva_agent",
"type":"library",
"description" : "query the pva agent",
"version":"0.1b",
"authors" : [
{
"name":"Ed Greenberg",
"email":"ed#precisionpros.com"
}
],
"minimum-stability":"dev",
"require": {},
"autoload": {
"psr-0": {
"Pva_agent": "."
}
}
}
My directory structure after composer installation of the package:
.
./vendor
./vendor/autoload.php
./vendor/Pva_agent
./vendor/Pva_agent/Agent.php
./vendor/Pva_agent/composer.json
./vendor/Pva_agent/.gitignore
./vendor/composer
./vendor/composer/autoload_psr4.php
./vendor/composer/autoload_real.php
./vendor/composer/autoload_classmap.php
./vendor/composer/autoload_namespaces.php
./vendor/composer/installed.json
./vendor/composer/autoload_static.php
./vendor/composer/ClassLoader.php
./vendor/composer/LICENSE
./composer.lock
./composer.json
./test_pva_agent.php
My test program:
<?php
require_once('vendor/autoload.php');
use Pva_agent\Agent;
$agent = new Agent();
My result:
edg#arthur pva_project $ php test_pva_agent.php
PHP Fatal error: Class 'Pva_agent\Agent' not found in /home/edg/PhpstormProjects/pva_project/test_pva_agent.php on line 6
PHP Stack trace:
PHP 1. {main}() /home/edg/PhpstormProjects/pva_project/test_pva_agent.php:0
edg#arthur pva_project $
I didn't think I needed the 'use' statement, since the autoloader should find the class, right?
Can somebody tell me where the problem lies?
Thanks,
Ed Greenberg
Your Pva_agent library should not sit in the vendor/ directory. This directory should contain only auto-generated data from Composer. This directory is usually not stored in VCS.
You should consider refactoring your directory structure to something similar to this one:
.
|____composer.json
|____composer.lock
|____src
| |____Pva_agent
|____vendor
Your library functionality should be added to src/Pva_agent directory.
Consider to use PSR-4 instead of PSR-0 for autoload functionality, as there is no need to regenerate the autoloader when you add classes. dump-autoloader has to be run in case of PSR-0 after adding classed.
For the directory structure above and the PSR-4 autoloader your composer.json autoload section should look similar to this one:
"autoload": {
"psr-4": { "Pva_agent\\": "src/Pva_agent" }
}
Your library should be auto loaded after this. Your auto-loaded library will be registered under the Pva_agent namespace.

How do I require a file or class in a vendor library in Yii2 that is not already included?

I have this class that Yii2 cannot see. All the other classes work. I tried the following. The commented lines did not work.
// use app\vendor\googleads\googleads-php-lib\src\Google\Api\Ads\Common\Util\ErrorUtils;
// require_once UTIL_PATH . '/ErrorUtils.php';
require_once('../vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util/ErrorUtils.php');
use \ErrorUtils;
This works, but it doesn't look right. Also it doesn't work in the command mode, which I need.
$ yii cron
PHP Warning: Uncaught exception 'yii\base\ErrorException' with message 'require_once(../vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util/ErrorUtils.php): failed to open stream: No such file or directory' in /cygdrive/c/Users/Chloe/workspace/xxx/models/GoogleAdWords.php:36
How can I require or use this class in Yii2?
Fisrt addto composer (shell command):
$ composer require googleads/googleads-php-lib
Then simply use te class:
\ErrorUtils::GetApiErrors($var);
Note that googleads don't use namespaces so it's "\" NS
The library that you use doesn't provide psr-4 autoloader settings for its classes. You need to add autoload classmap for classes that you want to load, into your composer.json in project's root directory like following:
"autoload": {
"classmap": [
"vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Lib",
"vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util"
]
},
and then in your console: composer dump-autoload
This will update the composer autoloader. After that you'll be able to call library classes with: \ErrorUtils::GetSourceOperationIndex($error)

Load git repo with composer - autoloading issue

I have a github repository https://github.com/KoulSlou/UPS and I would like to add it to my project.
In project root I created composer.json file and defined the following autoloading properties:
{
"autoload": {
"files": [
"libraries/Ups/Ups.php",
"libraries/Ups/Ups_Base.php",
"libraries/Ups/Ups_Base_Response.php",
"libraries/Ups/Ups_Live_Rates.php"
]
}
}
When I run
php composer.phar install
repository is being downloaded, but it looks like autoloader is not working. When I try to initialize one of the classes
$test = new Ups()
I got the following error:
Fatal error: Class 'Ups' not found in application/....
Did I define "autoload" property incorrectly?
I'd suggest not using the "files" autoloader, because that isn't very automatic - the files mentioned here are ALWAYS included. Replacing it with "classmap" would be better. And then you'd not be required to mention ALL files, but you can simply state the directory you want to have scanned for classes.
Now what I don't see anywhere: Did you initialize Composer's autoloader anywhere? This usually is something like
require "vendor/autoload.php";
Finaly, I have found out what was the problem. composer.json file in the project I was trying to load - UPS library -was invalid. I was able to download files when I ran:
composer.phar install
but it looks like composer.json file was ignored. I found it out when I ran
composer.phar update
and got
No valid composer.json was found
With option -v I got error that "name" is undefined index. So, I simply added "name" field to the composer.json. Final version is:
{
"name":"KoulSlou/UPS",
"autoload": {
"files": [
"libraries/Ups/Ups.php",
"libraries/Ups/Ups_Base.php",
"libraries/Ups/Ups_Base_Response.php",
"libraries/Ups/Ups_Live_Rates.php"
]
}
}

Categories