ATWF A Tcl Web Framwork

Artifact [81a5706a2a]
Login

Artifact [81a5706a2a]

Artifact 81a5706a2a15ed05fc121d0409fb9658c83b845e:


#*************************************************************************
#*
#*  $Author$
#*  $URL$
#*
#*  This module has been implemented in Tcl by using mostly the corresponding module
#*  Application/Resource/ResourceAbstract.php from the Zend framework see: http://framework.zend.com
#*  So the ideas are also copyrighted by Zend Technologies USA Inc. (http://www.zend.com)
#*  The implementation in Tcl uses ditcs and lists instead of php arrays.
#* 
#*  The Contents of this file are made available subject to the terms of
#*  the following license see @license below
#*
#*  Copyright 2010 Arnulf Wiedemann
#*
#*  Author: Arnulf Wiedemann
#*
#*  Contributor(s): 
#*
# @category    ATWF
# @package     library
# @subpackage  Applications::Resources::Resourcebase
# @license     BSD License (see file license.terms in the source top drectory)
# @version     $Id$
#************************************************************************/

namespace eval ::ATWF {

::itcl::extendedclass Applications::Resources::Resourcebase {
    inherit ::ATWF::Base

     # Parent bootstrap
     # 
     # @variable Application_Bootstrap_Bootstrapper
    protected variable _bootstrap [list]

     # Options for the resource
     # 
     # @variable array
    protected variable _options [list]

     # Option keys to skip when calling setOptions()
     #
     # @variable list
    protected variable _skipOptions [list options config]

    constructor {{options {}}} {}

    public method setOptions {options}
    public method getOptions {}
    public method mergeOptions {dict1 {dict2 {}}}
    public method setBootstrap {bootstrap}
    public method getBootstrap {}
}

  #============================ constructor ====================================
  # Create a instance with options
  #
  # @param mixed $options
::itcl::body Applications::Resources::Resourcebase::constructor {{options {}}} {
    if {[llength $options] >= 2} {
        setOptions $options
    } else {
        if {[::itcl::is object -class ::ATWF::Config $options]} {
            setOptions [$options toList]
	}
    }
}

  #============================ setOptions =====================================
  # Set options from array
  #
  # @param  array $options Configuration for resource
  # @return Applications::Resources::Resourcebase
::itcl::body Applications::Resources::Resourcebase::setOptions {options} {
    foreach {key value} $options {
        if {[lsearch $_skipOptions [string tolower $key]] >= 0} {
            continue
        }

        set method "set[string tolower $key]"
        if {[methodExists $this $method real_method]} {
            $real_method $value
        }
        if  {$key eq "bootstrap"} {
            set options [dict remove $options $key]
        }
    }
    
    set _options [mergeOptions $_options $options]

    return $this
}

  #============================ getOptions =====================================
  # Retrieve resource options
  # 
  # @return array
::itcl::body Applications::Resources::Resourcebase::getOptions {} {
    return $_options
}

  #============================ mergeOptions ===================================
  # Merge options recursively
  # 
  # @param  dict  dict1 
  # @param  mixed dict2 
  # @return array
::itcl::body Applications::Resources::Resourcebase::mergeOptions {dict1 {dict2 {}}} {
    if {$dict2 ne ""} {
        foreach {key val} $dict2 {
            if {[llength [dict get $dict2 $key]] >= 2} {
		if {[dict exists $dict1 $key] && ([llength [dict get $dict1 $key]] >= 2)} {
		    dict set dict1 $key [merge_options [dict get $dict1 $key] [dict get $dict2 $key]]
		} else {
		    dict set dict1 $key [dict get $dict2 $key]
		}
            } else {
                dict set dict1 $key $val
            }
        }
    }
    return $dict1
}

  #============================ setBootstrap ===================================
  # Set the bootstrap to which the resource is attached
  # 
  # @param  Application_Bootstrap_Bootstrapper bootstrap 
  # @return Applications::Resources::ResourcebaseResource
::itcl::body Applications::Resources::Resourcebase::setBootstrap {bootstrap} {
    set _bootstrap $bootstrap
    return $this
}

  #============================ getBootstrap ===================================
  # Retrieve the bootstrap to which the resource is attached
  # 
  # @return list|Application_Bootstrap_Bootstrapper
::itcl::body Applications::Resources::Resourcebase::getBootstrap {} {
    return $_bootstrap
}

} ; # END ::ATWF