1 |
<?php
|
2 |
/*
|
3 |
* $Id: Configurable.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $
|
4 |
*
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
16 |
*
|
17 |
* This software consists of voluntary contributions made by many individuals
|
18 |
* and is licensed under the LGPL. For more information, see
|
19 |
* <http://www.phpdoctrine.com>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_Configurable
|
24 |
* the base for Doctrine_Table, Doctrine_Manager and Doctrine_Connection
|
25 |
*
|
26 |
*
|
27 |
* @package Doctrine
|
28 |
* @subpackage Configurable
|
29 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
30 |
* @link www.phpdoctrine.com
|
31 |
* @since 1.0
|
32 |
* @version $Revision: 2963 $
|
33 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
34 |
*/
|
35 |
abstract class Doctrine_Configurable extends Doctrine_Locator_Injectable
|
36 |
{
|
37 |
/**
|
38 |
* @var array $attributes an array of containing all attributes
|
39 |
*/
|
40 |
protected $attributes = array();
|
41 |
|
42 |
/**
|
43 |
* @var Doctrine_Configurable $parent the parent of this component
|
44 |
*/
|
45 |
protected $parent;
|
46 |
|
47 |
/**
|
48 |
* @var array $_impl an array containing concrete implementations for class templates
|
49 |
* keys as template names and values as names of the concrete
|
50 |
* implementation classes
|
51 |
*/
|
52 |
protected $_impl = array();
|
53 |
|
54 |
/**
|
55 |
* setAttribute
|
56 |
* sets a given attribute
|
57 |
*
|
58 |
* <code>
|
59 |
* $manager->setAttribute(Doctrine::ATTR_PORTABILITY, Doctrine::PORTABILITY_ALL);
|
60 |
*
|
61 |
* // or
|
62 |
*
|
63 |
* $manager->setAttribute('portability', Doctrine::PORTABILITY_ALL);
|
64 |
* </code>
|
65 |
*
|
66 |
* @param mixed $attribute either a Doctrine::ATTR_* integer constant or a string
|
67 |
* corresponding to a constant
|
68 |
* @param mixed $value the value of the attribute
|
69 |
* @see Doctrine::ATTR_* constants
|
70 |
* @throws Doctrine_Exception if the value is invalid
|
71 |
* @return void
|
72 |
*/
|
73 |
public function setAttribute($attribute,$value)
|
74 |
{
|
75 |
if (is_string($attribute)) {
|
76 |
$upper = strtoupper($attribute);
|
77 |
|
78 |
$const = 'Doctrine::ATTR_' . $attribute;
|
79 |
if (defined($const)) {
|
80 |
$this->_state = constant($const);
|
81 |
} else {
|
82 |
throw new Doctrine_Exception('Unknown attribute ' . $attribute);
|
83 |
}
|
84 |
}
|
85 |
switch ($attribute) {
|
86 |
case Doctrine::ATTR_FETCHMODE:
|
87 |
throw new Doctrine_Exception('Deprecated attribute. See http://doctrine.pengus.net/doctrine/manual/new/?chapter=configuration');
|
88 |
case Doctrine::ATTR_LISTENER:
|
89 |
$this->setEventListener($value);
|
90 |
break;
|
91 |
case Doctrine::ATTR_COLL_KEY:
|
92 |
if ( ! ($this instanceof Doctrine_Table)) {
|
93 |
throw new Doctrine_Exception("This attribute can only be set at table level.");
|
94 |
}
|
95 |
if ($value !== null && ! $this->hasColumn($value)) {
|
96 |
throw new Doctrine_Exception("Couldn't set collection key attribute. No such column '$value'");
|
97 |
}
|
98 |
break;
|
99 |
case Doctrine::ATTR_CACHE:
|
100 |
if ($value !== null) {
|
101 |
if ( ! ($value instanceof Doctrine_Cache_Interface)) {
|
102 |
throw new Doctrine_Exception('Cache driver should implement Doctrine_Cache_Interface');
|
103 |
}
|
104 |
}
|
105 |
break;
|
106 |
case Doctrine::ATTR_VALIDATE:
|
107 |
case Doctrine::ATTR_QUERY_LIMIT:
|
108 |
case Doctrine::ATTR_QUOTE_IDENTIFIER:
|
109 |
case Doctrine::ATTR_PORTABILITY:
|
110 |
case Doctrine::ATTR_DEFAULT_TABLE_TYPE:
|
111 |
case Doctrine::ATTR_EMULATE_DATABASE:
|
112 |
case Doctrine::ATTR_USE_NATIVE_ENUM:
|
113 |
case Doctrine::ATTR_DEFAULT_SEQUENCE:
|
114 |
case Doctrine::ATTR_EXPORT:
|
115 |
case Doctrine::ATTR_DECIMAL_PLACES:
|
116 |
case Doctrine::ATTR_LOAD_REFERENCES:
|
117 |
case Doctrine::ATTR_RECORD_LISTENER:
|
118 |
case Doctrine::ATTR_THROW_EXCEPTIONS:
|
119 |
|
120 |
break;
|
121 |
case Doctrine::ATTR_SEQCOL_NAME:
|
122 |
if ( ! is_string($value)) {
|
123 |
throw new Doctrine_Exception('Sequence column name attribute only accepts string values');
|
124 |
}
|
125 |
break;
|
126 |
case Doctrine::ATTR_FIELD_CASE:
|
127 |
if ($value != 0 && $value != CASE_LOWER && $value != CASE_UPPER)
|
128 |
throw new Doctrine_Exception('Field case attribute should be either 0, CASE_LOWER or CASE_UPPER constant.');
|
129 |
break;
|
130 |
case Doctrine::ATTR_SEQNAME_FORMAT:
|
131 |
case Doctrine::ATTR_IDXNAME_FORMAT:
|
132 |
if ($this instanceof Doctrine_Table) {
|
133 |
throw new Doctrine_Exception('Sequence / index name format attributes cannot be set'
|
134 |
. 'at table level (only at connection or global level).');
|
135 |
}
|
136 |
break;
|
137 |
default:
|
138 |
throw new Doctrine_Exception("Unknown attribute.");
|
139 |
}
|
140 |
|
141 |
$this->attributes[$attribute] = $value;
|
142 |
|
143 |
}
|
144 |
|
145 |
/**
|
146 |
* setImpl
|
147 |
* binds given class to given template name
|
148 |
*
|
149 |
* this method is the base of Doctrine dependency injection
|
150 |
*
|
151 |
* @param string $template name of the class template
|
152 |
* @param string $class name of the class to be bound
|
153 |
* @return Doctrine_Configurable this object
|
154 |
*/
|
155 |
public function setImpl($template, $class)
|
156 |
{
|
157 |
$this->_impl[$template] = $class;
|
158 |
|
159 |
return $this;
|
160 |
}
|
161 |
|
162 |
/**
|
163 |
* getImpl
|
164 |
* returns the implementation for given class
|
165 |
*
|
166 |
* @return string name of the concrete implementation
|
167 |
*/
|
168 |
public function getImpl($template)
|
169 |
{
|
170 |
if ( ! isset($this->_impl[$template])) {
|
171 |
if (isset($this->parent)) {
|
172 |
return $this->parent->getImpl($template);
|
173 |
}
|
174 |
return null;
|
175 |
}
|
176 |
return $this->_impl[$template];
|
177 |
}
|
178 |
|
179 |
/**
|
180 |
* getCacheDriver
|
181 |
*
|
182 |
* @return Doctrine_Cache_Interface
|
183 |
*/
|
184 |
public function getCacheDriver()
|
185 |
{
|
186 |
if ( ! isset($this->attributes[Doctrine::ATTR_CACHE])) {
|
187 |
throw new Doctrine_Exception('Cache driver not initialized.');
|
188 |
}
|
189 |
|
190 |
return $this->attributes[Doctrine::ATTR_CACHE];
|
191 |
}
|
192 |
|
193 |
/**
|
194 |
* @param Doctrine_EventListener $listener
|
195 |
* @return void
|
196 |
*/
|
197 |
public function setEventListener($listener)
|
198 |
{
|
199 |
return $this->setListener($listener);
|
200 |
}
|
201 |
|
202 |
/**
|
203 |
* addRecordListener
|
204 |
*
|
205 |
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
|
206 |
* @return mixed this object
|
207 |
*/
|
208 |
public function addRecordListener($listener, $name = null)
|
209 |
{
|
210 |
if ( ! isset($this->attributes[Doctrine::ATTR_RECORD_LISTENER]) ||
|
211 |
! ($this->attributes[Doctrine::ATTR_RECORD_LISTENER] instanceof Doctrine_Record_Listener_Chain)) {
|
212 |
|
213 |
$this->attributes[Doctrine::ATTR_RECORD_LISTENER] = new Doctrine_Record_Listener_Chain();
|
214 |
}
|
215 |
$this->attributes[Doctrine::ATTR_RECORD_LISTENER]->add($listener, $name);
|
216 |
|
217 |
return $this;
|
218 |
}
|
219 |
|
220 |
/**
|
221 |
* getListener
|
222 |
*
|
223 |
* @return Doctrine_EventListener_Interface|Doctrine_Overloadable
|
224 |
*/
|
225 |
public function getRecordListener()
|
226 |
{
|
227 |
if ( ! isset($this->attributes[Doctrine::ATTR_RECORD_LISTENER])) {
|
228 |
if (isset($this->parent)) {
|
229 |
return $this->parent->getRecordListener();
|
230 |
}
|
231 |
return null;
|
232 |
}
|
233 |
return $this->attributes[Doctrine::ATTR_RECORD_LISTENER];
|
234 |
}
|
235 |
|
236 |
/**
|
237 |
* setListener
|
238 |
*
|
239 |
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
|
240 |
* @return Doctrine_Configurable this object
|
241 |
*/
|
242 |
public function setRecordListener($listener)
|
243 |
{
|
244 |
if ( ! ($listener instanceof Doctrine_Record_Listener_Interface)
|
245 |
&& ! ($listener instanceof Doctrine_Overloadable)
|
246 |
) {
|
247 |
throw new Doctrine_Exception("Couldn't set eventlistener. Record listeners should implement either Doctrine_Record_Listener_Interface or Doctrine_Overloadable");
|
248 |
}
|
249 |
$this->attributes[Doctrine::ATTR_RECORD_LISTENER] = $listener;
|
250 |
|
251 |
return $this;
|
252 |
}
|
253 |
|
254 |
/**
|
255 |
* addListener
|
256 |
*
|
257 |
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
|
258 |
* @return mixed this object
|
259 |
*/
|
260 |
public function addListener($listener, $name = null)
|
261 |
{
|
262 |
if ( ! isset($this->attributes[Doctrine::ATTR_LISTENER]) ||
|
263 |
! ($this->attributes[Doctrine::ATTR_LISTENER] instanceof Doctrine_EventListener_Chain)) {
|
264 |
|
265 |
$this->attributes[Doctrine::ATTR_LISTENER] = new Doctrine_EventListener_Chain();
|
266 |
}
|
267 |
$this->attributes[Doctrine::ATTR_LISTENER]->add($listener, $name);
|
268 |
|
269 |
return $this;
|
270 |
}
|
271 |
|
272 |
/**
|
273 |
* getListener
|
274 |
*
|
275 |
* @return Doctrine_EventListener_Interface|Doctrine_Overloadable
|
276 |
*/
|
277 |
public function getListener()
|
278 |
{
|
279 |
if ( ! isset($this->attributes[Doctrine::ATTR_LISTENER])) {
|
280 |
if (isset($this->parent)) {
|
281 |
return $this->parent->getListener();
|
282 |
}
|
283 |
return null;
|
284 |
}
|
285 |
return $this->attributes[Doctrine::ATTR_LISTENER];
|
286 |
}
|
287 |
|
288 |
/**
|
289 |
* setListener
|
290 |
*
|
291 |
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
|
292 |
* @return Doctrine_Configurable this object
|
293 |
*/
|
294 |
public function setListener($listener)
|
295 |
{
|
296 |
if ( ! ($listener instanceof Doctrine_EventListener_Interface)
|
297 |
&& ! ($listener instanceof Doctrine_Overloadable)
|
298 |
) {
|
299 |
throw new Doctrine_EventListener_Exception("Couldn't set eventlistener. EventListeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable");
|
300 |
}
|
301 |
$this->attributes[Doctrine::ATTR_LISTENER] = $listener;
|
302 |
|
303 |
return $this;
|
304 |
}
|
305 |
|
306 |
/**
|
307 |
* returns the value of an attribute
|
308 |
*
|
309 |
* @param integer $attribute
|
310 |
* @return mixed
|
311 |
*/
|
312 |
public function getAttribute($attribute)
|
313 |
{
|
314 |
$attribute = (int) $attribute;
|
315 |
|
316 |
if ($attribute < 0) {
|
317 |
throw new Doctrine_Exception('Unknown attribute.');
|
318 |
}
|
319 |
|
320 |
if ( ! isset($this->attributes[$attribute])) {
|
321 |
if (isset($this->parent)) {
|
322 |
return $this->parent->getAttribute($attribute);
|
323 |
}
|
324 |
return null;
|
325 |
}
|
326 |
return $this->attributes[$attribute];
|
327 |
}
|
328 |
|
329 |
/**
|
330 |
* getAttributes
|
331 |
* returns all attributes as an array
|
332 |
*
|
333 |
* @return array
|
334 |
*/
|
335 |
public function getAttributes()
|
336 |
{
|
337 |
return $this->attributes;
|
338 |
}
|
339 |
|
340 |
/**
|
341 |
* sets a parent for this configurable component
|
342 |
* the parent must be configurable component itself
|
343 |
*
|
344 |
* @param Doctrine_Configurable $component
|
345 |
* @return void
|
346 |
*/
|
347 |
public function setParent(Doctrine_Configurable $component)
|
348 |
{
|
349 |
$this->parent = $component;
|
350 |
}
|
351 |
|
352 |
/**
|
353 |
* getParent
|
354 |
* returns the parent of this component
|
355 |
*
|
356 |
* @return Doctrine_Configurable
|
357 |
*/
|
358 |
public function getParent()
|
359 |
{
|
360 |
return $this->parent;
|
361 |
}
|
362 |
}
|