Skip to content

Replacing Namespace

Justin Palmer edited this page Jul 5, 2021 · 4 revisions

Replacing namespace within a composer package

To replace the namespace in an additional package, the file bin/prefix-vendor-namespace.php needs to be modified.

1. Add the name to the $replacements array

For example:

'GuzzleHttp' => 'guzzlehttp',

This will result in having the GuzzleHttp namespace replaced in the folder vendor/guzzlehttp. Since Guzzle uses its main namespace and two sub namespaces, we have to replace them all in one:

GuzzleHttp          > guzzlehttp/guzzle
GuzzleHttp\Promises > guzzlehttp/promises
GuzzleHttp\Psr7     > guzzlehttp/psr7

2. Add dependent packages to the $dependencies array

For Guzzle this would be:

'guzzlehttp' => [
	'google/apiclient',
	'google/auth',
	'google/gax',
]

Each of those three packages makes use of the Guzzle package, so we need to ensure they use the new replaced namespace.

3. Add namespaces which are used directly within the code

A search needs to be done to find any of these, a command like this can be used:

grep -r --include \*.php 'GuzzleHttp' vendor/* | grep -v php:namespace | grep -v php:use | grep -v vendor/composer/

It will give some locations within comments where we don't need to replace the code, but for example an entry like this will need to be replaced:

$sink = \GuzzleHttp\Psr7\Utils::streamFor($sink);

For that line we add the following entry to the $direct_replacements array:

'GuzzleHttp\Psr7\Utils::streamFor'

4. Add namespaces to composer.json

The script replaces entries in vendor/composer/installed.json and then reruns composer dump-autoload to generate the classmap again. This means we don't have to manually add the replaced namespaces in autoload > psr-4. We do, however, need to check if there are any static files which are loaded in autoload > files – the file vendor/composer/autoload_static.php can be checked for a list of files which are loaded statically.

In the case of GuzzleHttp there are three files which are loaded statically. If we want to load these with our changed namespace then we would need to include them again in our composer.json file, for example:

"autoload": {
  ...
  "files": [
    "vendor/guzzlehttp/guzzle/src/functions_include.php",
    "vendor/guzzlehttp/promises/src/functions_include.php",
    "vendor/guzzlehttp/psr7/src/functions_include.php"
  ]
}

However, in this case we chose not to load these, because the functions included in these files are all deprecated and are only provided for backwards compatibility. Since we updated our libraries, we no longer call any of these deprecated functions, so it's not necessary to load them. For other packages it is important to check these static files as they can cause conflicts and they might be including functions we need.