All pastes #431777 Raw Edit

Stuff

public text v1 · immutable
·published 2007-04-09 21:45 UTC
rendered paste body
<?php
// Basic plugin structure
abstract class PluginApi
{
    public function __construct()
    {
        // Register ourselves at Core
        Core::getInstance()->registerPlugin($this);
    }

    /* Observer-Pattern */
    abstract public function update();
}


// Some concrete plugin
class SuperPlugin extends PluginApi
{
	private $aInfo = array();

	public function __construct()
	{
		//Register at Core
		Core::getInstance()->registerPlugin($this);

		$this->aInfo['name'] = 'TemplatePlugin';
		$this->aInfo['desc'] = 'Demonstrates the structure of the PluginAPI';
		$this->aInfo['plugins'] = '<needs plugin.. (array?)>';
		$this->aInfo['author'] = 'Created by me';

	}

	public function getInfo()
	{
		return $this->aInfo;
	}
    public function update()
    {
         echo "called update";
    }

}
new SuperPlugin();


// Application core
class Core
{
    private static $instance;
    private $plugins = array();


    /* Constructor */
    public function __construct()
    {
        self::$instance = $this;
    }

    /* Singleton pattern */
    public static function getInstance()
    {
        if (! self::$instance) {
            return new Core();
        }
        return self::$instance;
    }

    /* Init idiom */
    private function init()
    {
    }

    /* Observer-Pattern */
    public function registerPlugin(& $pPlugin)
    {
        $this->plugins[] =& $pPlugin;

    }

    protected function notify()
    {
        foreach($this->plugins as $observer) {
            $observer->update();
        }
    }

    public function getPlugin($name)
    {
        foreach($this->plugins as $observer) {
            $tmp = $observer->getInfo();
            if($tmp[0]==$name)
            	return $observer;
     	}
     }

}
new Core();
$plugin =& Core::getInstance()->getPlugin('TemplatePlugin');
print_r($plugin);
#$plugin->update();