New autoloader

This commit is contained in:
Mike Jolley 2019-06-12 20:47:46 +01:00
parent a986bfc4c0
commit ee949427e3
7 changed files with 267 additions and 9 deletions

View File

@ -27,3 +27,4 @@ filter:
- src/RestApi/Version2/
- src/RestApi/Version3/
- unit-tests/
- vendor/

104
classmap.php Normal file
View File

@ -0,0 +1,104 @@
<?php
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = __DIR__;
return array(
'WC_REST_CRUD_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-crud-controller.php',
'WC_REST_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-controller.php',
'WC_REST_Coupons_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-coupons-controller.php',
'WC_REST_Coupons_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-coupons-v1-controller.php',
'WC_REST_Coupons_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-coupons-v2-controller.php',
'WC_REST_Customer_Downloads_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-customer-downloads-controller.php',
'WC_REST_Customer_Downloads_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-customer-downloads-v1-controller.php',
'WC_REST_Customer_Downloads_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-customer-downloads-v2-controller.php',
'WC_REST_Customers_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-customers-controller.php',
'WC_REST_Customers_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-customers-v1-controller.php',
'WC_REST_Customers_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-customers-v2-controller.php',
'WC_REST_Data_Continents_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-data-continents-controller.php',
'WC_REST_Data_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-data-controller.php',
'WC_REST_Data_Countries_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-data-countries-controller.php',
'WC_REST_Data_Currencies_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-data-currencies-controller.php',
'WC_REST_Network_Orders_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-network-orders-controller.php',
'WC_REST_Network_Orders_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-network-orders-v2-controller.php',
'WC_REST_Order_Notes_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-order-notes-controller.php',
'WC_REST_Order_Notes_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-order-notes-v1-controller.php',
'WC_REST_Order_Notes_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-order-notes-v2-controller.php',
'WC_REST_Order_Refunds_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-order-refunds-controller.php',
'WC_REST_Order_Refunds_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-order-refunds-v1-controller.php',
'WC_REST_Order_Refunds_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-order-refunds-v2-controller.php',
'WC_REST_Orders_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-orders-controller.php',
'WC_REST_Orders_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-orders-v1-controller.php',
'WC_REST_Orders_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-orders-v2-controller.php',
'WC_REST_Payment_Gateways_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-payment-gateways-controller.php',
'WC_REST_Payment_Gateways_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-payment-gateways-v2-controller.php',
'WC_REST_Posts_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-posts-controller.php',
'WC_REST_Product_Attribute_Terms_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-product-attribute-terms-controller.php',
'WC_REST_Product_Attribute_Terms_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-product-attribute-terms-v1-controller.php',
'WC_REST_Product_Attribute_Terms_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-product-attribute-terms-v2-controller.php',
'WC_REST_Product_Attributes_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-product-attributes-controller.php',
'WC_REST_Product_Attributes_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-product-attributes-v1-controller.php',
'WC_REST_Product_Attributes_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-product-attributes-v2-controller.php',
'WC_REST_Product_Categories_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-product-categories-controller.php',
'WC_REST_Product_Categories_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-product-categories-v1-controller.php',
'WC_REST_Product_Categories_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-product-categories-v2-controller.php',
'WC_REST_Product_Reviews_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-product-reviews-controller.php',
'WC_REST_Product_Reviews_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-product-reviews-v1-controller.php',
'WC_REST_Product_Reviews_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-product-reviews-v2-controller.php',
'WC_REST_Product_Shipping_Classes_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-product-shipping-classes-controller.php',
'WC_REST_Product_Shipping_Classes_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-product-shipping-classes-v1-controller.php',
'WC_REST_Product_Shipping_Classes_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-product-shipping-classes-v2-controller.php',
'WC_REST_Product_Tags_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-product-tags-controller.php',
'WC_REST_Product_Tags_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-product-tags-v1-controller.php',
'WC_REST_Product_Tags_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-product-tags-v2-controller.php',
'WC_REST_Product_Variations_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-product-variations-controller.php',
'WC_REST_Product_Variations_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-product-variations-v2-controller.php',
'WC_REST_Products_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-products-controller.php',
'WC_REST_Products_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-products-v1-controller.php',
'WC_REST_Products_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-products-v2-controller.php',
'WC_REST_Report_Coupons_Totals_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-report-coupons-totals-controller.php',
'WC_REST_Report_Customers_Totals_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-report-customers-totals-controller.php',
'WC_REST_Report_Orders_Totals_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-report-orders-totals-controller.php',
'WC_REST_Report_Products_Totals_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-report-products-totals-controller.php',
'WC_REST_Report_Reviews_Totals_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-report-reviews-totals-controller.php',
'WC_REST_Report_Sales_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-report-sales-controller.php',
'WC_REST_Report_Sales_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-report-sales-v1-controller.php',
'WC_REST_Report_Sales_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-report-sales-v2-controller.php',
'WC_REST_Report_Top_Sellers_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-report-top-sellers-controller.php',
'WC_REST_Report_Top_Sellers_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-report-top-sellers-v1-controller.php',
'WC_REST_Report_Top_Sellers_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-report-top-sellers-v2-controller.php',
'WC_REST_Reports_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-reports-controller.php',
'WC_REST_Reports_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-reports-v1-controller.php',
'WC_REST_Reports_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-reports-v2-controller.php',
'WC_REST_Setting_Options_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-setting-options-controller.php',
'WC_REST_Setting_Options_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-setting-options-v2-controller.php',
'WC_REST_Settings_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-settings-controller.php',
'WC_REST_Settings_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-settings-v2-controller.php',
'WC_REST_Shipping_Methods_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-shipping-methods-controller.php',
'WC_REST_Shipping_Methods_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-shipping-methods-v2-controller.php',
'WC_REST_Shipping_Zone_Locations_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-shipping-zone-locations-controller.php',
'WC_REST_Shipping_Zone_Locations_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-shipping-zone-locations-v2-controller.php',
'WC_REST_Shipping_Zone_Methods_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-shipping-zone-methods-controller.php',
'WC_REST_Shipping_Zone_Methods_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-shipping-zone-methods-v2-controller.php',
'WC_REST_Shipping_Zones_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-shipping-zones-controller.php',
'WC_REST_Shipping_Zones_Controller_Base' => $baseDir . '/src/Controllers/Version3/class-wc-rest-shipping-zones-controller-base.php',
'WC_REST_Shipping_Zones_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-shipping-zones-v2-controller.php',
'WC_REST_System_Status_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-system-status-controller.php',
'WC_REST_System_Status_Tools_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-system-status-tools-controller.php',
'WC_REST_System_Status_Tools_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-system-status-tools-v2-controller.php',
'WC_REST_System_Status_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-system-status-v2-controller.php',
'WC_REST_Tax_Classes_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-tax-classes-controller.php',
'WC_REST_Tax_Classes_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-tax-classes-v1-controller.php',
'WC_REST_Tax_Classes_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-tax-classes-v2-controller.php',
'WC_REST_Taxes_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-taxes-controller.php',
'WC_REST_Taxes_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-taxes-v1-controller.php',
'WC_REST_Taxes_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-taxes-v2-controller.php',
'WC_REST_Terms_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-terms-controller.php',
'WC_REST_Webhook_Deliveries_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-webhook-deliveries-v1-controller.php',
'WC_REST_Webhook_Deliveries_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-webhook-deliveries-v2-controller.php',
'WC_REST_Webhooks_Controller' => $baseDir . '/src/Controllers/Version3/class-wc-rest-webhooks-controller.php',
'WC_REST_Webhooks_V1_Controller' => $baseDir . '/src/Controllers/Version1/class-wc-rest-webhooks-v1-controller.php',
'WC_REST_Webhooks_V2_Controller' => $baseDir . '/src/Controllers/Version2/class-wc-rest-webhooks-v2-controller.php',
);

View File

@ -4,11 +4,26 @@
"homepage": "https://github.com/woocommerce/woocommerce-rest-api",
"type": "project",
"license": "GPL-3.0-or-later",
"type": "wordpress-plugin",
"prefer-stable": true,
"minimum-stability": "dev",
"require-dev": {
"phpunit/phpunit": "6.5.14",
"woocommerce/woocommerce-sniffs": "0.0.6"
"woocommerce/woocommerce-sniffs": "0.0.6",
"slowprog/composer-copy-file": "~0.3"
},
"scripts": {
"post-install-cmd": [
"composer dump-autoload"
],
"post-update-cmd": [
"composer dump-autoload"
],
"pre-autoload-dump": [
"composer dump-autoload --no-dev --no-scripts",
"SlowProg\\CopyFile\\ScriptHandler::copy",
"sed -i '' 's/\\$baseDir = dirname(\\$vendorDir)/\\$baseDir = __DIR__/g' classmap.php"
]
},
"autoload": {
"classmap": [
@ -19,5 +34,13 @@
"psr-4": {
"WooCommerce\\RestApi\\": "src/"
}
},
"extra": {
"copy-file": {
"vendor/composer/autoload_classmap.php": "classmap.php"
},
"copy-file-dev": {
"vendor/composer/autoload_classmap.php": "classmap.php"
}
}
}

50
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "5ccf61a66c975a3a1861e095855c10e4",
"content-hash": "e99b4d430b010135822c6362c7ebd4a6",
"packages": [],
"packages-dev": [
{
@ -1604,6 +1604,54 @@
"homepage": "https://github.com/sebastianbergmann/version",
"time": "2016-10-03T07:35:21+00:00"
},
{
"name": "slowprog/composer-copy-file",
"version": "0.3.1",
"source": {
"type": "git",
"url": "https://github.com/slowprog/CopyFile.git",
"reference": "5a82ba4613948f70b2d4b041c13c1c9259fc6477"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/slowprog/CopyFile/zipball/5a82ba4613948f70b2d4b041c13c1c9259fc6477",
"reference": "5a82ba4613948f70b2d4b041c13c1c9259fc6477",
"shasum": ""
},
"require": {
"php": ">=5.6"
},
"require-dev": {
"composer/composer": "1.0.*@dev",
"mikey179/vfsstream": "~1",
"php-mock/php-mock-phpunit": "~1",
"phpunit/phpunit": "5.7.27",
"symfony/filesystem": "~2.7",
"symfony/finder": "~2.7"
},
"type": "library",
"autoload": {
"psr-4": {
"SlowProg\\CopyFile\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Andrey Tyshev",
"email": "slowprog@gmail.com"
}
],
"description": "Composer script copying your files after install",
"homepage": "https://github.com/SlowProg/composer-copy-file",
"keywords": [
"copy file"
],
"time": "2019-01-10T05:06:10+00:00"
},
{
"name": "squizlabs/php_codesniffer",
"version": "3.4.2",

View File

@ -6,6 +6,12 @@
*/
return function() {
require __DIR__ . '/src/Server.php';
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require __DIR__ . '/vendor/autoload.php';
} else {
require __DIR__ . '/src/Autoloader.php';
$classmap = require 'classmap.php';
\WooCommerce\RestApi\Autoloader::register( $classmap );
}
\WooCommerce\RestApi\Server::instance()->init();
};

82
src/Autoloader.php Normal file
View File

@ -0,0 +1,82 @@
<?php
/**
* Custom Autoloader used when included inside another project as a package.
*
* Since this package can be included multiple times, but we want to ensure the latest version
* is loaded (rather than the first package found), we need a custom autoloader.
*
* This autoloader is only loaded for the chosen (latest) package.
*
* For version 4 onwards, we use psr-4 class naming.
* For version 3 and below, we use a classmap to maintain class naming/backwards compatibility.
*
* @package WooCommerce/RestApi
*/
namespace WooCommerce\RestApi;
defined( 'ABSPATH' ) || exit;
/**
* Autoloader class.
*/
class Autoloader {
/**
* Holds the classmap.
*
* @var array
*/
protected static $classmap = [];
/**
* Register the autoloader.
*
* @param array $classmap Classmap of files to include.
*/
public static function register( $classmap ) {
self::$classmap = (array) $classmap;
spl_autoload_register( array( __CLASS__, 'autoload_class' ) );
}
/**
* Try to autoload a class.
*
* @param string $class Class being autoloaded.
* @return boolean
*/
public static function autoload_class( $class ) {
$prefix = 'WooCommerce\\RestApi\\';
$base_dir = __DIR__ . '/';
// does the class use the namespace prefix?
$len = strlen( $prefix );
if ( strncmp( $prefix, $class, $len ) !== 0 ) {
return self::autoload_from_classmap( $class );
}
$relative_class = substr( $class, $len );
$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
if ( file_exists( $file ) ) {
return include $file;
}
}
/**
* Try to autoload a class from a classmap.
*
* @param string $class Class being autoloaded.
* @return boolean
*/
protected static function autoload_from_classmap( $class ) {
if ( empty( self::$classmap ) ) {
return false;
}
if ( array_key_exists( $class, self::$classmap ) ) {
return include self::$classmap[ $class ];
}
}
}

View File

@ -9,12 +9,6 @@ namespace WooCommerce\RestApi;
defined( 'ABSPATH' ) || exit;
if ( file_exists( __DIR__ . '/../vendor/autoload.php' ) ) {
require __DIR__ . '/../vendor/autoload.php';
} else {
wp_die( 'WooCommerce Rest API build required' );
}
use WooCommerce\RestApi\Utilities\SingletonTrait;
/**