/var/www/html_us/wp-content/plugins/wp-smtp/vendor/lucatume/di52/src/Builders/ClassBuilder.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
 * Builds and returns object instances.
 *
 * @package lucatume\DI52
 */

namespace lucatume\DI52\Builders;

use 
lucatume\DI52\ContainerException;
use 
lucatume\DI52\NotFoundException;
use 
ReflectionException;
use 
ReflectionMethod;

/**
 * Class ClassBuilder
 *
 * @package lucatume\DI52\Builders
 */
class ClassBuilder implements BuilderInterfaceReinitializableBuilderInterface
{
    
/**
     * An array cache of resolved constructor parameters, shared across all instances of the builder.
     * @var array<string,array<Parameter>>
     */
    
protected static $constructorParametersCache = [];
    
/**
     * A set of arguments that will be passed to the class constructor.
     *
     * @var array<mixed>
     */
    
protected $buildArgs;
    
/**
     * The id associated with the builder by the resolver.
     * @var string
     */
    
protected $id;
    
/**
     * The fully-qualified class name the builder should build instances of.
     *
     * @var class-string
     */
    
protected $className;
    
/**
     * A set of methods to call on the built object.
     *
     * @var array<string>|null
     */
    
protected $afterBuildMethods;

    
/**
     * A reference to the resolver currently using the builder.
     *
     * @var Resolver
     */
    
protected $resolver;

    
/**
     * Whether the $className is an implementation of $id
     * and $id is an interface.
     *
     * @var bool
     */
    
protected $isInterface false;

    
/**
     * ClassBuilder constructor.
     *
     * @param string|class-string $id                   The identifier associated with this builder.
     * @param Resolver            $resolver             A reference to the resolver currently using the builder.
     * @param string              $className            The fully-qualified class name to build instances for.
     * @param array<string>|null  $afterBuildMethods    An optional set of methods to call on the built object.
     * @param mixed               ...$buildArgs         An optional set of build arguments that should be provided to
     *                                                  the class constructor method.
     *
     * @throws NotFoundException If the class does not exist.
     */
    
public function __construct($idResolver $resolver$className, array $afterBuildMethods null, ...$buildArgs)
    {
        if (!
class_exists($className)) {
            throw new 
NotFoundException(
                
"nothing is bound to the '$className' id and it's not an existing or instantiable class."
            
);
        }

        
$interfaces class_implements($className);

        if (
$interfaces && isset($interfaces[$id])) {
            
$this->isInterface true;
        }

        
$this->id $id;
        
$this->className $className;
        
$this->afterBuildMethods $afterBuildMethods;
        
$this->resolver $resolver;
        
$this->buildArgs $buildArgs;
    }

    
/**
     * Builds and returns an instance of the class.
     *
     * @return object An instance of the class.
     *
     * @throws ContainerException
     */
    
public function build()
    {
        
$constructorArgs $this->resolveConstructorParameters();
        
$built = new $this->className(...$constructorArgs);
        foreach ((array)
$this->afterBuildMethods as $afterBuildMethod) {
            
$built->{$afterBuildMethod}();
        }
        return 
$built;
    }

    
/**
     * Resolves the constructor arguments to concrete implementations or values.
     *
     * @return array<mixed> A set of resolved constructor arguments.
     *
     * @throws ContainerException If a constructor argument resolution raises issues.
     */
    
protected function resolveConstructorParameters()
    {
        
$constructorArgs = [];

        
/** @var Parameter $parameter */
        
foreach ($this->getResolvedConstructorParameters($this->className) as $i => $parameter) {
            
$this->resolver->addToBuildLine((string)$parameter->getType(), $parameter->getName());
            if (isset(
$this->buildArgs[$i])) {
                
$arg $this->buildArgs[$i];
                if (
$arg instanceof BuilderInterface) {
                    
$constructorArgs[] = $arg->build();
                    continue;
                }

                
$constructorArgs[] = $this->resolveBuildArg($this->buildArgs[$i]);
                continue;
            }

            
$constructorArgs [] = $this->resolveParameter($parameter);
            
$this->resolver->buildLinePop();
        }

        return 
$constructorArgs;
    }

    
/**
     * Returns a set of resolved constructor parameters.
     *
     * @param  class-string  $className  The fully-qualified class name to get the resolved constructor parameters yet.
     *
     * @return array<Parameter> A set of resolved constructor parameters.
     *
     * @throws ContainerException If the resolution of any constructor parameters is problematic.
     */
    
protected function getResolvedConstructorParameters($className)
    {
        if (isset(
self::$constructorParametersCache[$className])) {
            return 
self::$constructorParametersCache[$className];
        }

        try {
            
$constructorReflection = new ReflectionMethod($className'__construct');
        } catch (
ReflectionException $e) {
            static::
$constructorParametersCache[$className] = [];
            
// No constructor method, no args.
            
return [];
        }

        if (!
$constructorReflection->isPublic()) {
            throw new 
ContainerException("constructor method is not public.");
        }

        
$parameters = [];

        foreach (
$constructorReflection->getParameters() as $i => $reflectionParameter) {
            
$parameters[] = new Parameter($i$reflectionParameter);
        }

        
self::$constructorParametersCache[$className] = $parameters;

        return 
$parameters;
    }

    
/**
     * Resolves a build argument to a concrete implementation.
     *
     * @param mixed $arg The argument id or value to resolve.
     *
     * @return mixed The resolved build argument.
     *
     * @throws NotFoundException
     */
    
protected function resolveBuildArg($arg)
    {
        if (
is_string($arg) && ($this->resolver->isBound($arg) || class_exists($arg))) {
            return 
$this->resolver->resolve($arg);
        }
        return 
$arg;
    }

    
/**
     * Resolves a parameter to a concrete implementation or value.
     *
     * @param Parameter $parameter The parameter to resolve.
     *
     * @return mixed The resolved parameter.
     *
     * @throws ContainerException If the parameter resolution fails.
     */
    
protected function resolveParameter(Parameter $parameter)
    {
        
$paramClass $parameter->getClass();

        if (
$paramClass) {
            
$parameterImplementation $this->resolver->whenNeedsGive($this->id$paramClass);
        } elseif (
$this->isInterface) {
            
$name $parameter->getName();
            
// If an interface was requested, resolve the underlying concrete class instead.
            
$parameterImplementation $this->resolver->whenNeedsGive($this->className"\$$name");
        } else {
            
$name $parameter->getName();
            
$parameterImplementation $this->resolver->whenNeedsGive($this->id"\$$name");
        }

        try {
            return 
$parameterImplementation instanceof BuilderInterface ?
                
$parameterImplementation->build()
                : 
$this->resolver->resolve($parameterImplementation);
        } catch (
NotFoundException $e) {
            return 
$parameter->getDefaultValueOrFail();
        }
    }

    
/**
     * {@inheritdoc}
     */
    
public function reinit(array $afterBuildMethods null, ...$buildArgs)
    {
        
$this->afterBuildMethods $afterBuildMethods;
        
$this->buildArgs $buildArgs;
    }
}