Source for file ConfigLoader.php
Documentation is available at ConfigLoader.php
* "THE BEER-WARE LICENSE" (Revision 42):
* "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
* As long as you retain this notice you can do whatever you want with
* this stuff. If we meet some day, and you think this stuff is worth it,
* you can buy me a beer in return.
* @copyright Copyright (c) 2009 Sven Strittmatter
* This class loads configs from a given directory recursivly.
* By default this directory is APPLICATION_PATH . DIRECTORY_SEPARATOR . 'configs'.
* But you can change the directory:
* MediaSearch_ConfigLoader::setConfigDir('my/path/');
* The config loader generates on construction time a index by walking recursiv
* through the directory. The whole filepath without the file extension is used
* as the namespaced config name.
* This config dir structure...
* The file extension is skipped, because the config object factory determines
* the right implementation of MediaSearch_Config_Interface by the file
* extension. Beware name conflicts like file bla.txt and bla.sql in the same
* @todo Implement possibility to load multible configs by 'foo.bar.*'
* @todo Implement MediaSearch_Config_Xml
* @copyright Copyright (c) 2009 Sven Strittmatter
* Directory where the configs live.
private static $configDir;
* Instance for singleton implemenation.
* @var MediaSearch_ConfigLoader
private static $instance;
* Array with the config namespace as key and the full file path
* Array with objects of type MediaSearch_Config_Abstract
* Private for singleton implementation.
* Sets self::$configDir to APPLICATION_PATH . DIRECTORY_SEPARATOR . 'configs'
* if its nulll on construction time. And generates the config file list
private function __construct() {
if (null === self::$configDir) {
self::$configDir = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'configs';
$this->configFiles = array();
$this->configs = array();
$this->generateFileList(new RecursiveDirectoryIterator(self::$configDir));
* Main entry point to get a config object according to its name, if find.
* Additionaly you can pass parse options.
* These are objects implementing the interface MediaSearch_Config_ParseOptions_Interface.
* This method does lazy loading.
* Implements the singleton get instance method.
* @throws InvalidArgumentException If config file is not readable.
* @param array $parseOptions Array of MediaSearch_Config_ParseOptions_Interface
* @return MediaSearch_Config_Abstract
public static function get($name, array $parseOptions = null) {
if (null === self::$instance) {
self::$instance = new MediaSearch_ConfigLoader();
return self::$instance->getConfig($name, $parseOptions);
* Sets the config directory. Also resets the instance so that
* the directory index will be regenerated on next get access.
* @param string $configDir
self::$configDir = $configDir;
* Gets a config bey its name. Additionaly you can pass parse options.
* These are objects implementing the interface MediaSearch_Config_ParseOptions_Interface.
* This method does lazy loading. It loads the config on first time use
* @throws InvalidArgumentException If config file is not readable.
* @param array $parseOptions Array of MediaSearch_Config_ParseOptions_Interface
* @return MediaSearch_Config_Abstract
private function getConfig($name, array $parseOptions = null) {
if (!isset ($this->configs[$name])) {
$this->loadConfig($name, $parseOptions);
return $this->configs[$name];
* Tryes to load a config name by its namespaced name.
* @throws InvalidArgumentException If config file is not readable.
* @param array $parseOptions
private function loadConfig($name, array $parseOptions = null) {
if (!isset ($this->configFiles[$name])) {
$message = "Doesnt have a config for $name!";
throw new InvalidArgumentException($message);
* Generates the config files index recursivly starting in self::$configDir.
* Each directory represents a namespace. A file under bla/blub/foo.php is
* loadable by the namespace 'bla.blub.foo'.
* @param RecursiveDirectoryIterator $directoryIterator
* @param string $namespace
private function generateFileList(RecursiveDirectoryIterator $directoryIterator, $namespace = null) {
foreach ($directoryIterator as $entry) {
if ($directoryIterator->isDot()) {
$fileName = $entry->getFilename();
if (0 === strpos($fileName, '.')) {
if (null !== $namespace) {
$nameSpacedKey = $namespace. '.';
if ($directoryIterator->hasChildren()) {
$nameSpacedKey .= $fileName;
$this->generateFileList($directoryIterator->getChildren(), $nameSpacedKey);
$this->configFiles[$nameSpacedKey] = $entry->getPathname();;
|