Undo the registration of old singletons in the DI container.

The container will in principle be used only to register classes
in the `src` directory.

Also, CustomerProvider class removed.
This commit is contained in:
Nestor Soriano 2020-06-11 09:53:26 +02:00
parent 518c52b829
commit 281ec18158
9 changed files with 56 additions and 162 deletions

View File

@ -8,8 +8,6 @@
* @version 3.4.0 * @version 3.4.0
*/ */
use Automattic\WooCommerce\Tools\DependencyManagement\ObjectContainer;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -21,8 +19,6 @@ class WC_Checkout {
* The single instance of the class. * The single instance of the class.
* *
* @var WC_Checkout|null * @var WC_Checkout|null
*
* @deprecated 4.3.0 Use dependency injection instead, see the ObjectContainer class.
*/ */
protected static $instance = null; protected static $instance = null;
@ -53,23 +49,19 @@ class WC_Checkout {
* @since 2.1 * @since 2.1
* @static * @static
* @return WC_Checkout Main instance * @return WC_Checkout Main instance
*
* @deprecated 4.3.0 Use dependency injection instead, see the ObjectContainer class.
*/ */
public static function instance() { public static function instance() {
return self::$instance = ObjectContainer::get_instance_of( __CLASS__ ); if ( is_null( self::$instance ) ) {
} self::$instance = new self();
/** // Hook in actions once.
* Class constructor, hooks the appropriate actions. add_action( 'woocommerce_checkout_billing', array( self::$instance, 'checkout_form_billing' ) );
*/ add_action( 'woocommerce_checkout_shipping', array( self::$instance, 'checkout_form_shipping' ) );
public function __construct() {
// Hook in actions once.
add_action( 'woocommerce_checkout_billing', array( $this, 'checkout_form_billing' ) );
add_action( 'woocommerce_checkout_shipping', array( $this, 'checkout_form_shipping' ) );
// woocommerce_checkout_init action is ran once when the class is first constructed. // woocommerce_checkout_init action is ran once when the class is first constructed.
do_action( 'woocommerce_checkout_init', $this ); do_action( 'woocommerce_checkout_init', self::$instance );
}
return self::$instance;
} }
/** /**

View File

@ -9,7 +9,6 @@
*/ */
use Automattic\Jetpack\Constants; use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Tools\DependencyManagement\ObjectContainer;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
@ -29,8 +28,6 @@ class WC_Emails {
* The single instance of the class * The single instance of the class
* *
* @var WC_Emails * @var WC_Emails
*
* @deprecated 4.3.0 Use dependency injection instead, see the ObjectContainer class.
*/ */
protected static $_instance = null; protected static $_instance = null;
@ -49,11 +46,12 @@ class WC_Emails {
* @since 2.1 * @since 2.1
* @static * @static
* @return WC_Emails Main instance * @return WC_Emails Main instance
*
* @deprecated 4.3.0 Use dependency injection instead, see the ObjectContainer class.
*/ */
public static function instance() { public static function instance() {
return self::$_instance = ObjectContainer::get_instance_of( __CLASS__ ); if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
} }
/** /**
@ -292,14 +290,12 @@ class WC_Emails {
array( array(
'{site_title}', '{site_title}',
'{site_address}', '{site_address}',
'{site_url}',
'{woocommerce}', '{woocommerce}',
'{WooCommerce}', '{WooCommerce}',
), ),
array( array(
$this->get_blogname(), $this->get_blogname(),
$domain, $domain,
$domain,
'<a href="https://woocommerce.com">WooCommerce</a>', '<a href="https://woocommerce.com">WooCommerce</a>',
'<a href="https://woocommerce.com">WooCommerce</a>', '<a href="https://woocommerce.com">WooCommerce</a>',
), ),

View File

@ -8,8 +8,6 @@
* @package WooCommerce/Classes/Payment * @package WooCommerce/Classes/Payment
*/ */
use Automattic\WooCommerce\Tools\DependencyManagement\ObjectContainer;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -29,8 +27,6 @@ class WC_Payment_Gateways {
* *
* @var WC_Payment_Gateways * @var WC_Payment_Gateways
* @since 2.1.0 * @since 2.1.0
*
* @deprecated 4.3.0 Use dependency injection instead, see the ObjectContainer class.
*/ */
protected static $_instance = null; protected static $_instance = null;
@ -41,11 +37,12 @@ class WC_Payment_Gateways {
* *
* @since 2.1 * @since 2.1
* @return WC_Payment_Gateways Main instance * @return WC_Payment_Gateways Main instance
*
* @deprecated 4.3.0 Use dependency injection instead, see the ObjectContainer class.
*/ */
public static function instance() { public static function instance() {
return self::$_instance = ObjectContainer::get_instance_of( __CLASS__ ); if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
} }
/** /**

View File

@ -9,7 +9,6 @@
*/ */
use Automattic\Jetpack\Constants; use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Tools\DependencyManagement\ObjectContainer;
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; exit;
@ -53,8 +52,6 @@ class WC_Shipping {
* *
* @var WC_Shipping * @var WC_Shipping
* @since 2.1 * @since 2.1
*
* @deprecated 4.3.0 Use dependency injection instead, see the ObjectContainer class.
*/ */
protected static $_instance = null; protected static $_instance = null;
@ -65,11 +62,12 @@ class WC_Shipping {
* *
* @since 2.1 * @since 2.1
* @return WC_Shipping Main instance * @return WC_Shipping Main instance
*
* @deprecated 4.3.0 Use dependency injection instead, see the ObjectContainer class.
*/ */
public static function instance() { public static function instance() {
return self::$_instance = ObjectContainer::get_instance_of( __CLASS__ ); if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
} }
/** /**
@ -334,26 +332,7 @@ class WC_Shipping {
if ( ! is_array( $stored_rates ) || $package_hash !== $stored_rates['package_hash'] || 'yes' === get_option( 'woocommerce_shipping_debug_mode', 'no' ) ) { if ( ! is_array( $stored_rates ) || $package_hash !== $stored_rates['package_hash'] || 'yes' === get_option( 'woocommerce_shipping_debug_mode', 'no' ) ) {
foreach ( $this->load_shipping_methods( $package ) as $shipping_method ) { foreach ( $this->load_shipping_methods( $package ) as $shipping_method ) {
if ( ! $shipping_method->supports( 'shipping-zones' ) || $shipping_method->get_instance_id() ) { if ( ! $shipping_method->supports( 'shipping-zones' ) || $shipping_method->get_instance_id() ) {
/** $package['rates'] = $package['rates'] + $shipping_method->get_rates_for_package( $package ); // + instead of array_merge maintains numeric keys
* Fires before getting shipping rates for a package.
*
* @since 4.3.0
* @param array $package Package of cart items.
* @param WC_Shipping_Method $shipping_method Shipping method instance.
*/
do_action( 'woocommerce_before_get_rates_for_package', $package, $shipping_method );
// Use + instead of array_merge to maintain numeric keys.
$package['rates'] = $package['rates'] + $shipping_method->get_rates_for_package( $package );
/**
* Fires after getting shipping rates for a package.
*
* @since 4.3.0
* @param array $package Package of cart items.
* @param WC_Shipping_Method $shipping_method Shipping method instance.
*/
do_action( 'woocommerce_after_get_rates_for_package', $package, $shipping_method );
} }
} }

View File

@ -8,9 +8,6 @@
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Tools\DependencyManagement\ObjectContainer;
use Automattic\WooCommerce\Providers\CustomerProvider;
/** /**
* Main WooCommerce Class. * Main WooCommerce Class.
* *
@ -39,8 +36,6 @@ final class WooCommerce {
* *
* @var WooCommerce * @var WooCommerce
* @since 2.1 * @since 2.1
*
* @deprecated 4.3.0 Use dependency injection instead, see the ObjectContainer class.
*/ */
protected static $_instance = null; protected static $_instance = null;
@ -114,20 +109,6 @@ final class WooCommerce {
*/ */
public $deprecated_hook_handlers = array(); public $deprecated_hook_handlers = array();
/**
* The container used for dependency injection.
*
* @var ObjectContainer
*/
private $container;
/**
* The instance of CustomerProvider to use.
*
* @var CustomerProvider
*/
private $customer_provider;
/** /**
* Main WooCommerce Instance. * Main WooCommerce Instance.
* *
@ -137,11 +118,12 @@ final class WooCommerce {
* @static * @static
* @see WC() * @see WC()
* @return WooCommerce - Main instance. * @return WooCommerce - Main instance.
*
* @deprecated 4.3.0 Use dependency injection instead, see the ObjectContainer class.
*/ */
public static function instance() { public static function instance() {
return self::$_instance = ObjectContainer::get_instance_of( __CLASS__ ); if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
} }
/** /**
@ -177,10 +159,7 @@ final class WooCommerce {
/** /**
* WooCommerce Constructor. * WooCommerce Constructor.
*/ */
public function __construct( ObjectContainer $container, CustomerProvider $customer_provider ) { public function __construct() {
$this->container = $container;
$this->customer_provider = $customer_provider;
$this->define_constants(); $this->define_constants();
$this->define_tables(); $this->define_tables();
$this->includes(); $this->includes();
@ -498,8 +477,8 @@ final class WooCommerce {
} }
$this->theme_support_includes(); $this->theme_support_includes();
$this->query = $this->container->get( WC_Query::class ); $this->query = new WC_Query();
$this->api = $this->container->get( WC_API::class ); $this->api = new WC_API();
$this->api->init(); $this->api->init();
} }
@ -581,13 +560,13 @@ final class WooCommerce {
$this->load_plugin_textdomain(); $this->load_plugin_textdomain();
// Load class instances. // Load class instances.
$this->product_factory = $this->container->get( WC_Product_Factory::class ); $this->product_factory = new WC_Product_Factory();
$this->order_factory = $this->container->get( WC_Order_Factory::class ); $this->order_factory = new WC_Order_Factory();
$this->countries = $this->container->get( WC_Countries::class ); $this->countries = new WC_Countries();
$this->integrations = $this->container->get( WC_Integrations::class ); $this->integrations = new WC_Integrations();
$this->structured_data = $this->container->get( WC_Structured_Data::class ); $this->structured_data = new WC_Structured_Data();
$this->deprecated_hook_handlers['actions'] = $this->container->get( WC_Deprecated_Action_Hooks::class ); $this->deprecated_hook_handlers['actions'] = new WC_Deprecated_Action_Hooks();
$this->deprecated_hook_handlers['filters'] = $this->container->get( WC_Deprecated_Filter_Hooks::class ); $this->deprecated_hook_handlers['filters'] = new WC_Deprecated_Filter_Hooks();
// Classes/actions loaded for the frontend and for ajax requests. // Classes/actions loaded for the frontend and for ajax requests.
if ( $this->is_request( 'frontend' ) ) { if ( $this->is_request( 'frontend' ) ) {
@ -775,12 +754,12 @@ final class WooCommerce {
public function initialize_cart() { public function initialize_cart() {
// Cart needs customer info. // Cart needs customer info.
if ( is_null( $this->customer ) || ! $this->customer instanceof WC_Customer ) { if ( is_null( $this->customer ) || ! $this->customer instanceof WC_Customer ) {
$this->customer = $this->customer_provider->get_logged_in_customer(); $this->customer = new WC_Customer( get_current_user_id(), true );
// Customer should be saved during shutdown. // Customer should be saved during shutdown.
add_action( 'shutdown', array( $this->customer, 'save' ), 10 ); add_action( 'shutdown', array( $this->customer, 'save' ), 10 );
} }
if ( is_null( $this->cart ) || ! $this->cart instanceof WC_Cart ) { if ( is_null( $this->cart ) || ! $this->cart instanceof WC_Cart ) {
$this->cart = $this->container->get( WC_Cart::class ); $this->cart = new WC_Cart();
} }
} }
@ -794,7 +773,7 @@ final class WooCommerce {
// Session class, handles session data for users - can be overwritten if custom handler is needed. // Session class, handles session data for users - can be overwritten if custom handler is needed.
$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' ); $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
if ( is_null( $this->session ) || ! $this->session instanceof $session_class ) { if ( is_null( $this->session ) || ! $this->session instanceof $session_class ) {
$this->session = $this->container->get( $session_class ); $this->session = new $session_class();
$this->session->init(); $this->session->init();
} }
} }
@ -834,9 +813,11 @@ final class WooCommerce {
* Get queue instance. * Get queue instance.
* *
* @return WC_Queue_Interface * @return WC_Queue_Interface
*
* @deprecated 4.3.0 Use the container to get an instance of WC_Queue_Interface instead.
*/ */
public function queue() { public function queue() {
return $this->container->get( WC_Queue_Interface::class ); return WC_Queue::instance();
} }
/** /**
@ -845,7 +826,7 @@ final class WooCommerce {
* @return WC_Checkout * @return WC_Checkout
*/ */
public function checkout() { public function checkout() {
return $this->container->get( WC_Checkout::class ); return WC_Checkout::instance();
} }
/** /**
@ -854,7 +835,7 @@ final class WooCommerce {
* @return WC_Payment_Gateways * @return WC_Payment_Gateways
*/ */
public function payment_gateways() { public function payment_gateways() {
return $this->container->get( WC_Payment_Gateways::class ); return WC_Payment_Gateways::instance();
} }
/** /**
@ -863,7 +844,7 @@ final class WooCommerce {
* @return WC_Shipping * @return WC_Shipping
*/ */
public function shipping() { public function shipping() {
return $this->container->get( WC_Shipping::class ); return WC_Shipping::instance();
} }
/** /**
@ -872,7 +853,7 @@ final class WooCommerce {
* @return WC_Emails * @return WC_Emails
*/ */
public function mailer() { public function mailer() {
return $this->container->get( WC_Emails::class ); return WC_Emails::instance();
} }
/** /**

View File

@ -6,8 +6,6 @@
* @package WooCommerce/Interface * @package WooCommerce/Interface
*/ */
use Automattic\WooCommerce\Tools\DependencyManagement\ObjectContainer;
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly. exit; // Exit if accessed directly.
} }
@ -19,7 +17,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* *
* @version 3.5.0 * @version 3.5.0
* *
* @deprecated 4.3.0 Use dependency injection instead to get an instance of WC_Query_Interface, see the ObjectContainer class. * @deprecated 4.3.0 Use the container to get an instance of WC_Queue_Interface instead.
*/ */
class WC_Queue { class WC_Queue {
@ -43,7 +41,13 @@ class WC_Queue {
* @return WC_Queue_Interface * @return WC_Queue_Interface
*/ */
final public static function instance() { final public static function instance() {
return self::$instance = ObjectContainer::get_instance_of( WC_Queue_Interface::class );
if ( is_null( self::$instance ) ) {
$class = self::get_class();
self::$instance = new $class();
self::$instance = self::validate_instance( self::$instance );
}
return self::$instance;
} }
/** /**

View File

@ -1,36 +0,0 @@
<?php
/**
* CustomerProvider class file.
*
* @package Automattic\WooCommerce\Providers
*/
namespace Automattic\WooCommerce\Providers;
use \WC_Customer;
/**
* Provides methods to retrieve customer objects.
*/
class CustomerProvider {
/**
* Get a customer object for the currently logged in user.
*
* @return WC_Customer Customer object for the currently logged in user.
*/
public function get_logged_in_customer() {
return new WC_Customer( get_current_user_id(), true );
}
/**
* Get a customer object by id.
*
* @param int $id The id of the customer to retrieve.
*
* @return WC_Customer Customer object for the specified id.
*/
public function get_customer_by_id( int $id ) {
return new WC_Customer( $id );
}
}

View File

@ -101,8 +101,7 @@ final class ObjectContainer {
self::$instance->reflection_container = new WooReflectionContainer(); self::$instance->reflection_container = new WooReflectionContainer();
$container->delegate( self::$instance->reflection_container ); $container->delegate( self::$instance->reflection_container );
// Perform any required manual class and service provider registration. // Perform any required service provider registration.
self::$instance->register_classes();
self::$instance->register_service_providers(); self::$instance->register_service_providers();
} }
@ -115,24 +114,6 @@ final class ObjectContainer {
$this->container = $container; $this->container = $container;
} }
/**
* Register class resolutions for which default autowiring is not appropriate/enough.
*/
private function register_classes() {
$singletons = array(
\CustomerProvider::class,
\WC_Checkout::class,
\WC_Emails::class,
\WC_Payment_Gateways::class,
\WC_Shipping::class,
\WooCommerce::class,
);
foreach ( $singletons as $class_name ) {
$this->reflection_container->share( $class_name );
}
}
/** /**
* Register all the service providers inside the ServiceProviders directory. * Register all the service providers inside the ServiceProviders directory.
*/ */

View File

@ -46,7 +46,7 @@ ObjectContainer::init();
* @return WooCommerce * @return WooCommerce
*/ */
function WC() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid function WC() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
return ObjectContainer::get_instance_of( WooCommerce::class ); return WooCommerce::instance();
} }
// Global for backwards compatibility. // Global for backwards compatibility.