Configurable.php 10.6 KB
Newer Older
lsmith's avatar
lsmith committed
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
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
/**
 * Doctrine_Configurable
 * the base for Doctrine_Table, Doctrine_Manager and Doctrine_Connection
 *
 *
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
lsmith's avatar
lsmith committed
34 35
abstract class Doctrine_Configurable
{
lsmith's avatar
lsmith committed
36 37 38
    /**
     * @var array $attributes               an array of containing all attributes
     */
zYne's avatar
zYne committed
39
    protected $attributes = array();
lsmith's avatar
lsmith committed
40 41 42
    /**
     * @var $parent                         the parents of this component
     */
zYne's avatar
zYne committed
43
    protected $parent;
lsmith's avatar
lsmith committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    /**
     * setAttribute
     * sets a given attribute
     *
     * <code>
     * $manager->setAttribute(Doctrine::ATTR_PORTABILITY, Doctrine::PORTABILITY_ALL);
     *
     * // or
     *
     * $manager->setAttribute('portability', Doctrine::PORTABILITY_ALL);
     * </code>
     *
     * @param mixed $attribute              either a Doctrine::ATTR_* integer constant or a string
     *                                      corresponding to a constant
     * @param mixed $value                  the value of the attribute
     * @see Doctrine::ATTR_* constants
     * @throws Doctrine_Exception           if the value is invalid
     * @return void
     */
lsmith's avatar
lsmith committed
63 64
    public function setAttribute($attribute,$value)
    {
lsmith's avatar
lsmith committed
65
        switch ($attribute) {
66 67 68
            case Doctrine::ATTR_FETCHMODE:
                if ($value < 0) {
                   throw new Doctrine_Exception("Unknown fetchmode. See Doctrine::FETCH_* constants.");
lsmith's avatar
lsmith committed
69
                }
70 71 72 73 74 75 76
                break;
            case Doctrine::ATTR_LISTENER:
                $this->setEventListener($value);
                break;
            case Doctrine::ATTR_LOCKMODE:
                if ($this instanceof Doctrine_Connection) {
                    if ($this->transaction->getState() != Doctrine_Transaction::STATE_SLEEP) {
lsmith's avatar
lsmith committed
77 78
                        throw new Doctrine_Exception("Couldn't set lockmode. There are transactions open.");
                    }
79 80 81 82 83 84 85 86
                } elseif ($this instanceof Doctrine_Manager) {
                    foreach ($this as $connection) {
                        if ($connection->transaction->getState() != Doctrine_Transaction::STATE_SLEEP) {
                            throw new Doctrine_Exception("Couldn't set lockmode. There are transactions open.");
                        }
                    }
                } else {
                    throw new Doctrine_Exception("Lockmode attribute can only be set at the global or connection level.");
lsmith's avatar
lsmith committed
87
                }
88 89
                break;
            case Doctrine::ATTR_CREATE_TABLES:
zYne's avatar
zYne committed
90 91
                    $attribute = Doctrine::ATTR_EXPORT;
                if ($value) {
zYne's avatar
zYne committed
92
                    $value = Doctrine::EXPORT_TABLES;
zYne's avatar
zYne committed
93 94 95
                } else {
                    $value = Doctrine::EXPORT_NONE;
                }
96 97
                break;
            case Doctrine::ATTR_ACCESSORS:
zYne's avatar
zYne committed
98
                    throw new Doctrine_Exception("Get / Set filtering is deprecated (slowed down Doctrine too much)."); 
99 100 101 102 103 104 105 106 107 108
                break;
            case Doctrine::ATTR_COLL_LIMIT:
                if ($value < 1) {
                    throw new Doctrine_Exception("Collection limit should be a value greater than or equal to 1.");
                }
                break;
            case Doctrine::ATTR_COLL_KEY:
                if ( ! ($this instanceof Doctrine_Table)) {
                    throw new Doctrine_Exception("This attribute can only be set at table level.");
                }
zYne's avatar
zYne committed
109
                if ($value !== null && ! $this->hasColumn($value)) {
110 111 112
                    throw new Doctrine_Exception("Couldn't set collection key attribute. No such column '$value'");
                }
                break;
zYne's avatar
zYne committed
113 114 115 116 117 118 119 120 121
            case Doctrine::ATTR_DQL_CACHE:
            case Doctrine::ATTR_DQL_PARSER_CACHE:
            case Doctrine::ATTR_SQL_CACHE:
                if ($value !== null) {
                    if ( ! ($value instanceof Doctrine_Cache_Interface)) {
                        throw new Doctrine_Exception('Cache driver should implement Doctrine_Cache_Interface');
                    }                     
                }
                break;
122 123 124 125 126 127 128 129 130
            case Doctrine::ATTR_VLD:
            case Doctrine::ATTR_AUTO_LENGTH_VLD:
            case Doctrine::ATTR_AUTO_TYPE_VLD:
            case Doctrine::ATTR_QUERY_LIMIT:
            case Doctrine::ATTR_QUOTE_IDENTIFIER:
            case Doctrine::ATTR_PORTABILITY:
            case Doctrine::ATTR_DEFAULT_TABLE_TYPE:
            case Doctrine::ATTR_ACCESSOR_PREFIX_GET:
            case Doctrine::ATTR_ACCESSOR_PREFIX_SET:
zYne's avatar
zYne committed
131
            case Doctrine::ATTR_EMULATE_DATABASE:
zYne's avatar
zYne committed
132
            case Doctrine::ATTR_DEFAULT_SEQUENCE:
zYne's avatar
zYne committed
133
            case Doctrine::ATTR_EXPORT:
zYne's avatar
zYne committed
134
            case Doctrine::ATTR_DECIMAL_PLACES:
135
            case Doctrine::ATTR_LOAD_REFERENCES:
lsmith's avatar
lsmith committed
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                break;
            case Doctrine::ATTR_SEQCOL_NAME:
                if ( ! is_string($value)) {
                    throw new Doctrine_Exception('Sequence column name attribute only accepts string values');
                }
                break;
            case Doctrine::ATTR_FIELD_CASE:
                if ($value != 0 && $value != CASE_LOWER && $value != CASE_UPPER)
                    throw new Doctrine_Exception('Field case attribute should be either 0, CASE_LOWER or CASE_UPPER constant.');
                break;
            case Doctrine::ATTR_SEQNAME_FORMAT:
            case Doctrine::ATTR_IDXNAME_FORMAT:
                if ($this instanceof Doctrine_Table) {
                    throw new Doctrine_Exception('Sequence / index name format attributes cannot be set'
                                               . 'at table level (only at connection or global level).');
                }
                break;
            default:
                throw new Doctrine_Exception("Unknown attribute.");
lsmith's avatar
lsmith committed
156 157 158 159 160 161 162 163 164
        };

        $this->attributes[$attribute] = $value;

    }
    /**
     * @param Doctrine_EventListener $listener
     * @return void
     */
lsmith's avatar
lsmith committed
165 166
    public function setEventListener($listener)
    {
lsmith's avatar
lsmith committed
167 168 169 170 171
        return $this->setListener($listener);
    }
    /**
     * addListener
     *
172 173 174 175
     * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
     * @return Doctrine_Connection_Informix|Doctrine_Connection_Mssql|Doctrine_Connection_Oracle|
     *         Doctrine_Connection_Db2|Doctrine_Connection_Firebird|Doctrine_Connection_Common|
     *         Doctrine_Manager|Doctrine_Connection|Doctrine_Table
lsmith's avatar
lsmith committed
176
     */
lsmith's avatar
lsmith committed
177 178
    public function addListener($listener, $name = null)
    {
zYne's avatar
zYne committed
179
        if ( ! isset($this->attributes[Doctrine::ATTR_LISTENER]) ||
zYne's avatar
zYne committed
180 181
             ! ($this->attributes[Doctrine::ATTR_LISTENER] instanceof Doctrine_EventListener_Chain)) {
            
lsmith's avatar
lsmith committed
182 183 184 185 186 187 188 189 190
            $this->attributes[Doctrine::ATTR_LISTENER] = new Doctrine_EventListener_Chain();
        }
        $this->attributes[Doctrine::ATTR_LISTENER]->add($listener, $name);

        return $this;
    }
    /**
     * getListener
     *
191
     * @return Doctrine_EventListener_Interface|Doctrine_Overloadable
lsmith's avatar
lsmith committed
192
     */
lsmith's avatar
lsmith committed
193 194
    public function getListener()
    {
lsmith's avatar
lsmith committed
195 196 197 198 199 200 201 202 203 204 205
        if ( ! isset($this->attributes[Doctrine::ATTR_LISTENER])) {
            if (isset($this->parent)) {
                return $this->parent->getListener();
            }
            return null;
        }
        return $this->attributes[Doctrine::ATTR_LISTENER];
    }
    /**
     * setListener
     *
206 207 208 209
     * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
     * @return Doctrine_Connection_Informix|Doctrine_Connection_Mssql|Doctrine_Connection_Oracle|
     *         Doctrine_Connection_Db2|Doctrine_Connection_Firebird|Doctrine_Connection_Common|
     *         Doctrine_Manager|Doctrine_Connection|Doctrine_Table
lsmith's avatar
lsmith committed
210
     */
lsmith's avatar
lsmith committed
211 212
    public function setListener($listener)
    {
lsmith's avatar
lsmith committed
213 214 215
        if ( ! ($listener instanceof Doctrine_EventListener_Interface)
            && ! ($listener instanceof Doctrine_Overloadable)
        ) {
216
            throw new Doctrine_EventListener_Exception("Couldn't set eventlistener. EventListeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable");
lsmith's avatar
lsmith committed
217 218 219 220 221 222 223 224 225 226 227
        }
        $this->attributes[Doctrine::ATTR_LISTENER] = $listener;

        return $this;
    }
    /**
     * returns the value of an attribute
     *
     * @param integer $attribute
     * @return mixed
     */
lsmith's avatar
lsmith committed
228 229
    public function getAttribute($attribute)
    {
lsmith's avatar
lsmith committed
230 231
        $attribute = (int) $attribute;

zYne's avatar
zYne committed
232
        if ($attribute < 0) {
lsmith's avatar
lsmith committed
233
            throw new Doctrine_Exception('Unknown attribute.');
zYne's avatar
zYne committed
234
        }
lsmith's avatar
lsmith committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

        if ( ! isset($this->attributes[$attribute])) {
            if (isset($this->parent)) {
                return $this->parent->getAttribute($attribute);
            }
            return null;
        }
        return $this->attributes[$attribute];
    }
    /**
     * getAttributes
     * returns all attributes as an array
     *
     * @return array
     */
lsmith's avatar
lsmith committed
250 251
    public function getAttributes()
    {
lsmith's avatar
lsmith committed
252 253 254 255 256 257 258 259 260
        return $this->attributes;
    }
    /**
     * sets a parent for this configurable component
     * the parent must be configurable component itself
     *
     * @param Doctrine_Configurable $component
     * @return void
     */
lsmith's avatar
lsmith committed
261 262
    public function setParent(Doctrine_Configurable $component)
    {
lsmith's avatar
lsmith committed
263 264 265 266 267 268 269 270
        $this->parent = $component;
    }
    /**
     * getParent
     * returns the parent of this component
     *
     * @return Doctrine_Configurable
     */
lsmith's avatar
lsmith committed
271 272
    public function getParent()
    {
lsmith's avatar
lsmith committed
273 274 275
        return $this->parent;
    }
}