first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
<?php
namespace WPML\TM\Container;
use WPML\TM\ATE\ClonedSites\ApiCommunication;
use WPML\TM\ATE\ClonedSites\Lock;
use WPML\TM\ATE\Log\Storage;
use WPML\TM\Notices\AteLockNotice;
class Config {
public static function getDelegated() {
return [
'\WPML_Translation_Job_Factory' => 'wpml_tm_load_job_factory',
\WPML_TM_ATE_Job_Repository::class => 'wpml_tm_get_ate_jobs_repository',
\WPML_TM_Email_Notification_View::class => function () {
$factory = new \WPML_TM_Email_Twig_Template_Factory();
return new \WPML_TM_Email_Notification_View( $factory->create() );
},
];
}
public static function getSharedClasses() {
return [
'\WPML_TM_AMS_API',
'\WPML_TM_ATE_API',
'\WPML_TM_ATE_AMS_Endpoints',
'\WPML_TM_ATE_Authentication',
'\WPML_TM_AMS_ATE_Console_Section',
'\WPML_TM_Admin_Sections',
'\WPML_Translator_Records',
'\WPML_Translator_Admin_Records',
'\WPML_Translation_Manager_Records',
'\WPML_TM_MCS_ATE_Strings',
'\WPML_TM_AMS_Users',
'\WPML_TM_AMS_Translator_Activation_Records',
'\WPML_TM_REST_AMS_Clients',
'\WPML_TM_AMS_Check_Website_ID',
'\WPML_Translation_Job_Factory',
\WPML_TM_Translation_Status::class,
Storage::class,
ApiCommunication::class,
Lock::class,
AteLockNotice::class,
];
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace WPML\Container;
class Config {
public static function getSharedInstances() {
global $wpdb;
return [
$wpdb,
];
}
public static function getSharedClasses() {
return [
'\SitePress',
'\WPML\WP\OptionManager',
'\WP_Http',
'\WPML_WP_User_Query_Factory',
'\WPML_WP_User_Factory',
'\WPML_Notices',
\WPML_Locale::class,
\WPML_URL_Filters::class,
];
}
public static function getAliases() {
global $wpdb;
$aliases = [];
$wpdb_class = get_class( $wpdb );
if ( 'wpdb' !== $wpdb_class ) {
$aliases['wpdb'] = $wpdb_class;
}
return $aliases;
}
public static function getDelegated() {
return [
'\WPML_Notices' => 'wpml_get_admin_notices',
\WPML_REST_Request_Analyze::class => [ \WPML_REST_Request_Analyze_Factory::class, 'create' ],
\WP_Filesystem_Direct::class => 'wpml_get_filesystem_direct',
\WPML_Locale::class => [ \WPML_Locale::class, 'get_instance_from_sitepress' ],
\WPML_Post_Translation::class => [ \WPML_Post_Translation::class, 'getGlobalInstance' ],
\WPML_Term_Translation::class => [ \WPML_Term_Translation::class, 'getGlobalInstance' ],
\WPML_URL_Converter::class => [ \WPML_URL_Converter::class, 'getGlobalInstance' ],
\WPML_Post_Status::class => 'wpml_get_post_status_helper',
'\WPML_Language_Resolution' => function () {
global $wpml_language_resolution;
return $wpml_language_resolution;
},
\TranslationManagement::class => 'wpml_load_core_tm',
];
}
}

View File

@@ -0,0 +1,112 @@
<?php
namespace WPML\Container;
use WPML\Auryn\Injector as AurynInjector;
class Container {
/** @var Container $instance */
private static $instance = null;
/** @var AurynInjector|null */
private $injector = null;
private function __construct() {
$this->injector = new AurynInjector();
}
/**
* @return Container
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new Container();
}
return self::$instance;
}
/**
* class names or instances that should be shared.
* Shared means that only one instance is ever created when calling the make function.
*
* @param array $names_or_instances
*
* @throws \WPML\Auryn\ConfigException
*/
public static function share( array $names_or_instances ) {
$injector = self::get_instance()->injector;
wpml_collect( $names_or_instances )->each(
function ( $name_or_instance ) use ( $injector ) {
$injector->share( $name_or_instance );
}
);
}
/**
* This allows to define aliases classes to be used in place of type hints.
* e.g. [
* // generic => specific
* 'wpdb' => 'QM_DB',
* ]
*
* @param array $aliases
*
* @throws \WPML\Auryn\ConfigException
*/
public static function alias( array $aliases ) {
$injector = self::get_instance()->injector;
wpml_collect( $aliases )->each(
function ( $alias, $original ) use ( $injector ) {
$injector->alias( $original, $alias );
}
);
}
/**
* This allows to delegate the object instantiation to a factory.
* It can be any kind of callable (class or function).
*
* @param array $delegated [ $class_name => $instantiator ]
*
* @throws \WPML\Auryn\ConfigException
*/
public static function delegate( array $delegated ) {
$injector = self::get_instance()->injector;
wpml_collect( $delegated )->each(
function ( $instantiator, $class_name ) use ( $injector ) {
$injector->delegate( $class_name, $instantiator );
}
);
}
/**
* Make returns a new instance otherwise returns a shared instance if the
* class_name or an instance is set as shared using the share function
*
* @param string $class_name
* @param array $args
*
* @return mixed
* @throws \WPML\Auryn\InjectionException
*/
public static function make( $class_name, array $args = array() ) {
return self::get_instance()->injector->make( $class_name, $args );
}
/**
* Invoke the specified callable or class::method string, provisioning dependencies along the way
*
* @param mixed $callableOrMethodStr A valid PHP callable or a provisionable ClassName::methodName string
* @param array $args Optional array specifying params with which to invoke the provisioned callable
* @throws \WPML\Auryn\InjectionException
* @return mixed Returns the invocation result returned from calling the generated executable
*/
public static function execute( $callableOrMethodStr, array $args = [] ) {
return self::get_instance()->injector->execute( $callableOrMethodStr, $args );
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace WPML\Container;
use function WPML\FP\curryN;
if ( ! function_exists( 'WPML\Container\make' ) ) {
/**
* Curried function
*
* Make returns a new instance otherwise returns a shared instance if the
* class_name or an instance is set as shared using the share function
*
* @param string $class_name
* @param array $args
*
* @return mixed
* @throws \WPML\Auryn\InjectionException
*/
function make( $class_name = null, array $args = null ) {
$make = function ( $class_name, $args = [] ) {
if ( class_exists( $class_name ) || interface_exists( $class_name ) ) {
return Container::make( $class_name, $args );
}
return null;
};
return call_user_func_array( curryN( 1, $make ), func_get_args() );
}
}
if ( ! function_exists( 'WPML\Container\share' ) ) {
/**
* class names or instances that should be shared.
* Shared means that only one instance is ever created when calling the make function.
*
* @param array $names_or_instances
*
* @throws \WPML\Auryn\ConfigException
*/
function share( array $names_or_instances ) {
Container::share( $names_or_instances );
}
}
if ( ! function_exists( 'WPML\Container\alias' ) ) {
/**
* This allows to define aliases classes to be used in place of type hints.
* e.g. [
* // generic => specific
* 'wpdb' => 'QM_DB',
* ]
*
* @param array $aliases
*
* @throws \WPML\Auryn\ConfigException
*/
function alias( array $aliases ) {
Container::alias( $aliases );
}
}
if ( ! function_exists( 'WPML\Container\delegate' ) ) {
/**
* This allows to delegate the object instantiation to a factory.
* It can be any kind of callable (class or function).
*
* @param array $delegated [ $class_name => $instantiator ]
*
* @throws \WPML\Auryn\ConfigException
*/
function delegate( array $delegated ) {
Container::delegate( $delegated );
}
}
if ( ! function_exists( 'WPML\Container\execute' ) ) {
/**
* Curried function
*
* Invoke the specified callable or class::method string, provisioning dependencies along the way
*
* @param mixed $callableOrMethodStr A valid PHP callable or a provisionable ClassName::methodName string
* @param array $args array specifying params with which to invoke the provisioned callable
*
* @return mixed Returns the invocation result returned from calling the generated executable
* @throws \WPML\Auryn\InjectionException
*/
function execute( $callableOrMethodStr = null, $args = null ) {
return call_user_func_array( curryN( 1, [ Container::class, 'execute' ] ), func_get_args() );
}
}