Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Configurable.php 7490 2010-03-29 19:53:27Z jwage $
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.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Configurable
24
 * the base for Doctrine_Table, Doctrine_Manager and Doctrine_Connection
25
 *
26
 * @package     Doctrine
27
 * @subpackage  Configurable
28
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
29
 * @link        www.doctrine-project.org
30
 * @since       1.0
31
 * @version     $Revision: 7490 $
32
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
33
 */
34
abstract class Doctrine_Configurable extends Doctrine_Locator_Injectable
35
{
36
    /**
37
     * @var array $attributes               an array of containing all attributes
38
     */
39
    protected $attributes = array();
40
 
41
    /**
42
     * @var Doctrine_Configurable $parent   the parent of this component
43
     */
44
    protected $parent;
45
 
46
    /**
47
     * @var array $_impl                    an array containing concrete implementations for class templates
48
     *                                      keys as template names and values as names of the concrete
49
     *                                      implementation classes
50
     */
51
    protected $_impl = array();
52
 
53
    /**
54
     * @var array $_params                  an array of user defined parameters
55
     */
56
    protected $_params = array();
57
 
58
    /**
59
     * setAttribute
60
     * sets a given attribute
61
     *
62
     * <code>
63
     * $manager->setAttribute(Doctrine_Core::ATTR_PORTABILITY, Doctrine_Core::PORTABILITY_ALL);
64
     * </code>
65
     *
66
     * @param mixed $attribute              either a Doctrine_Core::ATTR_* integer constant or a string
67
     *                                      corresponding to a constant
68
     * @param mixed $value                  the value of the attribute
69
     * @see Doctrine_Core::ATTR_* constants
70
     * @throws Doctrine_Exception           if the value is invalid
71
     * @return void
72
     */
73
    public function setAttribute($attribute, $value)
74
    {
75
        switch ($attribute) {
76
            case Doctrine_Core::ATTR_LISTENER:
77
                $this->setEventListener($value);
78
                break;
79
            case Doctrine_Core::ATTR_COLL_KEY:
80
                if ( ! ($this instanceof Doctrine_Table)) {
81
                    throw new Doctrine_Exception("This attribute can only be set at table level.");
82
                }
83
                if ($value !== null && ! $this->hasField($value)) {
84
                    throw new Doctrine_Exception("Couldn't set collection key attribute. No such field '$value'.");
85
                }
86
                break;
87
            case Doctrine_Core::ATTR_CACHE:
88
            case Doctrine_Core::ATTR_RESULT_CACHE:
89
            case Doctrine_Core::ATTR_QUERY_CACHE:
90
                if ($value !== null) {
91
                    if ( ! ($value instanceof Doctrine_Cache_Interface)) {
92
                        throw new Doctrine_Exception('Cache driver should implement Doctrine_Cache_Interface');
93
                    }
94
                }
95
                break;
96
            case Doctrine_Core::ATTR_SEQCOL_NAME:
97
                if ( ! is_string($value)) {
98
                    throw new Doctrine_Exception('Sequence column name attribute only accepts string values');
99
                }
100
                break;
101
            case Doctrine_Core::ATTR_FIELD_CASE:
102
                if ($value != 0 && $value != CASE_LOWER && $value != CASE_UPPER)
103
                    throw new Doctrine_Exception('Field case attribute should be either 0, CASE_LOWER or CASE_UPPER constant.');
104
                break;
105
            case Doctrine_Core::ATTR_SEQNAME_FORMAT:
106
            case Doctrine_Core::ATTR_IDXNAME_FORMAT:
107
            case Doctrine_Core::ATTR_TBLNAME_FORMAT:
108
            case Doctrine_Core::ATTR_FKNAME_FORMAT:
109
                if ($this instanceof Doctrine_Table) {
110
                    throw new Doctrine_Exception('Sequence / index name format attributes cannot be set'
111
                                               . 'at table level (only at connection or global level).');
112
                }
113
                break;
114
        }
115
 
116
        $this->attributes[$attribute] = $value;
117
    }
118
 
119
    public function getParams($namespace = null)
120
    {
121
    	if ($namespace == null) {
122
    	    $namespace = $this->getAttribute(Doctrine_Core::ATTR_DEFAULT_PARAM_NAMESPACE);
123
    	}
124
 
125
    	if ( ! isset($this->_params[$namespace])) {
126
    	    return null;
127
    	}
128
 
129
        return $this->_params[$namespace];
130
    }
131
 
132
    public function getParamNamespaces()
133
    {
134
        return array_keys($this->_params);
135
    }
136
 
137
    public function setParam($name, $value, $namespace = null)
138
    {
139
    	if ($namespace == null) {
140
    	    $namespace = $this->getAttribute(Doctrine_Core::ATTR_DEFAULT_PARAM_NAMESPACE);
141
    	}
142
 
143
    	$this->_params[$namespace][$name] = $value;
144
 
145
    	return $this;
146
    }
147
 
148
    public function getParam($name, $namespace = null)
149
    {
150
    	if ($namespace == null) {
151
    	    $namespace = $this->getAttribute(Doctrine_Core::ATTR_DEFAULT_PARAM_NAMESPACE);
152
    	}
153
 
154
        if ( ! isset($this->_params[$namespace][$name])) {
155
            if (isset($this->parent)) {
156
                return $this->parent->getParam($name, $namespace);
157
            }
158
            return null;
159
        }
160
 
161
        return $this->_params[$namespace][$name];
162
    }
163
 
164
    /**
165
     * setImpl
166
     * binds given class to given template name
167
     *
168
     * this method is the base of Doctrine dependency injection
169
     *
170
     * @param string $template      name of the class template
171
     * @param string $class         name of the class to be bound
172
     * @return Doctrine_Configurable    this object
173
     */
174
    public function setImpl($template, $class)
175
    {
176
        $this->_impl[$template] = $class;
177
 
178
        return $this;
179
    }
180
 
181
    /**
182
     * getImpl
183
     * returns the implementation for given class
184
     *
185
     * @return string   name of the concrete implementation
186
     */
187
    public function getImpl($template)
188
    {
189
        if ( ! isset($this->_impl[$template])) {
190
            if (isset($this->parent)) {
191
                return $this->parent->getImpl($template);
192
            }
193
            return null;
194
        }
195
        return $this->_impl[$template];
196
    }
197
 
198
 
199
    public function hasImpl($template)
200
    {
201
        if ( ! isset($this->_impl[$template])) {
202
            if (isset($this->parent)) {
203
                return $this->parent->hasImpl($template);
204
            }
205
            return false;
206
        }
207
        return true;
208
    }
209
 
210
    /**
211
     * @param Doctrine_EventListener $listener
212
     * @return void
213
     */
214
    public function setEventListener($listener)
215
    {
216
        return $this->setListener($listener);
217
    }
218
 
219
    /**
220
     * addRecordListener
221
     *
222
     * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
223
     * @return Doctrine_Configurable        this object
224
     */
225
    public function addRecordListener($listener, $name = null)
226
    {
227
        if ( ! isset($this->attributes[Doctrine_Core::ATTR_RECORD_LISTENER]) ||
228
             ! ($this->attributes[Doctrine_Core::ATTR_RECORD_LISTENER] instanceof Doctrine_Record_Listener_Chain)) {
229
 
230
            $this->attributes[Doctrine_Core::ATTR_RECORD_LISTENER] = new Doctrine_Record_Listener_Chain();
231
        }
232
        $this->attributes[Doctrine_Core::ATTR_RECORD_LISTENER]->add($listener, $name);
233
 
234
        return $this;
235
    }
236
 
237
    /**
238
     * getListener
239
     *
240
     * @return Doctrine_EventListener_Interface|Doctrine_Overloadable
241
     */
242
    public function getRecordListener()
243
    {
244
        if ( ! isset($this->attributes[Doctrine_Core::ATTR_RECORD_LISTENER])) {
245
            if (isset($this->parent)) {
246
                return $this->parent->getRecordListener();
247
            }
248
            return null;
249
        }
250
        return $this->attributes[Doctrine_Core::ATTR_RECORD_LISTENER];
251
    }
252
 
253
    /**
254
     * setListener
255
     *
256
     * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
257
     * @return Doctrine_Configurable        this object
258
     */
259
    public function setRecordListener($listener)
260
    {
261
        if ( ! ($listener instanceof Doctrine_Record_Listener_Interface)
262
            && ! ($listener instanceof Doctrine_Overloadable)
263
        ) {
264
            throw new Doctrine_Exception("Couldn't set eventlistener. Record listeners should implement either Doctrine_Record_Listener_Interface or Doctrine_Overloadable");
265
        }
266
        $this->attributes[Doctrine_Core::ATTR_RECORD_LISTENER] = $listener;
267
 
268
        return $this;
269
    }
270
 
271
    /**
272
     * addListener
273
     *
274
     * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
275
     * @return Doctrine_Configurable    this object
276
     */
277
    public function addListener($listener, $name = null)
278
    {
279
        if ( ! isset($this->attributes[Doctrine_Core::ATTR_LISTENER]) ||
280
             ! ($this->attributes[Doctrine_Core::ATTR_LISTENER] instanceof Doctrine_EventListener_Chain)) {
281
 
282
            $this->attributes[Doctrine_Core::ATTR_LISTENER] = new Doctrine_EventListener_Chain();
283
        }
284
        $this->attributes[Doctrine_Core::ATTR_LISTENER]->add($listener, $name);
285
 
286
        return $this;
287
    }
288
 
289
    /**
290
     * getListener
291
     *
292
     * @return Doctrine_EventListener_Interface|Doctrine_Overloadable
293
     */
294
    public function getListener()
295
    {
296
        if ( ! isset($this->attributes[Doctrine_Core::ATTR_LISTENER])) {
297
            if (isset($this->parent)) {
298
                return $this->parent->getListener();
299
            }
300
            return null;
301
        }
302
        return $this->attributes[Doctrine_Core::ATTR_LISTENER];
303
    }
304
 
305
    /**
306
     * setListener
307
     *
308
     * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
309
     * @return Doctrine_Configurable        this object
310
     */
311
    public function setListener($listener)
312
    {
313
        if ( ! ($listener instanceof Doctrine_EventListener_Interface)
314
            && ! ($listener instanceof Doctrine_Overloadable)
315
        ) {
316
            throw new Doctrine_EventListener_Exception("Couldn't set eventlistener. EventListeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable");
317
        }
318
        $this->attributes[Doctrine_Core::ATTR_LISTENER] = $listener;
319
 
320
        return $this;
321
    }
322
 
323
    /**
324
     * returns the value of an attribute
325
     *
326
     * @param integer $attribute
327
     * @return mixed
328
     */
329
    public function getAttribute($attribute)
330
    {
331
        if (isset($this->attributes[$attribute])) {
332
            return $this->attributes[$attribute];
333
        }
334
 
335
        if (isset($this->parent)) {
336
            return $this->parent->getAttribute($attribute);
337
        }
338
        return null;
339
    }
340
 
341
    /**
342
     * Unset an attribute from this levels attributes
343
     *
344
     * @param integer $attribute
345
     * @return void
346
     */
347
    public function unsetAttribute($attribute)
348
    {
349
        if (isset($this->attributes[$attribute])) {
350
            unset($this->attributes[$attribute]);
351
        }
352
    }
353
 
354
    /**
355
     * getAttributes
356
     * returns all attributes as an array
357
     *
358
     * @return array
359
     */
360
    public function getAttributes()
361
    {
362
        return $this->attributes;
363
    }
364
 
365
    /**
366
     * Set the charset
367
     *
368
     * @param string $charset
369
     */
370
    public function setCharset($charset)
371
    {
372
        $this->setAttribute(Doctrine_Core::ATTR_DEFAULT_TABLE_CHARSET, $charset);
373
    }
374
 
375
    /**
376
     * Get the charset
377
     *
378
     * @return mixed
379
     */
380
    public function getCharset()
381
    {
382
        return $this->getAttribute(Doctrine_Core::ATTR_DEFAULT_TABLE_CHARSET);
383
    }
384
 
385
    /**
386
     * Set the collate
387
     *
388
     * @param string $collate
389
     */
390
    public function setCollate($collate)
391
    {
392
        $this->setAttribute(Doctrine_Core::ATTR_DEFAULT_TABLE_COLLATE, $collate);
393
    }
394
 
395
    /**
396
     * Get the collate
397
     *
398
     * @return mixed $collate
399
     */
400
    public function getCollate()
401
    {
402
        return $this->getAttribute(Doctrine_Core::ATTR_DEFAULT_TABLE_COLLATE);
403
    }
404
 
405
    /**
406
     * sets a parent for this configurable component
407
     * the parent must be configurable component itself
408
     *
409
     * @param Doctrine_Configurable $component
410
     * @return void
411
     */
412
    public function setParent(Doctrine_Configurable $component)
413
    {
414
        $this->parent = $component;
415
    }
416
 
417
    /**
418
     * getParent
419
     * returns the parent of this component
420
     *
421
     * @return Doctrine_Configurable
422
     */
423
    public function getParent()
424
    {
425
        return $this->parent;
426
    }
427
}