Have PHP child classes add items to a shared array which is stored on the parent class -
have php child classes add items shared array stored on parent class.
then @ end of load, have parent class access items in array added child classes.
in human terms, plugin system allow plugin classes extend parent abstract plugin class allows them add javascript files array , @ end load js files plugins page.
<?php class bookmarksplugin extends plugins{ public function __construct(){ // add 2 js files array property on parent plugins class $this->enqueue_js_file('file-key1', 'fdg/dgdfg/jas-file-1.js', 1); $this->enqueue_js_file('file-key2', 'fdg/dgdfg/js-file-2.js', 7); //parent::enqueue_js_file('file-key1', 'fdg/dgdfg/jas-file-1.js', 1); //parent::enqueue_js_file('file-key2', 'fdg/dgdfg/js-file-2.js', 7); } } class snippetsplugin extends plugins{ public function __construct(){ // add 2 js files array property on parent plugins class $this->enqueue_js_file('file-key3', 'fdg/dgdfg/jas-file-3.js', 1); $this->enqueue_js_file('file-key4', 'fdg/dgdfg/js-file-4.js', 7); //parent::enqueue_js_file('file-key3', 'fdg/dgdfg/jas-file-3.js', 1); //parent::enqueue_js_file('file-key4', 'fdg/dgdfg/js-file-4.js', 7); } } abstract class plugins{ private $_js_files = array(); public function __construct(){ } public function load_js_script_files(){ $html = ''; usort($this->_js_files, array($this, 'sortjsfiles')); foreach ($this->_js_files $obj => $jsscript) { //$jsscript->script_key = $script_key; //$jsscript->file_source = $file_source; $html .= '<script type="text/javascript" src="'.$jsscript->file_source.'"></script>\n'; $html .= 'script type="text/javascript" src="'.$jsscript->file_source.'"></script>\n'; } return $html; } public function enqueue_js_file($script_key, $file_source, $load_order = 0){ $jsscript = new \stdclass; $jsscript->load_order = $load_order; $jsscript->script_key = $script_key; $jsscript->file_source = $file_source; $this->_js_files[$script_key] = $jsscript; } public function sortjsfiles($a, $b) { if ($a->load_order == $b->load_order) { return 0; } return ($a->load_order < $b->load_order) ? -1 : 1; } } $bookmarksplugin = new bookmarksplugin; $snippetsplugin = new snippetsplugin; // function in core plugin class include js files // added array property other plugins. // // of course doesnt work flawed , asking // how can make plugins add js files single shared array , when done // have load js files declared plugins? echo $plugin->load_js_script_files(); ?>
how using traits or variant thereof. merge scripts each plugin before rendering them.
class jsscript { public $name; public $src; public $order; public function __construct($name, $src, $order) { $this->name = $name; $this->src = $src; $this->order = $order; } } trait jsloader { protected $scripts; public function getscripts() { return $this->_js_scripts; } public function enquejs(jsscript $script) { $this->_js_scripts[] = $script; } public function renderscripts() { usort($this->_js_scripts, array($this, 'sortscripts')); foreach($this->_js_scripts $script) { echo $script->src; } } protected function sortscripts($a, $b) { if ($a->order == $b->order) { return 0; } return ($a->order < $b->order) ? -1 : 1; } } class pluginrenderer { use jsloader { renderscripts jsloaderrenderscripts; } protected $plugins; public function __construct(array $plugins) { $this->plugins = $plugins; } public function renderscripts() { $scripts = array(); foreach($this->plugins $plugin) { $scripts = array_merge($scripts, $plugin->getscripts()); } $this->_js_scripts = $scripts; $this->jsloaderrenderscripts(); } } class bookmarkplugin { use jsloader; } class snippetsplugin { use jsloader; }
example use:
$bplugin = new bookmarkplugin; $bplugin->enquejs(new jsscript('foo', 'foo/bar/baz', 99)); $bplugin->enquejs(new jsscript('big', 'big/fat/mama', 2)); $splugin = new snippetsplugin; $splugin->enquejs(new jsscript('y', 'y/u/no', 2)); $renderer = new pluginrenderer(array($bplugin, $splugin)); $renderer->renderscripts();
Comments
Post a Comment