Completed
Push — location_multi_load ( e5e305...41e541 )
by André
51:38 queued 33:21
created

PersistenceLogger::getSimpleCallTrace()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 1
dl 0
loc 19
rs 9.3222
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Persistence Cache SPI logger class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache;
10
11
/**
12
 * Log un-cached use of SPI Persistence.
13
 *
14
 * Stops logging details when reaching $maxLogCalls to conserve memory use
15
 */
16
class PersistenceLogger
17
{
18
    const NAME = 'PersistenceLogger';
19
20
    /**
21
     * @var int
22
     */
23
    protected $count = 0;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $logCalls = true;
29
30
    /**
31
     * @var array
32
     */
33
    protected $calls = array();
34
35
    /**
36
     * @var array
37
     */
38
    protected $unCachedHandlers = array();
39
40
    /**
41
     * @param bool $logCalls Flag to enable logging of calls or not, should be disabled in prod
42
     */
43
    public function __construct($logCalls = true)
44
    {
45
        $this->logCalls = $logCalls;
46
    }
47
48
    /**
49
     * Log SPI calls with method name and arguments until $maxLogCalls is reached.
50
     *
51
     * @param string $method
52
     * @param array $arguments
53
     */
54
    public function logCall($method, array $arguments = array())
55
    {
56
        ++$this->count;
57
        if ($this->logCalls) {
58
            $this->calls[] = array(
59
                'method' => $method,
60
                'arguments' => $arguments,
61
                'trace' => $this->getSimpleCallTrace(
62
                    array_slice(
63
                        debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 7),
64
                        2
65
                    )
66
                ),
67
            );
68
        }
69
    }
70
71
    private function getSimpleCallTrace(array $backtrace): array
72
    {
73
        $calls = [];
74
        foreach ($backtrace as $call) {
75
            if (!isset($call['class']) || strpos($call['class'], '\\') === false) {
76
                // skip if class has no namspace (Symfony lazy proxy) or plain function
77
                continue;
78
            }
79
80
            $calls[] = $call['class'] . $call['type'] . $call['function'] . '()';
81
82
            // Break out as soon as we have listed 1 class outside of kernel
83
            if (strpos($call['class'], 'eZ\\Publish\\Core\\') !== 0) {
84
                break;
85
            }
86
        }
87
88
        return $calls;
89
    }
90
91
    /**
92
     * Log uncached handler being loaded.
93
     *
94
     * @param string $handler
95
     */
96
    public function logUnCachedHandler($handler)
97
    {
98
        if (!isset($this->unCachedHandlers[$handler])) {
99
            $this->unCachedHandlers[$handler] = 0;
100
        }
101
        ++$this->unCachedHandlers[$handler];
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getName()
108
    {
109
        return self::NAME;
110
    }
111
112
    /**
113
     * @return int
114
     */
115
    public function getCount()
116
    {
117
        return $this->count;
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    public function isCallsLoggingEnabled()
124
    {
125
        return $this->logCalls;
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    public function getCalls()
132
    {
133
        return $this->calls;
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    public function getLoadedUnCachedHandlers()
140
    {
141
        return $this->unCachedHandlers;
142
    }
143
}
144