-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 09e01f3
Showing
10 changed files
with
1,025 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Ignore hidden files | ||
.* | ||
|
||
# Ignore temporary files | ||
*~ | ||
|
||
# Annoying Windows files | ||
Thumbs.db | ||
ehthumbs.db | ||
Desktop.ini | ||
$RECYCLE.BIN/ | ||
|
||
# Generated content | ||
/vendor/ | ||
|
||
# Exceptions | ||
!.gitignore | ||
!.gitkeep | ||
!/.php_cs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
return PhpCsFixer\Config::create() | ||
->setUsingCache(false) | ||
->setRules([ | ||
'@PSR2' => true, | ||
'@Symfony' => true, | ||
'array_syntax' => ['syntax' => 'short'], | ||
'binary_operator_spaces' => ['align_double_arrow' => true, 'align_equals' => null], | ||
'concat_space' => ['spacing' => 'one'], | ||
'ordered_imports' => true, | ||
|
||
// Overrides | ||
'phpdoc_summary' => false, | ||
]) | ||
->setFinder(PhpCsFixer\Finder::create() | ||
->in(['.']) | ||
->exclude('vendor') | ||
->exclude('tests') | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Composer Dependency Isolation | ||
|
||
**What this plugin does** | ||
|
||
This plugin prefixes all vendor namespaces with a chosen value. This | ||
includes all declared namespaces, use statements, fully qualified | ||
references, and most string values that reference the namespace. | ||
|
||
All vendor code and composer autoload mappings are updated to reference | ||
the prefixed namespace. | ||
|
||
**What this plugin does not do** | ||
|
||
It will not touch any code in your project. It only affects code in the | ||
vendor directory, and it only affects code referencing the affected | ||
namespaces. You must update all references in your code yourself if you | ||
apply this to an existing project. | ||
|
||
**Why would I want to use this?** | ||
|
||
This plugin allows you to run two projects that utilize composer | ||
dependencies in the same runtime, without worrying about conflicting | ||
dependencies. The most common example is in a WordPress environment, | ||
where all plugins execute in the same runtime, and may rely on the same | ||
composer dependencies. Each project utilizing the plugin can't conflict | ||
with any other project unless the vendor code is not namespaced (in which | ||
case there aren't many options...). | ||
|
||
## Usage | ||
|
||
Using the plugin is straightforward. Install the plugin by requiring it | ||
in your project: `composer require 0x6d617474/isolate`. | ||
|
||
Configure the plugin by adding the following to your `composer.json`: | ||
``` | ||
"config" : { | ||
"isolate": { | ||
"prefix": "Your\\Prefix\\Here\\", | ||
"blacklist": [], | ||
"autorun": false, | ||
"require-dev": false, | ||
"replacements" : {} | ||
} | ||
} | ||
``` | ||
|
||
The only required value is the `prefix`. | ||
|
||
After you have configured the plugin, run the isolation: | ||
``` | ||
composer isolate | ||
composer dump | ||
``` | ||
|
||
Your vendor code is now prefixed! | ||
|
||
Be sure to `dump` after you `isolate`, or your autoload mappings will | ||
be incorrect! | ||
|
||
## Configuration | ||
|
||
**prefix** | ||
|
||
This is the value that will be prepended to all vendor namespaces. It | ||
should be a valid namespace, and should be unique to your project. I | ||
recommend you don't use your main project namespace, or at least add | ||
`\\Vendor` to the end. | ||
|
||
**blacklist** | ||
|
||
This is a list of packages you don't want to prefix. Matching packages | ||
will not be scanned for namespaces, but will still have code rewritten | ||
if it contains namespaces from other non-blacklisted packages. | ||
|
||
**autorun** | ||
|
||
Setting this value to true automatically runs the isolation process | ||
before every `dump`. | ||
|
||
**require-dev** | ||
|
||
By default, only `require` packages are scanned for namespaces, and | ||
`require-dev` packages are ignored (as above, they will still have code | ||
rewritten if they contain namespaces from other packages). | ||
|
||
Setting this value to `true` includes the `require-dev` packages in the | ||
scan, and any found namespaces will be prefixed. | ||
|
||
**replacements** | ||
|
||
This is a place for manually fixing things in the vendor code that either | ||
were not detected and replaced, or replaced when they should not have been. | ||
|
||
After each file has been parsed and rewritten, if there is an entry in the | ||
replacements list, it will do a string replace on the file. | ||
|
||
String replacements should be idempotent, or things will break on multiple | ||
executions. | ||
|
||
The syntax is: | ||
``` | ||
"replacements" : { | ||
"path/relative/to/vendor/root/file.php" : { | ||
"search" : "replace", | ||
"search" : "replace", | ||
}, | ||
"path/relative/to/vendor/root/file.php" : { | ||
"search" : "replace", | ||
"search" : "replace", | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "0x6d617474/isolate", | ||
"description": "Isolates composer dependencies by prefixing namespace for all vendor code within a project", | ||
"license": "MIT", | ||
"minimum-stability": "alpha", | ||
"type": "composer-plugin", | ||
"require": { | ||
"composer-plugin-api": "^1.1", | ||
"nikic/php-parser": "^2.1" | ||
}, | ||
"require-dev": { | ||
"composer/composer": "^1.4", | ||
"friendsofphp/php-cs-fixer": "^2.3" | ||
}, | ||
"autoload":{ | ||
"psr-4":{ | ||
"Ox6d617474\\Isolate\\":"src/" | ||
} | ||
}, | ||
"extra": { | ||
"class": "Ox6d617474\\Isolate\\Plugin" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Ox6d617474\Isolate; | ||
|
||
use Composer\Command\BaseCommand; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class Command extends BaseCommand | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this->setName('isolate'); | ||
$this->setDescription('Isolate dependencies'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->getComposer()->getEventDispatcher()->dispatch('__isolate-dependencies'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Ox6d617474\Isolate; | ||
|
||
use Composer\Plugin\Capability\CommandProvider as CommandProviderCapability; | ||
|
||
final class CommandProvider implements CommandProviderCapability | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCommands() | ||
{ | ||
return [new Command()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Ox6d617474\Isolate; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Namespace_; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
final class DiscoveryVisitor extends NodeVisitorAbstract | ||
{ | ||
/** | ||
* Discovered namespaces | ||
* | ||
* @var array | ||
*/ | ||
private $namespaces; | ||
|
||
/** | ||
* Class constructor | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->namespaces = []; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function enterNode(Node $node) | ||
{ | ||
if ($node instanceof Namespace_) { | ||
if (isset($node->name)) { | ||
$this->namespaces[implode($node->name->parts, '\\')] = true; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get the list of discovered namespaces | ||
* | ||
* @return array | ||
*/ | ||
public function getNamespaces() | ||
{ | ||
return $this->namespaces; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Ox6d617474\Isolate; | ||
|
||
final class NamespaceChecker | ||
{ | ||
/** | ||
* Discovered namespaces | ||
* | ||
* @var array | ||
*/ | ||
private $namespaces; | ||
|
||
/** | ||
* Prefix | ||
* | ||
* @var string | ||
*/ | ||
private $prefix; | ||
|
||
/** | ||
* Class constructor | ||
* | ||
* @param array $namespaces | ||
* @param string $prefix | ||
*/ | ||
public function __construct(array $namespaces, $prefix) | ||
{ | ||
$this->namespaces = $namespaces; | ||
$this->prefix = $prefix; | ||
} | ||
|
||
/** | ||
* Is the given string a valid namespace | ||
* | ||
* @param string $string | ||
* | ||
* @return bool | ||
*/ | ||
public static function isNamespace($string) | ||
{ | ||
// Must contain a backslash, and may only contain alphanumeric and underscore | ||
if (!preg_match('/^[0-9a-z_\\\]+$/i', $string) || !preg_match('/[\\\]+/i', $string)) { | ||
return false; | ||
} | ||
|
||
// Don't match only slashes... | ||
if (preg_match('/^[\\\]+$/i', $string)) { | ||
return false; | ||
} | ||
// Don't match a single word between slashes... | ||
if (preg_match('/^[\\\]+[0-9a-z_]+[\\\]+$/i', $string)) { | ||
return false; | ||
} | ||
|
||
// Sections should not begin with a number | ||
$parts = array_filter(explode('\\', $string)); | ||
foreach ($parts as $part) { | ||
if (preg_match('/^[0-9]+/', $part)) { | ||
return false; | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Should the given namespace be transformed? | ||
* | ||
* @param string $string | ||
* | ||
* @return bool | ||
*/ | ||
public function shouldTransform($string) | ||
{ | ||
// Never transform non-namespace strings | ||
if (!self::isNamespace($string)) { | ||
return false; | ||
} | ||
|
||
// Trim ends to ensure valid matches | ||
$string = trim($string, '\\'); | ||
|
||
// We never want to match our own prefix | ||
if (preg_match('/^' . preg_quote($this->prefix, '/') . '/i', $string)) { | ||
return false; | ||
} | ||
|
||
// We only want to match our list of namespaces | ||
return isset($this->namespaces[$string]); | ||
} | ||
} |
Oops, something went wrong.