adachsoft / composer-lib
Composer command library for non-interactive operations
Installs: 11
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Forks: 0
pkg:composer/adachsoft/composer-lib
Requires
- php: >=8.3
- adachsoft/collection: ^3.0.0
- adachsoft/command-executor-lib: ^1.0.2
Requires (Dev)
- adachsoft/php-code-style: ^0.2.1
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^2.1
- phpunit/phpunit: 12.4.*
README
Library providing a high-level, non-interactive wrapper around Composer CLI. It is designed to be used from other PHP applications and tools that need to run Composer commands safely and programmatically.
Features
- Public API (
ComposerClientInterface) for executing Composer operations:installupdaterequire(asrequirePackages)remove(asremovePackages)dump-autoloadvalidateoutdateddiagnoseshowlicensesdependsprohibitsrun-script(asrunScript)clear-cache(asclearCache)
- Non-interactive execution enforced by guards:
NonInteractiveGuard(adds flags and env vars like--no-interaction,CI=1)PromptDetector(detects interactive prompts in output and throws exception)
- Pluggable command execution via
CommandExecutorInterface - DTO-based configuration of all operations (options, execution policy, working directory, env vars)
Installation
Install via Composer:
composer require adachsoft/composer-lib
Basic Usage
Creating a client
The recommended way to get a ready-to-use client is the factory:
use AdachSoft\ComposerLib\PublicApi\Factory\ComposerClientFactory;
$client = ComposerClientFactory::create();
This will create:
- a default command executor based on
adachsoft/command-executor-lib - a default Composer configuration that runs the
composerbinary from yourPATH(no custom locator required) - non-interactive guard and prompt detector
If you need a custom executor (for logging, sandboxing, etc.), you can pass it:
use AdachSoft\ComposerLib\Infrastructure\Execution\CommandExecutorInterface;
use AdachSoft\ComposerLib\PublicApi\Factory\ComposerClientFactory;
/** @var CommandExecutorInterface $executor */
$client = ComposerClientFactory::create($executor);
Running operations
Each operation is configured by its own options DTO. Example for install:
use AdachSoft\ComposerLib\PublicApi\ComposerClientInterface;
use AdachSoft\ComposerLib\PublicApi\Dto\Enum\ConfigPatchModeEnum;
use AdachSoft\ComposerLib\PublicApi\Dto\Options\ConfigPatchPolicyDto;
use AdachSoft\ComposerLib\PublicApi\Dto\Options\ExecutionPolicyDto;
use AdachSoft\ComposerLib\PublicApi\Dto\Options\InstallOptionsDto;
use AdachSoft\ComposerLib\PublicApi\Dto\Value\WorkingDirectory;
/** @var ComposerClientInterface $client */
$workingDirectory = new WorkingDirectory('/path/to/project');
$executionPolicy = new ExecutionPolicyDto(
hardTimeoutSeconds: 60,
activityTimeoutSeconds: 30,
nonInteractiveEnforced: true,
);
$configPatchPolicy = new ConfigPatchPolicyDto(ConfigPatchModeEnum::PERMANENT);
$options = new InstallOptionsDto(
workingDirectory: $workingDirectory,
configPatchPolicy: $configPatchPolicy,
executionPolicy: $executionPolicy,
dev: true,
preferDist: true,
preferStable: true,
);
$result = $client->install($options);
if (!$result->success) {
// handle failure, inspect $result->stderr, $result->exitCode, etc.
}
Example for running a custom script via composer run-script:
use AdachSoft\ComposerLib\PublicApi\Dto\Options\ExecutionPolicyDto;
use AdachSoft\ComposerLib\PublicApi\Dto\Options\RunScriptOptionsDto;
use AdachSoft\ComposerLib\PublicApi\Dto\Value\WorkingDirectory;
$workingDirectory = new WorkingDirectory('/path/to/project');
$executionPolicy = new ExecutionPolicyDto(
hardTimeoutSeconds: 60,
activityTimeoutSeconds: 30,
nonInteractiveEnforced: true,
);
$options = new RunScriptOptionsDto(
workingDirectory: $workingDirectory,
executionPolicy: $executionPolicy,
script: 'my-script',
args: ['--flag', 'value'],
);
$result = $client->runScript($options);
Other operations (update, requirePackages, removePackages, dumpAutoload,
validate, outdated, diagnose, show, licenses, depends, prohibits,
runScript, clearCache) follow the same pattern with their respective
options DTOs.
Non-Interactive Safety
This library is built to prevent accidental interactive Composer runs in CI or automated tools:
NonInteractiveGuardensures flags like--no-interactionand appropriate environment variables are always present.PromptDetectorscans stdout/stderr and throwsNonInteractiveViolationExceptionwhen it detects likely interactive prompts (e.g. "Do you want to continue?", "[y/N]", etc.).
Command Execution Abstraction
The infrastructure layer defines its own CommandExecutorInterface and ships
an adapter for adachsoft/command-executor-lib:
AdachSoft\ComposerLib\Infrastructure\Execution\CommandExecutorInterfaceAdachSoft\ComposerLib\Infrastructure\Execution\AdachsoftCommandExecutorAdapter
Composer processes inherit the base PHP environment (such as HOME and
COMPOSER_HOME) and their environment is extended or overridden by values
coming from the EnvVarMap DTO passed to operations.
You can provide your own implementation of CommandExecutorInterface if you
need to control how processes are spawned, logged or sandboxed.
Testing
The project ships with:
- unit tests for guards, operations, DTO collections, infrastructure
- a functional test that exercises the full public API against a temporary fixture project
To run tests:
vendor/bin/phpunit
To run static analysis:
composer stan
To check or fix code style:
composer cs:check
composer cs:fix
License
This library is released under the MIT License.