MediaSearch
[ class tree: MediaSearch ] [ index: MediaSearch ] [ all elements ]

Source for file ConfigLoader.php

Documentation is available at ConfigLoader.php

  1. <?php
  2. /**
  3.  * MediaSearch
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author$
  14.  * $Revision$
  15.  *
  16.  * @category    MediaSearch
  17.  * @package     MediaSearch
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * This class loads configs from a given directory recursivly.
  23.  *
  24.  * By default this directory is APPLICATION_PATH . DIRECTORY_SEPARATOR . 'configs'.
  25.  * But you can change the directory:
  26.  *
  27.  * <code>
  28.  * MediaSearch_ConfigLoader::setConfigDir('my/path/');
  29.  * </code>
  30.  *
  31.  * The config loader generates on construction time a index by walking recursiv
  32.  * through the directory. The whole filepath without the file extension is used
  33.  * as the namespaced config name.
  34.  *
  35.  * Example:
  36.  * This config dir structure...
  37.  * <pre>
  38.  * CONFIGDIR
  39.  *  +-foo/
  40.  *  |   bar/
  41.  *  |     +blub.php
  42.  *  |     +bla.php
  43.  *  +   baz/
  44.  *        +blub.txt
  45.  *        +bla.sql
  46.  * </pre>
  47.  *
  48.  * ... will be index as:
  49.  * - foo.bar.blub
  50.  * - foo.bar.bla
  51.  * - foo.baz.blub
  52.  * - foo.baz.bla
  53.  *
  54.  * The file extension is skipped, because the config object factory determines
  55.  * the right implementation of MediaSearch_Config_Interface by the file
  56.  * extension. Beware name conflicts like file bla.txt and bla.sql in the same
  57.  * directory!
  58.  *
  59.  * @todo Implement possibility to load multible configs by 'foo.bar.*'
  60.  * @todo Implement MediaSearch_Config_Xml
  61.  *
  62.  * @final
  63.  * @category   MediaSearch
  64.  * @package    MediaSearch
  65.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  66.  */
  67. final class MediaSearch_ConfigLoader {
  68.     const TRACE_LELVEL 55;
  69.  
  70.     /**
  71.      * Directory where the configs live.
  72.      *
  73.      * @static
  74.      * @access private
  75.      * @var string 
  76.      */
  77.     private static $configDir;
  78.     /**
  79.      * Instance for singleton implemenation.
  80.      *
  81.      * @static
  82.      * @access private
  83.      * @var MediaSearch_ConfigLoader 
  84.      */
  85.     private static $instance;
  86.     /**
  87.      * Array with the config namespace as key and the full file path
  88.      * as value.
  89.      *
  90.      * @access private
  91.      * @var array 
  92.      */
  93.     private $configFiles;
  94.     /**
  95.      * Loaded config objects.
  96.      * Array with objects of type MediaSearch_Config_Abstract
  97.      * 
  98.      * @access private
  99.      * @var array 
  100.      */
  101.     private $configs;
  102.  
  103.     /**
  104.      * Private for singleton implementation.
  105.      * Sets self::$configDir to APPLICATION_PATH . DIRECTORY_SEPARATOR . 'configs'
  106.      * if its nulll on construction time. And generates the config file list
  107.      * in $this->configs.
  108.      *
  109.      * @access private
  110.      */
  111.     private function __construct({
  112.         if (null === self::$configDir{
  113.             self::$configDir APPLICATION_PATH DIRECTORY_SEPARATOR 'configs';
  114.         }
  115.  
  116.         $this->configFiles  array();
  117.         $this->configs        array();
  118.         $this->generateFileList(new RecursiveDirectoryIterator(self::$configDir));
  119.     }
  120.  
  121.     /**
  122.      * Main entry point to get a config object according to its name, if find.
  123.      *
  124.      * Additionaly you can pass parse options.
  125.      * These are objects implementing the interface MediaSearch_Config_ParseOptions_Interface.
  126.      * This method does lazy loading.
  127.      *
  128.      * Implements the singleton get instance method.
  129.      *
  130.      * @static
  131.      * @access public
  132.      * @throws InvalidArgumentException If config file is not readable.
  133.      * @param atring $name 
  134.      * @param array $parseOptions Array of MediaSearch_Config_ParseOptions_Interface
  135.      * @return MediaSearch_Config_Abstract 
  136.      */
  137.     public static function get($namearray $parseOptions null{
  138.         if (null === self::$instance{
  139.             self::$instance new MediaSearch_ConfigLoader();
  140.         }
  141.  
  142.         return self::$instance->getConfig($name$parseOptions);
  143.     }
  144.  
  145.     /**
  146.      * Sets the config directory. Also resets the instance so that
  147.      * the directory index will be regenerated on next get access.
  148.      *
  149.      * @static
  150.      * @access public
  151.      * @param string $configDir 
  152.      */
  153.     public static function setConfigDir($configDir{
  154.         self::$configDir $configDir;
  155.         self::$instance  null;
  156.     }
  157.     /**
  158.      * Gets a config bey its name. Additionaly you can pass parse options.
  159.      * These are objects implementing the interface MediaSearch_Config_ParseOptions_Interface.
  160.      * This method does lazy loading. It loads the config on first time use
  161.      * and stores it static.
  162.      *
  163.      * @access private
  164.      * @throws InvalidArgumentException If config file is not readable.
  165.      * @param atring $name 
  166.      * @param array $parseOptions Array of MediaSearch_Config_ParseOptions_Interface
  167.      * @return MediaSearch_Config_Abstract 
  168.      */
  169.     private function getConfig($namearray $parseOptions null{
  170.         if (!isset($this->configs[$name])) {
  171.             $this->loadConfig($name$parseOptions);
  172.         }
  173.  
  174.         return $this->configs[$name];
  175.     }
  176.  
  177.     /**
  178.      * Tryes to load a config name by its namespaced name.
  179.      *
  180.      * @access private
  181.      * @throws InvalidArgumentException If config file is not readable.
  182.      * @param string $name 
  183.      * @param array $parseOptions 
  184.      */
  185.     private function loadConfig($namearray $parseOptions null{
  186.         if (!isset($this->configFiles[$name])) {
  187.             $message "Doesnt have a config for $name!";
  188.             throw new InvalidArgumentException($message);
  189.         }
  190.  
  191.         $this->configs[$nameMediaSearch_Config_Abstract::createConfig($this->configFiles[$name]$parseOptions);
  192.     }
  193.  
  194.     /**
  195.      * Generates the config files index recursivly starting in self::$configDir.
  196.      * Each directory represents a namespace. A file under bla/blub/foo.php is
  197.      * loadable by the namespace 'bla.blub.foo'.
  198.      *
  199.      * @access private
  200.      * @param RecursiveDirectoryIterator $directoryIterator 
  201.      * @param string $namespace 
  202.      */
  203.     private function generateFileList(RecursiveDirectoryIterator $directoryIterator$namespace null{
  204.         foreach ($directoryIterator as $entry{
  205.             if ($directoryIterator->isDot()) {
  206.                 continue;
  207.             }
  208.  
  209.             $fileName $entry->getFilename();
  210.  
  211.             if (=== strpos($fileName'.')) {
  212.                 continue;
  213.             }
  214.  
  215.             $nameSpacedKey '';
  216.  
  217.             if (null !== $namespace{
  218.                 $nameSpacedKey $namespace.'.';
  219.             }
  220.  
  221.             if ($directoryIterator->hasChildren()) {
  222.                 $nameSpacedKey .= $fileName;
  223.                 $this->generateFileList($directoryIterator->getChildren()$nameSpacedKey);
  224.             else {
  225.                 $nameSpacedKey .= str_replace('.'''MediaSearch_File_Util::stripExtension($fileName));
  226.                 $this->configFiles[$nameSpacedKey$entry->getPathname();;
  227.             }
  228.         }
  229.     }
  230. }

Documentation generated on Mon, 17 Aug 2009 14:51:56 +0200 by phpDocumentor 1.4.2