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