Download the PHP package plesk/php-scoper without Composer

On this page you can find all versions of the php package plesk/php-scoper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-scoper

PHP-Scoper

Package version Build Status Scrutinizer Code Quality Code Coverage Slack

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

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:

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

Installation

Phive

You can install PHP-Scoper with Phive

To upgrade php-scoper use the following command:

PHAR

The preferred method of installation is to use the PHP-Scoper PHAR, which can be downloaded from the most recent Github Release.

Composer

You can install PHP-Scoper with Composer:

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. Example:

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

Usage

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, 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

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.

Prefix

The prefix to be used to isolate the code. If null or '' (empty string) is given, then a random prefix will be automatically generated.

Finders and paths

By default when running php-scoper add-prefix, it will prefix all relevant code found in the current working directory. You can however define which files should be scoped by using Finders in the configuration:

Besides the finder, you can also add any path via the command:

Paths added manually are appended to the paths found by the finders.

Patchers

When scoping PHP files, there will be scenarios where some of the code being scoped indirectly references the original namespace. These will include, for example, strings or string manipulations. PHP-Scoper has limited support for prefixing such strings, so you may need to define patchers, one or more callables in a scoper.inc.php configuration file which can be used to replace some of the code being scoped.

Here's a simple example:

You can imagine instantiating a class from a variable which is based on a known namespace, but also on a variable classname which is selected at runtime. Perhaps code similar to:

If we scoped the Humbug namespace to PhpScoperABC\Humbug, then the above snippet would fail as PHP-Scoper cannot interpret the above as being a namespaced class. To complete the scoping successfully, a) the problem must be located and b) the offending line replaced.

The patched code which would resolve this issue might be:

This and similar issues may arise after scoping, and can be debugged by running the scoped code and checking for issues. For this purpose, having a couple of end to end tests to validate post-scoped code or PHARs is recommended.

Applying such a change can be achieved by defining a suitable patcher in scoper.inc.php:

Whitelisted files

For the files listed in files-whitelist, their content will be left untouched during the scoping process.

Excluded Symbols

Symbols can be marked as excluded as follows:

This enriches the list of Symbols PHP-Scoper's Reflector considers as "internal", i.e. PHP engine or extension symbols and that will be left completely untouched.

This feature should be use very carefully as it can easily break the Composer autoloading and is recommended to be used only for compensating missing symbols from the PhpStorm's stubs shipped with PHP-Scoper.

Whitelist

PHP-Scoper's goal is to make sure that all code for a project lies in a distinct PHP namespace. However, you may want to share a common API between the bundled code of your PHAR and the consumer code. For example if you have a PHPUnit PHAR with isolated code, you still want the PHAR to be able to understand the PHPUnit\Framework\TestCase class.

Constants & Classes & functions from the global namespace

By default, PHP-Scoper will prefix the user defined constants, classes and functions belonging to the global namespace. You can however change that setting for them to be prefixed as usual unless explicitly whitelisted:

Symbols

You can whitelist classes, interfaces, constants and functions like so like so:

Caveats

This will not work on traits and this alias mechanism is case insensitive, i.e. when passing 'iter\count', if a class Iter\Count is found, this class will also be whitelisted.

Implementation insights

Class whitelisting

The class aliasing mechanism is done like follows:

It is done this way to ensure prefixed and whitelisted classes can co-exist together without breaking the autoloading. The class_exists() statements are dumped in vendor/scoper-autoload.php, do not forget to include this file in favour of vendor/autoload.php. This part is however sorted out by Box if you are using it with the PhpScoper compactor.

So if you have the following file with a whitelisted class:

The scoped file will look like this:

With:

Constants whitelisting

The constant aliasing mechanism is done by transforming the constant declaration into a define() statement when this is not already the case. Note that there is a difference here since define() defines a constant at runtime whereas const defines it at compile time. You have a more details post regarding the differences here

Give the following file with a whitelisted constant:

The scoped file will look like this:

Functions whitelisting

The function aliasing mechanism is done by declaring the original function as an alias of the prefixed one in the vendor/scoper-autoload.php file.

Given the following file with a function declaration:

The file will be scoped as usual in order to avoid any autoloading issue:

And the following function which will serve as an alias will be declared in the scoper-autoload.php file:

Namespaces whitelisting

If you want to be more generic and whitelist a whole namespace, you can do it so like this:

Now anything under the PHPUnit\Framework namespace will not be prefixed. Note this works as well for the global namespace:

Note that this may lead to autoloading issues. Indeed if you have the following package:

And whitelist the namespace PHPUnit\Framework\*, then the autoloading for this package will be faulty and will not work. For this to work, the whole package PHPUnit\* would need to be whitelisted.

Warning: the following is not a namespace whitelist:

Building a Scoped PHAR

With Box

If you are using Box to build your PHAR, you can use the existing PHP-Scoper integration. Box will take care of most of the things for you so you should only have to adjust the PHP-Scoper configuration to your needs.

Without Box

Step 1: Configure build location and prep vendors

Assuming you do not need any development dependencies, run:

This will allow you to save time in the scoping process by not processing unnecessary files.

Step 2: Run PHP-Scoper

PHP-Scoper copies code to a new location during prefixing, leaving your original code untouched. The default location is ./build. You can change the default location using the --output-dir option. By default, it also generates a random prefix string. You can set a specific prefix string using the --prefix option. If automating builds, you can set the --force option to overwrite any code existing in the output directory without being asked to confirm.

Onto the basic command assuming default options from your project's root directory:

As there are no path arguments, the current working directory will be scoped to ./build in its entirety. Of course, actual prefixing is limited to PHP files, or PHP scripts. Other files are copied unchanged, though we also need to scope certain Composer related files.

Speaking of scoping Composer related files... The next step is to dump the Composer autoloader if we depend on it, so everything works as expected:

Recommendations

There is 3 things to manage when dealing with isolated PHARs:

As a result, you should have end-to-end tests for your (at the minimum) your released PHAR.

Since dealing with the 3 issues mentioned above at once can be tedious, it is highly recommended to have several tests for each steps.

For example you can have a test for both your non-isolated PHAR and your isolated PHAR, this way you will know which step is causing an issue. If the isolated PHAR is not working, you can try to test the isolated code directly outside the PHAR to make sure the scoping process is not the issue.

To check if the isolated code is working correctly, you have a number of solutions:

Also take into consideration that bundling code in a PHAR is not guaranteed to work out of the box either. Indeed there is a number of things such as

For this reason, you should also h

Limitations

Dynamic symbols

PHP-Scoper tries to prefix strings as well whenever possible. There will however be cases in which it will not be possible such as:

Date symbols

You code may be using a convention for the date string formats which could be mistaken for classes, e.g.:

In this scenario, PHP-Scoper has no way to tell that string 'Ymd\THis\Z' does not refer to a symbol but is a date format. In this case, you will have to rely on patchers. Note however that PHP-Scoper will be able to handle some cases such as, see the date-spec.

Heredoc values

If you consider the following code:

The content of PHP_HEREDOC will not be prefixed. Some partial support could be added in the future but is bound to be very limited due to the dynamic nature of heredocs. If you consider the following for example:

It would be very hard to properly scope the relevant classes.

Callables

If you consider the two following values:

The classes used there will not be scoped. It should not be impossible to add support for it but it is currently not supported. See #286.

String values

PHP-Scoper tries whenever possible to prefix strings as well:

PHP-Scoper uses a regex to determine if the string is a class name that must be prefixed. But there is bound to have confusing cases. For example:

Native functions and constants shadowing

In the following example:

No use statement is used for the function is_array. This means that PHP will try to load the function \Foo\is_array and if fails to do so will fallback on \is_array (note that PHP does so only for functions and constants: not classes).

In order to bring some performance optimisation, the call will nonetheless be prefixed in \is_array. This will break your code if you were relying on \Foo\is_array instead. This however should be extremely rare, so if that happens you have two solutions: use a patcher or simpler remove any ambiguity by making use of a use statement (which is unneeded outside of the context of prefixing your code):

The situation is exactly the same for constants.

Grouped constants whitelisting

When a grouped constant declaration like the following is given:

PHP-Scoper will not be able to whitelist either X or Y. The statement above should be replaced by multiple constant statements:

Composer Autoloader

PHP-Scoper does not support prefixing the dumped Composer autoloader and autoloading files. This is why you have to manually dump the autoloader again after prefixing an application.

Note: when using Box, Box is able to take care of that step for you.

PHP-Scoper also can not handle Composers static file autoloaders. This is due to Composer loading files based on a hash which is generated from package name and relative file path. For a workaround see #298.

Composer Plugins

Composer plugins are not supported. The issue is that for whitelisting symbols PHP-Scoper relies on the fact that you should load the vendor/scoper-autoload.php file instead of vendor/autoload.php to trigger the loading of the right classes with their class aliases. However Composer does not do that and as a result interfaces such as Composer\Plugin\Capability\Capable are prefixed but the alias is not registered.

This cannot be changed easily so for now when you are using an isolated version of Composer, you will need to use the --no-plugins option.

PSR-0 Partial support

As of now, given the following directory structure:

with the following configuration:

The autoloading will not work. Indeed, PHP-Scoper attempts to support PSR-0 by transforming it to PSR-4, i.e. in the case above:

If this will work for the classes under src/JsonMapper/, it will not for JsonMapper.php.

WordPress

As of now PHP-Scoper does not easily allow to completely leave unchanged non-included third-party code causing some hurdles with Wordpress plugins. There is a standing issue about providing a better integration out of the box (See #303) but alternatively you can already use WP React Starter which is a boilerplate for WordPress plugins.

If you are not able to migrate you can have a look at the solution itself, read more about it here #303 (comment).

Contributing

Contribution Guide

Credits

Project originally created by: Bernhard Schussek (@webmozart) which has now been moved under the Humbug umbrella.


All versions of php-scoper with dependencies

PHP Build Version
Package Version
Requires php Version ^7.3 || ^8.0
composer/package-versions-deprecated Version ^1.8
jetbrains/phpstorm-stubs Version ^2020.2
nikic/php-parser Version ^4.0
symfony/console Version ^3.2 || ^4.0
symfony/filesystem Version ^3.2 || ^4.0
symfony/finder Version ^3.2 || ^4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package plesk/php-scoper contains the following files

Loading the files please wait ....