humbug/php-scoper
PHP-Scoper prefixes your project and its dependencies into a unique namespace to avoid conflicts, especially when building PHARs that bundle vendor code and run alongside other PHP projects with overlapping packages or versions.
php-scoper is a critical fit for Laravel-based PHAR deployments (e.g., CLI tools, standalone executables, or microservices). It resolves namespace collisions between bundled dependencies and host environments by prefixing all namespaces, ensuring compatibility with realpath() and other PHAR-specific edge cases.box-project/box require manual scoping configuration, whereas php-scoper automates it.composer.json (supports --working-dir for scoped builds).post-autoload-dump).AppServiceProvider with aliases.Phar::extractTo() for debugging scoped code.eval(), create_function()). Mitigation: Exclude symbols via config.--no-plugins.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Namespace Collisions | High | Validate with composer validate post-scoping. |
| Broken Autoloading | High | Always run composer dump-autoload --classmap-authoritative in build/. |
| PHAR Path Issues | Medium | Patch realpath() calls or use Phar::convertToRelativePath(). |
| Dependency Versioning | Medium | Pin composer.lock to avoid runtime conflicts. |
| Laravel-Specific Bugs | Low | Test with Laravel’s Illuminate\Support\Manager (e.g., cache, queue). |
composer.lock or floating versions?php-scoper step to Laravel’s deploy.php (Deployer).php-scoper inspect vs. PHAR extraction).app()->make() with unresolved interfaces)?Psr4 with non-standard paths)?php-scoper is agnostic to the PHAR tool.php-scoper inspect on critical files to identify excluded/exposed symbols.wp_* functions if using WordPress plugins.composer install --no-dev --prefer-dist
php-scoper add-prefix --output-dir=build --prefix=LaravelScoped
composer dump-autoload --working-dir=build
./build/vendor/bin/laravel (if CLI-based).composer.json scripts:
"scripts": {
"post-autoload-dump": [
"@php-scoper",
"php-scoper add-prefix --output-dir=build --prefix=LaravelScoped --force"
],
"php-scoper": "composer dump-autoload --working-dir=build --classmap-authoritative"
}
- name: Scope and Build PHAR
run: |
composer install --no-dev
php-scoper add-prefix --output-dir=build
box compile --debug
composer.lock.Class 'LaravelScoped\Illuminate\Support\Facades\Log' not found).| Component | Compatibility Notes |
|---|---|
| Laravel Facades | Requires exposing Illuminate\Support\Facades\* or patching app() calls. |
| Blade Templates | No impact (static files copied as-is). |
| Queue Workers | Test with php artisan queue:work in scoped environment. |
| Horizon | May need patchers for realpath() in Laravel Horizon’s process management. |
| Laravel Mix | No impact (frontend assets untouched). |
| Custom Providers | Ensure register() methods use fully qualified names (e.g., LaravelScoped\App\Providers\...). |
composer install --no-dev to exclude dev dependencies..php-scoper.php for:
['wp_*']).['Illuminate\Support\Facades']).php-scoper add-prefix with --force in CI.build/ directory../build/vendor/bin/laravel.Phar::create() with scoped files..php-scoper.php may fall out of sync with dependency changes.php-scoper releases may introduce breaking changes (e.g., PHP 8.1+ features).composer.json or use PHAR install.php-scoper inspect to debug individual files.Phar::extractTo() and compare against build/ directory.composer dump-autoload --optimize output.use statement or excluded symbol.call_user_func()) may need patchers.Phar::canBeOverwritten() is handled.| Issue Type | Resolution Path |
|---|
How can I help you explore Laravel packages today?