Jump to content

Module system question


DeadxBeat

Recommended Posts

Ok...so I'm trying to code a module loading system and I've got it working to where if I name the modules in a database it will load them from their correct folders in the module folder. Now I want to take it a step further and run a construct in my module class that will put the directories in the module folder into an array and then automatically add them to a database if they aren't in there and also remove the items from the database whose folders no longer exist to prevent from trying to load a module with no files.

 

Really, all I need to know is the most efficient way to scan a directory that is given and put the names of all folders only(no files) into an array.

 

Any help would be greatly appreciated, I have looked at scandir() and such but I need to be given the context in how to use it the way I want to, also how to exclude the files and the . and .. from the listing

Link to comment
Share on other sites

I'd profile out using the SPL directory iterator versus what you have going at the moment, I would think it would be faster.

 

You probably know this, but what you said previously about only searching for directories could be taken a couple of ways.  Most APIs in most languages don't differentiate between a file and a directory while scanning without an additional call to the disc.  When placed on the disc, file and directories use up an inode (or a variant mapping method), the inode contains meta data and data fragments.  The difference being a file might span across many, many inodes to create the entirety of the file.  When these functions are searching they grab the basic inode data, place (on disc) and name being all they usually need.  This is to speed up things.  Imagine a single php script called from a relative path, when called the php interpretor has to track down its own config, look through all them one at a time going through file and directories looking for the correct one.  Then it loads up your include path and scans down all of those directories looking for your file.  Discs are under a constant barrage of calls for info on disc objects.

 

This also brings up a good point, use absolute paths if you can.  With your loader, perhaps you could try creating a hash table lookup generated at release, your files probably aren't going anywhere then and it'll save on all the file scanning.  IIRC that's what the new plugin loader for Zend Framework 2 uses.

Link to comment
Share on other sites

Woohoo! Get to quote the same php file twice today!

 

function Load_Module($module) {

	//Set up the directory string.
	$load = SITE_ROOT . '/' . $module . '/class/' .  $module . '.class.php';
		$c = pathinfo($load);
		if (is_dir($c['dirname'])) {

			$dir = $c['dirname'];
			$filename = $dir . NR_DS . $c['filename'] . '.' . $c['extension'];

		}
		else {
			return false;
		}
		if (include($filename)) {
			return true;	
		}


$modules = array($loadedModulesFromDB);

foreach ($modules as $module) {
   Load_Module($module);
}

 

This would require that you have the same directory structure for all your modules. It is also untested so I don't even know if it'd work the way as expected. Having your modules all over the place is messy and not good practice or efficient. This also loads the php file, but I'm sure you could figure out how to modify it if it's useful to you.

Link to comment
Share on other sites

This is my alpha module class that I finished yesterday and works well except Im still trying to figure out a way to include css from individual modules into the main css of the site when those modules are loaded. I documented a little bit, basically I call loadMods() from the _module class on the index page in the nav bar where modules will go( Modules for my cms will be like Polls, Latest Posts, Friends, etc..)

 

<?php

class _module { // Class for all things module related

	private $modlist = null; // Variable containing list of all modules

	private function getFolders() { // Function to put all directory names in the module folder into a class property array

		$base_dir = "./modules/"; // Path to modules directory

		$bad = array(".",".."); // Exclude these "directories"
		$result = scandir($base_dir); // Scan the directory and put all results into array
		$dir = array_diff($result, $bad); // Remove the directories that are to be excluded

		chdir($base_dir); // Make the current working directory the module directory

		foreach($dir as $test) { // Loop through contents of modules directory

			if(is_dir($test) == true){ // If the current content is a directory then...
				$this->modlist[] = $test; // Add directory to global array
			}
		}

		chdir("../"); // Go back to root directory

	} // End function getFolders()

	private function updateMods() { // Function to add new modules into database

		$this->getFolders(); // Get all the directories within the module directory

		foreach($this->modlist as $module) { // Loop through contents and add new folders to database if they dont exist

			// TODO REMINDER: Make this so it only adds the folder if the module.php file resides inside folder

			$sql = "SELECT * FROM cmsModules WHERE name='" . $module . "' LIMIT 1";
			$result = mysql_query($sql) or die(mysql_error());

			if(mysql_num_rows($result) == 0) {
				$sql = "INSERT INTO cmsModules VALUES (null, '$module', 0)";
				mysql_query($sql) or die(mysql_error());
			}

		}

	} // End function updateMods()

	public function loadMods() { // Load the module and display to page

		$this->updateMods(); // Update the database of modules first

		$module_base_dir = './modules/';
		$module_ext = '.module.php';

		$sql = "SELECT * FROM cmsModules WHERE enabled=1"; // Only select enabled modules
		$result = mysql_query($sql) or die(mysql_error());

		while($row = mysql_fetch_assoc($result)) {
			$module = $row['name'];
			$module_base_file = $module . $module_ext;

			if(file_exists($module_base_dir . $module . '/' . $module_base_file)) {  // Make sure file exists then...
				include_once($module_base_dir . $module . '/' . $module_base_file);	 // Include file once
				$moduleClass = new $module; // Instantiate modules class

				if(method_exists($moduleClass, 'loadMod')) {
					$moduleClass->loadMod(); // Run modules load mechanism
				}
				else {
					echo 'Error: Incorrectly formatted module.';	
				}

			}
			else {
				echo 'Error: Module ' . $module . ' does not exist<br />';	
			}

		}

	} // End construct

}

?>

 

Any constructive feedback on ways to improve this would be greatly appreciated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.