Back to Woocommerce

WooCommerce Lib Directory

plugins/woocommerce/lib/README.md

10.9.0-dev4.0 KB
Original Source

WooCommerce Lib Directory

This directory contains a dummy package responsible for managing WooCommerce dependencies that require conflict avoidance. The contents of the packages and classes directories get automatically generated by composer install and composer update.

This allows us to prefix namespaces and classmap classes to avoid conflicts with plugins that include the same package. All namespaces are prefixed with Automattic\WooCommerce\Vendor and classmap classes prefixed with WC_Vendor_.

Do not make direct changes in the files contained in packages or classes! Any changes will be lost!

Adding Packages

In order to avoid including the original dependencies in the root autoloader we must utilize require-dev for them. Composer treats require dependencies as transitive while require-dev dependencies get ignored by consumers.

  1. Add package to require-dev section of composer.json
  2. Add package slug to extra/mozart/packages section of composer.json
  3. Run composer run-script build-lib from the root directory (You should now see the package in packages/VendorName/PackageName or classes)

Updating Packages

Updating a package is as easy as changing the version in composer.json and then running composer run-script build-lib from the root directory.

build-lib prefers composer install so that routine rebuilds respect lib/composer.lock and don't silently bump unrelated dependencies to their latest in-range version. It automatically switches to composer update when lib/composer.json has been modified (so a newly added or bumped package can refresh the lock). Pass --update to force a full update even when composer.json hasn't changed — useful for picking up in-range upstream releases deliberately:

sh
composer run-script build-lib -- --update

The -- separator tells Composer to forward --update to the build script rather than interpret it as one of its own options.

Ignoring Packages

If you would like to add a package which does not undergo conflict avoidance you must take steps to ensure it appears in the root autoloader.

  1. Add package to the require section of both the lib/composer.json and root composer.json file instead of require-dev
  2. Add package slug to extra/mozart/excluded-packages section of composer.json
  3. Run composer run-script build-lib from the root directory (You should not see the package in packages/VendorName/PackageName or classes) - see the note about MobileDetect below.

A note about the webonyx/graphql-php library

Mozart rewrites namespace declarations and use statements, but it can miss stringified FQCNs (class names embedded in string literals). Before shipping a webonyx version bump, audit the rebuilt package for any such strings that still reference the bare GraphQL\ namespace by running this from plugins/woocommerce/:

sh
grep -rn "'GraphQL\\\\\\|\"GraphQL\\\\" lib/packages/GraphQL/

The grep should return no results. If it does, patch the offending file manually and commit it, mirroring the MobileDetect workflow below.

A note about the MobileDetect library

The lib/packages/Detection/MobileDetect.php file got manual changes to prevent deprecation warnings in PHP 8. These fixes are already present in newer versions of the package, but we can't update to any of these versions because they all require PHP 8. The package version currently in use is the newest one supporting PHP 7.4.

Therefore, as long as WooCommerce runs in PHP 7.4 and no alternative solution is found for this, the changes in lib/packages/Detection/MobileDetect.php must be reverted after running composer run-script build-lib. bin/build-lib.sh does this automatically via git restore at the end of the build when run inside a git working tree. If you rebuild outside of git (e.g. from a tarball), restore the patched file manually: git restore ./plugins/woocommerce/lib/packages/Detection/MobileDetect.php from the root of the repository.