Overview

Packages

  • log4php
    • appenders
    • configurators
    • filters
    • helpers
    • layouts
    • pattern
    • renderers

Classes

  • LoggerFormattingInfo
  • LoggerOptionConverter
  • LoggerPatternConverter
  • LoggerPatternParser
  • LoggerUtils
  • Overview
  • Package
  • Class
  • Tree
  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:  * Contains various helper methods.
 23:  * 
 24:  * @package log4php
 25:  * @subpackage helpers
 26:  * @since 2.3
 27:  */
 28: class LoggerUtils {
 29:     
 30:     /**
 31:      * Splits a fully qualified class name into fragments delimited by the 
 32:      * namespace separator (\). 
 33:      * 
 34:      * For backward compatibility, a dot (.) can be used as a delimiter as
 35:      * well. 
 36:      * 
 37:      * @param string $name
 38:      * 
 39:      * @return array Class name split into fragments.
 40:      */
 41:     public static function tokenizeClassName($name) {
 42:         $name = str_replace('.', '\\', $name);
 43:         $name = trim($name, ' \\');
 44:         $fragments = explode('\\', $name);
 45:         
 46:         foreach($fragments as $key => $fragment) {
 47:             if (trim($fragment) === '') {
 48:                 unset($fragments[$key]);
 49:             }
 50:         }
 51:         
 52:         return $fragments;
 53:     }
 54:     
 55:     /**
 56:      * Attempts to shorten the given class name to the desired length.
 57:      * 
 58:      * This is done by separating the class name into fragments (delimited
 59:      * by \ or .) and trimming individual fragments, starting with the left,
 60:      * until desired length has been reached. 
 61:      * 
 62:      * The final fragment (i.e. class name) will never be shortened so the 
 63:      * result may still be longer than given length.
 64:      * 
 65:      * @param string $name The (qualified) class name.  
 66:      * @param integer $length The length to shorten to. If null or 0 is given,
 67:      * the name will be returned without shortening. 
 68:      */
 69:     public static function shortenClassName($name, $length) {
 70:         if ($length === null || $length < 0) {
 71:             return $name;
 72:         }
 73:         
 74:         $name = str_replace('.', '\\', $name);
 75:         $name = trim($name, ' \\');
 76:         
 77:         // Check if any shortening is required
 78:         $currentLength = strlen($name);
 79:         if ($currentLength <= $length) {
 80:             return $name;
 81:         }
 82:     
 83:         // Split name into fragments
 84:         $fragments = explode('\\', $name);
 85: 
 86:         // If zero length is specified, return only last fragment
 87:         if ($length == 0) {
 88:             return array_pop($fragments);
 89:         }
 90:         
 91:         // If the name splits to only one fragment, then it cannot be shortened
 92:         $count = count($fragments);
 93:         if ($count == 1) {
 94:             return $name;
 95:         }
 96:     
 97:         foreach($fragments as $key => &$fragment) {
 98:     
 99:             // Never shorten last fragment
100:             if ($key == $count - 1) {
101:                 break;
102:             }
103:     
104:             // Check for empty fragments (shouldn't happen but it's possible)
105:             $fragLen = strlen($fragment);
106:             if ($fragLen <= 1) {
107:                 continue;
108:             }
109:     
110:             // Shorten fragment to one character and check if total length satisfactory
111:             $fragment = substr($fragment, 0, 1);
112:             $currentLength = $currentLength - $fragLen + 1;
113:     
114:             if ($currentLength <= $length) {
115:                 break;
116:             }
117:         }
118:         unset($fragment);
119:     
120:         return implode('\\', $fragments);
121:     }
122: }
123: 
124: 
Apache log4php API documentation generated by ApiGen 2.8.0