{
"event": "PreToolUse",
"tool_name": "Read",
"tool_input": {
"file_path": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Framework\/Container.php"
}
}
{
"tool_response": {
"type": "text",
"file": {
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Framework\/Container.php",
"content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Framework;\n\n\/\/ @responsibility: DI-Container mit Autowiring und Singleton-Caching\n\nuse ReflectionClass;\nuse ReflectionNamedType;\nuse ReflectionParameter;\n\nfinal class Container\n{\n \/** @var array<string, object> Cached singleton instances *\/\n private array $instances = [];\n\n \/** @var array<string, callable> Factory functions *\/\n private array $factories = [];\n\n \/**\n * Register a factory for a service.\n *\n * @param string $id Service identifier (usually class name)\n * @param callable(Container): object $factory Factory function\n *\/\n public function set(string $id, callable $factory): void\n {\n $this->factories[$id] = $factory;\n \/\/ Clear cached instance if re-registering\n unset($this->instances[$id]);\n }\n\n \/**\n * Register an existing instance as a singleton.\n *\n * @param string $id Service identifier\n * @param object $instance The instance to register\n *\/\n public function instance(string $id, object $instance): void\n {\n $this->instances[$id] = $instance;\n }\n\n \/**\n * Check if a service is registered.\n *\/\n public function has(string $id): bool\n {\n return isset($this->instances[$id]) || isset($this->factories[$id]) || class_exists($id);\n }\n\n \/**\n * Get a service by ID.\n *\n * Resolution order:\n * 1. Check cached instances\n * 2. Check registered factories\n * 3. Try autowiring if ID is a class name\n *\n * @param string $id Service identifier (usually class name)\n * @return object The resolved service instance\n * @throws ContainerException If service cannot be resolved\n *\/\n public function get(string $id): object\n {\n \/\/ Return cached instance if available\n if (isset($this->instances[$id])) {\n return $this->instances[$id];\n }\n\n \/\/ Use factory if registered\n if (isset($this->factories[$id])) {\n $this->instances[$id] = ($this->factories[$id])($this);\n\n return $this->instances[$id];\n }\n\n \/\/ Try autowiring for class names\n if (class_exists($id)) {\n $this->instances[$id] = $this->autowire($id);\n\n return $this->instances[$id];\n }\n\n throw new ContainerException(\"Service not found: {$id}\");\n }\n\n \/**\n * Autowire a class by resolving constructor dependencies.\n *\n * @param class-string $class\n * @throws ContainerException\n *\/\n private function autowire(string $class): object\n {\n $reflection = new ReflectionClass($class);\n\n if (!$reflection->isInstantiable()) {\n throw new ContainerException(\"Cannot instantiate {$class}\");\n }\n\n $constructor = $reflection->getConstructor();\n\n \/\/ No constructor = no dependencies\n if ($constructor === null) {\n return new $class();\n }\n\n $parameters = $constructor->getParameters();\n $dependencies = [];\n\n foreach ($parameters as $param) {\n $dependencies[] = $this->resolveParameter($param, $class);\n }\n\n return $reflection->newInstanceArgs($dependencies);\n }\n\n \/**\n * Resolve a single constructor parameter.\n *\n * @throws ContainerException\n *\/\n private function resolveParameter(ReflectionParameter $param, string $class): mixed\n {\n $type = $param->getType();\n\n \/\/ Handle nullable\/optional parameters with default values\n if ($param->isDefaultValueAvailable()) {\n \/\/ Try to resolve the type, but fall back to default if it fails\n if ($type instanceof ReflectionNamedType && !$type->isBuiltin()) {\n $typeName = $type->getName();\n \/\/ Only try to resolve if explicitly registered (not just class_exists)\n if (isset($this->instances[$typeName]) || isset($this->factories[$typeName])) {\n try {\n return $this->get($typeName);\n } catch (ContainerException) {\n \/\/ Fall through to default value\n }\n }\n }\n\n return $param->getDefaultValue();\n }\n\n \/\/ No type hint\n if ($type === null) {\n throw new ContainerException(\n \"Cannot resolve parameter \\${$param->getName()} in {$class}: no type hint\"\n );\n }\n\n \/\/ Union types not supported\n if (!$type instanceof ReflectionNamedType) {\n throw new ContainerException(\n \"Cannot resolve parameter \\${$param->getName()} in {$class}: union types not supported\"\n );\n }\n\n \/\/ Built-in types (string, int, etc.) cannot be autowired\n if ($type->isBuiltin()) {\n if ($type->allowsNull()) {\n return null;\n }\n\n throw new ContainerException(\n \"Cannot resolve parameter \\${$param->getName()} in {$class}: built-in type {$type->getName()}\"\n );\n }\n\n \/\/ Resolve the dependency\n return $this->get($type->getName());\n }\n}\n",
"numLines": 176,
"startLine": 1,
"totalLines": 176
}
}
}