summaryrefslogtreecommitdiff
path: root/framework/Log/EventLog/context.php
blob: 518d411e7523f9344f158d30fae9d4abcd2359a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
 * File containing the ezcLogContext class.
 *
 * @package EventLog
 * @version //autogentag//
 * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
 * @license http://ez.no/licenses/new_bsd New BSD License
 * @access private
 */

/**
 * Stores the contexts for the severities and sources.
 *
 * @package EventLog
 * @version //autogentag//
 * @access private
 */
class ezcLogContext
{

    /**
     * Stores the contexts for the severities.
     *
     * @var array(integer=>array)
     */
    protected $severityMap;

    /**
     * Stores the contexts for the sources.
     *
     * @var array(string=>array)
     */
    protected $sourceMap;


    /**
     * Resets this object to its initial state by removing all context mappings.
     *
     * @return void
     */
    public function reset()
    {
        unset( $this->severityMap );
        unset( $this->sourceMap );
    }

    /**
     * Set the context $context for the sevirities specified by $severityMask.
     *
     * If the severity already exist, the value will update the old value with
     * the new one.
     *
     * $context is of the format array('key'=>'value').
     *
     * @param int $severityMask
     *        Bitmask that specifies all the event types that share the given
     *        context.
     * @param array(string=>string) $context
     * @return void
     */
    public function setSeverityContext( $severityMask, $context )
    {
        // For all the matching bits, add the context to the array.
        $input = 1;
        while ( $input <= $severityMask )
        {
            if ( $severityMask & $input )
            {
                if ( !isset( $this->severityMap[$input] ) )
                {
                    $this->severityMap[$input] = array();
                }

                $this->severityMap[$input] = array_merge( (array) $this->severityMap[$input], (array) $context );
            }

            $input <<= 1;
        }
    }

    /**
     * Remove the contexts for the severities given by $severityMask.
     *
     * $severityMask is a bitmask that specifies all the event types that should remove
     * their context.
     *
     * @param int $severityMask
     * @return void
     */
    public function unsetSeverityContext( $severityMask )
    {
        // For all the matching bits, remove the context.
        $input = 1;
        while ( $input <= $severityMask )
        {
            if ( $severityMask & $input )
            {
                unset( $this->severityMap[$input] );
            }

            $input <<= 1;
        }
    }

    /**
     * Set the context $context for each eventSource specified by $eventSources.
     *
     * If a certain key from the given context does already exist, the
     * new value will replace the value stored in the context itself. (Value is
     * updated).
     *
     * @param array(string) $eventSources
     * @param array(string=>string) $context
     *        Specifies the keys and values that should be stored into this
     *        context object.
     * @return void
     */
    public function setSourceContext($eventSources, $context )
    {
        foreach ( $eventSources as $eventSource )
        {
            if ( !isset( $this->sourceMap[$eventSource] ) )
            {
                $this->sourceMap[$eventSource] = array();
            }

            $this->sourceMap[$eventSource] = array_merge( (array) $this->sourceMap[$eventSource], (array) $context );
        }
    }

    /**
     * Remove the contexts for the given $eventSources.
     *
     * @param array(string) $eventSources
     * @return void
     */
    public function unsetSourceContext($eventSources )
    {
        foreach ( $eventSources as $eventSource )
        {
            unset( $this->sourceMap[$eventSource] );
        }
    }

    /**
     * Returns the complete context for the event type $eventType and event source $eventSource.
     *
     * If there is no context available this method will return an empty array.
     *
     *
     * @param int $eventType   The integer that specifies the event type.
     *                             The range of this integer is 2^(N+):
     *                             ( 1, 2, 4, 8, ... )
     * @param string $eventSource
     * @return array
     */
    public function getContext( $eventType, $eventSource )
    {
        $a = isset( $this->severityMap[$eventType] ) ? $this->severityMap[$eventType] : array();
        $b = isset( $this->sourceMap[$eventSource] ) ? $this->sourceMap[$eventSource] : array();
        return array_merge( $a, $b );
        //return array_merge( (array) $this->severityMap[$eventType], (array) $this->sourceMap[$eventSource] );
    }

}
?>