library/log4php/LoggerConfigurable.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Licensed to the Apache Software Foundation (ASF) under one or more
       
     4  * contributor license agreements. See the NOTICE file distributed with
       
     5  * this work for additional information regarding copyright ownership.
       
     6  * The ASF licenses this file to You under the Apache License, Version 2.0
       
     7  * (the "License"); you may not use this file except in compliance with
       
     8  * the License. You may obtain a copy of the License at
       
     9  *
       
    10  *       http://www.apache.org/licenses/LICENSE-2.0
       
    11  *
       
    12  * Unless required by applicable law or agreed to in writing, software
       
    13  * distributed under the License is distributed on an "AS IS" BASIS,
       
    14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    15  * See the License for the specific language governing permissions and
       
    16  * limitations under the License.
       
    17  *
       
    18  * @package log4php
       
    19  */
       
    20 
       
    21 /**
       
    22  * A base class from which all classes which have configurable properties are
       
    23  * extended. Provides a generic setter with integrated validation.
       
    24  *
       
    25  * @package log4php
       
    26  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
       
    27  * @version $Revision $
       
    28  * @since 2.2
       
    29  */
       
    30 abstract class LoggerConfigurable {
       
    31 
       
    32     /** Setter function for boolean type. */
       
    33     protected function setBoolean($property, $value) {
       
    34         try {
       
    35             $this->$property = LoggerOptionConverter::toBooleanEx($value);
       
    36         } catch (Exception $ex) {
       
    37             $value = var_export($value, true);
       
    38             $this->warn("Invalid value given for '$property' property: [$value]. Expected a boolean value. Property not changed.");
       
    39         }
       
    40     }
       
    41 
       
    42     /** Setter function for integer type. */
       
    43     protected function setInteger($property, $value) {
       
    44         try {
       
    45             $this->$property = LoggerOptionConverter::toIntegerEx($value);
       
    46         } catch (Exception $ex) {
       
    47             $value = var_export($value, true);
       
    48             $this->warn("Invalid value given for '$property' property: [$value]. Expected an integer. Property not changed.");
       
    49         }
       
    50     }
       
    51 
       
    52     /** Setter function for LoggerLevel values. */
       
    53     protected function setLevel($property, $value) {
       
    54         try {
       
    55             $this->$property = LoggerOptionConverter::toLevelEx($value);
       
    56         } catch (Exception $ex) {
       
    57             $value = var_export($value, true);
       
    58             $this->warn("Invalid value given for '$property' property: [$value]. Expected a level value. Property not changed.");
       
    59         }
       
    60     }
       
    61 
       
    62     /** Setter function for integer type. */
       
    63     protected function setPositiveInteger($property, $value) {
       
    64         try {
       
    65             $this->$property = LoggerOptionConverter::toPositiveIntegerEx($value);
       
    66         } catch (Exception $ex) {
       
    67             $value = var_export($value, true);
       
    68             $this->warn("Invalid value given for '$property' property: [$value]. Expected a positive integer. Property not changed.");
       
    69         }
       
    70     }
       
    71 
       
    72     /** Setter for file size. */
       
    73     protected function setFileSize($property, $value) {
       
    74         try {
       
    75             $this->$property = LoggerOptionConverter::toFileSizeEx($value);
       
    76         } catch (Exception $ex) {
       
    77             $value = var_export($value, true);
       
    78             $this->warn("Invalid value given for '$property' property: [$value]. Expected a file size value.  Property not changed.");
       
    79         }
       
    80     }
       
    81 
       
    82     /** Setter function for numeric type. */
       
    83     protected function setNumeric($property, $value) {
       
    84         try {
       
    85             $this->$property = LoggerOptionConverter::toNumericEx($value);
       
    86         } catch (Exception $ex) {
       
    87             $value = var_export($value, true);
       
    88             $this->warn("Invalid value given for '$property' property: [$value]. Expected a number. Property not changed.");
       
    89         }
       
    90     }
       
    91 
       
    92     /** Setter function for string type. */
       
    93     protected function setString($property, $value, $nullable = false) {
       
    94         if ($value === null) {
       
    95             if ($nullable) {
       
    96                 $this->$property = null;
       
    97             } else {
       
    98                 $this->warn("Null value given for '$property' property. Expected a string. Property not changed.");
       
    99             }
       
   100         } else {
       
   101             try {
       
   102                 $value = LoggerOptionConverter::toStringEx($value);
       
   103                 $this->$property = LoggerOptionConverter::substConstants($value);
       
   104             } catch (Exception $ex) {
       
   105                 $value = var_export($value, true);
       
   106                 $this->warn("Invalid value given for '$property' property: [$value]. Expected a string. Property not changed.");
       
   107             }
       
   108         }
       
   109     }
       
   110 
       
   111     /** Triggers a warning. */
       
   112     protected function warn($message) {
       
   113         $class = get_class($this);
       
   114         trigger_error("log4php: $class: $message", E_USER_WARNING);
       
   115     }
       
   116 }