Make the Content classes dependency-injectable
authorChuck Hagenbuch <chuck@horde.org>
Sat, 16 Jan 2010 20:30:54 +0000 (15:30 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Sat, 16 Jan 2010 21:30:47 +0000 (16:30 -0500)
content/app/controllers/ApplicationController.php
content/lib/Objects/Manager.php
content/lib/Tagger.php
content/lib/Types/Manager.php
content/lib/Users/Manager.php
content/test/AllTests.php
content/test/Tags/TaggerTest.php

index 87d2898..b4c04d5 100644 (file)
@@ -15,19 +15,6 @@ class Content_ApplicationController extends Horde_Controller_Base
     protected function _initializeApplication()
     {
         $CONTENT_DIR = dirname(__FILE__) . '/../';
-
-        $this->db = $GLOBALS['injector']->getInstance('db-writer');
-        $context = array('dbAdapter' => $this->db);
-
-        $this->typeManager = new Content_Types_Manager($context);
-        $context['typeManager'] = $this->typeManager;
-
-        $this->userManager = new Content_Users_Manager($context);
-        $context['userManager'] = $this->userManager;
-
-        $this->objectManager = new Content_Objects_Manager($context);
-        $context['objectManager'] = $this->objectManager;
-
-        $this->tagger = new Content_Tagger($context);
+        $this->tagger = $GLOBALS['injector']->getInstance('Content_Tagger');
     }
 }
index 2630333..9f10af1 100644 (file)
@@ -43,19 +43,10 @@ class Content_Objects_Manager
     /**
      * Constructor
      */
-    public function __construct($context = array())
+    public function __construct(Horde_Db_Adapter_Base $db, Content_Types_Manager $typeManager)
     {
-        if (!empty($context['dbAdapter'])) {
-            $this->_db = $context['dbAdapter'];
-        }
-
-        if (!empty($context['typeManager'])) {
-            $this->_typeManager = $context['typeManager'];
-        }
-
-        if (!empty($context['tables'])) {
-            $this->_tables = array_merge($this->_tables, $context['tables']);
-        }
+        $this->_db = $db;
+        $this->_typeManager = $typeManager;
     }
 
     /**
index b19956b..cb5fc07 100644 (file)
@@ -74,30 +74,17 @@ class Content_Tagger
     protected $_defaultRadius = 10;
 
     /**
-     * Constructor - can take an array of arguments that set the managers
-     * and DbAdapter
+     * Constructor
      */
-    public function __construct($context = array())
+    public function __construct(Horde_Db_Adapter_Base $db,
+                                Content_Users_Manager $userManager,
+                                Content_Types_Manager $typeManager,
+                                Content_Objects_Manager $objectManager)
     {
-        if (!empty($context['dbAdapter'])) {
-            $this->_db = $context['dbAdapter'];
-        }
-
-        if (!empty($context['userManager'])) {
-            $this->_userManager = $context['userManager'];
-        }
-
-        if (!empty($context['typeManager'])) {
-            $this->_typeManager = $context['typeManager'];
-        }
-
-        if (!empty($context['objectManager'])) {
-            $this->_objectManager = $context['objectManager'];
-        }
-
-        if (!empty($context['tables'])) {
-            $this->_tables = array_merge($this->_tables, $context['tables']);
-        }
+        $this->_db = $db;
+        $this->_userManager = $userManager;
+        $this->_typeManager = $typeManager;
+        $this->_objectManager = $objectManager;
     }
 
     /**
index b2687c6..7436a6a 100644 (file)
@@ -32,15 +32,9 @@ class Content_Types_Manager
         'types' => 'rampage_types',
     );
 
-    public function __construct($context = array())
+    public function __construct(Horde_Db_Adapter_Base $db)
     {
-        if (!empty($context['dbAdapter'])) {
-            $this->_db = $context['dbAdapter'];
-        }
-
-        if (!empty($context['tables'])) {
-            $this->_tables = array_merge($this->_tables, $context['tables']);
-        }
+        $this->_db = $db;
     }
 
     /**
index f0204db..1c6c86d 100644 (file)
@@ -32,15 +32,9 @@ class Content_Users_Manager
         'users' => 'rampage_users',
     );
 
-    public function __construct($context = array())
+    public function __construct(Horde_Db_Adapter_Base $db)
     {
-        if (!empty($context['dbAdapter'])) {
-            $this->_db = $context['dbAdapter'];
-        }
-
-        if (!empty($context['tables'])) {
-            $this->_tables = array_merge($this->_tables, $context['tables']);
-        }
+        $this->_db = $db;
     }
 
     /**
index b8e5b09..f0aceb5 100644 (file)
@@ -8,7 +8,7 @@ if (!defined('PHPUnit_MAIN_METHOD')) {
     define('PHPUnit_MAIN_METHOD', 'Content_AllTests::main');
 }
 
-require_once 'PHPUnit/Framework/TestSuite.php';
+require_once 'PHPUnit/Framework.php';
 require_once 'PHPUnit/TextUI/TestRunner.php';
 
 require 'Horde/Autoloader.php';
index fc289a0..0e89786 100644 (file)
@@ -18,23 +18,12 @@ class Content_Tags_TaggerTest extends PHPUnit_Framework_TestCase
 {
     protected function setUp()
     {
-        $this->db = new Horde_Db_Adapter_Pdo_Sqlite(array(
-            'dbname' => ':memory:',
-        ));
+        $injector = new Horde_Injector(new Horde_Injector_TopLevel());
 
-        $context = array('dbAdapter' => $this->db);
+        $db = new Horde_Db_Adapter_Pdo_Sqlite(array('dbname' => ':memory:'));
+        $injector->setInstance('Horde_Db_Adapter_Base', $db);
 
-        $this->typeManager = new Content_Types_Manager($context);
-        $context['typeManager'] = $this->typeManager;
-
-        $this->userManager = new Content_Users_Manager($context);
-        $context['userManager'] = $this->userManager;
-
-        $this->objectManager = new Content_Objects_Manager($context);
-        $context['objectManager'] = $this->objectManager;
-
-        // Create tagger
-        $this->tagger = new Content_Tagger($context);
+        $this->tagger = $injector->getInstance('Content_Tagger');
 
         // Read sql schema file
         $statements = array();
@@ -57,7 +46,7 @@ class Content_Tags_TaggerTest extends PHPUnit_Framework_TestCase
 
         // Run statements
         foreach ($statements as $stmt) {
-            $this->db->execute($stmt);
+            $db->execute($stmt);
         }
     }