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

Source for file Abstract.php

Documentation is available at Abstract.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_Config
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * @see MediaSearch_Config_Interface
  23.  */
  24. require_once('MediaSearch/Config/Interface.php');
  25.  
  26. /**
  27.  * @see MediaSearch_File_Util
  28.  */
  29. require_once('MediaSearch/File/Util.php');
  30.  
  31. /**
  32.  * This is a default implementaion of a config file. It provides general
  33.  * purpose methods for the concrete config file objects.
  34.  *
  35.  * @abstract
  36.  * @category   MediaSearch
  37.  * @package    MediaSearch_Config
  38.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  39.  */
  40. abstract class MediaSearch_Config_Abstract implements MediaSearch_Config_Interface {
  41.     const TRACE_LELVEL =  9;
  42.     /**
  43.      * The full filepath to the config file which will be loaded and parsed.
  44.      *
  45.      * @access private
  46.      * @var string 
  47.      */
  48.     private $fileName;
  49.     /**
  50.      * The original loaded string from the config file.
  51.      *
  52.      * @access protected
  53.      * @var string 
  54.      */
  55.     protected $rawData;
  56.     /**
  57.      * Contains the parsed config data after parsing.
  58.      *
  59.      * @access protected
  60.      * @var array 
  61.      */
  62.     protected $parsedData;
  63.     /**
  64.      * Contains optiona parse option objects of type MediaSearch_Config_ParseOptions_Interface.
  65.      *
  66.      * @access private
  67.      * @var array 
  68.      */
  69.     private $parseOptions;
  70.  
  71.     /**
  72.      * The constructor requires the full filename to the config file.
  73.      * Optional you can add an array with some parse option objects
  74.      * (@see MediaSearch_Config_ParseOptions_Interface).
  75.      *
  76.      * The constructor will call load() to initiaize the config.
  77.      *
  78.      * @access public
  79.      * @param string $fileName 
  80.      * @param array $parseOptions Optional, default is null.
  81.      */
  82.     public function __construct($fileNamearray $parseOptions null{
  83.         $this->setFileName($fileName);
  84.         $this->removeParseOptions();
  85.         $this->addParseOptions($parseOptions);
  86.         $this->parseData();
  87.     }
  88.  
  89.     /**
  90.      * @see MediaSearch_Config_Interface
  91.      */
  92.     public function addParseOption(MediaSearch_Config_ParseOptions_Interface $option{
  93.         $this->parseOptions[$option;
  94.     }
  95.     /**
  96.      * Adds multiple parse options in an array.
  97.      *
  98.      * @access public
  99.      * @param array $options Array of MediaSearch_Config_ParseOptions_Interface
  100.      */
  101.     public function addParseOptions(array $options{
  102.         if (count($options=== 0{
  103.             return;
  104.         }
  105.         
  106.         foreach ($options as $option{
  107.             $this->addParseOption($option);
  108.         }
  109.     }
  110.  
  111.     /**
  112.      * @see MediaSearch_Config_Interface
  113.      */
  114.     public function removeParseOptions({
  115.         $this->parseOptions array();
  116.     }
  117.  
  118.     /**
  119.      * @see MediaSearch_Config_Interface
  120.      */
  121.     public function hasParseOptions({
  122.         return count($this->parseOptions0;
  123.     }
  124.  
  125.     /**
  126.      * @see MediaSearch_Config_Interface
  127.      */
  128.     public function setFileName($fileName{
  129.         $this->fileName $fileName;
  130.     }
  131.  
  132.     /**
  133.      * @see MediaSearch_Config_Interface
  134.      */
  135.     public function getFileName({
  136.         $this->fileName;
  137.     }
  138.  
  139.     /**
  140.      * @see MediaSearch_Config_Interface
  141.      *
  142.      *  Implements lazy loading and loads the
  143.      *  raw data on first use.
  144.      *
  145.      * @access public
  146.      */
  147.     public function getRawData({
  148.         if (null === $this->rawData{
  149.             $this->loadRawData();
  150.         }
  151.  
  152.         return $this->rawData;
  153.     }
  154.  
  155.     /**
  156.      * Loads the data by file_get_contents() from file.
  157.      *
  158.      * @access protected
  159.      */
  160.     protected function loadRawData({
  161.         $this->rawData file_get_contents($this->fileName);
  162.     }
  163.  
  164.     /**
  165.      * @see MediaSearch_Config_Interface
  166.      *
  167.      *  Returns nulll if the given $name is not found in the config.
  168.      * 
  169.      * @access public
  170.      * @param string $name Optional, returns the whole parsed data if not set.
  171.      * @return mixed 
  172.      */
  173.     public function get($name null{
  174.         if (null === $name{
  175.             return $this->parsedData;
  176.         }
  177.  
  178.         if (isset($this->parsedData[$name])) {
  179.             return $this->parsedData[$name];
  180.         }
  181.  
  182.         return null;
  183.     }
  184.  
  185.     /**
  186.      * Factory method which creates a proper config file implementation
  187.      * depending on its type. The type is determined by the filename extension
  188.      * (@see MediaSearch_File_Util::getExtension()).
  189.      *
  190.      * @static
  191.      * @access public
  192.      * @throws InvalidArgumentException If unsoported file type is fiven.
  193.      * @param string $fileName The full file path to the config file.
  194.      * @param array $parseOptions Array of optional parse option obejcts.
  195.      * @return MediaSearch_Config_Abstract 
  196.      */
  197.     public static function createConfig($fileNamearray $parseOptions null{
  198.         switch(MediaSearch_File_Util::getExtension($fileName)) {
  199.             case self::TYPE_TEXT:
  200.                 return new MediaSearch_Config_Text($fileName$parseOptions);
  201.             case self::TYPE_JSON:
  202.                 $rawData file_get_contents($fileName$parseOptions);
  203.                 return new MediaSearch_Config_Json($fileName$parseOptions);
  204.             case self::TYPE_PHP:
  205.                 return new MediaSearch_Config_Php($fileName$parseOptions);
  206.             case self::TYPE_SQL:
  207.                 return new MediaSearch_Config_Sql($fileName$parseOptions);
  208.             default:
  209.                 $message "Cant discover config file type for $fileName";
  210.                 throw new InvalidArgumentException($message);
  211.         }
  212.     }
  213.  
  214.     /**
  215.      * The default parse behaviour is copying the loaded file string with no
  216.      * modification to parsed data.
  217.      *
  218.      * This behaviour could be modified by overwriting in the concrete
  219.      * implementations.
  220.      *
  221.      * Attend that after that applayParseOptions() is called.
  222.      * 
  223.      * @access public
  224.      */
  225.     public function parseData({
  226.         $this->parsedData $this->getRawData();
  227.         $this->applayParseOptions();
  228.     }
  229.  
  230.     /**
  231.      * Apply the added parse options to the data parsed by  parseData().
  232.      *
  233.      * @access protected
  234.      */
  235.     protected function applayParseOptions({
  236.         if (!$this->hasParseOptions()) {
  237.             return;
  238.         }
  239.  
  240.         foreach ($this->parseOptions as $option{
  241.             $this->parsedData $option->process($this->parsedData);
  242.         }
  243.     }
  244. }

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