Subversion-Projekte lars-tiefland.php_share

Revision

Blame | Letzte Änderung | Log anzeigen | RSS feed

<?php
/* $Id: Functions.class.php 10 2007-05-02 12:47:02Z tiefland $ */

/**
 * Functions handling class, for modules.
 *
 * This class handles the loading of any function needed by
 * a module e.g. Shop or any else. You easy could instance
 * this in the main Class of the module. 
 * @see Shop()
 *
 * PHP versions 4 and 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @category   CMS
 * @package    Functions
 * @author     Markus Niewerth <markus@weban.de>
 * @copyright  1997-2005 The PHP Group
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 * @since      File available since Release 1.0.1
 */
 
 
// {{{ constants

         // -------------------
         // IF any put in here!
         // -------------------

// }}}
// {{{ GLOBALS
 
         // -------------------
         // IF any put in here!
         // -------------------
 
// }}}
// {{{ Functions

/**
 * Funktions Verwaltungs Modul
 *
 * This class handles the loading of any function needed by
 * a module e.g. Shop or any else. You easy could instance
 * this in the main Class of the module. 
 * @see Shop()
 *
 * Diese Klasse behandelt das laden von Funktionen, die mit hilfe
 * von listFiles aufgelistet werden, oder durch �bergabe von 
 * parametern, geladen werden k�nnen.
 *
 * @category   CMS
 * @package    Weban_Shop
 * @author     Markus Niewerth  <markus@weban.de>
 * @author     Lars Tiefland    <tiefland@weban.de>
 * @copyright  1997-2007 Webagentur Niewerth
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 * @link       http://server2/intranet/3_0.txt
 * @see        Functions
 * @since      Class available since Release 1.1
 */


class Functions  {

        // {{{ properties
        
        /**
         * Holds all file/function names.
         * 
         * @var array
     */
        var $functions;
        
        /**
         * The Shops ID number.
         * 
         * Its a very important ID, cause the 
         * ID is saved in any userdefined data-
         * base entry like articles a.s.o.
         *
         * @var int     
     */ 
        var $shopsId;
        
        /**
         * The Shop language.
         *
         * The Base language is DE. Note that this
         * value could be mixed! But stadard is DE
         *
         * @var         array    
     */
        var $language;
        
        /**
         * Wich function is really defined after loading?
         *
         * Sometimes its possible that there are 
         * more then one function defined in a file.
         * So we have to check wich functions are 
         * temporary loaded while executed the factory. 
         *
         * @var array
     */ 
        var $defined;
        
        /**
         * Extensions to load.
         * 
         * This parameter is needed in the file loader/includer.
         *
         * @var string  
         * @access private
     */
        var $_ext;
        
        /**
         * Server OS for call_user_method.
         *
         * @var string
         * @access private      
     */
        var $_os;
        
        /**
         * Helper array for defined and loaded functions.
         *
         * @var array
         * @access private
     */
        var $_defined;
        
        /**
         * Load indexer.
         * 
         * Need in the context of navigating through the 
         * functions array. Helps to get the primary and
         * last array key of the _defined array
         * {@see $_defined}
         *
         * @param       int             
         * @access private       
     */
        var $_indexer;
        
        // }}}
        // {{{ Functions()
        
        function Functions ($shopsId=1, $language = __BASELANGUAGE__) 
        {
                // Init needed Parameters.
                $this->shopsId          = $shopsId;
                $this->language         = $language;    
                $this->_os                      = substr(PHP_OS, 0, 3);
                $this->functions        = array();
                $this->_ext                     = "php";
                $this->_defined         = array();      
                $this->defined          = array();              
                $this->_indexer         = 0;
        }
        // }}}
        // {{{ factory()
        
        function factory ($_option='load') 
        {
                if( strlen($_option) < 3 ) {
                        return false;
                } elseif($_option == 'load') {
                        foreach
                                        ( 
                                                call_user_method 
                                                        ( 
                                                                "listFiles" . $this->_os , 
                                                                $this, 
                                                                $_SESSION['INI']['systemPath'], 
                                                                $this->_ext
                                                        ) 
                                                as $file 
                                        )
                        {
                                $this->_engine ($_option, $file);
                        }
                        // Den Funktions- Ueberpruefungsindex resetten
                        $this->_defined = array();
                        return true;    
                } else {
                        return false;
                }
        }
        // }}}
        // {{{ _engine()
        
        function _engine ($_type, $_file) 
        {
                switch ($_type) {
                        case 'load':
                                $this->functions[] = $_file;
                                return $this->_load ($_file);
                                break;
                                
                        case 'remove':
                                return false;
                                break;
                                
                }
                return true;
        }
        // }}}
        // {{{ _load()
        function _load ($_function) 
        {
                $_fname = basename ($_function,".php");
                
                if (function_exists($_fname)) 
                        return false;
                        
                $this->_indexer = sizeof($this->_defined) + 1;
                $_last = $this->_indexer - 1;
                $_next = $this->_indexer + 1;

                $tmp = get_defined_functions();
                $this->_defined[$this->_indexer] = $tmp['user'];
                
                require_once($_function);
                
                // Den Funktionsindex auf dem Laufenden halten...
                // Alle Funktionen die �ber diese Factory geladen werden,
                // werden in einem Array gespeichert.
                // FixMe: Letze geladene Funktion kann so nicht erfasst werden.
                // Und sollte require_once vor get_defined_functions gesetzt werden,
                // wird die erste Funktion nicht erfasst. L�sung: Es m�ssen zwei Arrays
                // nebeneinander existieren die sich durcheinander abgleichen.
                        
                if (is_array($this->_defined[$_last])) {
                        foreach(array_keys($this->_defined[$this->_indexer]) AS $elem) {
                                if ($this->_defined[$_last][$elem] != $this->_defined[$this->_indexer][$elem]) {
                                        $this->defined[] = $this->_defined[$this->_indexer][$elem];
                                }
                        }
                }
                return true;
        }
        // }}}
        // {{{ _remove()
        
        function _remove () 
        {
                return 0;
        }
        // }}}
        // {{{ _add()
        
        function _add () 
        {
                return 1;
        }
        // }}}
        // {{{ listFilesLin()
        
        // Linux behavior
        function listFilesLin ($_dir, $_ext="php")
        {
                $files=array();
                exec("find $_dir -type f -name '*.$_ext'", $files);
                return $files;
        }
        // }}}
        // {{{ listFilesWin()
        
        // Windows behavior
        function listFilesWin ($_dir, $_ext=".php")
        {
                $files=array();
                $dir = dir($_dir);
                while (false !== ($fEntry = $dir->read())) {
                   if(ereg($_ext,$fEntry))
                        $files[] = $_dir.$fEntry;
                }
                $dir->close();
                return $files;
        }
}
// }}}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * c-hanging-comment-ender-p: nil
 * End:
 */
?>