/var/www/html_de/wp-content/plugins/woocommerce/packages/email-editor/src/class-container.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
 * This file is part of the WooCommerce Email Editor package
 *
 * @package Automattic\WooCommerce\EmailEditor
 */

declare( strict_types );
namespace 
Automattic\WooCommerce\EmailEditor;

/**
 * Class Container is a simple dependency injection container.
 *
 * @package Automattic\WooCommerce\EmailEditor
 */
class Container {
    
/**
     * A list of registered services
     *
     * @var array<string, callable> $services
     */
    
protected array $services = array();

    
/**
     * A list of created instances
     *
     * @var array<string, object> $instances
     */
    
protected array $instances = array();

    
/**
     * The method for registering a new service.
     *
     * @param string   $name     The name of the service.
     * @param callable $callback The callable that will be used to create the service.
     * @return void
     * @phpstan-template T of object
     * @phpstan-param class-string<T> $name
     */
    
public function setstring $name, callable $callback ): void {
        
$this->services$name ] = $callback;
    }

    
/**
     * Method for getting a registered service.
     *
     * @param string $name The name of the service.
     * @return object The service instance.
     * @throws \Exception If the service is not found.
     * @phpstan-template T of object
     * @phpstan-param class-string<T> $name
     * @phpstan-return T
     */
    
public function getstring $name ): object {
        
// Check if the service is already instantiated.
        
if ( isset( $this->instances$name ] ) ) {
            
/**
             * Instance.
             *
             * @var T $instance Instance of requested service.
             */
            
$instance $this->instances$name ];
            return 
$instance;
        }

        
// Check if the service is registered.
        
if ( ! isset( $this->services$name ] ) ) {
            throw new 
\Exceptionesc_html"Service not found: $name) );
        }

        
/**
         * Instance.
         *
         * @var T $instance Instance of requested service.
         */
        
$instance                 $this->services$name ]( $this );
        
$this->instances$name ] = $instance;

        return 
$instance;
    }
}