Test without init code - just an autoloader

This commit is contained in:
Mike Jolley 2019-06-20 16:51:47 +01:00
parent 3be0213828
commit 97bf084d81
8 changed files with 69 additions and 248 deletions

View File

@ -1,104 +0,0 @@
<?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

@ -6,6 +6,9 @@
"type": "wordpress-plugin",
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
"automattic/jetpack-autoloader": "^1"
},
"require-dev": {
"phpunit/phpunit": "6.5.14",
"woocommerce/woocommerce-sniffs": "0.0.6",
@ -17,27 +20,11 @@
],
"post-update-cmd": [
"composer dump-autoload"
],
"pre-autoload-dump": [
"composer dump-autoload --no-dev --no-scripts",
"SlowProg\\CopyFile\\ScriptHandler::copy",
"sed -i.bak -e 's/\\$baseDir = dirname(\\$vendorDir)/\\$baseDir = __DIR__/g' classmap.php",
"rm classmap.php.bak"
]
},
"autoload": {
"classmap": [
"src/Controllers/Version1",
"src/Controllers/Version2",
"src/Controllers/Version3"
]
},
"extra": {
"copy-file": {
"vendor/composer/autoload_classmap.php": "classmap.php"
},
"copy-file-dev": {
"vendor/composer/autoload_classmap.php": "classmap.php"
"psr-4": {
"WooCommerce\\RestApi\\": "src"
}
}
}

41
composer.lock generated
View File

@ -4,8 +4,45 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "e99b4d430b010135822c6362c7ebd4a6",
"packages": [],
"content-hash": "0654700fc5735080c502c67e64be6c00",
"packages": [
{
"name": "automattic/jetpack-autoloader",
"version": "v1.1.0",
"source": {
"type": "git",
"url": "https://github.com/Automattic/jetpack-autoloader.git",
"reference": "cf08e893a481e71feec6e61164efff0eeee98ccd"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Automattic/jetpack-autoloader/zipball/cf08e893a481e71feec6e61164efff0eeee98ccd",
"reference": "cf08e893a481e71feec6e61164efff0eeee98ccd",
"shasum": ""
},
"require": {
"composer-plugin-api": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^5.7 || ^6.5 || ^7.5"
},
"type": "composer-plugin",
"extra": {
"class": "Automattic\\Jetpack\\Autoloader\\CustomAutoloaderPlugin"
},
"autoload": {
"psr-4": {
"Automattic\\Jetpack\\Autoloader\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"GPL-2.0-or-later"
],
"description": "Creates a custom autoloader for a plugin or theme.",
"time": "2019-06-17T13:03:00+00:00"
}
],
"packages-dev": [
{
"name": "dealerdirect/phpcodesniffer-composer-installer",

View File

@ -1,17 +0,0 @@
<?php
/**
* Anonomous function used to init this version of the REST API.
*
* @package WooCommerce/RestApi
*/
return function() {
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();
};

View File

@ -1,82 +0,0 @@
<?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

@ -66,6 +66,13 @@ class Server {
* @return array
*/
protected function get_v1_controllers() {
// Include legacy classes which are not autoloaded.
$controllers = glob( 'Version1/class-wc-*.php' );
foreach ( $controllers as $file ) {
include $file;
}
return [
'coupons' => 'WC_REST_Coupons_V1_Controller',
'customer-downloads' => 'WC_REST_Customer_Downloads_V1_Controller',
@ -96,6 +103,13 @@ class Server {
* @return array
*/
protected function get_v2_controllers() {
// Include legacy classes which are not autoloaded.
$controllers = glob( 'Version2/class-wc-*.php' );
foreach ( $controllers as $file ) {
include $file;
}
return [
'coupons' => 'WC_REST_Coupons_V2_Controller',
'customer-downloads' => 'WC_REST_Customer_Downloads_V2_Controller',
@ -137,6 +151,13 @@ class Server {
* @return array
*/
protected function get_v3_controllers() {
// Include legacy classes which are not autoloaded.
$controllers = glob( 'Version3/class-wc-*.php' );
foreach ( $controllers as $file ) {
include $file;
}
return [
'coupons' => 'WC_REST_Coupons_Controller',
'customer-downloads' => 'WC_REST_Customer_Downloads_Controller',

View File

@ -1,8 +0,0 @@
<?php
/**
* Return the package version.
*
* @package WooCommerce/RestApi
*/
return '1.0.0';

View File

@ -18,20 +18,7 @@ if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
return;
}
/**
* Get API feature plugin version and callback function.
*/
$version = include __DIR__ . '/version.php';
$init_callback = include __DIR__ . '/init.php';
/**
* This callback registers this version of the API with WooCommerce.
*/
$register_callback = function() use ( $version, $init_callback ) {
if ( ! class_exists( '\WooCommerce\Core\PackageManager' ) ) {
return;
}
\WooCommerce\Core\PackageManager::register( 'woocommerce-rest-api', $version, $init_callback, __DIR__ );
};
add_action( 'woocommerce_loaded', $register_callback );
$autoloader = __DIR__ . '/vendor/autoload_packages.php';
if ( is_readable( $autoloader ) ) {
require $autoloader;
}