2006-12-29 17:01:31 +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 . com >.
*/
2007-10-21 10:23:59 +04:00
2006-12-29 17:01:31 +03:00
/**
* Doctrine_Configurable
* the base for Doctrine_Table , Doctrine_Manager and Doctrine_Connection
*
*
* @ package Doctrine
2007-10-04 01:43:22 +04:00
* @ subpackage Configurable
2006-12-29 17:01:31 +03:00
* @ license http :// www . opensource . org / licenses / lgpl - license . php LGPL
* @ link www . phpdoctrine . com
* @ since 1.0
* @ version $Revision $
* @ author Konsta Vesterinen < kvesteri @ cc . hut . fi >
*/
2007-10-08 00:58:08 +04:00
abstract class Doctrine_Configurable extends Doctrine_Locator_Injectable
2006-12-29 17:40:47 +03:00
{
2006-12-29 17:01:31 +03:00
/**
* @ var array $attributes an array of containing all attributes
*/
2007-06-12 23:45:28 +04:00
protected $attributes = array ();
2007-10-21 10:23:59 +04:00
2006-12-29 17:01:31 +03:00
/**
2007-08-03 14:41:20 +04:00
* @ var Doctrine_Configurable $parent the parent of this component
2006-12-29 17:01:31 +03:00
*/
2007-06-12 23:45:28 +04:00
protected $parent ;
2007-10-21 10:23:59 +04:00
2007-08-03 14:41:20 +04:00
/**
* @ var array $_impl an array containing concrete implementations for class templates
2007-09-02 12:53:16 +04:00
* keys as template names and values as names of the concrete
2007-08-03 14:41:20 +04:00
* implementation classes
*/
protected $_impl = array ();
2007-10-21 10:23:59 +04:00
2006-12-29 17:01:31 +03:00
/**
* 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
*/
2006-12-29 17:40:47 +03:00
public function setAttribute ( $attribute , $value )
{
2007-07-07 00:55:15 +04:00
if ( is_string ( $attribute )) {
$upper = strtoupper ( $attribute );
2007-09-02 12:53:16 +04:00
2007-07-07 00:55:15 +04:00
$const = 'Doctrine::ATTR_' . $attribute ;
if ( defined ( $const )) {
$this -> _state = constant ( $const );
} else {
throw new Doctrine_Exception ( 'Unknown attribute ' . $attribute );
}
}
2006-12-29 17:01:31 +03:00
switch ( $attribute ) {
2006-12-30 00:30:37 +03:00
case Doctrine :: ATTR_FETCHMODE :
2007-09-04 20:36:12 +04:00
throw new Doctrine_Exception ( 'Deprecated attribute. See http://doctrine.pengus.net/doctrine/manual/new/?chapter=configuration' );
2006-12-30 00:30:37 +03:00
case Doctrine :: ATTR_LISTENER :
$this -> setEventListener ( $value );
break ;
case Doctrine :: ATTR_COLL_KEY :
if ( ! ( $this instanceof Doctrine_Table )) {
throw new Doctrine_Exception ( " This attribute can only be set at table level. " );
}
2007-01-22 01:26:38 +03:00
if ( $value !== null && ! $this -> hasColumn ( $value )) {
2006-12-30 00:30:37 +03:00
throw new Doctrine_Exception ( " Couldn't set collection key attribute. No such column ' $value ' " );
}
break ;
2007-06-29 14:18:05 +04:00
case Doctrine :: ATTR_CACHE :
2007-05-24 20:53:51 +04:00
if ( $value !== null ) {
if ( ! ( $value instanceof Doctrine_Cache_Interface )) {
throw new Doctrine_Exception ( 'Cache driver should implement Doctrine_Cache_Interface' );
2007-06-29 14:18:05 +04:00
}
2007-05-24 20:53:51 +04:00
}
break ;
2007-09-03 00:36:58 +04:00
case Doctrine :: ATTR_VALIDATE :
2006-12-30 00:30:37 +03:00
case Doctrine :: ATTR_QUERY_LIMIT :
case Doctrine :: ATTR_QUOTE_IDENTIFIER :
case Doctrine :: ATTR_PORTABILITY :
case Doctrine :: ATTR_DEFAULT_TABLE_TYPE :
2007-01-25 01:50:49 +03:00
case Doctrine :: ATTR_EMULATE_DATABASE :
2007-09-02 12:53:16 +04:00
case Doctrine :: ATTR_USE_NATIVE_ENUM :
2007-03-02 23:47:17 +03:00
case Doctrine :: ATTR_DEFAULT_SEQUENCE :
2007-03-22 01:11:18 +03:00
case Doctrine :: ATTR_EXPORT :
2007-05-11 23:08:16 +04:00
case Doctrine :: ATTR_DECIMAL_PLACES :
2007-05-30 14:20:21 +04:00
case Doctrine :: ATTR_LOAD_REFERENCES :
2007-07-11 18:39:15 +04:00
case Doctrine :: ATTR_RECORD_LISTENER :
2007-07-12 02:03:47 +04:00
case Doctrine :: ATTR_THROW_EXCEPTIONS :
2006-12-30 00:46:14 +03:00
2006-12-30 00:30:37 +03:00
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. " );
2007-08-03 14:41:20 +04:00
}
2006-12-29 17:01:31 +03:00
$this -> attributes [ $attribute ] = $value ;
}
2007-10-21 10:23:59 +04:00
2007-08-03 14:41:20 +04: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
*/
public function setImpl ( $template , $class )
{
$this -> _impl [ $template ] = $class ;
2007-09-02 12:53:16 +04:00
2007-08-03 14:41:20 +04:00
return $this ;
}
2007-10-21 10:23:59 +04:00
2007-08-03 14:41:20 +04:00
/**
* getImpl
* returns the implementation for given class
*
* @ return string name of the concrete implementation
*/
public function getImpl ( $template )
{
2007-08-03 15:52:24 +04:00
if ( ! isset ( $this -> _impl [ $template ])) {
2007-08-03 14:41:20 +04:00
if ( isset ( $this -> parent )) {
2007-08-03 15:52:24 +04:00
return $this -> parent -> getImpl ( $template );
2007-08-03 14:41:20 +04:00
}
return null ;
}
2007-08-03 15:52:24 +04:00
return $this -> _impl [ $template ];
2007-08-03 14:41:20 +04:00
}
2007-10-21 10:23:59 +04:00
2007-06-29 14:18:05 +04:00
/**
* getCacheDriver
*
* @ return Doctrine_Cache_Interface
*/
public function getCacheDriver ()
{
if ( ! isset ( $this -> attributes [ Doctrine :: ATTR_CACHE ])) {
throw new Doctrine_Exception ( 'Cache driver not initialized.' );
}
2007-09-02 12:53:16 +04:00
2007-06-29 14:18:05 +04:00
return $this -> attributes [ Doctrine :: ATTR_CACHE ];
}
2007-10-21 10:23:59 +04:00
2006-12-29 17:01:31 +03:00
/**
* @ param Doctrine_EventListener $listener
* @ return void
*/
2006-12-29 17:40:47 +03:00
public function setEventListener ( $listener )
{
2006-12-29 17:01:31 +03:00
return $this -> setListener ( $listener );
}
2007-10-21 10:23:59 +04:00
2007-07-11 18:39:15 +04:00
/**
* addRecordListener
*
* @ param Doctrine_EventListener_Interface | Doctrine_Overloadable $listener
* @ return mixed this object
*/
public function addRecordListener ( $listener , $name = null )
{
if ( ! isset ( $this -> attributes [ Doctrine :: ATTR_RECORD_LISTENER ]) ||
2007-07-11 19:32:13 +04:00
! ( $this -> attributes [ Doctrine :: ATTR_RECORD_LISTENER ] instanceof Doctrine_Record_Listener_Chain )) {
2007-09-02 12:53:16 +04:00
2007-07-11 18:39:15 +04:00
$this -> attributes [ Doctrine :: ATTR_RECORD_LISTENER ] = new Doctrine_Record_Listener_Chain ();
}
$this -> attributes [ Doctrine :: ATTR_RECORD_LISTENER ] -> add ( $listener , $name );
return $this ;
}
2007-10-21 10:23:59 +04:00
2007-07-11 18:39:15 +04:00
/**
* getListener
*
* @ return Doctrine_EventListener_Interface | Doctrine_Overloadable
*/
public function getRecordListener ()
{
if ( ! isset ( $this -> attributes [ Doctrine :: ATTR_RECORD_LISTENER ])) {
if ( isset ( $this -> parent )) {
return $this -> parent -> getRecordListener ();
}
return null ;
}
return $this -> attributes [ Doctrine :: ATTR_RECORD_LISTENER ];
}
2007-10-21 10:23:59 +04:00
2007-07-11 18:39:15 +04: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 " );
}
$this -> attributes [ Doctrine :: ATTR_RECORD_LISTENER ] = $listener ;
return $this ;
}
2007-10-21 10:23:59 +04:00
2006-12-29 17:01:31 +03:00
/**
* addListener
*
2007-06-25 21:48:44 +04:00
* @ param Doctrine_EventListener_Interface | Doctrine_Overloadable $listener
2007-07-11 18:39:15 +04:00
* @ return mixed this object
2006-12-29 17:01:31 +03:00
*/
2006-12-29 17:40:47 +03:00
public function addListener ( $listener , $name = null )
{
2007-06-12 23:45:28 +04:00
if ( ! isset ( $this -> attributes [ Doctrine :: ATTR_LISTENER ]) ||
2007-06-07 22:21:07 +04:00
! ( $this -> attributes [ Doctrine :: ATTR_LISTENER ] instanceof Doctrine_EventListener_Chain )) {
2007-09-02 12:53:16 +04:00
2006-12-29 17:01:31 +03:00
$this -> attributes [ Doctrine :: ATTR_LISTENER ] = new Doctrine_EventListener_Chain ();
}
$this -> attributes [ Doctrine :: ATTR_LISTENER ] -> add ( $listener , $name );
return $this ;
}
2007-10-21 10:23:59 +04:00
2006-12-29 17:01:31 +03:00
/**
* getListener
*
2007-06-25 21:48:44 +04:00
* @ return Doctrine_EventListener_Interface | Doctrine_Overloadable
2006-12-29 17:01:31 +03:00
*/
2006-12-29 17:40:47 +03:00
public function getListener ()
{
2006-12-29 17:01:31 +03:00
if ( ! isset ( $this -> attributes [ Doctrine :: ATTR_LISTENER ])) {
if ( isset ( $this -> parent )) {
return $this -> parent -> getListener ();
}
return null ;
}
return $this -> attributes [ Doctrine :: ATTR_LISTENER ];
}
2007-10-21 10:23:59 +04:00
2006-12-29 17:01:31 +03:00
/**
* setListener
*
2007-06-25 21:48:44 +04:00
* @ param Doctrine_EventListener_Interface | Doctrine_Overloadable $listener
2007-06-29 14:18:05 +04:00
* @ return Doctrine_Configurable this object
2006-12-29 17:01:31 +03:00
*/
2006-12-29 17:40:47 +03:00
public function setListener ( $listener )
{
2006-12-29 17:01:31 +03:00
if ( ! ( $listener instanceof Doctrine_EventListener_Interface )
&& ! ( $listener instanceof Doctrine_Overloadable )
) {
2007-06-20 03:33:04 +04:00
throw new Doctrine_EventListener_Exception ( " Couldn't set eventlistener. EventListeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable " );
2006-12-29 17:01:31 +03:00
}
$this -> attributes [ Doctrine :: ATTR_LISTENER ] = $listener ;
return $this ;
}
2007-10-21 10:23:59 +04:00
2006-12-29 17:01:31 +03:00
/**
* returns the value of an attribute
*
* @ param integer $attribute
* @ return mixed
*/
2006-12-29 17:40:47 +03:00
public function getAttribute ( $attribute )
{
2006-12-29 17:01:31 +03:00
$attribute = ( int ) $attribute ;
2007-05-24 20:53:51 +04:00
if ( $attribute < 0 ) {
2006-12-29 17:01:31 +03:00
throw new Doctrine_Exception ( 'Unknown attribute.' );
2007-05-24 20:53:51 +04:00
}
2006-12-29 17:01:31 +03:00
if ( ! isset ( $this -> attributes [ $attribute ])) {
if ( isset ( $this -> parent )) {
return $this -> parent -> getAttribute ( $attribute );
}
return null ;
}
return $this -> attributes [ $attribute ];
}
2007-10-21 10:23:59 +04:00
2006-12-29 17:01:31 +03:00
/**
* getAttributes
* returns all attributes as an array
*
* @ return array
*/
2006-12-29 17:40:47 +03:00
public function getAttributes ()
{
2006-12-29 17:01:31 +03:00
return $this -> attributes ;
}
2007-10-21 10:23:59 +04:00
2006-12-29 17:01:31 +03:00
/**
* sets a parent for this configurable component
* the parent must be configurable component itself
*
* @ param Doctrine_Configurable $component
* @ return void
*/
2006-12-29 17:40:47 +03:00
public function setParent ( Doctrine_Configurable $component )
{
2006-12-29 17:01:31 +03:00
$this -> parent = $component ;
}
2007-10-21 10:23:59 +04:00
2006-12-29 17:01:31 +03:00
/**
* getParent
* returns the parent of this component
*
* @ return Doctrine_Configurable
*/
2006-12-29 17:40:47 +03:00
public function getParent ()
{
2006-12-29 17:01:31 +03:00
return $this -> parent ;
}
2007-10-06 01:18:40 +04:00
}