Wrapping all composer vendor packages inside your own namespace. Intended for WordPress plugins.
Built with โฅ by Typist Tech
Imposter is an open source project and completely free to use.
However, the amount of effort needed to maintain and develop new features is not sustainable without proper financial backing. If you have the capability, please consider donating using the links below:
Wrapping all composer vendor packages inside your own namespace. Intended for WordPress plugins.
Because of the lack of dependency management in WordPress, if two plugins bundled conflicting versions of the same package, hard-to-reproduce bugs arise. Monkey patching composer vendor packages, wrapping them inside your own namespace is a less-than-ideal solution to avoid such conflicts.
See:
- A Narrative of Using Composer in a WordPress Plugin
- A Warning About Using Composer With WordPress
- Plugin Dependencies (Yet Another Plugin Dependencies Ticket)
If you want to hook Imposter into composer command events, install Imposter Plugin instead. See: How can I integrate Imposter with composer?
Installation should be done via composer, details of how to install composer can be found at https://getcomposer.org/.
composer require typisttech/imposter
In your composer.json
:
"extra": {
"imposter": {
"namespace": "My\\App\\Vendor",
"excludes": [
"dummy/dummy-excluded"
]
}
}
Required String
This is the namespace prefix to be added to vendor packages.
Optional Array of strings
Vendor packages which needs to be excluded from namespace prefixing.
All composer-made packages are excluded by default.
Besides, anything under the Composer
namespace will be excluded.
After every $ composer install
and $ composer update
:
<?php
use TypistTech\Imposter\ImposterFactory;
$imposter = ImposterFactory::forProject('/path/to/project/root');
$imposter->run();
The above snippet:
- Look for
/path/to/project/root/composer.json
- Find out vendor-dir
- Find out all required packages, including those required by dependencies
- Find out all autoload paths for all required packages
- Prefix all namespaces with the imposter namespace defined in your
composer.json
Before:
<?php
namespace Dummy\File;
use AnotherDummy\{
SubAnotherDummy, SubOtherDummy
};
use Composer;
use Composer\Plugin\PluginInterface;
use Dummy\SubOtherDummy;
use OtherDummy\SubOtherDummy;
use RuntimeException;
use \UnexpectedValueException;
use function OtherVendor\myFunc;
use const OtherVendor\MY_MAGIC_NUMBER;
$closure = function () use ($aaa) {
// Just testing.
};
class DummyClass
{
public function useClosure()
{
array_map(function () use ($xxx) {
// Just testing.
}, []);
}
}
function dummyFunction(string $namespace = null, string $use = null): array
{
if (! is_null($namespace) && $namespace === 'dummy string' && $use === 'dummy string') {
// Just testing.
}
return [];
}
foreach ([] as $namespace => $prefix) {
$aaaa = '{' . $namespace . '}' . $prefix;
}
/** Just a comment for testing $namespace transformation */
After:
<?php
namespace MyPlugin\Vendor\Dummy\File;
use MyPlugin\Vendor\AnotherDummy\{
SubAnotherDummy, SubOtherDummy
};
use Composer;
use Composer\Plugin\PluginInterface;
use MyPlugin\Vendor\Dummy\SubOtherDummy;
use MyPlugin\Vendor\OtherDummy\SubOtherDummy;
use RuntimeException;
use \UnexpectedValueException;
use function MyPlugin\Vendor\OtherVendor\myFunc;
use const MyPlugin\Vendor\OtherVendor\MY_MAGIC_NUMBER;
$closure = function () use ($aaa) {
// Just testing.
};
class DummyClass
{
public function useClosure()
{
array_map(function () use ($xxx) {
// Just testing.
}, []);
}
}
function dummyFunction(string $namespace = null, string $use = null): array
{
if (! is_null($namespace) && $namespace === 'dummy string' && $use === 'dummy string') {
// Just testing.
}
return [];
}
foreach ([] as $namespace => $prefix) {
$aaaa = '{' . $namespace . '}' . $prefix;
}
/** Just a comment for testing $namespace transformation */
Typist Tech is ready to build your next awesome WordPress site. Hire us!
Help Wanted. Pull requests are welcomed.
- Traits are not transformed
- Virtual packages are not supported
Use Imposter Plugin instead. It hooks imposter into composer command events.
Yes for all. PSR-4 and PSR-0 autoloading, classmap generation and files includes are supported.
Yes, see extra.imposter.excludes
.
All composer made packages are excluded by default.
Imposter do nothing on require-dev
packages because imposter is intended for avoiding production environment., not for development environment.
Imposter skips classes that on global namespace, for example: \ArrayObject
, \RuntimeException
Not for now. Tell me your idea by opening an issue.
Not for now. We need a better regex(or something better than regex) in the Transformer class. Tell me your idea by opening an issue
Until WordPress core comes up with a solution on dependency managment, keep clam and carry on.
In the meantime, checkout these tools to make WordPress suck less modernizing WordPress development:
- bring OOP into WordPress
- leveraging development practices from other languages and ecosystems, and applying them to WordPress
Both v1 and v2.
Never! This plugin will only work on actively supported PHP versions.
Don't use it on end of life or security fixes only PHP versions.
- Articles on Typist Tech's blog
- Tang Rufus' WordPress plugins on wp.org
- More projects on Typist Tech's GitHub profile
- Stay tuned on Typist Tech's newsletter
- Follow Tang Rufus' Twitter account
- Hire Tang Rufus to build your next awesome site
Thanks! Glad you like it. It's important to let me knows somebody is using this project. Please consider:
- tweet something good with mentioning @TangRufus
- โ star the Github repo
- ๐ watch the Github repo
- write tutorials and blog posts
- hire Typist Tech
composer test
composer style:check
Here is a list of alternatives that I found. However, none of these satisfied my requirements.
If you know other similar projects, feel free to edit this section!
-
Mozart by Coen Jacobs
- Works with PSR0 and PSR4
- Dependency packages store in a different directory
-
- Prefixes all PHP namespaces in a file/directory to isolate the code bundled in PHARs
Please provide feedback! We want to make this project as useful as possible. Please submit an issue and point out what you do and don't like, or fork the project and send pull requests. No issue is too small.
If you discover a security vulnerability within this project, please email us at imposter@typist.tech. All security vulnerabilities will be promptly addressed.
Imposter is a Typist Tech project and maintained by Tang Rufus, freelance developer for hire.
Full list of contributors can be found here.
Imposter is released under the MIT License.