Post by Qphoria » Tue Sep 08, 2009 11:19 pm

OpenCart has a very powerful module extension system, but it stops there as far as dynamic extensions.

Here are some additional ones that I've added for some clients, or just ideas that I've been pondering but haven't fully come to fruition:

Autoload javascript folder
Javascripts are so cool because they can be kept in their own abstract layer, without having to edit files 99% of the time. But ironically enough, to add a new script, you have to edit the code to add the script tag. Instead there should be an autoload subdirectory in the javascript folder where all script files inside this folder are automatically added as script tags to the main layout.

The layout file would then have a simple glob() call for that directory and loop through each file, adding it to the <head></head> tags of the page. Something like:

Code: Select all

<?php // Autoload Javascript ?>
<?php $files = glob(DIR_AUTOLOADJS . '*.js'); ?>
<?php if ($files) { ?>
<?php foreach ($files as $filename) { ?>
          <script type="text/javascript" src="<?php echo DIR_AUTOLOADJS . $filename; ?>"></script>		
<?php } ?>
<?php } ?>
You could even go a step further by adding subcategories using controller names, then only load those files if on that controller page. (e.g. autoloadjs/product/xxx.js would only load if looking at the product page. It would not load for the home page or checkout pages, etc)

Dynamic Includes for loading new Registry entries
A folder that auto-includes for the index.php page so that new helpers and libraries can be loaded. Additionally, a way to dynamically include "pre-actions" that can load before other things.

Something like:

Code: Select all

// Weight
Registry::set('measurement', new HelperMeasurement());

// Cart
Registry::set('cart', new HelperCart());

// Dynamic includes
$files = glob(DIR_INCLUDES . '*.php'); ?>
if ($files) {
          foreach ($files as $filename) { 
                    include (DIR_INCLUDES . $filename;);
          }
}
I believe the php autoload function can also do something like this.

On-the-fly php preg_replace/str_replace
Anytime a php is included either by include or before it is eval'd with obstart()
There could be a check for a snippet file that uses some sort of relative naming convention. Example:
The helper function loads helper files. If you wanted to add code to change one of the helper files. You could change the function from this:

Code: Select all

public function helper($helper) {
	$file = DIR_SYSTEM . 'helper/' . $helper . '.php';
	
	if (file_exists($file)) {
		include_once($file);
	} else {
		exit('Error: Could not load helper ' . $helper . '!');
	}
}
TO:

Code: Select all

public function helper($helper) {
	$file = DIR_SYSTEM . 'helper/' . $helper . '.php';
	$patch_find = DIR_SYSTEM . 'helper/' . $helper . '.php_find';
	$patch_repl = DIR_SYSTEM . 'helper/' . $helper . '.php_repl';

	if (file_exists($file)) {
		if (file_exists($patch)) {
			$filedata = file_get_contents($file);
			$find = file_get_contents($patch_find);
			$repl = file_get_contents($patch_repl);
			$filedata = str_replace($find, $repl, $filedata);
			file_put_contents($filedata, $helper . '_tmp');
			$file = DIR_SYSTEM . 'helper/' . $helper . '_tmp.php';
		}
		include_once($file);
	} else {
		exit('Error: Could not load helper ' . $helper . '!');
	}
}
That is a very neanderthalic way of writing it just to make the idea clearer, but clearly it could use some improvements and optimization. But that will essentially take a file, lets say "helper/customer.php". read its data, do a find/replace on it, lets say change
private function login ($email, $pass) {
to
private function login ($email, $pass, $xxx = '') {
Then load that version of the file with the change.

This could then make core mods easier by making find/repl patches that won't actually edit any core files.

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by imaginetech » Wed Sep 09, 2009 1:15 pm

Definitely would like to have the javascript directory autoloaded.

Even having an autoloaded javascript directory under the theme/yourtheme directory would be handy. Would allow templates to include specific javascript files without "reaching" outside the theme directory.

Image
www.opencartstore.com
Get OpenCart Templates and OpenCart Modules.


User avatar
Active Member

Posts

Joined
Fri Sep 04, 2009 12:25 pm
Location - Darwin, Australia
Who is online

Users browsing this forum: No registered users and 9 guests