PHPackages                             brianhenryie/strauss - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. brianhenryie/strauss

ActiveLibrary

brianhenryie/strauss
====================

Prefixes dependencies namespaces so they are unique to your plugin

0.27.0(2mo ago)183340.9k↓16.2%34[32 issues](https://github.com/BrianHenryIE/strauss/issues)[7 PRs](https://github.com/BrianHenryIE/strauss/pulls)17MITPHP

Since Mar 6Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/BrianHenryIE/strauss)[ Packagist](https://packagist.org/packages/brianhenryie/strauss)[ RSS](/packages/brianhenryie-strauss/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (46)Versions (75)Used By (17)

[![PHPUnit ](.github/coverage.svg)](https://brianhenryie.github.io/strauss/) [![PHPStan ](.github/phpstan.svg)](https://phpstan.org/)

Strauss – PHP Namespace Renamer
===============================

[](#strauss--php-namespace-renamer)

A tool to prefix namespaces, classnames, and constants in PHP files to avoid autoloading collisions.

A fork of [Mozart](https://github.com/coenjacobs/mozart/) for [Composer](https://getcomposer.org/) for PHP.

Have you ever activated a WordPress plugin that has a conflict with another because the plugins use two different versions of the same PHP library? **Strauss is the solution to that problem** - it ensures that *your* plugin's PHP dependencies are isolated and loaded from your plugin rather than loading from whichever plugin's autoloader registers &amp; runs first.

> ⚠️ **Sponsorship**: It would be neat if you were to offer me a license to your plugin, or at least [post about where this is used](https://github.com/BrianHenryIE/strauss/discussions).

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
    - [As a `.phar` file](#as-a-phar-file-recommended) (recommended)
    - [As a dev dependency via composer](#as-a-dev-dependency-via-composer-not-recommended) (not recommended)
    - [Edit `composer.json` scripts](#edit-composerjson-scripts)
- [Usage](#usage)
- [Configuration](#configuration)
- [Autoloading](#autoloading)
- [Motivation &amp; Comparison to Mozart](#motivation--comparison-to-mozart)
- [Alternatives](#alternatives)
- [Breaking Changes](#breaking-changes)
- [Acknowledgements](#acknowledgements)

Installation
------------

[](#installation)

### As a `.phar` file (recommended)

[](#as-a-phar-file-recommended)

There are a couple of small steps to make this possible.

#### Create a `bin/.gitkeep` file

[](#create-a-bingitkeep-file)

This ensures that there is a `bin/` directory in the root of your project. This is where the `.phar` file will go.

```
mkdir bin
touch bin/.gitkeep
```

#### `.gitignore` the `.phar` file

[](#gitignore-the-phar-file)

Add the following to your `.gitignore`:

```
bin/strauss.phar
```

#### Edit `composer.json` `scripts

[](#edit-composerjson-scripts)

In your `composer.json`, add `strauss` to the `scripts` section:

```
"scripts": {
    "prefix-namespaces": [
        "sh -c 'test -f ./bin/strauss.phar || curl -o bin/strauss.phar -L -C - https://github.com/BrianHenryIE/strauss/releases/latest/download/strauss.phar'",
        "@php bin/strauss.phar",
        "@composer dump-autoload"
    ],
    "post-install-cmd": [
        "@prefix-namespaces"
    ],
    "post-update-cmd": [
        "@prefix-namespaces"
    ],
    "post-autoload-dump": [
        "@php bin/strauss.phar include-autoloader"
    ]
}
```

This provides `composer strauss`, which does the following:

1. The `sh -c` command tests if `bin/strauss.phar` exists, and if not, downloads it from [releases](https://github.com/BrianHenryIE/strauss/releases).
2. Then `@php bin/strauss.phar` is run to prefix the namespaces.
3. Ensure that composer's autoload map is updated.

### As a dev dependency via composer (not recommended)

[](#as-a-dev-dependency-via-composer-not-recommended)

If you prefer to include Strauss as a dev dependency, you can still do so. You mileage may vary when you include it this way.

```
composer require --dev brianhenryie/strauss

```

#### Edit `composer.json` `scripts

[](#edit-composerjson-scripts-1)

```
"scripts": {
    "prefix-namespaces": [
        "strauss",
        "@php composer dump-autoload"
    ],
    "post-install-cmd": [
        "@prefix-namespaces"
    ],
    "post-update-cmd": [
        "@prefix-namespaces"
    ],
    "post-autoload-dump": [
        "strauss include-autoloader"
    ]
}
```

Usage
-----

[](#usage)

If you add Strauss to your `composer.json` as indicated in [Installation](#installation), it will run when you `composer install` or `composer update`. To run Strauss directly, simply use:

```
composer prefix-namespaces
```

To update the files that call the prefixed classes, you can use `--updateCallSites=true` which uses your autoload key, or `--updateCallSites=includes,templates` to explicitly specify the files and directories.

```
composer -- prefix-namespaces --updateCallSites=true
```

or

```
composer -- prefix-namespaces --updateCallSites=includes,templates
```

To try it out without making changes, you can use the `--dry-run` flag:

strauss --dry-run[![](.github/strauss.mp4)](.github/strauss.mp4)

Verbosity can be controlled with `--notice` (default), `--info`, `--debug` and `--silent`.

Configuration
-------------

[](#configuration)

Strauss potentially requires zero configuration, but likely you'll want to customize a little, by adding in your `composer.json` an `extra/strauss` object. The following is the default config, where the `namespace_prefix` and `classmap_prefix` are determined from your `composer.json`'s `autoload` or `name` key and `packages` is determined from the `require` key:

```
"extra": {
    "strauss": {
        "target_directory": "vendor-prefixed",
        "namespace_prefix": "BrianHenryIE\\My_Project\\",
        "classmap_prefix": "BrianHenryIE_My_Project_",
        "constant_prefix": "BHMP_",
        "packages": [
        ],
        "update_call_sites": false,
        "include_root_autoload": false,
        "optimize_autoloader": true,
        "override_autoload": {
        },
        "exclude_from_copy": {
            "packages": [
            ],
            "namespaces": [
            ],
            "file_patterns": [
            ]
        },
        "exclude_from_prefix": {
            "packages": [
            ],
            "namespaces": [
            ],
            "file_patterns": [
            ]
        },
        "exclude_constants": {
            "packages": [
            ],
            "namespaces": [
            ],
            "file_patterns": [
            ],
            "constants": [
            ]
        },
        "namespace_replacement_patterns" : {
        },
        "delete_vendor_packages": false,
        "delete_vendor_files": false
    }
},
```

The following configuration is inferred:

- `target_directory` defines the directory the files will be copied to, default `vendor-prefixed`
- `namespace_prefix` defines the default string to prefix each namespace with
- `classmap_prefix` defines the default string to prefix class names in the global namespace
- `packages` is the list of packages to process. If absent, all packages in the `require` key of your `composer.json` are included
- `classmap_output` is a `bool` to decide if Strauss will create `autoload-classmap.php` and `autoload.php`. If it is not set, it is `false` if `target_directory` is in your project's `autoload` key, `true` otherwise.

The following configuration is default:

- `delete_vendor_packages`: `false` a boolean flag to indicate if the packages' vendor directories should be deleted after being processed. It defaults to false, so any destructive change is opt-in.
- `delete_vendor_files`: `false` a boolean flag to indicate if files copied from the packages' vendor directories should be deleted after being processed. It defaults to false, so any destructive change is opt-in. This is maybe deprecated! Is there any use to this that is more appropriate than `delete_vendor_packages`?
- `include_modified_date` is a `bool` to decide if Strauss should include a date in the (phpdoc) header written to modified files. Defaults to `true`.
- `include_author` is a `bool` to decide if Strauss should include the author name in the (phpdoc) header written to modified files. Defaults to `true`.
- `update_call_sites`: `false`. This can be `true`, `false` or an `array` of directories/filepaths. When set to `true` it defaults to the directories and files in the project's `autoload` key. The PHP files and directories' PHP files will be updated where they call the prefixed classes.
- `include_root_autoload`: `false` is a boolean flag to indicate whether Strauss should include the root autoload section of your project when creating its autoloader. It is false by default. Enabling this option will allow you to require only the Strauss autoloader in your project. Note that conflicts may occur if your project enables this option, requires both the Composer and Strauss autoloaders, and uses `files` autoloading.
- `optimize_autoloader`: `true` is a boolean flag to indicate whether Strauss should force optimized/classmap-authoritative autoload generation. Set it to `false` to still regenerate autoload files without authoritative mode.

To disable optimized/classmap-authoritative Composer autoload generation:

```
{
  "extra": {
    "strauss": {
      "optimize_autoloader": false
    }
  }
}
```

The remainder is empty:

- `constant_prefix` is for `define( "A_CONSTANT", value );` -&gt; `define( "MY_PREFIX_A_CONSTANT", value );`. If it is empty, constants are not prefixed (this may change to an inferred value).
- `override_autoload` a dictionary, keyed with the package names, of autoload settings to replace those in the original packages' `composer.json` `autoload` property.
- `exclude_from_prefix` / [`file_patterns`](https://github.com/BrianHenryIE/strauss/blob/83484b79cfaa399bba55af0bf4569c24d6eb169d/src/ChangeEnumerator.php#L92-L96)
- `exclude_from_copy`
    - [`packages`](https://github.com/BrianHenryIE/strauss/blob/83484b79cfaa399bba55af0bf4569c24d6eb169d/src/FileEnumerator.php#L77-L79) array of package names to be skipped
    - [`namespaces`](https://github.com/BrianHenryIE/strauss/blob/83484b79cfaa399bba55af0bf4569c24d6eb169d/src/FileEnumerator.php#L95-L97) array of namespaces to skip (exact match from the package autoload keys)
    - [`file_patterns`](https://github.com/BrianHenryIE/strauss/blob/83484b79cfaa399bba55af0bf4569c24d6eb169d/src/FileEnumerator.php#L133-L137) array of regex patterns to check filenames against (including vendor relative path) where Strauss will skip that file if there is a match
- `exclude_from_prefix`
    - [`packages`](https://github.com/BrianHenryIE/strauss/blob/83484b79cfaa399bba55af0bf4569c24d6eb169d/src/ChangeEnumerator.php#L86-L90) array of package names to exclude from prefixing.
    - [`namespaces`](https://github.com/BrianHenryIE/strauss/blob/83484b79cfaa399bba55af0bf4569c24d6eb169d/src/ChangeEnumerator.php#L177-L181) array of exact match namespaces to exclude (i.e. not substring/parent namespaces)
- `exclude_constants` – same shape as `exclude_from_prefix`, but applies only to constants (e.g. from `define()` or `const`). Use to avoid prefixing runtime constants like `WP_PLUGIN_DIR`, `ABSPATH`.
    - `packages` array of package names whose constants are not prefixed
    - `namespaces` array of namespaces (prefix match) whose constants are not prefixed
    - `file_patterns` array of regex patterns for file paths
    - `constants` array of constant names to never prefix (e.g. `["WP_PLUGIN_DIR", "ABSPATH"]`)
- [`namespace_replacement_patterns`](https://github.com/BrianHenryIE/strauss/blob/83484b79cfaa399bba55af0bf4569c24d6eb169d/src/ChangeEnumerator.php#L183-L190) a dictionary to use in `preg_replace` instead of prefixing with `namespace_prefix`.

Autoloading
-----------

[](#autoloading)

Strauss uses Composer's own tools to generate a set of autoload files in the `target_directory` and creates an `autoload.php` alongside it, so in many projects autoloading is just a matter of:

```
require_once __DIR__ . '/vendor-prefixed/autoload.php';
```

If you plan to continue using Composer's autoloader you probably want to turn on `delete_vendor_packages` or set `target_directory` to `vendor`.

You can use `strauss include-autoloader` to add a line to `vendor/autoload.php` which includes the autoloader for the new files.

If you don't plan to use Composer's autoloader, you may wish to enable `include_root_autoload` so that the Strauss autoloader includes the autoload for your project.

When `delete_vendor_packages` is enabled, `vendor/composer/autoload_aliases.php` is created to allow modified classes to be loaded with their old name during development. This file should not be included in your production code.

Motivation &amp; Comparison to Mozart
-------------------------------------

[](#motivation--comparison-to-mozart)

I was happy to make PRs to Mozart to fix bugs, but they weren't being reviewed and merged. At the time of writing, somewhere approaching 50% of Mozart's code [was written by me](https://github.com/coenjacobs/mozart/graphs/contributors) with an additional [nine open PRs](https://github.com/coenjacobs/mozart/pulls?q=is%3Apr+author%3ABrianHenryIE+) and the majority of issues' solutions [provided by me](https://github.com/coenjacobs/mozart/issues?q=is%3Aissue+). This fork is a means to merge all outstanding bugfixes I've written and make some more drastic changes I see as a better approach to the problem.

Benefits over Mozart:

- A single output directory whose structure matches source vendor directory structure (conceptually easier than Mozart's independent `classmap_directory` and `dep_directory`)
- A generated `autoload.php` to `include` in your project (analogous to Composer's `vendor/autoload.php`)
- Handles `files` autoloaders – and any autoloaders that Composer itself recognises, since Strauss uses Composer's own tooling to parse the packages
- Zero configuration – Strauss infers sensible defaults from your `composer.json`
- No destructive defaults – `delete_vendor_files` defaults to `false`, so any destruction is explicitly opt-in
- Licence files are included and PHP file headers are edited to adhere to licence requirements around modifications. My understanding is that re-distributing code that Mozart has handled is non-compliant with most open source licences – illegal!
- Extensively tested – PhpUnit tests have been written to validate that many of Mozart's bugs are not present in Strauss
- More configuration options – allowing exclusions in copying and editing files, and allowing specific/multiple namespace renaming
- Respects `composer.json` `vendor-dir` configuration
- Prefixes constants (`define`)
- Handles meta-packages and virtual-packages

Strauss will read the Mozart configuration from your `composer.json` to enable a seamless migration.

Alternatives
------------

[](#alternatives)

I don't have a strong opinion on these. I began using Mozart because it was easy, then I adapted it to what I felt was most natural. I've never used these.

- [humbug/php-scoper](https://github.com/humbug/php-scoper)
- [TypistTech/imposter-plugin](https://github.com/TypistTech/imposter-plugin)
- [Automattic/jetpack-autoloader](https://github.com/Automattic/jetpack-autoloader)
- [tschallacka/wordpress-composer-plugin-builder](https://github.com/tschallacka/wordpress-composer-plugin-builder)
- [Interfacelab/namespacer](https://github.com/Interfacelab/namespacer)
- [PHP-Prefixer](https://github.com/PHP-Prefixer) SaaS!

### Interesting

[](#interesting)

- [composer-unused/composer-unused](https://github.com/composer-unused/composer-unused)
- [sdrobov/autopsr4](https://github.com/sdrobov/autopsr4)
- [jaem3l/unfuck](https://github.com/jaem3l/unfuck)
- [bamarni/composer-bin-plugin](https://github.com/bamarni/composer-bin-plugin)
- [phar-io/composer-distributor](https://github.com/phar-io/composer-distributor)

Breaking Changes
----------------

[](#breaking-changes)

- v0.25.0 – will copy all files from a package to the target directory
- v0.21.0 – will prefix global functions
- v0.16.0 – will no longer prefix PHP built-in classes seen in polyfill packages
- v0.14.0 – `psr/*` packages no longer excluded by default
- v0.12.0 – default output `target_directory` changes from `strauss` to `vendor-prefixed`

Please open issues to suggest possible breaking changes. I think we can probably move to 1.0.0 soon.

### Backward Compatibility Promise

[](#backward-compatibility-promise)

This project will not increase its minimum required PHP version ahead of WordPress.

Changes before v1.0
-------------------

[](#changes-before-v10)

- Comprehensive attribution of code forked from Mozart – changes have been drastic and `git blame` is now useless, so I intend to add more attributions
- More consistent naming. Are we prefixing or are we renaming?
- Further unit tests, particularly file-system related
- Regex patterns in config need to be validated
- Change the name? "Renamespacer"?

Changes before v2.0
-------------------

[](#changes-before-v20)

The correct approach to this problem is probably via [PHP-Parser](https://github.com/nikic/PHP-Parser/). At least all the tests will be useful.

Acknowledgements
----------------

[](#acknowledgements)

[Coen Jacobs](https://github.com/coenjacobs/) and all the [contributors to Mozart](https://github.com/coenjacobs/mozart/graphs/contributors), particularly those who wrote nice issues.

###  Health Score

63

—

FairBetter than 99% of packages

Maintenance83

Actively maintained with recent releases

Popularity55

Moderate usage in the ecosystem

Community37

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 85.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~54 days

Recently: every ~24 days

Total

61

Last Release

73d ago

PHP version history (4 changes)0.2.2PHP &gt;=5.5.0

0.4.0PHP ^7.1

0.5.0PHP ^7.2

0.8.0PHP ^7.4|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3541a510f89dedd04f0e7103201e411f6f22685a13e941b3384d97d4c7d32b09?d=identicon)[BrianHenryIE](/maintainers/BrianHenryIE)

---

Top Contributors

[![BrianHenryIE](https://avatars.githubusercontent.com/u/4720401?v=4)](https://github.com/BrianHenryIE "BrianHenryIE (1756 commits)")[![coenjacobs](https://avatars.githubusercontent.com/u/245703?v=4)](https://github.com/coenjacobs "coenjacobs (199 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (27 commits)")[![paulgoodchild](https://avatars.githubusercontent.com/u/10562196?v=4)](https://github.com/paulgoodchild "paulgoodchild (10 commits)")[![markjaquith](https://avatars.githubusercontent.com/u/353790?v=4)](https://github.com/markjaquith "markjaquith (7 commits)")[![Dartui](https://avatars.githubusercontent.com/u/2657856?v=4)](https://github.com/Dartui "Dartui (5 commits)")[![hirasso](https://avatars.githubusercontent.com/u/869813?v=4)](https://github.com/hirasso "hirasso (4 commits)")[![defunctl](https://avatars.githubusercontent.com/u/1066195?v=4)](https://github.com/defunctl "defunctl (4 commits)")[![szaleq](https://avatars.githubusercontent.com/u/25011639?v=4)](https://github.com/szaleq "szaleq (4 commits)")[![AndrewJDawes](https://avatars.githubusercontent.com/u/53574062?v=4)](https://github.com/AndrewJDawes "AndrewJDawes (4 commits)")[![0zd0](https://avatars.githubusercontent.com/u/67220210?v=4)](https://github.com/0zd0 "0zd0 (3 commits)")[![Eitan-brightleaf](https://avatars.githubusercontent.com/u/160501761?v=4)](https://github.com/Eitan-brightleaf "Eitan-brightleaf (3 commits)")[![doekenorg](https://avatars.githubusercontent.com/u/529515?v=4)](https://github.com/doekenorg "doekenorg (3 commits)")[![MalIlluminated](https://avatars.githubusercontent.com/u/119440080?v=4)](https://github.com/MalIlluminated "MalIlluminated (3 commits)")[![sovetski](https://avatars.githubusercontent.com/u/13520683?v=4)](https://github.com/sovetski "sovetski (2 commits)")[![stephenharris](https://avatars.githubusercontent.com/u/3255034?v=4)](https://github.com/stephenharris "stephenharris (2 commits)")[![Konamiman](https://avatars.githubusercontent.com/u/937723?v=4)](https://github.com/Konamiman "Konamiman (2 commits)")[![UVLabs](https://avatars.githubusercontent.com/u/10324144?v=4)](https://github.com/UVLabs "UVLabs (1 commits)")[![arjen-mediasoep](https://avatars.githubusercontent.com/u/122452581?v=4)](https://github.com/arjen-mediasoep "arjen-mediasoep (1 commits)")[![borkweb](https://avatars.githubusercontent.com/u/430385?v=4)](https://github.com/borkweb "borkweb (1 commits)")

---

Tags

composernamespaces

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/brianhenryie-strauss/health.svg)

```
[![Health](https://phpackages.com/badges/brianhenryie-strauss/health.svg)](https://phpackages.com/packages/brianhenryie-strauss)
```

###  Alternatives

[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[laravel/framework

The Laravel Framework.

34.7k509.9M17.0k](/packages/laravel-framework)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)[infection/infection

Infection is a Mutation Testing framework for PHP. The mutation adequacy score can be used to measure the effectiveness of a test set in terms of its ability to detect faults.

2.2k26.2M1.8k](/packages/infection-infection)[silverstripe/framework

The SilverStripe framework

7213.5M2.5k](/packages/silverstripe-framework)[contao/core-bundle

Contao Open Source CMS

1231.6M2.4k](/packages/contao-core-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
