library/log4php/pattern/LoggerPatternConverterSuperglobal.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     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  * Returns a value from a superglobal array corresponding to the
       
    23  * given key.
       
    24  *
       
    25  * Option: the key to look up within the superglobal array
       
    26  *
       
    27  * Also, it is possible that a superglobal variable is not populated by PHP
       
    28  * because of the settings in the variables-order ini directive. In this case
       
    29  * the converter will return an empty value.
       
    30  *
       
    31  * @see http://php.net/manual/en/language.variables.superglobals.php
       
    32  * @see http://www.php.net/manual/en/ini.core.php#ini.variables-order
       
    33  *
       
    34  * @package log4php
       
    35  * @subpackage pattern
       
    36  * @version $Revision: 1326626 $
       
    37  * @since 2.3
       
    38  */
       
    39 abstract class LoggerPatternConverterSuperglobal extends LoggerPatternConverter {
       
    40 
       
    41     /**
       
    42      * Name of the superglobal variable, to be defined by subclasses.
       
    43      * For example: "_SERVER" or "_ENV".
       
    44      */
       
    45     protected $name;
       
    46 
       
    47     protected $value = '';
       
    48 
       
    49     public function activateOptions() {
       
    50         // Read the key from options array
       
    51         if (isset($this->option) && $this->option !== '') {
       
    52             $key = $this->option;
       
    53         }
       
    54 
       
    55         /*
       
    56          * There is a bug in PHP which doesn't allow superglobals to be
       
    57          * accessed when their name is stored in a variable, e.g.:
       
    58          *
       
    59          * $name = '_SERVER';
       
    60          * $array = $$name;
       
    61          *
       
    62          * This code does not work when run from within a method (only when run
       
    63          * in global scope). But the following code does work:
       
    64          *
       
    65          * $name = '_SERVER';
       
    66          * global $$name;
       
    67          * $array = $$name;
       
    68          *
       
    69          * That's why global is used here.
       
    70          */
       
    71         global ${$this->name};
       
    72 
       
    73         // Check the given superglobal exists. It is possible that it is not initialized.
       
    74         if (!isset(${$this->name})) {
       
    75             $class = get_class($this);
       
    76             trigger_error("log4php: $class: Cannot find superglobal variable \${$this->name}.", E_USER_WARNING);
       
    77             return;
       
    78         }
       
    79 
       
    80         $source = ${$this->name};
       
    81 
       
    82         // When the key is set, display the matching value
       
    83         if (isset($key)) {
       
    84             if (isset($source[$key])) {
       
    85                 $this->value = $source[$key];
       
    86             }
       
    87         } // When the key is not set, display all values
       
    88         else {
       
    89             $values = array();
       
    90             foreach ($source as $key => $value) {
       
    91                 $values[] = "$key=$value";
       
    92             }
       
    93             $this->value = implode(', ', $values);
       
    94         }
       
    95     }
       
    96 
       
    97     public function convert(LoggerLoggingEvent $event) {
       
    98         return $this->value;
       
    99     }
       
   100 }