[] */ private array $chain = []; /** * Resolutions. * * @var array * @phpstan-var array */ private array $resolutions = []; /** * Add class to injection chain. * * @since 1.6.0 * * @param string $class_name Class to add to injection chain. * @return self Modified injection chain. * * @phpstan-param class-string $class_name */ public function add_to_chain( string $class_name ): self { $new_chain = clone $this; $new_chain->chain[] = $class_name; return $new_chain; } /** * Add resolution for circular reference detection. * * @since 1.6.0 * * @param string $resolution Resolution to add. * @return self Modified injection chain. * * @phpstan-param class-string $resolution */ public function add_resolution( string $resolution ): self { $new_chain = clone $this; $new_chain->resolutions[ $resolution ] = true; return $new_chain; } /** * Get the last class that was pushed to the injection chain. * * @since 1.6.0 * * @throws \LogicException If the injection chain is accessed too early. * * @return string Last class pushed to the injection chain. * * @phpstan-return class-string */ public function get_class(): string { if ( empty( $this->chain ) ) { throw new \LogicException( 'Access to injection chain before any resolution was made.' ); } return \end( $this->chain ) ?: ''; } /** * Get the injection chain. * * @since 1.6.0 * * @return string[] Chain of injections. * * @phpstan-return class-string[] */ public function get_chain(): array { return \array_reverse( $this->chain ); } /** * Check whether the injection chain already has a given resolution. * * @since 1.6.0 * * @param string $resolution Resolution to check for. * @return bool Whether the resolution was found. * * @phpstan-param class-string $resolution */ public function has_resolution( string $resolution ): bool { return \array_key_exists( $resolution, $this->resolutions ); } /** * Check whether the injection chain already encountered a class. * * @since 1.6.0 * * @param string $class_name Class to check. * @return bool Whether the given class is already part of the chain. */ public function is_in_chain( string $class_name ): bool { return \in_array( $class_name, $this->chain, true ); } }