*/ private array $services = []; /** * Register a service factory or instance. * * @since 1.0.0 * @param string $id Service identifier. * @param callable|object $service Service factory or instance. * @throws \InvalidArgumentException If service ID is empty. */ public function set(string $id, $service): void { if (empty($id)) { throw new \InvalidArgumentException('Service ID cannot be empty'); } $this->services[$id] = $service; } /** * Get a service by ID, lazily instantiating if necessary. * * @since 1.0.0 * @param string $id Service identifier. * @return mixed Service instance. * @throws \RuntimeException If service is not found. */ public function get(string $id) { if (!isset($this->services[$id])) { throw new \RuntimeException( sprintf('Service not found: %s', sanitize_key($id)) ); } $entry = $this->services[$id]; // If it's a factory function, call it and cache the result if (is_callable($entry)) { $instance = $entry(); $this->services[$id] = $instance; return $instance; } return $entry; } }