Some example of how to use Horde_Injector.
authorGunnar Wrobel <p@rdus.de>
Fri, 2 Oct 2009 13:40:33 +0000 (15:40 +0200)
committerGunnar Wrobel <p@rdus.de>
Fri, 2 Oct 2009 13:40:33 +0000 (15:40 +0200)
framework/Injector/examples/Horde/Injector/binder.php [new file with mode: 0644]
framework/Injector/examples/Horde/Injector/factory.php [new file with mode: 0644]
framework/Injector/examples/Horde/Injector/implementation.php [new file with mode: 0644]
framework/Injector/examples/Horde/Injector/setget.php [new file with mode: 0644]

diff --git a/framework/Injector/examples/Horde/Injector/binder.php b/framework/Injector/examples/Horde/Injector/binder.php
new file mode 100644 (file)
index 0000000..5058aaf
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+/**
+ * Demonstrates how we register binders so that the instances get created only
+ * when actually accessing the instance.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Injector
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @link     http://pear.horde.org/index.php?package=Injector
+ */
+
+require_once 'Horde/Autoloader.php';
+
+/**
+ * A dummy binder.
+ *
+ * @category Horde
+ * @package  Injector
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @link     http://pear.horde.org/index.php?package=Injector
+ */
+class Binder implements Horde_Injector_Binder
+{
+    /**
+     * Create an instance.
+     *
+     * @param Horde_Injector $injector The injector should provide all required
+     *                                 dependencies for creating the instance.
+     *
+     * @return mixed The concrete instance.
+     */
+    public function create(Horde_Injector $injector)
+    {
+        return 'constructed';
+    }
+
+    /**
+     * Determine if one binder equals another binder
+     *
+     * @param Horde_Injector_Binder $binder The binder to compare against $this
+     *
+     * @return bool true if they are equal, or false if they are not equal
+     */
+    public function equals(Horde_Injector_Binder $binder)
+    {
+        return false;
+    }
+}
+
+$a = new Horde_Injector(new Horde_Injector_TopLevel());
+$a->addBinder('constructed', new Binder());
+var_dump($a->getInstance('constructed'));
\ No newline at end of file
diff --git a/framework/Injector/examples/Horde/Injector/factory.php b/framework/Injector/examples/Horde/Injector/factory.php
new file mode 100644 (file)
index 0000000..a4bd4e7
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+/**
+ * Demonstrates how to use the default factory binder with Horde_Injector.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Injector
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @link     http://pear.horde.org/index.php?package=Injector
+ */
+
+require_once 'Horde/Autoloader.php';
+
+class Greet
+{
+    public function __construct($somebody)
+    {
+        $this->somebody = $somebody;
+    }
+
+    public function greet()
+    {
+        print 'Hello ' . $this->somebody;
+    }
+}
+
+class Factory
+{
+    static public function getGreeter(Horde_Injector $injector)
+    {
+        return new Greet($injector->getInstance('Person'));
+    }
+}
+
+$a = new Horde_Injector(new Horde_Injector_TopLevel());
+$a->setInstance('Person', 'Bob');
+$a->bindFactory('Greet', 'Factory', 'getGreeter');
+$a->getInstance('Greet')->greet();
\ No newline at end of file
diff --git a/framework/Injector/examples/Horde/Injector/implementation.php b/framework/Injector/examples/Horde/Injector/implementation.php
new file mode 100644 (file)
index 0000000..eb488eb
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Demonstrates how to use the default factory binder with Horde_Injector.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Injector
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @link     http://pear.horde.org/index.php?package=Injector
+ */
+
+require_once 'Horde/Autoloader.php';
+
+interface Person
+{
+    public function __toString();
+}
+
+class World implements Person
+{
+    public function __toString()
+    {
+        return 'World';
+    }
+}
+
+interface Greeter
+{
+    public function greet();
+}
+
+class Hello implements Greeter
+{
+    public function __construct(Person $somebody)
+    {
+        $this->somebody = $somebody;
+    }
+
+    public function greet()
+    {
+        print 'Hello ' . $this->somebody;
+    }
+}
+
+$a = new Horde_Injector(new Horde_Injector_TopLevel());
+$a->bindImplementation('Person', 'World');
+$a->bindImplementation('Greeter', 'Hello');
+$a->getInstance('Greeter')->greet();
\ No newline at end of file
diff --git a/framework/Injector/examples/Horde/Injector/setget.php b/framework/Injector/examples/Horde/Injector/setget.php
new file mode 100644 (file)
index 0000000..a2e0543
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+/**
+ * Demonstrates settings/getting concrete instances.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Injector
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @link     http://pear.horde.org/index.php?package=Injector
+ */
+
+require_once 'Horde/Autoloader.php';
+
+$a = new Horde_Injector(new Horde_Injector_TopLevel());
+$a->setInstance('a', 'a');
+var_dump($a->getInstance('a'));