| Server IP : 151.101.66.159 / Your IP : 216.73.216.66 Web Server : Flywheel/5.1.0 System : Linux wordpress 6.12.113+ #1 SMP Thu Oct 22 02:38:20 UTC 2026 x86_64 User : root ( ) PHP Version : 7.4.33.18 Disable Function : disk_free_space,diskfreespace,dl,exec,fpassthru,getmypid,getmyuid,highlight_file,link,opcache_compile_file,opcache_get_configuration,opcache_is_script_cached,passthru,pcntl_alarm,pcntl_exec,pcntl_fork,pcntl_get_last_error,pcntl_getpriority,pcntl_setpriority,pcntl_signal,pcntl_signal_dispatch,pcntl_sigprocmask,pcntl_sigtimedwait,pcntl_sigwaitinfo,pcntl_strerror,pcntl_wait,pcntl_waitpid,pcntl_wexitstatus,pcntl_wifcontinued,pcntl_wifexited,pcntl_wifsignaled,pcntl_wifstopped,pcntl_wstopsig,pcntl_wtermsig,phpinfo,popen,posix_ctermid,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_times,posix_ttyname,posix_uname,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,shell_exec,show_source,socket_listen,source,system,virtual MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /www/wp-content/plugins/pods/vendor/vendor-prefixed/lucatume/di52/src/Builders/ |
Upload File : |
<?php
/**
* Builds and returns object instances.
*
* @package lucatume\DI52
*
* @license GPL-3.0
* Modified by Scott Kingsley Clark on 24-February-2026 using Strauss.
* @see https://github.com/BrianHenryIE/strauss
*/
namespace Pods\Prefixed\lucatume\DI52\Builders;
use Pods\Prefixed\lucatume\DI52\ContainerException;
use Pods\Prefixed\lucatume\DI52\NotFoundException;
use ReflectionException;
use ReflectionMethod;
/**
* Class ClassBuilder
*
* @package Pods\Prefixed\lucatume\DI52\Builders
*/
class ClassBuilder implements BuilderInterface, ReinitializableBuilderInterface
{
/**
* 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($id, Resolver $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;
}
}