Skip to content

Instantly share code, notes, and snippets.

@beingzoe
Created January 25, 2011 23:25
Show Gist options
  • Save beingzoe/795923 to your computer and use it in GitHub Desktop.
Save beingzoe/795923 to your computer and use it in GitHub Desktop.
protected static $_known_appliances;
// Registers a loadable class
public function registerToLoad($library, $path, $class_name = FALSE) {
$_known_appliances[$library]['path'] = $path;
$_known_appliances[$library]['class_name'] = $class_name; // FALSE if appliance is not a class
}
// Load classes and instantiate
public function load($library, $constructor_args = NULL, $doInstantiate = TRUE) {
if (array_key_exists($library, KST::$_known_appliances)) { // Find the known appliance to load
require_once KST::$_known_classes[$library]['path']; // Load the file
if ( $_known_appliances[$library]['class_name'] ) { // FALSE if appliance is not a class
$class_name = KST::$_known_classes[$library]['class_name'];
if ( $instantiate )
$result = $this->{$library} = new $class_name($constructor_args);
} else {
return TRUE; // Just tell them we finished
}
} else {
$result = FALSE;
}
return FALSE;
}
// All bundled KST loadable appliances would just be set during the main init
// So we could just load any of the bundled like so...
$my_kitchen->load('asides', $args);
// A plugin needing to add an available class that a kitchen needs to know about would...
$my_kitchen->registerToLoad('twitter_sidebar', '/their/absolute/path/TwitterSidebar.php', 'JOE_TwitterSidebar');
$my_kitchen->load('asides', array('asides', 'gallery'));
// Or
$my_kitchen->registerToLoad('MetaBox', KST_DIR_VENDOR . '/WPALCHEMY/MetaBox.php', 'WPAlchemy_MetaBox');
$args = array (
'id' => '_custom_meta',
'title' => 'My Custom Meta',
'template' => TEMPLATEPATH . '/custom/meta.php'
);
$my_kitchen->load('MetaBox');
$my_kitchen->MetaBox->the_field('description');
$my_kitchen->MetaBox->the_name();
// and so on...
// If it is a static class like ZUI_FormHelper.php
// Mainly just a convenience I think to have one method for loading everything
$my_kitchen->load('asides', $args, FALSE); // Where false means just require the file and don't instanitate
// Similarly...if it is just a functions library or possibly even (heaven forbid procedural code)
// It just gets loaded
$my_kitchen->load('sensible_defaults');
// And otherwise if you need to create multiple instances you could...
$my_metabox_1 = $my_kitchen->load('MetaBox', $args1);
$my_metabox_2 = $my_kitchen->load('MetaBox', $args2);
// And then themes can load and use all the bundled stuff in multiple ays without knowing where anything is
// Does this make sense?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment