Guest User

Untitled

a guest
Nov 5th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.14 KB | None | 0 0
  1. <?php
  2.  
  3. require __DIR__ . '/LoggerException.php';
  4. require __DIR__ . '/helpers/LoggerOptionConverter.php';
  5.  
  6. /** A base class from which all classes which have configurable properties are extended. */
  7. abstract class LoggerBaseClass {
  8.  
  9.     // Various property types
  10.     const PROP_TYPE_BOOLEAN = 'boolean';
  11.     const PROP_TYPE_INTEGER = 'integer';
  12.     const PROP_TYPE_FLOAT = 'float';
  13.     const PROP_TYPE_STRING = 'string';
  14.     const PROP_TYPE_FILESIZE = 'filesize';
  15.     const PROP_TYPE_LEVEL = 'level';
  16.    
  17.     /**
  18.      * The main setter function, used for setting all properties.
  19.      *
  20.      * @return TRUE if the property was successfully set, FALSE if an error occured.
  21.      */
  22.     protected function set($property, $value, $type, $check = null)
  23.     {
  24.         if (!property_exists($this, $property)) {
  25.             $this->warn("Property '$property' does not exist.");
  26.             return;
  27.         }
  28.        
  29.         switch($type) {
  30.             case self::PROP_TYPE_BOOLEAN:
  31.                 return $this->setBoolean($property, $value);
  32.                
  33.             case self::PROP_TYPE_STRING:
  34.                 return $this->setString($property, $value);
  35.                
  36.             default:
  37.                 $this->warn("No setter defined for property type [$type].");
  38.                 return false;
  39.         }
  40.     }
  41.    
  42.     /** Setter function for boolean type. */
  43.     private function setBoolean($property, $value)
  44.     {
  45.         try {
  46.             $this->$property = LoggerOptionConverter::toBooleanEx($value);
  47.         } catch (Exception $ex) {
  48.             $value = var_export($value, true);
  49.             $propValue = var_export($this->$property, true);
  50.             $this->warn("Invalid value given for '$property' property: [$value]. Expected a boolean value. Property not changed, current value: [$propValue].");
  51.         }
  52.     }
  53.    
  54.     /** Setter function for string type. */
  55.     private function setString($property, $value)
  56.     {
  57.         if (is_string($value)) {
  58.             $this->$property = $value;
  59.         } else {
  60.             $value = var_export($value, true);
  61.             $propValue = var_export($this->$property, true);
  62.             $this->warn("Invalid value given for '$property' property: [$value]. Expected a string. Property not changed, current value: [$propValue].");
  63.         }
  64.     }
  65.    
  66.     /*
  67.      * TODO: implement other setters here
  68.      */
  69.    
  70.     /** Triggers a warning. */
  71.     protected function warn($message)
  72.     {
  73.         $class = get_class($this);
  74.         trigger_error("log4php: $class: $message", E_USER_WARNING);
  75.     }
  76. }
  77.  
  78. abstract class LoggerAppender extends LoggerBaseClass { }
  79.  
  80. /** Sample appender class with two properties. */
  81. class LoggerAppenderFile extends LoggerAppender {
  82.  
  83.     const DEFAULT_APPEND = true;
  84.    
  85.     /** A string property. */
  86.     protected $file;
  87.    
  88.     /** A boolean property. */
  89.     protected $append = self::DEFAULT_APPEND;
  90.    
  91.     public function setAppend($value) {
  92.         $this->set('append', $value, self::PROP_TYPE_BOOLEAN);
  93.     }
  94.    
  95.     public function getAppend() {
  96.         return $this->append;
  97.     }
  98.    
  99.     public function setFile($value) {
  100.         $this->set('file', $value, self::PROP_TYPE_STRING);
  101.     }
  102.    
  103.     public function getFile() {
  104.         return $this->file;
  105.     }
  106. }
  107.  
  108. // Testing
  109. $c = new LoggerAppenderFile();
  110.  
  111. // These work ok.
  112. $c->setAppend('false');
  113. $c->setFile('/var/log/log.txt');
  114. var_dump($c->getAppend());
  115. var_dump($c->getFile());
  116.  
  117. // These create a warning
  118. $c->setAppend('bla'); // not convertable to boolean
  119. $c->setFile(13); // not a string
  120.  
  121.  
  122. ?>
  123.  
Advertisement
Add Comment
Please, Sign In to add comment