<?php
 
/**
 
 * Script to provide different examples of using the registry/service locator
 
 * 
 
 * This program is free software: you can redistribute it and/or modify
 
 * it under the terms of the GNU Lesser General Public License as
 
 * published by the Free Software Foundation, either version 3 of the
 
 * License, or (at your option) any later version.
 
 * 
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU Lesser General Public License for more details.
 
 * 
 
 * You should have received a copy of the GNU Lesser General Public License
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 * 
 
 * @author SoBeNoFear <[email protected]>
 
 * @copyright 2007-2009 Ian Unruh
 
 * @license LGPLv3 http://gnu.org/licenses/lgpl.txt
 
 * @version 1.0
 
 * @package RSL
 
 */
 
include_once 'Registry.class.php';
 
 
###################################################################################
 
## Provide a fake application class to test the registry on
 
###################################################################################
 
class App {
 
    protected $r;
 
    ## This constructor demonstrates the ability to leave the registry instance
 
    ## in a property of the app; this shortens the amount of typing
 
    public function __construct(){
 
        ## INSTANCE or SINGLETON both work; it's whatever you prefer
 
        $this->r = Registry::instance();
 
    }
 
    ## Call a property from a registry object with the magic method
 
    public function who(){
 
        return $this->r->user->name;
 
    }
 
    ## Call a method from an abstract service
 
    public function encrypt(){
 
        $encryptionDriver = Registry::getConcrete('Encryption');
 
        return $encryptionDriver->identify();
 
    }
 
    ## Check a property from a registry object using quick access
 
    public function checkConfig($key){
 
        return Registry::singleton()->getObj('config')->$key;
 
    }
 
}
 
 
###################################################################################
 
## A couple test classes you might want to share with the rest of your app
 
###################################################################################
 
class User {
 
    public $name;
 
    public function __construct($name){
 
        $this->name = $name;
 
    }
 
}
 
class Config {
 
    public $settingOne = true;
 
    public $settingTwo = false;
 
}
 
 
###################################################################################
 
## An encryption interface that can be interchanged
 
###################################################################################
 
interface IEncryption {
 
    function identify();
 
}
 
class AESEncryption implements IEncryption {
 
    public function identify(){
 
        return "Hi, I'm AES encryption";
 
    }
 
}
 
class BlowfishEncryption implements IEncryption {
 
    public function identify(){
 
        return "Hi, I'm Blowfish encryption";
 
    }
 
}
 
 
###################################################################################
 
## Now grab registry and add classes
 
###################################################################################
 
$registry = Registry::singleton();
 
 
## You can turn the overwrite on and off (default: off)
 
$registry->setOverwrite(false);
 
 
## Magic method
 
$registry->user = new User('Tally');
 
## ArrayAccess method
 
$registry['config'] = new Config();
 
 
###################################################################################
 
## Service locator-specific functions
 
###################################################################################
 
## Bind an encryption interface to the abstract Encryption
 
Registry::bind('Encryption', 'BlowfishEncryption');
 
## Check if the interface is bound
 
echo "Encryption interface bound: ";
 
var_dump(Registry::bound('Encryption'));
 
echo "<br /><br />\n";
 
## Get a concrete object for encryption
 
$database = Registry::getConcrete('Encryption');
 
 
###################################################################################
 
## Call up the app and check if all these work
 
###################################################################################
 
$app = new App();
 
echo "Hi, my username is <b>{$app->who()}</b><br />
 
<br />
 
This app is using an encryption engine; It says:<br />
 
<b>{$app->encrypt()}</b><br />
 
<br />
 
I have a configuration item that is set to:<br />
 
<b>{$app->checkConfig('settingOne')}</b>";
 
 
###################################################################################
 
## Test the iterator interface
 
###################################################################################
 
echo "<pre>";
 
foreach(Registry::singleton() as $a=>$o){
 
    echo "Alias/abstract <b>{$a}</b> is set to object/concrete: <b>";
 
    var_dump($o);
 
    echo "</b>\n";
 
}
 
echo "</pre>";
 
 
## Unbind the encryption interface
 
Registry::unbind('Encryption');
 
## Unset the User class
 
unset($registry->user);
 
 
###################################################################################
 
## Try to clone Registry class
 
###################################################################################
 
$newRegistry = clone($registry);
 
 |