dflydev/embedded-composer-core
Core library for running Composer in embedded contexts. Provides the underlying utilities and runtime pieces used by embedded-composer integrations to programmatically install, update, and manage PHP dependencies from within an application.
Installation Add the package via Composer:
composer require dflydev/embedded-composer-core
Requires PHP 7.4+ and Composer (for embedding its core functionality).
First Use Case Use the package to embed Composer’s core logic in a custom script or application (e.g., a CLI tool, installer, or build system) without requiring Composer as a standalone dependency. Example:
use Dflydev\EmbeddedComposer\Core\Composer;
$composer = new Composer();
$composer->run(['install', '--prefer-dist']);
Where to Look First
src/Composer.php: Entry point for embedding Composer’s core.tests/: Demonstrates usage patterns (e.g., running commands, handling output).README.md: Basic setup and CLI integration hints.Embedding Composer Logic
Use the Composer class to execute Composer commands programmatically:
$composer = new Composer(['--no-interaction']);
$composer->run(['update', 'vendor/package']);
Custom Command Execution Extend functionality by wrapping commands in your application’s workflow:
$composer = new Composer();
$exitCode = $composer->run(['validate']);
if ($exitCode !== 0) {
throw new RuntimeException('Composer validation failed');
}
Integration with Laravel Artisan Create a custom Artisan command to leverage Composer:
use Dflydev\EmbeddedComposer\Core\Composer;
use Illuminate\Console\Command;
class InstallDependencies extends Command {
protected $signature = 'app:install';
public function handle() {
$composer = new Composer();
$composer->run(['install', '--optimize-autoloader']);
}
}
Handling Output/Errors Capture Composer output for logging or user feedback:
$composer = new Composer(['--verbose']);
$output = $composer->run(['show', '-i']);
$this->info($output);
post-install-cmd) via the embedded instance.Environment Assumptions
git, curl) are missing. Validate dependencies explicitly:
if (!file_exists('/usr/bin/git')) {
throw new RuntimeException('Git is required for Composer');
}
Configuration Conflicts
composer.json from the current working directory. Ensure the file exists or provide a custom path:
$composer = new Composer(['--config-file=/path/to/composer.json']);
Memory/Performance
--optimize-autoloader to mitigate:
$composer->run(['dump-autoload', '--optimize']);
Output Handling
STDOUT. Redirect to a file or buffer if needed:
$composer = new Composer(['--no-ansi', '--quiet']);
ob_start();
$composer->run(['show']);
$output = ob_get_clean();
$composer = new Composer(['--verbose']);
$composer->run() return values (non-zero indicates failure).$composer = new Composer(['--repository-url=https://custom.pkg.example']);
Custom Composer Class
Extend Dflydev\EmbeddedComposer\Core\Composer to add domain-specific methods:
class CustomComposer extends Composer {
public function installDependencies(array $packages) {
$this->run(['require'] + $packages);
}
}
Event Listeners
Hook into Composer events (e.g., post-update-cmd) by extending the class and overriding event handlers.
Dependency Isolation
Use the embedded Composer to generate a vendor/ directory in a temporary location, then symlink it into your project.
~/.composer/config.json but may override it with CLI args.$composer = new Composer(['--cache-dir=/custom/cache']);
How can I help you explore Laravel packages today?