_backtrace_formatArgsHelper()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.0555
c 0
b 0
f 0
cc 9
nc 8
nop 1
1
<?php
2
3
/**
4
 * Nexcess.net Turpentine Extension for Magento
5
 * Copyright (C) 2012  Nexcess.net L.L.C.
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 */
21
22
class Nexcessnet_Turpentine_Helper_Debug extends Mage_Core_Helper_Abstract {
23
    /**
24
     * Logs errors
25
     *
26
     * @param $message
27
     * @return string
28
     */
29 View Code Duplication
    public function logError($message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        if (func_num_args() > 1) {
32
            $message = $this->_prepareLogMessage(func_get_args());
33
        }
34
35
        return $this->_log(Zend_Log::ERR, $message);
36
    }
37
38
    /**
39
     * Logs warnings
40
     *
41
     * @param $message
42
     * @return string
43
     */
44 View Code Duplication
    public function logWarn($message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        if (func_num_args() > 1) {
47
            $message = $this->_prepareLogMessage(func_get_args());
48
        }
49
50
        return $this->_log(Zend_Log::WARN, $message);
51
    }
52
53
    /**
54
     * Logs notices
55
     *
56
     * @param $message
57
     * @return string
58
     */
59 View Code Duplication
    public function logNotice($message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        if (func_num_args() > 1) {
62
            $message = $this->_prepareLogMessage(func_get_args());
63
        }
64
65
        return $this->_log(Zend_Log::NOTICE, $message);
66
    }
67
68
    /**
69
     * Logs info.
70
     *
71
     * @param $message
72
     * @return string
73
     */
74 View Code Duplication
    public function logInfo($message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        if (func_num_args() > 1) {
77
            $message = $this->_prepareLogMessage(func_get_args());
78
        }
79
80
        return $this->_log(Zend_Log::INFO, $message);
81
    }
82
83
    /**
84
     * Logs debug.
85
     *
86
     * @param $message
87
     * @return string
88
     */
89
    public function logDebug($message)
90
    {
91
        if ( ! Mage::helper('turpentine/varnish')->getVarnishDebugEnabled()) {
92
            return;
93
        }
94
95
        if (func_num_args() > 1) {
96
            $message = $this->_prepareLogMessage(func_get_args());
97
        }
98
99
        return $this->_log(Zend_Log::DEBUG, $message);
100
    }
101
102
    /**
103
     * Prepares advanced log message.
104
     *
105
     * @param array $args
106
     * @return string
107
     */
108
    protected function _prepareLogMessage(array $args)
109
    {
110
        $pattern = $args[0];
111
        $substitutes = array_slice($args, 1);
112
113
        if ( ! $this->_validatePattern($pattern, $substitutes)) {
114
            return $pattern;
115
        }
116
117
        return vsprintf($pattern, $substitutes);
118
    }
119
120
    /**
121
     * Validates string and attributes for substitution as per sprintf function.
122
     *
123
     * NOTE: this could be implemented as shown at
124
     * http://stackoverflow.com/questions/2053664/how-to-check-that-vsprintf-has-the-correct-number-of-arguments-before-running
125
     * although IMHO it's too time consuming to validate the patterns.
126
     *
127
     * @param string $pattern
128
     * @param array $arguments
129
     * @return bool
130
     */
131
    protected function _validatePattern($pattern, $arguments)
132
    {
133
        return true;
134
    }
135
136
    /**
137
     * Dump a variable to output with <pre/> tags and disable cache flag
138
     *
139
     * @param mixed $value
140
     */
141
    public function dump($value) {
142
        Mage::register('turpentine_nocache_flag', true, true);
143
        $this->logValue($value);
144
        echo '<pre>'.PHP_EOL;
145
        var_dump($value);
146
        echo '</pre>'.PHP_EOL;
147
    }
148
149
    /**
150
     * Log message through Magento's logging facility, works like sprintf
151
     *
152
     * @param  string $message
153
     * @param  mixed  ...
154
     * @return null
155
     */
156
    public function log($message) {
157
        $args = func_get_args();
158
        return call_user_func_array(array($this, 'logDebug'), $args);
159
    }
160
161
    /**
162
     * Log a backtrace, can pass a already generated backtrace to use
163
     *
164
     * @param  array $backTrace
165
     * @return null
166
     */
167
    public function logBackTrace($backTrace = null) {
168
        if (is_null($backTrace)) {
169
            $backTrace = debug_backtrace();
170
            array_shift($backTrace);
171
        }
172
        $btuuid = Mage::helper('turpentine/data')->generateUuid();
173
        $this->log('TRACEBACK: START ** %s **', $btuuid);
174
        $this->log('TRACEBACK: URL: %s', $_SERVER['REQUEST_URI']);
175
        for ($i = 0; $i < count($backTrace); $i++) {
176
            $line = $backTrace[$i];
177
            $this->log('TRACEBACK: #%02d: %s:%d',
178
                $i, $line['file'], $line['line']);
179
            $this->log('TRACEBACK: ==> %s%s%s(%s)',
180
                (is_object(@$line['object']) ?
181
                    get_class($line['object']) : @$line['class']),
182
                @$line['type'],
183
                $line['function'],
184
                $this->_backtrace_formatArgs($line['args']));
185
        }
186
        $this->log('TRACEBACK: END ** %s **', $btuuid);
187
    }
188
189
    /**
190
     * Like var_dump to the log
191
     *
192
     * @param  mixed $value
193
     * @return null
194
     */
195
    public function logValue($value, $name = null) {
196
        if (is_null($name)) {
197
            $name = 'VALUE';
198
        }
199
        $this->log('%s => %s', $name,
200
            $this->_backtrace_formatArgsHelper($value));
201
    }
202
203
    /**
204
     * Log a message through Magento's logging facility
205
     *
206
     * @param  int $level
207
     * @param  string $message
208
     * @return string
209
     */
210
    protected function _log($level, $message) {
211
        $message = 'TURPENTINE: '.$message;
212
        Mage::log($message, $level, $this->_getLogFileName());
213
        return $message;
214
    }
215
216
    /**
217
     * Get the name of the log file to use
218
     * @return string
219
     */
220
    protected function _getLogFileName() {
221
        if ($this->useCustomLogFile()) {
222
            return $this->getCustomLogFileName();
223
        }
224
        return '';
225
    }
226
227
    /**
228
     * Check if custom log file should be used
229
     * @return bool
230
     */
231
    public function useCustomLogFile() {
232
        return Mage::getStoreConfigFlag(
233
            'turpentine_varnish/logging/use_custom_log_file' );
234
    }
235
236
    /**
237
     * Get custom log file name
238
     * @return string
239
     */
240
    public function getCustomLogFileName() {
241
        return (string) Mage::getStoreConfig(
242
            'turpentine_varnish/logging/custom_log_file_name' );
243
    }
244
245
    /**
246
     * Format a list of function arguments for the backtrace
247
     *
248
     * @param  array $args
249
     * @return string
250
     */
251
    protected function _backtrace_formatArgs($args) {
252
        return implode(', ',
253
            array_map(
254
                array($this, '_backtrace_formatArgsHelper'),
255
                $args
256
            )
257
        );
258
    }
259
260
    /**
261
     * Format a value for inclusion in the backtrace
262
     *
263
     * @param  mixed $arg
264
     * @return null
265
     */
266
    protected function _backtrace_formatArgsHelper($arg) {
267
        $value = $arg;
268
        if (is_object($arg)) {
269
            $value = sprintf('OBJECT(%s)', get_class($arg));
270
        } elseif (is_resource($arg)) {
271
            $value = 'RESOURCE';
272
        } elseif (is_array($arg)) {
273
            $value = 'ARRAY[%s](%s)';
274
            $c = array();
275
            foreach ($arg as $k => $v) {
276
                $c[] = sprintf('%s => %s', $k,
277
                    $this->_backtrace_formatArgsHelper($v));
278
            }
279
            $value = sprintf($value, count($arg), implode(', ', $c));
280
        } elseif (is_string($arg)) {
281
            $value = sprintf('\'%s\'', $arg);
282
        } elseif (is_bool($arg)) {
283
            $value = $arg ? 'TRUE' : 'FALSE';
284
        } elseif (is_null($arg)) {
285
            $value = 'NULL';
286
        }
287
        return $value;
288
    }
289
}
290