humbug/php-scoper
PHP-Scoper moves any body of code, including all vendor dependencies, into a new distinct namespace. This prevents conflicts when building PHARs that bundle their own dependencies and interact with other PHP projects.
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). |
| Enum Interface Scoping | Low | Fixed in 0.18.20; pin >= 0.18.20 if the codebase uses enums implementing interfaces (PHP 8.1+). |
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)?BackedEnum/custom contracts)?>= 0.18.20 is now the safe floor.php-scoper >= 0.18.20.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\...). |
| PHP Enums | Interfaces implemented by enums are correctly scoped from 0.18.20 onward. |
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. Minimum recommended floor is 0.18.20 for apps using enums-with-interfaces (bugfix in this release) and to benefit from the stabilised PhpStormStubs constraint.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.>= 0.18.20; pre-fix, interfaces implemented by enums were not correctly prefixed.| Issue Type | Resolution Path |
|---|---|
| Namespace mismatch | Re-run php-scoper add-prefix --force and verify .php-scoper.php exclusions. |
How can I help you explore Laravel packages today?