1
0
mirror of synced 2024-12-13 14:56:01 +03:00
doctrine2/lib/Doctrine/Configurable.php

447 lines
15 KiB
PHP
Raw Normal View History

2007-11-02 19:59:20 +03:00
<?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.org>.
2007-11-02 19:59:20 +03:00
*/
/**
* Doctrine_Configurable
*
*
* @package Doctrine
* @subpackage Configurable
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.org
2007-11-02 19:59:20 +03:00
* @since 1.0
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
abstract class Doctrine_Configurable
2007-11-02 19:59:20 +03:00
{
/**
* @var array $attributes an array of containing all attributes
*/
2008-03-17 16:26:34 +03:00
protected $_attributes = array();
2007-11-02 19:59:20 +03:00
/**
* @var Doctrine_Configurable $parent the parent of this component
*/
protected $parent;
/**
* @var array $_impl an array containing concrete implementations for class templates
* keys as template names and values as names of the concrete
* implementation classes
*/
2008-03-17 16:26:34 +03:00
//protected $_impl = array();
2007-11-02 19:59:20 +03:00
/**
* @var array $_params an array of user defined parameters
*/
2008-03-17 16:26:34 +03:00
//protected $_params = array();
2007-11-02 19:59:20 +03:00
/**
* setAttribute
* sets a given attribute
*
* <code>
* $manager->setAttribute(Doctrine::ATTR_PORTABILITY, Doctrine::PORTABILITY_ALL);
*
* // or
*
* $manager->setAttribute('portability', Doctrine::PORTABILITY_ALL);
2008-01-23 10:28:04 +03:00
*
* // or
*
* $manager->setAttribute('portability', 'all');
2007-11-02 19:59:20 +03:00
* </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
*/
2008-01-23 10:28:04 +03:00
public function setAttribute($attribute, $value)
2007-11-02 19:59:20 +03:00
{
if (is_string($attribute)) {
$upper = strtoupper($attribute);
2008-01-23 10:28:04 +03:00
$const = 'Doctrine::ATTR_' . $upper;
2007-11-02 19:59:20 +03:00
if (defined($const)) {
2008-01-23 10:28:04 +03:00
$attribute = constant($const);
2007-11-02 19:59:20 +03:00
} else {
2008-01-23 10:28:04 +03:00
throw new Doctrine_Exception('Unknown attribute: "' . $attribute . '"');
2007-11-02 19:59:20 +03:00
}
}
2008-01-23 10:28:04 +03:00
if (is_string($value) && isset($upper)) {
$const = 'Doctrine::' . $upper . '_' . strtoupper($value);
if (defined($const)) {
$value = constant($const);
} else {
throw new Doctrine_Exception('Unknown attribute value: "' . $value . '"');
}
}
2007-11-02 19:59:20 +03:00
switch ($attribute) {
2008-03-17 16:26:34 +03:00
case Doctrine::ATTR_FETCHMODE: // deprecated
throw new Doctrine_Exception('Deprecated attribute. See http://www.phpdoctrine.org/documentation/manual?chapter=configuration');
2007-11-02 19:59:20 +03:00
case Doctrine::ATTR_LISTENER:
$this->setEventListener($value);
break;
2008-03-17 16:26:34 +03:00
case Doctrine::ATTR_COLL_KEY: // class attribute
if ( ! ($this instanceof Doctrine_ClassMetadata)) {
throw new Doctrine_Exception("This attribute can only be set at class level.");
2007-11-02 19:59:20 +03:00
}
if ($value !== null && ! $this->hasField($value)) {
throw new Doctrine_Exception("Couldn't set collection key attribute. No such field '$value'");
2007-11-02 19:59:20 +03:00
}
break;
2008-03-17 16:26:34 +03:00
case Doctrine::ATTR_CACHE: // deprecated
case Doctrine::ATTR_RESULT_CACHE:// manager/session attribute
case Doctrine::ATTR_QUERY_CACHE: // manager/session attribute
2007-11-02 19:59:20 +03:00
if ($value !== null) {
if ( ! ($value instanceof Doctrine_Cache_Interface)) {
throw new Doctrine_Exception('Cache driver should implement Doctrine_Cache_Interface');
}
}
break;
2008-03-17 16:26:34 +03:00
case Doctrine::ATTR_VALIDATE: // manager/session attribute
case Doctrine::ATTR_QUERY_LIMIT: // manager/session attribute
case Doctrine::ATTR_QUOTE_IDENTIFIER: // manager/session attribute
case Doctrine::ATTR_PORTABILITY: // manager/session attribute
case Doctrine::ATTR_DEFAULT_TABLE_TYPE: // manager/session attribute
case Doctrine::ATTR_EMULATE_DATABASE: // manager/session attribute
case Doctrine::ATTR_USE_NATIVE_ENUM: // manager/session attribute
case Doctrine::ATTR_DEFAULT_SEQUENCE: // ??
case Doctrine::ATTR_EXPORT: // manager/session attribute
case Doctrine::ATTR_DECIMAL_PLACES: // manager/session attribute
case Doctrine::ATTR_LOAD_REFERENCES: // class attribute
case Doctrine::ATTR_RECORD_LISTENER: // not an attribute
case Doctrine::ATTR_THROW_EXCEPTIONS: // manager/session attribute
2007-11-02 19:59:20 +03:00
case Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE:
2008-03-17 16:26:34 +03:00
case Doctrine::ATTR_MODEL_LOADING: // manager/session attribute
2007-11-02 19:59:20 +03:00
break;
2008-03-17 16:26:34 +03:00
case Doctrine::ATTR_SEQCOL_NAME: // class attribute
2007-11-02 19:59:20 +03:00
if ( ! is_string($value)) {
throw new Doctrine_Exception('Sequence column name attribute only accepts string values');
}
break;
2008-03-17 16:26:34 +03:00
case Doctrine::ATTR_FIELD_CASE: // manager/session attribute
2007-11-02 19:59:20 +03:00
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;
2008-03-17 16:26:34 +03:00
case Doctrine::ATTR_SEQNAME_FORMAT: // manager/session attribute
case Doctrine::ATTR_IDXNAME_FORMAT: // manager/session attribute
case Doctrine::ATTR_TBLNAME_FORMAT: // manager/session attribute
if ($this instanceof Doctrine_ClassMetadata) {
2007-11-02 19:59:20 +03:00
throw new Doctrine_Exception('Sequence / index name format attributes cannot be set'
2008-03-17 16:26:34 +03:00
. ' at class level (only at connection or global level).');
2007-11-02 19:59:20 +03:00
}
break;
default:
throw new Doctrine_Exception("Unknown attribute.");
}
2008-03-17 16:26:34 +03:00
$this->_attributes[$attribute] = $value;
2007-11-02 19:59:20 +03:00
}
2008-03-17 16:26:34 +03:00
/*public function getParams($namespace = null)
2007-11-02 19:59:20 +03:00
{
if ($namespace == null) {
$namespace = $this->getAttribute(Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE);
}
2007-11-02 19:59:20 +03:00
if ( ! isset($this->_params[$namespace])) {
return null;
}
return $this->_params[$namespace];
2008-03-17 16:26:34 +03:00
}*/
2008-03-17 16:26:34 +03:00
/*public function getParamNamespaces()
2007-11-02 19:59:20 +03:00
{
2007-11-12 21:08:20 +03:00
return array_keys($this->_params);
2008-03-17 16:26:34 +03:00
}*/
2007-11-02 19:59:20 +03:00
2008-03-17 16:26:34 +03:00
/*public function setParam($name, $value, $namespace = null)
2007-11-02 19:59:20 +03:00
{
if ($namespace == null) {
$namespace = $this->getAttribute(Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE);
}
2007-11-02 19:59:20 +03:00
$this->_params[$namespace][$name] = $value;
2007-11-02 19:59:20 +03:00
return $this;
2008-03-17 16:26:34 +03:00
}*/
2008-03-17 16:26:34 +03:00
/*public function getParam($name, $value, $namespace)
2007-11-02 19:59:20 +03:00
{
if ($namespace == null) {
$namespace = $this->getAttribute(Doctrine::ATTR_DEFAULT_PARAM_NAMESPACE);
}
2007-11-02 19:59:20 +03:00
if ( ! isset($this->_params[$name])) {
if (isset($this->parent)) {
return $this->parent->getParam($name);
}
return null;
}
return $this->_params[$name];
2008-03-17 16:26:34 +03:00
}*/
2007-11-02 19:59:20 +03:00
/**
* setImpl
* binds given class to given template name
*
* this method is the base of Doctrine dependency injection
*
* @param string $template name of the class template
* @param string $class name of the class to be bound
* @return Doctrine_Configurable this object
*/
2008-03-17 16:26:34 +03:00
/*public function setImpl($template, $class)
2007-11-02 19:59:20 +03:00
{
$this->_impl[$template] = $class;
return $this;
2008-03-17 16:26:34 +03:00
}*/
2007-11-02 19:59:20 +03:00
/**
* getImpl
* returns the implementation for given class
*
* @return string name of the concrete implementation
*/
2008-03-17 16:26:34 +03:00
/*public function getImpl($template)
2007-11-02 19:59:20 +03:00
{
if ( ! isset($this->_impl[$template])) {
if (isset($this->parent)) {
return $this->parent->getImpl($template);
}
return null;
}
return $this->_impl[$template];
2008-03-17 16:26:34 +03:00
}*/
2008-03-17 16:26:34 +03:00
/*public function hasImpl($template)
2007-11-12 21:08:20 +03:00
{
if ( ! isset($this->_impl[$template])) {
if (isset($this->parent)) {
return $this->parent->hasImpl($template);
}
return false;
}
return true;
2008-03-17 16:26:34 +03:00
}*/
2007-11-02 19:59:20 +03:00
/**
* @param Doctrine_EventListener $listener
* @return void
*/
public function setEventListener($listener)
{
return $this->setListener($listener);
}
/**
* addRecordListener
*
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
* @return mixed this object
*/
public function addRecordListener($listener, $name = null)
{
2008-03-17 16:26:34 +03:00
if ( ! isset($this->_attributes[Doctrine::ATTR_RECORD_LISTENER]) ||
! ($this->_attributes[Doctrine::ATTR_RECORD_LISTENER] instanceof Doctrine_Record_Listener_Chain)) {
2007-11-02 19:59:20 +03:00
2008-03-17 16:26:34 +03:00
$this->_attributes[Doctrine::ATTR_RECORD_LISTENER] = new Doctrine_Record_Listener_Chain();
2007-11-02 19:59:20 +03:00
}
2008-03-17 16:26:34 +03:00
$this->_attributes[Doctrine::ATTR_RECORD_LISTENER]->add($listener, $name);
2007-11-02 19:59:20 +03:00
return $this;
}
/**
* getListener
*
* @return Doctrine_EventListener_Interface|Doctrine_Overloadable
*/
public function getRecordListener()
{
2008-03-17 16:26:34 +03:00
if ( ! isset($this->_attributes[Doctrine::ATTR_RECORD_LISTENER])) {
2007-11-02 19:59:20 +03:00
if (isset($this->parent)) {
return $this->parent->getRecordListener();
}
2008-03-17 16:26:34 +03:00
$this->_attributes[Doctrine::ATTR_RECORD_LISTENER] = new Doctrine_Record_Listener();
2007-11-02 19:59:20 +03:00
}
2008-03-17 16:26:34 +03:00
return $this->_attributes[Doctrine::ATTR_RECORD_LISTENER];
2007-11-02 19:59:20 +03:00
}
/**
* setListener
*
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
* @return Doctrine_Configurable this object
*/
public function setRecordListener($listener)
{
if ( ! ($listener instanceof Doctrine_Record_Listener_Interface)
&& ! ($listener instanceof Doctrine_Overloadable)
) {
throw new Doctrine_Exception("Couldn't set eventlistener. Record listeners should implement either Doctrine_Record_Listener_Interface or Doctrine_Overloadable");
}
2008-03-17 16:26:34 +03:00
$this->_attributes[Doctrine::ATTR_RECORD_LISTENER] = $listener;
2007-11-02 19:59:20 +03:00
return $this;
}
2008-03-17 16:26:34 +03:00
public function removeRecordListeners()
{
2008-03-17 16:26:34 +03:00
$this->_attributes[Doctrine::ATTR_RECORD_LISTENER] = null;
}
2007-11-02 19:59:20 +03:00
/**
* addListener
*
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
* @return mixed this object
*/
public function addListener($listener, $name = null)
{
2008-03-17 16:26:34 +03:00
if ( ! isset($this->_attributes[Doctrine::ATTR_LISTENER]) ||
! ($this->_attributes[Doctrine::ATTR_LISTENER] instanceof Doctrine_EventListener_Chain)) {
2007-11-02 19:59:20 +03:00
2008-03-17 16:26:34 +03:00
$this->_attributes[Doctrine::ATTR_LISTENER] = new Doctrine_EventListener_Chain();
2007-11-02 19:59:20 +03:00
}
2008-03-17 16:26:34 +03:00
$this->_attributes[Doctrine::ATTR_LISTENER]->add($listener, $name);
2007-11-02 19:59:20 +03:00
return $this;
}
/**
* getListener
*
* @return Doctrine_EventListener_Interface|Doctrine_Overloadable
*/
public function getListener()
{
2008-03-17 16:26:34 +03:00
if ( ! isset($this->_attributes[Doctrine::ATTR_LISTENER])) {
2007-11-02 19:59:20 +03:00
if (isset($this->parent)) {
return $this->parent->getListener();
}
return null;
}
2008-03-17 16:26:34 +03:00
return $this->_attributes[Doctrine::ATTR_LISTENER];
2007-11-02 19:59:20 +03:00
}
/**
* setListener
*
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
* @return Doctrine_Configurable this object
*/
public function setListener($listener)
{
if ( ! ($listener instanceof Doctrine_EventListener_Interface)
&& ! ($listener instanceof Doctrine_Overloadable)) {
2007-11-02 19:59:20 +03:00
throw new Doctrine_EventListener_Exception("Couldn't set eventlistener. EventListeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable");
}
2008-03-17 16:26:34 +03:00
$this->_attributes[Doctrine::ATTR_LISTENER] = $listener;
2007-11-02 19:59:20 +03:00
return $this;
}
/**
* returns the value of an attribute
*
* @param integer $attribute
* @return mixed
*/
public function getAttribute($attribute)
{
if (is_string($attribute)) {
$upper = strtoupper($attribute);
$const = 'Doctrine::ATTR_' . $upper;
if (defined($const)) {
$attribute = constant($const);
} else {
throw new Doctrine_Exception('Unknown attribute: "' . $attribute . '"');
}
}
2007-11-02 19:59:20 +03:00
$attribute = (int) $attribute;
if ($attribute < 0) {
throw new Doctrine_Exception('Unknown attribute.');
}
2008-03-17 16:26:34 +03:00
if (isset($this->_attributes[$attribute])) {
return $this->_attributes[$attribute];
}
if (isset($this->parent)) {
return $this->parent->getAttribute($attribute);
2007-11-02 19:59:20 +03:00
}
return null;
2007-11-02 19:59:20 +03:00
}
/**
* getAttributes
* returns all attributes as an array
*
* @return array
*/
public function getAttributes()
{
2008-03-17 16:26:34 +03:00
return $this->_attributes;
2007-11-02 19:59:20 +03:00
}
/**
* Sets a parent for this configurable component
* the parent must be a configurable component itself.
2007-11-02 19:59:20 +03:00
*
* @param Doctrine_Configurable $component
* @return void
*/
2008-03-17 16:26:34 +03:00
public function setConfigurableParent(Doctrine_Configurable $component)
2007-11-02 19:59:20 +03:00
{
$this->parent = $component;
}
/**
* getParent
* Returns the parent of this component.
2007-11-02 19:59:20 +03:00
*
* @return Doctrine_Configurable
*/
public function getParent()
{
return $this->parent;
}
}