PHPackages                             plesk/php-scoper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. plesk/php-scoper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

plesk/php-scoper
================

Prefixes all PHP namespaces in a file or directory.

0.15.0(5y ago)015MITPHPPHP ^7.3 || ^8.0

Since Jun 17Pushed 4y agoCompare

[ Source](https://github.com/plesk/php-scoper)[ Packagist](https://packagist.org/packages/plesk/php-scoper)[ GitHub Sponsors](https://github.com/theofidry)[ RSS](/packages/plesk-php-scoper/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (10)Versions (43)Used By (0)

PHP-Scoper
==========

[](#php-scoper)

[![Package version](https://camo.githubusercontent.com/80893499ff0bcf99a07498b0d723dbb59987c7a619d3825f420434336f057ff4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68756d6275672f7068702d73636f7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/humbug/php-scoper)[![Build Status](https://github.com/humbug/php-scoper/workflows/Build/badge.svg)](https://github.com/humbug/php-scoper/actions)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/8f25b4f517a4db4b9a3067609c139a9c5809f51b00fe6cc8c8cd3bf11eafe8e3/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f68756d6275672f7068702d73636f7065722e7376673f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/humbug/php-scoper/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/25a396f5b6acc1e4b55bb843d6ab2a9a7901dcad98818d8276099107268c63a6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f68756d6275672f7068702d73636f7065722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/humbug/php-scoper/?branch=master)[![Slack](https://camo.githubusercontent.com/2f6358cd8cd3927356f9a75fc3b8fd8e83ce5bbad7f9efa4879e54f29780ebfb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736c61636b2d25323368756d6275672d7265642e7376673f7374796c653d666c61742d737175617265)](https://symfony.com/slack-invite)[![License](https://camo.githubusercontent.com/10e85a5778fe7601504a17ecd18dfa7097f473186b0f947bc10db2d3e4f530e4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d7265642e7376673f7374796c653d666c61742d737175617265)](LICENSE)

PHP-Scoper is a tool which essentially moves any body of code, including all dependencies such as vendor directories, to a new and distinct namespace.

Goal
----

[](#goal)

PHP-Scoper's goal is to make sure that all code for a project lies in a distinct PHP namespace. This is necessary, for example, when building PHARs that:

- Bundle their own vendor dependencies; and
- Load/execute code from arbitrary PHP projects with similar dependencies

When a package (of possibly different versions) exists, and is found in both a PHAR and the executed code, the one from the PHAR will be used. This means these PHARs run the risk of raising conflicts between their bundled vendors and the vendors of the project they are interacting with, leading to issues that are potentially very difficult to debug due to dissimilar or unsupported package versions.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
    - [Phive](#phive)
    - [PHAR](#phar)
    - [Composer](#composer)
- [Usage](#usage)
- [Configuration](#configuration)
    - [Prefix](#prefix)
    - [Finders and paths](#finders-and-paths)
    - [Patchers](#patchers)
    - [Whitelisted files](#whitelisted-files)
    - [Excluded Symbols](#excluded-symbols)
    - [Whitelist](#whitelist)
        - [Constants &amp; functions from the global namespace](#constants--classes--functions-from-the-global-namespace)
        - [Symbols](#symbols)
            - [Caveats](#caveats)
            - [Implementation insights](#implementation-insights)
                - [Class whitelisting](#class-whitelisting)
                - [Constants whitelisting](#constants-whitelisting)
                - [Functions whitelisting](#functions-whitelisting)
        - [Namespaces whitelisting](#namespaces-whitelisting)
- [Building a scoped PHAR](#building-a-scoped-phar)
    - [With Box](#with-box)
    - [Without Box](#without-box)
        - [Step 1: Configure build location and prep vendors](#step-1-configure-build-location-and-prep-vendors)
        - [Step 2: Run PHP-Scoper](#step-2-run-php-scoper)
- [Recommendations](#recommendations)
- [Limitations](#limitations)
    - [Dynamic symbols](#dynamic-symbols)
    - [Date symbols](#date-symbols)
    - [Heredoc values](#heredoc-values)
    - [Callables](#callables)
    - [String values](#string-values)
    - [Native functions and constants shadowing](#native-functions-and-constants-shadowing)
    - [Grouped constants whitelisting](#grouped-constants-whitelisting)
    - [Composer](#composer-autoloader)
    - [Composer Plugins](#composer-plugins)
    - [PSR-0 Partial support](#psr-0-partial-support)
    - [WordPress support](#wordpress)
- [Contributing](#contributing)
- [Credits](#credits)

Installation
------------

[](#installation)

### Phive

[](#phive)

You can install PHP-Scoper with [Phive](https://github.com/phar-io/phive)

```
$ phive install humbug/php-scoper --force-accept-unsigned
```

To upgrade `php-scoper` use the following command:

```
$ phive update humbug/php-scoper --force-accept-unsigned
```

### PHAR

[](#phar)

The preferred method of installation is to use the PHP-Scoper PHAR, which can be downloaded from the most recent [Github Release](https://github.com/humbug/php-scoper/releases).

### Composer

[](#composer)

You can install PHP-Scoper with Composer:

```
composer global require humbug/php-scoper
```

If you cannot install it because of a dependency conflict or you prefer to install it for your project, we recommend you to take a look at [bamarni/composer-bin-plugin](https://github.com/bamarni/composer-bin-plugin). Example:

```
composer require --dev bamarni/composer-bin-plugin
composer bin php-scoper config minimum-stability dev
composer bin php-scoper config prefer-stable true
composer bin php-scoper require --dev humbug/php-scoper
```

Keep in mind however that this library is not designed to be extended.

Usage
-----

[](#usage)

```
php-scoper add-prefix
```

This will prefix all relevant namespaces in code found in the current working directory. The prefixed files will be accessible in a `build` folder. You can then use the prefixed code to build your PHAR.

**Warning**: After prefixing the files, if you are relying on Composer for the autoloading, dumping the autoloader again is required.

For a more concrete example, you can take a look at PHP-Scoper's build step in [Makefile](Makefile), especially if you are using Composer as there are steps both before and after running PHP-Scoper to consider.

Refer to TBD for an in-depth look at scoping and building a PHAR taken from PHP-Scoper's makefile.

Configuration
-------------

[](#configuration)

If you need more granular configuration, you can create a `scoper.inc.php` by running the command `php-scoper init`. A different file/location can be passed with a `--config` option.

```
