-
-
Save scragz/795944 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class KST_Kitchen { | |
protected static $_appliances; | |
// Registers a loadable appliance and shortname for it | |
public function registerAppliance($shortname, $path, $class_name=false) { | |
if (array_key_exists($shortname, $_appliances)) { | |
// collision! | |
} else { | |
self::$_appliances[$shortname] = []; | |
self::$_appliances[$shortname]['path'] = $path; | |
self::$_appliances[$shortname]['class_name'] = $class_name; // FALSE if appliance is not a class | |
} | |
} | |
// Load classes and instantiate | |
/** | |
* load appliance | |
* | |
* @param $shortname String or Array The appliance shortname or an array of the shortname and the property you want to use to access this appliance | |
* @params *args Variable amount of remaining arguments will be passed to the constructor | |
*/ | |
public function load($shortname) { | |
$args = func_get_args(); | |
shift($args); // get rid of $shortname | |
if (is_array($shortname)) { | |
list($shortname, $property) = $shortname; | |
} else { | |
$property = $shortname; | |
} | |
if (array_key_exists($shortname, self::$_appliances)) { // Find the known appliance to load | |
$appliance = self::$_appliances[$shortname]; | |
require_once $appliance['path']; // Load the file | |
if ( $appliance['class_name'] ) { // FALSE if appliance is not a class | |
$reflection = new ReflectionClass($appliance['class_name']); | |
$this->{$property} = $_reflection->newInstanceArgs($args); | |
return true; | |
} else { | |
return TRUE; // Just tell them we finished | |
} | |
} else { | |
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->loadA('asides', 'gallery', 'tweets'); | |
// $my_kithcen->asides->something(); | |
// A plugin needing to add an available class that a kitchen needs to know about would... | |
$twitter_sidebar_kitchen->registerAppliance('twitter_sidebar', '/their/absolute/path/TwitterSidebar.php', 'TwitterSidebar'); | |
$my_kitchen->load(array('twitter_sidebar', 'my_twitter'), 'foo', 'bar'); | |
$my_kitchen->my_twitter->foo(); | |
// Or | |
$metabox_kitchen->registerAppliance('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->loadAppliance('MetaBox'); | |
$my_kitchen->MetaBox->the_meta($args); | |
// and so on... | |
// I didn't update below here | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment