1111PHP-Scoper is a tool which essentially moves any body of code, including all
1212dependencies such as vendor directories, to a new and distinct namespace.
1313
14+
1415## Goal
1516
1617PHP-Scoper's goal is to make sure that all code for a project lies in a
@@ -25,18 +26,24 @@ PHARs run the risk of raising conflicts between their bundled vendors and the
2526vendors of the project they are interacting with, leading to issues that are
2627potentially very difficult to debug due to dissimilar or unsupported package versions.
2728
29+
2830## Table of Contents
2931
3032- [ Installation] ( #installation )
3133- [ Usage] ( #usage )
32- - [ Patchers] ( #patchers )
33- - [ Global Namespace Whitelisting] ( #global-namespace-whitelisting )
34+ - [ Configuration] ( #configuration )
35+ - [ Finders and paths] ( #finders-and-paths )
36+ - [ Patchers] ( #patchers )
37+ - [ Global Namespace Whitelisting] ( #global-namespace-whitelisting )
38+ - [ Whitelist] ( #whitelist )
3439- [ Contributing] ( #contributing )
3540- [ Building A Scoped PHAR] ( #building-a-scoped-phar )
3641- [ Credits] ( #credits )
3742
43+
3844## Installation
3945
46+
4047### PHAR (preferred)
4148
4249The preferred method of installation is to use the PHP-Scoper PHAR, which can
@@ -52,6 +59,7 @@ As the PHAR is signed, you should also download the matching
5259to ` php-scoper ` , you should also rename ` php-scoper.phar.pubkey ` to
5360` php-scoper.pubkey ` .
5461
62+
5563### Composer
5664
5765You can install PHP-Scoper with Composer:
@@ -69,6 +77,7 @@ composer require --dev bamarni/composer-bin-plugin
6977composer bin php-scoper require --dev humbug/php-scoper
7078```
7179
80+
7281## Usage
7382
7483``` bash
@@ -89,7 +98,75 @@ there are steps both before and after running PHP-Scoper to consider.
8998Refer to TBD for an in-depth look at scoping and building a PHAR taken from
9099PHP-Scoper's makefile.
91100
92- ## Patchers
101+
102+ ## Configuration
103+
104+ By default, PHP-Scoper will look for the file ` scoper.inc.php ` in the current
105+ working directory:
106+
107+ ``` php
108+ <?php declare(strict_types=1);
109+
110+ // scoper.inc.php
111+
112+ use Symfony\Component\Finder\Finder;
113+
114+ return [
115+ 'finders' => [],
116+ 'patchers' => [],
117+ 'global_namespace_whitelist' => [],
118+ 'whitelist' => [],
119+ ];
120+ ```
121+
122+
123+ ### Finders and paths
124+
125+ By default when running ` php-scoper add-prefix ` , it will prefix all relevant
126+ code found in the current working directory. You can however define which
127+ files should be scoped by using [ Finders] [ symfony_finder ] in the configuration:
128+
129+ ``` php
130+ <?php declare(strict_types=1);
131+
132+ // scoper.inc.php
133+
134+ use Symfony\Component\Finder\Finder;
135+
136+ return [
137+ 'finders' => [
138+ Finder::create()->files()->in('src'),
139+ Finder::create()
140+ ->files()
141+ ->ignoreVCS(true)
142+ ->notName('/LICENSE|.*\\.md|.*\\.dist|Makefile|composer\\.json|composer\\.lock/')
143+ ->exclude([
144+ 'doc',
145+ 'test',
146+ 'test_old',
147+ 'tests',
148+ 'Tests',
149+ 'vendor-bin',
150+ ])
151+ ->in('vendor'),
152+ Finder::create()->append([
153+ 'bin/php-scoper',
154+ 'composer.json',
155+ ])
156+ ],
157+ ];
158+ ```
159+
160+ Besides the finder, you can also add any path via the command:
161+
162+ ```
163+ php-scoper add-prefix file1.php bin/file2.php
164+ ```
165+
166+ Paths added manually are appended to the paths found by the finders.
167+
168+
169+ ### Patchers
93170
94171When scoping PHP files, there will be scenarios where some of the code being
95172scoped indirectly references the original namespace. These will include, for
@@ -132,7 +209,7 @@ Applying such a change can be achieved by defining a suitable patcher in
132209` scoper.inc.php ` :
133210
134211``` php
135- <?php declare(strict_types=1)
212+ <?php declare(strict_types=1);
136213
137214// scoper.inc.php
138215
@@ -155,19 +232,19 @@ return [
155232];
156233```
157234
158- ## Global Namespace Whitelisting
235+ ### Global Namespace Whitelisting
159236
160237By default, PHP-Scoper only scopes (or prefixes) code where the namespace is
161238non-global. In other words, non-namespaced code is not scoped. This leaves the
162239majority of classes, functions and constants in PHP, and most extensions,
163240untouched.
164241
165- This is not necessarily a desireable outcome for vendor dependencies which are
242+ This is not necessarily a desirable outcome for vendor dependencies which are
166243also not namespaced. To ensure they are isolated, you can configure PHP-Scoper to
167244allow their prefixing from ` scoper.inc.php ` using basic strings or callables:
168245
169246``` php
170- <?php declare(strict_types=1)
247+ <?php declare(strict_types=1);
171248
172249// scoper.inc.php
173250
@@ -178,24 +255,50 @@ return [
178255 return 'PHPUnit' === substr($className, 0, 6);
179256 },
180257 ],
181- 'patchers' => [
182- // patchers if relevant
183- ],
184258];
185259```
186260
187261In this example, we're ensuring that the ` AppKernal ` class, and any
188262non-namespaced PHPUnit packages are prefixed.
189263
264+
265+ ### Whitelist
266+
267+ PHP-Scoper's goal is to make sure that all code for a project lies in a
268+ distinct PHP namespace. However, you may want to share a common API between
269+ the bundled code of your PHAR and the consumer code. For example if you have
270+ a PHPUnit PHAR with isolated code, you still want the PHAR to be able to
271+ understand the ` PHPUnit\Framework\TestCase ` class.
272+
273+ A way to achieve this is by specifying a list of classes to not prefix:
274+
275+ ``` php
276+ <?php declare(strict_types=1);
277+
278+ // scoper.inc.php
279+
280+ return [
281+ 'whitelist' => [
282+ 'PHPUnit\Framework\TestCase',
283+ ],
284+ ];
285+ ```
286+
287+ Note that only classes are whitelisted, this does not affect constants
288+ or functions.
289+
290+
190291## Contributing
191292
192293[ Contribution Guide] ( CONTRIBUTING.md )
193294
295+
194296## Building A Scoped PHAR
195297
196298This is a brief run through of the basic steps encoded in PHP-Scoper's own
197299[ Makefile] ( Makefile ) and elsewhere to build a PHAR from scoped code.
198300
301+
199302### Step 1: Configure build location and prep vendors
200303
201304If, for example, you are using [ Box] ( box ) to build your PHAR, you
@@ -214,6 +317,7 @@ Assuming you need no dev dependencies, run:
214317composer install --no-dev --prefer-dist
215318```
216319
320+
217321### Step 2: Run PHP-Scoper
218322
219323PHP-Scoper copies code to a new location during prefixing, leaving your original
@@ -242,6 +346,7 @@ Composer autoloader if we depend on it, so everything works as expected:
242346composer dump-autoload -d build --classmap-authoritative
243347```
244348
349+
245350### Step 3: Build, test, and cleanup
246351
247352If using [ Box] ( box ) , you can now move onto actually building the PHAR:
@@ -261,16 +366,18 @@ re-install dev dependencies removed during Step 1:
261366composer install
262367```
263368
369+
264370## Credits
265371
266372Project originally created by: [ Bernhard Schussek] ([ @webmozart ] ) which has
267373now been moved under the
268374[ Humbug umbrella] [ humbug ] .
269375
270376
271- [ Bernhard Schussek ] : https://webmozart.io/
272377[ @webmozart ] : https://twitter.com/webmozart
273- [ humbug ] : https://github.com/humbug
274378[ bamarni/composer-bin-plugin ] : https://github.com/bamarni/composer-bin-plugin
379+ [ Bernhard Schussek ] : https://webmozart.io/
275380[ box ] : https://github.com/box-project/box2
381+ [ humbug ] : https://github.com/humbug
276382[ releases ] : https://github.com/humbug/php-scoper/releases
383+ [ symfony_finder ] : https://symfony.com/doc/current/components/finder.html
0 commit comments