summaryrefslogtreecommitdiff
path: root/demos/time-tracker/protected/App_Code/DaoManager.php
blob: 5e3eef56c58d1e69b800dc7f8aa5ad785fafc245 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
 * DaoManager class file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2006 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package Demos
 */
 
/**
 * DaoManager class.
 * 
 * A Registry for Dao and an implementation of that type.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Id$
 * @package Demos
 * @since 3.1
 */
class DaoManager extends TModule
{
	/**
	 * @var TSqlMapper sqlmap client
	 */
	private $_connection;
	/**
	 * @var boolean if the module has been initialized
	 */
	private $_initialized=false;	
	/**
	 * @var array registered list of dao
	 */
	private $_dao=array();
	/**
	 * Initializes the module.
	 * This method is required by IModule and is invoked by application.
	 * It loads dao information from the module configuration.
	 * @param TXmlElement module configuration
	 */
	public function init($config)
	{ 	
		if($this->_connection === null)
			throw new TimeTrackerException('daomanager_connection_required');
		$app = $this->getApplication();
		if(is_string($this->_connection))
		{
			if(($conn=$app->getModule($this->_connection)->getClient())===null)
				throw new TimeTrackerException('daomanager_undefined_connection',$this->_connection);
			if(!($conn instanceof TSqlMapper))
				throw new TimeTrackerException('daomanager_invalid_connection',	$this->_connection);
			$this->_connection = $conn;
		}
		$this->includeDaoImplementation($config->getElementsByTagName('dao'));
		$this->_initialized = true;
	}
	
	/**
	 * Register the dao type and implementation class names.
	 * @param array list of TXmlDocument nodes.
	 */
	protected function includeDaoImplementation($nodes)
	{
		foreach($nodes as $node)
		{
			$id = $node->getAttribute('id');
			$class = $node->getAttribute('class');
			$this->_dao[$id] = array('class' => $class);
		}
	}
	
	/**
	 * @return array list of registered Daos
	 */
	public function getDaos()
	{
		return $this->_dao;
	}
	
	/**
	 * Returns an implementation of a Dao type, implements the Registery
	 * pattern. Multiple calls returns the same Dao instance.
	 * @param string Dao type to find.
	 * @return object instance of the Dao implementation.
	 */
	public function getDao($class)
	{
		if(isset($this->_dao[$class]))
		{
			if(!isset($this->_dao[$class]['instance']))
			{
				$dao = Prado::createComponent($this->_dao[$class]['class']);
				$dao->setConnection($this->getConnection());
				$this->_dao[$class]['instance'] = $dao;	
			}
			return $this->_dao[$class]['instance'];
		}
		else
			throw new TimeTrackerException('daomanager_undefined_dao', $class);
	}
	
	/**
	 * @return TSqlMapper sqlmap client instance
	 */
	public function getConnection()
	{
		return $this->_connection;
	}
	
	/**
	 * Sets the connection for all Daos registered.
	 * @param string|TSqlMapper sqlmap client module id or TSqlMapper instance.
	 */
	public function setConnection($client)
	{
		if($this->_initialized)
			throw new TimeTrackerException('daomanager_unchangeable');
		if(!is_string($client) && !($client instanceof TSqlMapper))
			throw new TConfigurationException('daomanager_invalid_connection',$client);
		$this->_connection = $client;
	}
}

?>