HomePHP Page 3 - Building a Site Engine with PHP, Part 2
OK, Plug it in Now - PHP
If you read the first article in this series, you’d know that we’re about to start talking about the plug-in and module system for our site engine. Along with the plug-ins and modules, we’re also going to be discussing some of the general functions that are needed to make site engine work correctly.
Now that the past couple of classes are all set up, we're ready to start loading up all our plug-ins. Well, we're ready to make the class that will load them. First we'll select the plug-in ID, name, directory, and file from the database. Then we'll set them all to an array in the plug-ins class, that way the information about the plug-ins can be used later without having to select it from the database again. After we've built our list of initialized plug-ins, we'll use a function that will actually load the plug-ins and include them into our engine. With that all said, let's take a look at the class file.
<?PHP
class plugins{
public $pcount; //this will hold the number of plug-ins that we actually load into the engine public $plugin_list; //this will hold our array of plug-in information public $plugin_error; //this will hold any errors that are produced by loading a plug-in public $lcount; //this will hold the number of plugins in our plug-ins list
function plugins($site_id=""){ //we name the main function the same as the class so that it initializes when the class is called global $sql,$site; //this requests the $sql and $site classes as a global variables $this->pcount=0; //set the default of $pcount to 0 because we don't yet have any plug-ins loaded $this->lcount=0; //set the default of $lcount to 0 because we don't yet have any plug-ins in the plug-ins list if($site_id==""){ //check to see if the administrator overrides the $site_id to list plug-ins for other sites $site_id = $site->site['ID']; //if the site_id isn't overridden set it to the current site's id } $plugins_list=$sql->_query("SELECT plugins.plugin_ID, plugins.plugin_name, plugins.plugin_dir, plugins.plugin_file FROM plugin_status, plugins WHERE plugins.plugin_ID = plugin_status.plugin_ID AND plugin_status.site_ID = '$site_id' AND plugin_status.plugin_status = 'initialized' ORDER BY plugins.plugin_priority DESC"); //select all the initialized plug-in information from the database while($plugin=$sql->_fetch_object($plugins_list)){ //start a loop to get the information from the select query using the _fetch_object function $this->lcount++; // update lcount for each plug-in we add to the plug-ins list $this->plugin_list['plug_ID'][]=$plugin->plugin_ID; //add the current plug-in's ID to the plugin list $this->plugin_list['plug_name'][]=$plugin->plugin_name; //add the current plug-in's name to the plugin list $this->plugin_list['plug_dir'][]=$plugin->plugin_dir; //add the current plug-in's directory to the plugin list $this->plugin_list['plug_file'][]=$plugin->plugin_file; //add the current plug-in's file to the plugin list } if($this->pcount==0){ //check to see if any plug-ins have been loaded into the engine $this->load(); //if no plug-ins are loaded, call the load() function to load the plug-ins } }
function load(){ //this function loads the plug-ins from the plug-ins list if(is_array($this->plugin_list)){ //make sure that the plug-ins list as an array, to prevent errors for($i=0;$i<$this->lcount;$i++){ //start a loop to identify each plug-in in the plug-ins list if(file_exists("{$this->plugin_list['plug_dir'][$i]}/{$this->plugin_list['plug_file'][$i]}/main.plug.php")){ //make sure the plug-in's file exists, to prevent errors @include("{$this->plugin_list['plug_dir'][$i]}/{$this->plugin_list['plug_file'][$i]}/main.plug.php"); //include the plug-in into the engine $this->pcount++; //update pcount for each plug-in that loads }else{ $this->plugin_error[]="The plug-in file: <b>/{$this->plugin_list['plug_dir'][$i]}/{$this->plugin_list['plug_file'][$i]}/main.plug.php</b> Does not exist"; //if the plug-in cant load add an error to the error list } } } } } ?>
Save this as "your_site_dir/inc/plugins.inc.php". Now that we have the plug-ins class set all up, let's get the modules class ready to go.