From 7dbab7c26c7764f4e3cd64fb111dbf6c32a0c2a2 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Tue, 12 Mar 2024 15:14:35 +0100 Subject: [PATCH] context.php: add Dependency Injection container implementation --- inc/context.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 inc/context.php diff --git a/inc/context.php b/inc/context.php new file mode 100644 index 00000000..4746be0d --- /dev/null +++ b/inc/context.php @@ -0,0 +1,31 @@ +definitions = $definitions; + } + + public function get(string $name): mixed { + if (!isset($this->definitions[$name])) { + throw new \RuntimeException("Could not find a dependency named $name"); + } + + $ret = $this->definitions[$name]; + if (is_callable($ret) && !is_string($ret) && !is_array($ret)) { + $ret = $ret($this); + $this->definitions[$name] = $ret; + } + return $ret; + } +} + +function build_context(array $config): Context { + return new Context([ + 'config' => $config, + ]); +}