Skip to content

Wrapping all composer vendor packages inside your own namespace. Intended for WordPress plugins

License

Notifications You must be signed in to change notification settings

pentatonicfunk/imposter

 
 

Repository files navigation

Imposter

Latest Stable Version PHP from Packagist Total Downloads Build Status codecov License Donate via PayPal Hire Typist Tech

Wrapping all composer vendor packages inside your own namespace. Intended for WordPress plugins.

Why?

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:

Install

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

Config

In your composer.json:

"extra": {
    "imposter": {
        "namespace": "My\\App\\Vendor",
        "excludes": [
            "dummy/dummy-excluded"
        ]
    }
}

extra.imposter.namespace

Required String

This is the namespace prefix to be added to vendor packages.

extra.imposter.excludes

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.

Usage

After every $ composer install and $ composer update:

<?php

use TypistTech\Imposter\ImposterFactory;

$imposter = ImposterFactory::forProject('/path/to/project/root');
$imposter->run();

The above snippet:

  1. Look for /path/to/project/root/composer.json
  2. Find out vendor-dir
  3. Find out all required packages, including those required by dependencies
  4. Find out all autoload paths for all required packages
  5. 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;

class DummyClass
{
}

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;

class DummyClass
{
}

Frequently Asked Questions

How can I integrate imposter with composer?

Use imposter-plugin instead. It hooks imposter into composer command events.

Does imposter support PSR4, PSR0, Classmap and Files?

Yes for all. PSR-4 and PSR-0 autoloading, classmap generation and files includes are supported.

Can I exclude some of the packages from imposter?

Yes, see extra.imposter.excludes. All composer made packages are excluded by default.

Does imposter support exclude-from-classmap?

Not for now. Pull requests are welcome.

How about require-dev packages?

Imposter do nothing on require-dev packages because imposter is intended for avoiding production environment., not for development environment.

How about PHP built-in classes?

Imposter skips classes that on global namespace, for example: \ArrayObject, \RuntimeException

How about packages that don't use namespaces?

Not for now. Tell me your idea by opening an issue

How about packages that use fully qualified name?

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

Will you add support for older PHP versions?

Never! This package will only works on actively supported PHP versions. Don't use it on end of life or security fixes only PHP versions.

It looks awesome. Where can I find some more goodies like this?

This package isn't on wp.org. Where can I give a ⭐⭐⭐⭐⭐ review?

Thanks!

Consider writing a blog post, submitting pull requests, donating or hiring me instead.

Alternatives

Here is a list of alternatives that I found. But none 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
  • PHP Scoper

    • Prefixes all PHP namespaces in a file/directory to isolate the code bundled in PHARs

Support

Love imposter? Help me maintain it, a donation here can help with it.

Why don't you hire me?

Ready to take freelance WordPress jobs. Contact me via the contact form here or, via email info@typist.tech

Want to help in other way? Want to be a sponsor?

Contact: Tang Rufus

Running the Tests

Run the tests:

$ composer test
$ composer check-style

Feedback

Please provide feedback! We want to make this library useful in as many projects as possible. Please submit an issue and point out what you do and don't like, or fork the project and make suggestions. No issue is too small.

Change log

Please see CHANGELOG for more information on what has changed recently.

Security

If you discover any security related issues, please email imposter@typist.tech instead of using the issue tracker.

Credits

imposter is a Typist Tech project and maintained by Tang Rufus, freelance developer for hire.

Full list of contributors can be found here.

License

The MIT License (MIT). Please see License File for more information.

About

Wrapping all composer vendor packages inside your own namespace. Intended for WordPress plugins

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%