1 |
<?php
|
2 |
/*
|
3 |
* $Id: Configurable.php 3223 2007-11-25 19:07:30Z romanb $
|
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: 3223 $
|
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 |
* @var array $_params an array of user defined parameters
|
56 |
*/
|
57 |
protected $_params = array();
|
58 |
|
59 |
/**
|
60 |
* setAttribute
|
61 |
* sets a given attribute
|
62 |
*
|
63 |
* <code>
|
64 |
* $manager->setAttribute(Doctrine::ATTR_PORTABILITY, Doctrine::PORTABILITY_ALL);
|
65 |
*
|
66 |
* // or
|
67 |
*
|
68 |
* $manager->setAttribute('portability', Doctrine::PORTABILITY_ALL);
|
69 |
* </code>
|
70 |
*
|
71 |
* @param mixed $attribute either a Doctrine::ATTR_* integer constant or a string
|
72 |
* corresponding to a constant
|
73 |
* @param mixed $value the value of the attribute
|
74 |
* @see Doctrine::ATTR_* constants
|
75 |
* @throws Doctrine_Exception if the value is invalid
|
76 |
* @return void
|
77 |
*/
|
78 |
public function setAttribute($attribute,$value)
|
79 |
{
|
80 |
if (is_string($attribute)) {
|
81 |
$upper = strtoupper($attribute);
|
82 |
|
83 |
$const = 'Doctrine::ATTR_' . $attribute;
|
84 |
if (defined($const)) {
|
85 |
$this->_state = constant($const);
|
86 |
} else {
|
87 |
throw new Doctrine_Exception('Unknown attribute ' . $attribute);
|
88 |
}
|
89 |
}
|
90 |
switch ($attribute) {
|
91 |
case Doctrine::ATTR_FETCHMODE:
|
92 |
throw new Doctrine_Exception('Deprecated attribute. See http://doctrine.pengus.net/doctrine/manual/new/?chapter=configuration');
|
93 |
case Doctrine::ATTR_LISTENER:
|
94 |
$this->setEventListener($value);
|
95 |
break;
|
96 |
case Doctrine::ATTR_COLL_KEY:
|
97 |
if ( ! ($this instanceof Doctrine_Table)) {
|
98 |
throw new Doctrine_Exception("This attribute can only be set at table level.");
|
99 |
}
|
100 |
if ($value !== null && ! $this->hasColumn($value)) {
|
101 |
throw new Doctrine_Exception("Couldn't set collection key attribute. No such column '$value'");
|
102 |
}
|
103 |
break;
|
104 |
case Doctrine::ATTR_CACHE:
|
105 |
case Doctrine::ATTR_RESULT_CACHE:
|
106 |
case Doctrine::ATTR_QUERY_CACHE:
|
107 |
if ($value !== null) {
|
108 |
if ( ! ($value instanceof Doctrine_Cache_Interface)) {
|
109 |
throw new Doctrine_Exception('Cache driver should implement Doctrine_Cache_Interface');
|
110 |
}
|
111 |
}
|
112 |
break;
|
113 |
case Doctrine::ATTR_VALIDATE:
|
114 |
case Doctrine::ATTR_QUERY_LIMIT:
|
115 |
case Doctrine::ATTR_QUOTE_IDENTIFIER:
|
116 |
case Doctrine::ATTR_PORTABILITY:
|
117 |
case Doctrine::ATTR_DEFAULT_TABLE_TYPE:
|
118 |
case Doctrine::ATTR_EMULATE_DATABASE:
|
119 |
case Doctrine::ATTR_USE_NATIVE_ENUM:
|
120 |
case Doctrine::ATTR_DEFAULT_SEQUENCE:
|
121 |
case Doctrine::ATTR_EXPORT:
|
122 |
case Doctrine::ATTR_DECIMAL_PLACES:
|
123 |
case Doctrine::ATTR_LOAD_REFERENCES:
|
124 |
case Doctrine::ATTR_RECORD_LISTENER:
|
125 |
case Doctrine::ATTR_THROW_EXCEPTIONS:
|
126 |
case Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE:
|
127 |
|
128 |
break;
|
129 |
case Doctrine::ATTR_SEQCOL_NAME:
|
130 |
if ( ! is_string($value)) {
|
131 |
throw new Doctrine_Exception('Sequence column name attribute only accepts string values');
|
132 |
}
|
133 |
break;
|
134 |
case Doctrine::ATTR_FIELD_CASE:
|
135 |
if ($value != 0 && $value != CASE_LOWER && $value != CASE_UPPER)
|
136 |
throw new Doctrine_Exception('Field case attribute should be either 0, CASE_LOWER or CASE_UPPER constant.');
|
137 |
break;
|
138 |
case Doctrine::ATTR_SEQNAME_FORMAT:
|
139 |
case Doctrine::ATTR_IDXNAME_FORMAT:
|
140 |
case Doctrine::ATTR_TBLNAME_FORMAT:
|
141 |
if ($this instanceof Doctrine_Table) {
|
142 |
throw new Doctrine_Exception('Sequence / index name format attributes cannot be set'
|
143 |
. 'at table level (only at connection or global level).');
|
144 |
}
|
145 |
break;
|
146 |
default:
|
147 |
throw new Doctrine_Exception("Unknown attribute.");
|
148 |
}
|
149 |
|
150 |
$this->attributes[$attribute] = $value;
|
151 |
|
152 |
}
|
153 |
|
154 |
public function getParams($namespace = null)
|
155 |
{
|
156 |
if ($namespace == null) {
|
157 |
$namespace = $this->getAttribute(Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE);
|
158 |
}
|
159 |
|
160 |
if ( ! isset($this->_params[$namespace])) {
|
161 |
return null;
|
162 |
}
|
163 |
|
164 |
return $this->_params[$namespace];
|
165 |
}
|
166 |
|
167 |
public function getParamNamespaces()
|
168 |
{
|
169 |
return array_keys($this->_params);
|
170 |
}
|
171 |
|
172 |
public function setParam($name, $value, $namespace = null)
|
173 |
{
|
174 |
if ($namespace == null) {
|
175 |
$namespace = $this->getAttribute(Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE);
|
176 |
}
|
177 |
|
178 |
$this->_params[$namespace][$name] = $value;
|
179 |
|
180 |
return $this;
|
181 |
}
|
182 |
|
183 |
public function getParam($name, $value, $namespace)
|
184 |
{
|
185 |
if ($namespace == null) {
|
186 |
$namespace = $this->getAttribute(Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE);
|
187 |
}
|
188 |
|
189 |
if ( ! isset($this->_params[$name])) {
|
190 |
if (isset($this->parent)) {
|
191 |
return $this->parent->getParam($name);
|
192 |
}
|
193 |
return null;
|
194 |
}
|
195 |
return $this->_params[$name];
|
196 |
}
|
197 |
/**
|
198 |
* setImpl
|
199 |
* binds given class to given template name
|
200 |
*
|
201 |
* this method is the base of Doctrine dependency injection
|
202 |
*
|
203 |
* @param string $template name of the class template
|
204 |
* @param string $class name of the class to be bound
|
205 |
* @return Doctrine_Configurable this object
|
206 |
*/
|
207 |
public function setImpl($template, $class)
|
208 |
{
|
209 |
$this->_impl[$template] = $class;
|
210 |
|
211 |
return $this;
|
212 |
}
|
213 |
|
214 |
/**
|
215 |
* getImpl
|
216 |
* returns the implementation for given class
|
217 |
*
|
218 |
* @return string name of the concrete implementation
|
219 |
*/
|
220 |
public function getImpl($template)
|
221 |
{
|
222 |
if ( ! isset($this->_impl[$template])) {
|
223 |
if (isset($this->parent)) {
|
224 |
return $this->parent->getImpl($template);
|
225 |
}
|
226 |
return null;
|
227 |
}
|
228 |
return $this->_impl[$template];
|
229 |
}
|
230 |
|
231 |
|
232 |
public function hasImpl($template)
|
233 |
{
|
234 |
if ( ! isset($this->_impl[$template])) {
|
235 |
if (isset($this->parent)) {
|
236 |
return $this->parent->hasImpl($template);
|
237 |
}
|
238 |
return false;
|
239 |
}
|
240 |
return true;
|
241 |
}
|
242 |
|
243 |
/**
|
244 |
* @param Doctrine_EventListener $listener
|
245 |
* @return void
|
246 |
*/
|
247 |
public function setEventListener($listener)
|
248 |
{
|
249 |
return $this->setListener($listener);
|
250 |
}
|
251 |
|
252 |
/**
|
253 |
* addRecordListener
|
254 |
*
|
255 |
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
|
256 |
* @return mixed this object
|
257 |
*/
|
258 |
public function addRecordListener($listener, $name = null)
|
259 |
{
|
260 |
if ( ! isset($this->attributes[Doctrine::ATTR_RECORD_LISTENER]) ||
|
261 |
! ($this->attributes[Doctrine::ATTR_RECORD_LISTENER] instanceof Doctrine_Record_Listener_Chain)) {
|
262 |
|
263 |
$this->attributes[Doctrine::ATTR_RECORD_LISTENER] = new Doctrine_Record_Listener_Chain();
|
264 |
}
|
265 |
$this->attributes[Doctrine::ATTR_RECORD_LISTENER]->add($listener, $name);
|
266 |
|
267 |
return $this;
|
268 |
}
|
269 |
|
270 |
/**
|
271 |
* getListener
|
272 |
*
|
273 |
* @return Doctrine_EventListener_Interface|Doctrine_Overloadable
|
274 |
*/
|
275 |
public function getRecordListener()
|
276 |
{
|
277 |
if ( ! isset($this->attributes[Doctrine::ATTR_RECORD_LISTENER])) {
|
278 |
if (isset($this->parent)) {
|
279 |
return $this->parent->getRecordListener();
|
280 |
}
|
281 |
return null;
|
282 |
}
|
283 |
return $this->attributes[Doctrine::ATTR_RECORD_LISTENER];
|
284 |
}
|
285 |
|
286 |
/**
|
287 |
* setListener
|
288 |
*
|
289 |
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
|
290 |
* @return Doctrine_Configurable this object
|
291 |
*/
|
292 |
public function setRecordListener($listener)
|
293 |
{
|
294 |
if ( ! ($listener instanceof Doctrine_Record_Listener_Interface)
|
295 |
&& ! ($listener instanceof Doctrine_Overloadable)
|
296 |
) {
|
297 |
throw new Doctrine_Exception("Couldn't set eventlistener. Record listeners should implement either Doctrine_Record_Listener_Interface or Doctrine_Overloadable");
|
298 |
}
|
299 |
$this->attributes[Doctrine::ATTR_RECORD_LISTENER] = $listener;
|
300 |
|
301 |
return $this;
|
302 |
}
|
303 |
|
304 |
/**
|
305 |
* addListener
|
306 |
*
|
307 |
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
|
308 |
* @return mixed this object
|
309 |
*/
|
310 |
public function addListener($listener, $name = null)
|
311 |
{
|
312 |
if ( ! isset($this->attributes[Doctrine::ATTR_LISTENER]) ||
|
313 |
! ($this->attributes[Doctrine::ATTR_LISTENER] instanceof Doctrine_EventListener_Chain)) {
|
314 |
|
315 |
$this->attributes[Doctrine::ATTR_LISTENER] = new Doctrine_EventListener_Chain();
|
316 |
}
|
317 |
$this->attributes[Doctrine::ATTR_LISTENER]->add($listener, $name);
|
318 |
|
319 |
return $this;
|
320 |
}
|
321 |
|
322 |
/**
|
323 |
* getListener
|
324 |
*
|
325 |
* @return Doctrine_EventListener_Interface|Doctrine_Overloadable
|
326 |
*/
|
327 |
public function getListener()
|
328 |
{
|
329 |
if ( ! isset($this->attributes[Doctrine::ATTR_LISTENER])) {
|
330 |
if (isset($this->parent)) {
|
331 |
return $this->parent->getListener();
|
332 |
}
|
333 |
return null;
|
334 |
}
|
335 |
return $this->attributes[Doctrine::ATTR_LISTENER];
|
336 |
}
|
337 |
|
338 |
/**
|
339 |
* setListener
|
340 |
*
|
341 |
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
|
342 |
* @return Doctrine_Configurable this object
|
343 |
*/
|
344 |
public function setListener($listener)
|
345 |
{
|
346 |
if ( ! ($listener instanceof Doctrine_EventListener_Interface)
|
347 |
&& ! ($listener instanceof Doctrine_Overloadable)
|
348 |
) {
|
349 |
throw new Doctrine_EventListener_Exception("Couldn't set eventlistener. EventListeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable");
|
350 |
}
|
351 |
$this->attributes[Doctrine::ATTR_LISTENER] = $listener;
|
352 |
|
353 |
return $this;
|
354 |
}
|
355 |
|
356 |
/**
|
357 |
* returns the value of an attribute
|
358 |
*
|
359 |
* @param integer $attribute
|
360 |
* @return mixed
|
361 |
*/
|
362 |
public function getAttribute($attribute)
|
363 |
{
|
364 |
$attribute = (int) $attribute;
|
365 |
|
366 |
if ($attribute < 0) {
|
367 |
throw new Doctrine_Exception('Unknown attribute.');
|
368 |
}
|
369 |
|
370 |
if (isset($this->attributes[$attribute])) {
|
371 |
return $this->attributes[$attribute];
|
372 |
}
|
373 |
|
374 |
if (isset($this->parent)) {
|
375 |
return $this->parent->getAttribute($attribute);
|
376 |
}
|
377 |
return null;
|
378 |
}
|
379 |
|
380 |
/**
|
381 |
* getAttributes
|
382 |
* returns all attributes as an array
|
383 |
*
|
384 |
* @return array
|
385 |
*/
|
386 |
public function getAttributes()
|
387 |
{
|
388 |
return $this->attributes;
|
389 |
}
|
390 |
|
391 |
/**
|
392 |
* sets a parent for this configurable component
|
393 |
* the parent must be configurable component itself
|
394 |
*
|
395 |
* @param Doctrine_Configurable $component
|
396 |
* @return void
|
397 |
*/
|
398 |
public function setParent(Doctrine_Configurable $component)
|
399 |
{
|
400 |
$this->parent = $component;
|
401 |
}
|
402 |
|
403 |
/**
|
404 |
* getParent
|
405 |
* returns the parent of this component
|
406 |
*
|
407 |
* @return Doctrine_Configurable
|
408 |
*/
|
409 |
public function getParent()
|
410 |
{
|
411 |
return $this->parent;
|
412 |
}
|
413 |
}
|