Skip to content
Deathart edited this page Aug 27, 2015 · 6 revisions

Category:Library::Community | Category:Library::Loader

<?php
    /*****************************************************************
    | biz_Loader.php
    |
    | CI_Loader extension class.
    |
    | Copyright (c) 2008, Biz Computing, Inc.
    | Portions based on code Copyright (c) 2006, EllisLab, Inc., All rights reserved.
    |
    | Permission is hereby granted, free of charge, to any person obtaining a copy
    | of this software and associated documentation files (the "Software"), to deal
    | in the Software without restriction, including without limitation the rights
    | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    | copies of the Software, and to permit persons to whom the Software is
    | furnished to do so, subject to the following conditions:
    |
    | The above copyright notice and this permission notice shall be included in
    | all copies or substantial portions of the Software.
    |
    | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    | THE SOFTWARE.
    |
    | Version 1.0 - Written by Jim Hendricks, Biz Computing, Inc.
    |   - library method extended with $instantiate argument defaulted to TRUE.
    |     If this argument is FALSE, method will only load the class/
    |   - Added a module method.  This method loads special purpose libraries
    |     that extend the module class and reside in the <application>/modules
    |     folder.
    |
    | NOTES:
    |   - Both extensions above rely on a private method _biz_load_class.
    |   - These extensions only check the application set of folders and does
    |     not fall back to CI system folders when not found in application
    |     folders.
    |   - Since modules are assumed to be an extension of the module class,
    |     a class called module must exist in your libraries folder.  There
    |     are no requirements as to the structure of this class, only that
    |     it exists.
    |
    *******************************************************************/

    class biz_Loader extends CI_Loader {


        /*****************************************************************
        | biz_Loader (constructor)
        |
        | Currently only exists to extend the default constructor to output
        | a debug message.
        |
        | Version 1.0 - Written by Jim Hendricks, Biz Computing, Inc.
        *******************************************************************/
        function biz_Loader() {
            parent::CI_Loader();
            log_message('debug', "biz_Loader Class Initialized");
        }


        /*****************************************************************
        | module
        |
        | Loader method for loading libraries that reside in the
        | <application>/modules folder and extend the
        | <application>/libraries/module library. It's current purpose is
        | to act as a sub controller in a nested MVC.  Sub Controllers are
        | modules that can load their own view and model thru the hosting
        | controller.
        |
        | arguments:
        |   - $module      - Either a string with the name of 1 module to load
        |                    or an array of module names to load multiple
        |                    modules in 1 call.
        |   - $params      - Arguments array to pass to a module when
        |                    instantiating.  If $module is an array, the
        |                    same $params will be provided to each module.
        |   - $instantiate - Flag to supress instantiation.  In it's default
        |                    condition, module(s) will be loaded and instantiated.
        |                    If this flag is passed as FALSE, module(s) will
        |                    only be loaded.
        |
        | return:
        |   - FALSE if no module is provided, no return otherwise.
        |
        | Version 1.0 - Written by Jim Hendricks, Biz Computing, Inc.
        *******************************************************************/
        function module($module = '', $params = NULL, $instantiate = TRUE) {
            // 1st load module class
            $this->library('module', NULL, FALSE);

            if ($module == '') {
                return FALSE;
            }

            if( !is_array($module) ) {
                $module = array( $module );
            }
            foreach ($module as $class) {
                $result = $this->_biz_load_class($class, 'modules/');

                if( $instantiate && $result !== 'dupe' ) {
                    $this->_ci_init_class($class, '', $params);
                    $this->_ci_assign_to_models();
                }
            }
        }


        /*****************************************************************
        | library
        |
        | Method that is identical to the CI system loader library method
        | except an additional argument is passed that allows supression
        | of instantiation of the loaded class.
        |
        | arguments:
        |   - $library     - Either a string with the name of 1 library to load
        |                    or an array of library names to load multiple
        |                    libraries in 1 call.
        |   - $params      - Arguments array to pass to a library when
        |                    instantiating.  If $library is an array, the
        |                    same $params will be provided to each library.
        |   - $instantiate - Flag to supress instantiation.  In it's default
        |                    condition, library(s) will be loaded and instantiated.
        |                    If this flag is passed as FALSE, libary(s) will
        |                    only be loaded.
        |
        | return:
        |   - FALSE if no library is provided, no return otherwise.
        |
        | Version 1.0 - Written by Jim Hendricks, Biz Computing, Inc.
        *******************************************************************/
        function library($library = '', $params = NULL, $instantiate = TRUE) {
            if( $instantiate ) {
                parent::library( $library, $params );
            } else {
                if ($library == '') {
                    return FALSE;
                }

                if (is_array($library)) {
                    foreach ($library as $class) {
                        $this->_biz_load_class($class);
                    }
                }    else {
                    $this->_biz_load_class($library);
                }
            }
        }


        /*****************************************************************
        | _biz_load_class
        |
        | Private method for performance of actual loading of libraries and
        | modules loaded via biz_Loader extended functions.  This is actually
        | a copy of _ci_load_class with subclassing, instantiation, and
        | system class loading removed.  Additionally a path was added
        | to allow loading of classes from paths other than the default
        | <application>/libraries folder.
        |
        | arguments:
        |   - $class - Name of class to load
        |   - $path  - <application> subfolder from where to load class
        |
        | return:
        |   - 'dupe' if loaded class is already loaded, nothing returned otherwise.
        |
        | Version 1.0 - Written by Jim Hendricks, Biz Computing, Inc.
        *******************************************************************/
        function _biz_load_class($class, $path = 'libraries/' ) {
            // Get the class name
            $class = str_replace(EXT, '', $class);

            // We'll test for both lowercase and capitalized versions of the file name
            foreach (array(ucfirst($class), strtolower($class)) as $class) {

                // Lets search for the requested library file and load it.
                $is_duplicate = FALSE;

                $filepath = APPPATH.$path.$class.EXT;

                // Does the file exist?  No?  Bummer...
                if ( ! file_exists($filepath)) {
                    continue;
                }

                // Safety:  Was the class already loaded by a previous call?
                if (in_array($filepath, $this->_ci_classes)) {
                    $is_duplicate = TRUE;
                    log_message('debug', $class." class already loaded. Second attempt ignored.");
                    return 'dupe';
                }

                include($filepath);
                $this->_ci_classes[] = $filepath;
                return;
            } // END FOREACH

            // If we got this far we were unable to find the requested class.
            // We do not issue errors if the load call failed due to a duplicate request
            if ($is_duplicate == FALSE) {
                log_message('error', "Unable to load the requested class: ".$class);
                show_error("Unable to load the requested class: ".$class);
            }
        }

    }
?>



<?php
    /*****************************************************************
    | module.php
    |
    | Copyright (c) 2008, Biz Computing, Inc.
    |
    | Permission is hereby granted, free of charge, to any person obtaining a copy
    | of this software and associated documentation files (the "Software"), to deal
    | in the Software without restriction, including without limitation the rights
    | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    | copies of the Software, and to permit persons to whom the Software is
    | furnished to do so, subject to the following conditions:
    |
    | The above copyright notice and this permission notice shall be included in
    | all copies or substantial portions of the Software.
    |
    | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    | THE SOFTWARE.
    |
    | Module acts as a controller for a simulated nested MVC
    |
    | Module should be subclassed.  Actual load of module should be
    | via customized Loader library call with $instantiate set to FALSE
    | This will load Module as just a class def and will then allow
    | subclassed models to load as normal library loads but will still
    | be able to locate the base class.
    |
    | -- method index should be extended to provide display controller
    | -- method submit should be extended to provide handling of form
    |    submission
    | -- method validate should be extended to provide any validations
    | -- method save should be extended to provide actual save code
    |
    | refer to the comments on each method for more detailed use
    |
    *******************************************************************/

    class module {

        // reference to parent controller
        var $ci;


        /*****************************************************************
        | module (constructor)
        |
        | setup standard module
        |
        | Subclasses should add additional initializations needed by
        | more than one of modules methods.
        |
        | Version 1.0 - Written by Jim Hendricks, Biz Computing, Inc.
        *******************************************************************/
        function module() {
           $this->ci =& get_instance();
        }


        /*****************************************************************
        | index
        |
        | method should be called by parent controller to produce the
        | modules view
        |
        | args:
        |   $data - optional array of parameters needed by the module
        |           direct access to base controller properties also
        |           available via $this->ci->
        |
        | returns:
        |        key/value array of any data needed by parent controller
        |   standard keys:
        |   -- 'view' - actual html text to add to parent view
        |   -- 'message' - array of messages
        |
        | Version 1.0 - Written by Jim Hendricks, Biz Computing, Inc.
        *******************************************************************/
        function index( $data = array() ) {
        }


        /*****************************************************************
        | submit
        |
        | method should be called by parent controller to handle form
        | submission.  Parent controller will only call module submit
        | if action variable does not match a parent controller action.
        |
        | args:
        |   $action - Action command stripped from pressed submit button
        |             through get_form_action() - this then assumes
        |             that submit buttons follow the action[module_command]
        |             naming convention.
        |   $data   - optional array of parameters needed by the module
        |             direct access to base controller properties also
        |             available via $this->ci->
        |
        | returns:
        |        key/value array of any data needed by parent controller
        |   standard keys:
        |   -- 'result'  - 'handled', 'handled_redisplay', 'not_handled'
        |   -- 'message' - array of messages
        |
        | Version 1.0 - Written by Jim Hendricks, Biz Computing, Inc.
        *******************************************************************/
        function submit( $action, $data = array() ) {
        }


        /*****************************************************************
        | validate
        |
        | method should be called by parent controller to validate data
        | module submit validations should call this method.
        |
        | args:
        |   $data   - optional array of parameters needed by the module
        |             direct access to base controller properties also
        |             available via $this->ci->
        |
        | returns:
        |        key/value array of any data needed by parent controller
        |   standard keys:
        |   -- 'valid'   - TRUE/FALSE
        |   -- 'message' - array of messages
        |
        | Version 1.0 - Written by Jim Hendricks, Biz Computing, Inc.
        *******************************************************************/
        function validate( $data = array() ) {
        }


        /*****************************************************************
        | save
        |
        | method should be called by parent controller to save module data
        | module submit saves should call this method.
        |
        | args:
        |   $data   - optional array of parameters needed by the module
        |             direct access to base controller properties also
        |             available via $this->ci->
        |
        | returns:
        |        key/value array of any data needed by parent controller
        |   standard keys:
        |   -- 'save'   - TRUE/FALSE
        |   -- 'message' - array of messages
        |
        | Version 1.0 - Written by Jim Hendricks, Biz Computing, Inc.
        *******************************************************************/
        function save( $data = array() ) {
        }

    }
?>
Clone this wiki locally