Commands and Code snippets for working with the PHP package manager Composer.

15.02.2019 - Manuel Wohlers

Switching between Composer versions

# switching between composer version 1 and version 2
composer self-update --1
composer self-update --2 

Disable composer's memory limit

If you get PHP Fatal error: Allowed memory size of 123456789 bytes exhausted (tried to allocate 4096 bytes) in… the following might be the solution.

# set a high memory limit
php -d memory_limit=3G composer update
 # or completely disable php memory limit
php -d memory_limit=-1 composer require xyz/def # or whatever composer command you want.

Note: Depending on your environment you may need to use the full path to php or composer like /usr/local/bin/composer.

Get the root directory of composer vendor packages within PHP script

Bitte beachte, dass du wahrscheinlich deine Software-Architektur überdenken solltest, falls du folgenden Code benötigst. Aber wie immer: es kommt drauf an und es ist grundsätzlich möglich...

// get composer vendor directory
$reflection = new \ReflectionClass(\Composer\Autoload\ClassLoader::class);
$vendorDir = dirname(dirname($reflection->getFileName()));