From: Chuck Hagenbuch Date: Sat, 16 Jan 2010 20:30:54 +0000 (-0500) Subject: Make the Content classes dependency-injectable X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=59b85a6b925a2a24ec91d8f3b686ac2c6d6e336a;p=horde.git Make the Content classes dependency-injectable --- diff --git a/content/app/controllers/ApplicationController.php b/content/app/controllers/ApplicationController.php index 87d2898f0..b4c04d50a 100644 --- a/content/app/controllers/ApplicationController.php +++ b/content/app/controllers/ApplicationController.php @@ -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'); } } diff --git a/content/lib/Objects/Manager.php b/content/lib/Objects/Manager.php index 263033371..9f10af1fa 100644 --- a/content/lib/Objects/Manager.php +++ b/content/lib/Objects/Manager.php @@ -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; } /** diff --git a/content/lib/Tagger.php b/content/lib/Tagger.php index b19956b3b..cb5fc07b4 100644 --- a/content/lib/Tagger.php +++ b/content/lib/Tagger.php @@ -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; } /** diff --git a/content/lib/Types/Manager.php b/content/lib/Types/Manager.php index b2687c61a..7436a6a42 100644 --- a/content/lib/Types/Manager.php +++ b/content/lib/Types/Manager.php @@ -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; } /** diff --git a/content/lib/Users/Manager.php b/content/lib/Users/Manager.php index f0204db90..1c6c86d5c 100644 --- a/content/lib/Users/Manager.php +++ b/content/lib/Users/Manager.php @@ -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; } /** diff --git a/content/test/AllTests.php b/content/test/AllTests.php index b8e5b0918..f0aceb5b1 100644 --- a/content/test/AllTests.php +++ b/content/test/AllTests.php @@ -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'; diff --git a/content/test/Tags/TaggerTest.php b/content/test/Tags/TaggerTest.php index fc289a004..0e89786ad 100644 --- a/content/test/Tags/TaggerTest.php +++ b/content/test/Tags/TaggerTest.php @@ -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); } }