#*************************************************************************
#*
#*  $Author$
#*  $URL$
#*
#*  This module has been implemented in Tcl by using mostly the corresponding module
#*  Application/Resource/Modules.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 dicts 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
# @license     BSD License (see file license.terms in the source top drectory)
# @version     $Id$
#************************************************************************/

namespace eval ::ATWF {

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

     # @variable list
    protected variable _bootstraps [list]

    constructor {{options {}}} {
        set _bootstraps [dict create]
	chain $options
    } {}

    public method init {}
    public method getExecutedBootstraps {}
    protected method _formatModuleName {name}
}
  #============================ constructor ====================================
  # Constructor
  #
  # @param  mixed options
  # @return void
::itcl::body Applications::Resources::Modules::constructor {{options {}}} {
}

  #============================ init ===========================================
  # Initialize modules
  #
  # @return array
  # @throws Application_Resource_Exception When bootstrap class was not found
::itcl::body Applications::Resources::Modules::init {} {
    set bootstrap [getBootstrap]
    $bootstrap bootstrap FrontController
    set front [$bootstrap getResource FrontController]

    set modules [$front getControllerDirectory]
    set default [$front getDefaultModule]
    foreach module [dict keys $modules] {
        if {$module eq $default} {
            continue
        }

        set bootstrapClass [_formatModuleName $module]::Bootstrap
        if {![classExists $bootstrapClass]} {
            set bootstrapPath  [$front getModuleDirectory $module]/Bootstrap.tcl
            if {[file exists $bootstrapPath]} {
                uplevel #0 source $bootstrapPath
                if {![classExists $bootstrapClass]} {
                    return -code error "Bootstrap file found for module '$module' but bootstrap class '$bootstrapClass' not found"
                }
            } else {
                continue
            }
        }

        set moduleBootstrap [uplevel #0 $bootstrapClass #auto [list $bootstrap [$front getModuleDirectory $module]]]
        $moduleBootstrap bootstrap
        dict set _bootstraps $module $moduleBootstrap
    }

    return $_bootstraps
}

  #============================ getExecutedBootstraps ==========================
  # Get bootstraps that have been run
  #
  # @return ArrayObject
::itcl::body Applications::Resources::Modules::getExecutedBootstraps {} {
    return $_bootstraps
}

  #============================ _formatModuleName ==============================
  # Format a module name to the module class prefix
  #
  # @param  string $name
  # @return string
::itcl::body Applications::Resources::Modules::_formatModuleName {name} {
    set name [string tolower $name]
    set name [regsub -all -- {-} $name { }]
    set name [regsub -all -- {[.]} $name { }]
    set name [::ATWF::Helpers::TitleCaseWords $name]
    set name [regsub -all { } $name {}]
    return $name
}

} ; # END ::ATWF
