|
| 1 | +# Composer Dependency Isolation |
| 2 | + |
| 3 | +**What this plugin does** |
| 4 | + |
| 5 | +This plugin prefixes all vendor namespaces with a chosen value. This |
| 6 | +includes all declared namespaces, use statements, fully qualified |
| 7 | +references, and most string values that reference the namespace. |
| 8 | + |
| 9 | +All vendor code and composer autoload mappings are updated to reference |
| 10 | +the prefixed namespace. |
| 11 | + |
| 12 | +**What this plugin does not do** |
| 13 | + |
| 14 | +It will not touch any code in your project. It only affects code in the |
| 15 | +vendor directory, and it only affects code referencing the affected |
| 16 | +namespaces. You must update all references in your code yourself if you |
| 17 | +apply this to an existing project. |
| 18 | + |
| 19 | +**Why would I want to use this?** |
| 20 | + |
| 21 | +This plugin allows you to run two projects that utilize composer |
| 22 | +dependencies in the same runtime, without worrying about conflicting |
| 23 | +dependencies. The most common example is in a WordPress environment, |
| 24 | +where all plugins execute in the same runtime, and may rely on the same |
| 25 | +composer dependencies. Each project utilizing the plugin can't conflict |
| 26 | +with any other project unless the vendor code is not namespaced (in which |
| 27 | +case there aren't many options...). |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +Using the plugin is straightforward. Install the plugin by requiring it |
| 32 | +in your project: `composer require 0x6d617474/isolate`. |
| 33 | + |
| 34 | +Configure the plugin by adding the following to your `composer.json`: |
| 35 | +``` |
| 36 | +"config" : { |
| 37 | + "isolate": { |
| 38 | + "prefix": "Your\\Prefix\\Here\\", |
| 39 | + "blacklist": [], |
| 40 | + "autorun": false, |
| 41 | + "require-dev": false, |
| 42 | + "replacements" : {} |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +The only required value is the `prefix`. |
| 48 | + |
| 49 | +After you have configured the plugin, run the isolation: |
| 50 | +``` |
| 51 | +composer isolate |
| 52 | +composer dump |
| 53 | +``` |
| 54 | + |
| 55 | +Your vendor code is now prefixed! |
| 56 | + |
| 57 | +Be sure to `dump` after you `isolate`, or your autoload mappings will |
| 58 | +be incorrect! |
| 59 | + |
| 60 | +## Configuration |
| 61 | + |
| 62 | +**prefix** |
| 63 | + |
| 64 | +This is the value that will be prepended to all vendor namespaces. It |
| 65 | +should be a valid namespace, and should be unique to your project. I |
| 66 | +recommend you don't use your main project namespace, or at least add |
| 67 | +`\\Vendor` to the end. |
| 68 | + |
| 69 | +**blacklist** |
| 70 | + |
| 71 | +This is a list of packages you don't want to prefix. Matching packages |
| 72 | +will not be scanned for namespaces, but will still have code rewritten |
| 73 | +if it contains namespaces from other non-blacklisted packages. |
| 74 | + |
| 75 | +**autorun** |
| 76 | + |
| 77 | +Setting this value to true automatically runs the isolation process |
| 78 | +before every `dump`. |
| 79 | + |
| 80 | +**require-dev** |
| 81 | + |
| 82 | +By default, only `require` packages are scanned for namespaces, and |
| 83 | +`require-dev` packages are ignored (as above, they will still have code |
| 84 | +rewritten if they contain namespaces from other packages). |
| 85 | + |
| 86 | +Setting this value to `true` includes the `require-dev` packages in the |
| 87 | +scan, and any found namespaces will be prefixed. |
| 88 | + |
| 89 | +**replacements** |
| 90 | + |
| 91 | +This is a place for manually fixing things in the vendor code that either |
| 92 | +were not detected and replaced, or replaced when they should not have been. |
| 93 | + |
| 94 | +After each file has been parsed and rewritten, if there is an entry in the |
| 95 | +replacements list, it will do a string replace on the file. |
| 96 | + |
| 97 | +String replacements should be idempotent, or things will break on multiple |
| 98 | +executions. |
| 99 | + |
| 100 | +The syntax is: |
| 101 | +``` |
| 102 | +"replacements" : { |
| 103 | + "path/relative/to/vendor/root/file.php" : { |
| 104 | + "search" : "replace", |
| 105 | + "search" : "replace", |
| 106 | + }, |
| 107 | + "path/relative/to/vendor/root/file.php" : { |
| 108 | + "search" : "replace", |
| 109 | + "search" : "replace", |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
0 commit comments