I think I'm comming down with pattern fever 🙁 :p
Do you guys think this is prehapse going a little far? I can see it being usefull for a large site with the potential for loads of configuration options but mine is only going to be pretty small. Oh well, I think I'm going to use it anyway just for the fun of it.
I've got a few ideas for extra functionality which I was hoping for some views on. A print method so you can display the current configuration options, a get iterator method for traversing the tree and a remove method.
Attached is the code for the coposite pattern classes. RepositoryComponent, Repository and RepositoryItem.
Below is an example of use:
<?php
include 'classes/autoloader.php';
//Here's the setup, this would be in a seperate configuration file included at the top
$repository = new Repository('Root', 'The main repository for all my singletons');
$databases = new Repository('Databases', 'A sub-repository to hold my database instances');
$mysql = new RepositoryItem('MySQLDatabase', 'Here\'s my MySQL connection instance');
$databases->add($mysql);
$repository->add($databases);
//Here's the usage
try {
$db=$repository->getChild('Databases')->getChild('MySQLDatabase')->getInstance();
$db->sql("SELECT * FROM test");
foreach($db as $result) {
echo("{$result['unique_id']}:{$result['thetime']}:{$result['astring']}\n");
}
} catch(Exception $e) {
print_r($e);
}
?>