PHP
  Home arrow PHP arrow Page 4 - Building a Site Engine with PHP, Part 2
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Building a Site Engine with PHP, Part 2
By: James Murray
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 70
    2004-06-14


    Table of Contents:
  • Building a Site Engine with PHP, Part 2
  • Best Site() Ever Made
  • OK, Plug it in Now
  • Module Madness
  • Bring it Together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Building a Site Engine with PHP, Part 2 - Module Madness
    ( Page 4 of 5 )

    The modules are loaded in almost the exact same way as the plug-ins. The only difference is that we need to check to see if the plug-in that the module depends on is loaded. There's not much more to it than that. So let's take a look at the module class.

    <?PHP
    class modules{
       
        public $lcount;        //this will hold the number of modules that we actually load into the module list
        public $mcount;        //this will hold the number of modules that we actually load into the engine
        public $module_list;    //this will hold our array of module information
        public $module_error;    //this will hold any errors that are produced by loading a module
        public $module_load;    //this will hold the names of all the modules we've loaded
       
        function modules($site_id=""){    //we name the main function the same as the class so that it initializes when the class is called
            global $sql, $site, $plugins;    //this requests the $sql, $site, and $plugins classes as a global variables
            $this->lcount=0;    //set the default of $mcount to 0 because we don't yet have any modules in the modules list yet
            $this->mcount=0;    //set the default of $mcount to 0 because we don't yet have any modules loaded
            if($site_id==""){    //check to see if the administrator overrides the $site_id to list modules for other sites
                $site_id = $site->site['ID'];    //if the site_id isn't overridden set it to the current site's id
            }
            $modules_list=$sql->_query("SELECT
                                        DISTINCT
                                             modules.mod_ID,
                                             modules.mod_name,
                                             modules.mod_dir,
                                             modules.mod_file,
                                             modules.plugin_ID
                                        FROM
                                             module_status,
                                             plugin_status,
                                             modules
                                        WHERE
                                             modules.mod_ID = module_status.mod_ID AND
                                             modules.plugin_ID = plugin_status.plugin_ID AND
                                             module_status.mod_status = 'initialized' AND
                                             plugin_status.plugin_status = 'initialized' AND
                                             module_status.site_ID = '$site_id'");    //select all the initialized module information from the database
            while($module=$sql->_fetch_object($modules_list)){    //start a loop to get the information from the select query using the _fetch_object function
                    $this->module_list['mod_ID'][]=$module->mod_ID;    //add the current modules ID to the plugin list
                    $this->module_list['mod_name'][]=$module->mod_name;    //add the current modules name to the plugin list
                    $this->module_list['mod_dir'][]=$module->mod_dir;    //add the current modules directory to the plugin list
                    $this->module_list['mod_file'][]=$module->mod_file;    //add the current modules filename to the plugin list
                    $plug_key=array_keys($plugins->plugin_list['plug_ID'],$module->plugin_ID);    //select the plug-in ID from the plug-in list using the array_keys() function with the modules "plugin_id" as a search parameter
                    $this->module_list['plug_dir'][]=$plugins->plugin_list['plug_dir'][$plug_key[0]];    //add the current modules dependency's directory to the plugin list
                    $this->module_list['plug_file'][]=$plugins->plugin_list['plug_file'][$plug_key[0]];    //add the current modules dependency's file to the plugin list
                    $this->lcount++;    // update mcount for each module we add to the module list
            }
            if($this->mcount==0){    //check to see if any modules have been loaded into the engine
                $this->load();    //if no modules are loaded, call the load() function to load the modules
            }
        }
       
        function load(){    //this function loads the modules from the modules list
            if(is_array($this->module_list)){    //make sure that the modules list as an array, to prevent errors
                for($i=0;$i<$this->mcount;$i++){    //start a loop to identify each module in the plug-ins list
                    if(file_exists("/{$this->module_list['plug_dir'][$i]}/{$this->module_list['plug_file'][$i]}/{$this->module_list['mod_dir'][$i]}/{$this->module_list['mod_file'][$i]}.mod.php")){    //make sure the modules file exists, to prevent errors
                        include("/{$this->module_list['plug_dir'][$i]}/{$this->module_list['plug_file'][$i]}/{$this->module_list['mod_dir'][$i]}/{$this->module_list['mod_file'][$i]}.mod.php");    //include the module into the engine
                        $this->module_load[]=$this->module_list['mod_name'][$i];    //add the modules name to list of loaded modules
                        $this->mcount++;    //update mcount for each module that's loaded
                    }else{
                        $this->module_error[]="The module file: <b>/{$this->module_list['plug_dir'][$i]}/{$this->module_list['plug_file'][$i]}/{$this->module_list['mod_dir'][$i]}/{$this->module_list['mod_file'][$i]}.mod.php</b> Does not exist";    //if the module can't load add an error to the error list
                    }
                }
            }   
        }
       
    }
    ?>

    As you can see, it's not much different from the plug-ins class. In fact it's frighteningly the same. Nevertheless it all works great and very fast. Save this as "your_site_dir/inc/modules.inc.php". There is one thing I'd like to point out though; the line that looks like this:

    $plug_key=array_keys($plugins->plugin_list['plug_ID'],$module->plugin_ID);

    array_keys() is a great function. It's very useful. It allows you to return an array of the keys from another array. But, that's not the best part of array_keys(). The best part is that you can specify a search parameter to match. That's what I've done here. I've taken the value of $module->plugin_id and used it as the search parameters to search the plug-ins list array and then return the key to the specific plug-in that the particular module depends on. Now that we've gotten the plug-ins and module systems set up, all we have left is to put it all together.



     
     
    >>> More PHP Articles          >>> More By James Murray
     

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek