Completed
Branch dev (a5fbd1)
by Michael
05:49
created

AdminPageFramework_Debug_Log::_getLogHeadingLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 4
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2016 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * A base class of the debug class.
12
 *
13
 * @since           3.8.9
14
 * @extends         AdminPageFramework_Debug_Base
15
 * @package         AdminPageFramework
16
 * @subpackage      Common/Utility
17
 */
18
class AdminPageFramework_Debug_Log extends AdminPageFramework_Debug_Base {
19
20
    /**
21
     * Logs the given variable output to a file.
22
     * 
23
     * @param       mixed       $mValue         The value to log.  
24
     * @param       string      $sFilePath      The log file path.
25
     * @since       3.8.9
26
     * @return      void
27
     **/
28
    static protected function _log( $mValue, $sFilePath=null ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
29
                
30
        static $_fPreviousTimeStamp = 0;
31
        
32
        $_oCallerInfo       = debug_backtrace();
33
        $_sCallerFunction   = self::getElement(
34
            $_oCallerInfo,  // subject array
35
            array( 1, 'function' ), // key
36
            ''      // default
37
        );                        
38
        $_sCallerClass      = self::getElement(
39
            $_oCallerInfo,  // subject array
40
            array( 1, 'class' ), // key
41
            ''      // default
42
        );           
43
        $_fCurrentTimeStamp = microtime( true );
44
        
45
        file_put_contents( 
46
            self::_getLogFilePath( $sFilePath, $_sCallerClass ), 
47
            self::_getLogHeadingLine( 
48
                $_fCurrentTimeStamp,
49
                round( $_fCurrentTimeStamp - $_fPreviousTimeStamp, 3 ),     // elapsed time
50
                $_sCallerClass,
51
                $_sCallerFunction
52
            ) . PHP_EOL
53
            . self::_getLegibleDetails( $mValue ) . PHP_EOL . PHP_EOL,
54
            FILE_APPEND 
55
        );     
56
        
57
        $_fPreviousTimeStamp = $_fCurrentTimeStamp;
58
        
59
    }   
60
        /**
61
         * Determines the log file path.
62
         * @since       3.5.3 
63
         * @internal    
64
         * @return      string      The path of the file to log the contents.
65
         */
66
        static private function _getLogFilePath( $bsFilePath, $sCallerClass ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
67
        
68
            $_bFileExists = self::_createFile( $bsFilePath );
69
            if ( $_bFileExists ) {
70
                return $bsFilePath;
71
            }
72
            // Return a generated default log path.
73
            if ( true === $bsFilePath ) {
74
                return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . basename( get_class() ) . '_' . date( "Ymd" ) . '.log';
75
            }
76
            return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . basename( get_class() ) . '_' . basename( $sCallerClass ) . '_' . date( "Ymd" ) . '.log';
77
            
78
        }
79
            /**
80
             * Creates a file.
81
             * @return      boolean
82
             * @internal
83
             */
84
            static private function _createFile( $sFilePath ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
85
                if ( ! $sFilePath || true === $sFilePath ) {
86
                    return false;
87
                }
88
                if ( file_exists( $sFilePath ) ) {
89
                    return true;
90
                }
91
                // Otherwise, create a file.
92
                $_bhResrouce = fopen( $sFilePath, 'w' );
93
                return ( boolean ) $_bhResrouce;                
94
            }
95
96
        /**
97
         * Returns the heading part of a log item.
98
         * @since       3.5.3
99
         * @internal
100
         * @return      string      the heading part of a log item.
101
         */
102
        static private function _getLogHeadingLine( $fCurrentTimeStamp, $nElapsed, $sCallerClass, $sCallerFunction ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
103
            
104
            $_nGMTOffset        = self::_getSiteGMTOffset();
105
            $_iPageLoadID       = self::_getPageLoadID() ;
106
            $_nNow              = $fCurrentTimeStamp + ( $_nGMTOffset * 60 * 60 );
107
            $_nMicroseconds     = str_pad( round( ( $_nNow - floor( $_nNow ) ) * 10000 ), 4, '0' );
108
            
109
            $_aOutput           = array(
110
                date( "Y/m/d H:i:s", $_nNow ) . '.' . $_nMicroseconds,
111
                self::_getFormattedElapsedTime( $nElapsed ),
112
                $_iPageLoadID,
113
                AdminPageFramework_Registry::getVersion(),
114
                $sCallerClass . '::' . $sCallerFunction,
115
                current_filter(),
116
                self::getCurrentURL(),
117
            );
118
            return implode( ' ', $_aOutput );         
119
            
120
        }
121
    
122
            /**
123
             * Returns the GMT offset of the site.
124
             * 
125
             * @return      numeric
126
             */
127
            static private function _getSiteGMTOffset() {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
128
                static $_nGMTOffset;
129
                $_nGMTOffset        = isset( $_nGMTOffset ) 
130
                    ? $_nGMTOffset 
131
                    : get_option( 'gmt_offset' );          
132
                return $_nGMTOffset;
133
            }
134
            
135
            /**
136
             * @return      integer
137
             */
138
            static private function _getPageLoadID() {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
139
                static $_iPageLoadID;
140
                $_iPageLoadID       = $_iPageLoadID 
141
                    ? $_iPageLoadID 
142
                    : uniqid();                
143
                return $_iPageLoadID;
144
            }
145
        
146
            /**
147
             * Returns formatted elapsed time.
148
             * @since       3.5.3
149
             * @internal
150
             * @return      string      Formatted elapsed time.
151
             */
152
            static private function _getFormattedElapsedTime( $nElapsed ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
153
                
154
                $_aElapsedParts     = explode( ".", ( string ) $nElapsed );
155
                $_sElapsedFloat     = str_pad(
156
                    self::getElement(
157
                        $_aElapsedParts, // subject array
158
                        1, // key
159
                        0  // default
160
                    ),      
161
                    3, 
162
                    '0'
163
                );
164
                $_sElapsed          = self::getElement(
165
                    $_aElapsedParts,  // subject array
166
                    0,  // key
167
                    0   // default
168
                );                                   
169
                $_sElapsed          = strlen( $_sElapsed ) > 1 
170
                    ? '+' . substr( $_sElapsed, -1, 2 ) 
171
                    : ' ' . $_sElapsed;
172
                return $_sElapsed . '.' . $_sElapsedFloat;
173
            
174
            }
175
    
176
}
177