]> git.andy128k.dev Git - missing-tools.git/commitdiff
dependency injection container
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 17 Dec 2013 19:35:30 +0000 (21:35 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 17 Dec 2013 19:35:30 +0000 (21:35 +0200)
src/container.php [new file with mode: 0644]
t/ContainerTest.php [new file with mode: 0644]

diff --git a/src/container.php b/src/container.php
new file mode 100644 (file)
index 0000000..6859be7
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+
+namespace PFF;
+
+// Dependency injection container
+
+class Container
+{
+    private $values = array();
+
+    public function getWithArgs($id, $args)
+    {
+        if (!array_key_exists($id, $this->values))
+            throw new InvalidArgumentException('Identifier "'.$id.'" is not defined.');
+
+        $v = $this->values[$id];
+        if ($v[0] == 'function') {
+            return call_user_func_array($v[1], $args);
+        } elseif ($v[0] == 'factory') {
+            $value = call_user_func_array($v[1], $args);
+            $this->values[$id] = array('value', $value);
+            return $value;
+        } else /*value*/ {
+            return $v[1];
+        }
+    }
+
+    public function get()
+    {
+        $args = func_get_args();
+        $id = array_shift($args);
+        return $this->getWithArgs($id, $args);
+    }
+
+    public function __call($name, $args)
+    {
+        switch ($name) {
+        case 'set':
+            $this->values[$args[0]] = array('value', $args[1]);
+            break;
+        case 'setFunction':
+            $this->values[$args[0]] = array('function', $args[1]);
+            break;
+        case 'setFactory':
+            $this->values[$args[0]] = array('factory', $args[1]);
+            break;
+        default:
+            return $this->getWithArgs($name, $args);
+        }
+    }
+
+    private static $instance = null;
+
+    public static function getInstance()
+    {
+        if (!self::$instance)
+            self::$instance = new self;
+        return self::$instance;
+    }
+
+    public static function __callStatic($name, $args)
+    {
+        return call_user_func_array(array(self::getInstance(), $name), $args);
+    }
+}
+
diff --git a/t/ContainerTest.php b/t/ContainerTest.php
new file mode 100644 (file)
index 0000000..1948e68
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+class ContainerTest extends PHPUnit_Framework_TestCase
+{
+    public function testValue()
+    {
+        \PFF\Container::set('text', 'Hello, World');
+        $this->assertEquals('Hello, World', \PFF\Container::text());
+    }
+
+    public function testFunction()
+    {
+        \PFF\Container::setFunction('wrap', function($str) { return '('.$str.')'; });
+        $this->assertEquals('(i love lisp)', \PFF\Container::wrap('i love lisp'));
+    }
+
+    public function testFactory()
+    {
+        \PFF\Container::setFactory('wrapOnce', function($str) { return '('.$str.')'; });
+        $this->assertEquals('(i love lisp)', \PFF\Container::wrapOnce('i love lisp'));
+        $this->assertEquals('(i love lisp)', \PFF\Container::wrapOnce('i hate lisp'));
+    }
+}
+