1: <?php
2:
3: /*
4: * The MIT License (MIT)
5: *
6: * Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
7: *
8: * Permission is hereby granted, free of charge, to any person obtaining a copy of
9: * this software and associated documentation files (the "Software"), to deal in
10: * the Software without restriction, including without limitation the rights to
11: * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12: * the Software, and to permit persons to whom the Software is furnished to do so,
13: * subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in all
16: * copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21: * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22: * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23: * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24: */
25:
26: namespace Kint\Parser;
27:
28: use Kint\Object\BasicObject;
29: use Mysqli;
30:
31: /**
32: * Adds support for Mysqli object parsing.
33: *
34: * Due to the way mysqli is implemented in PHP, this will cause
35: * warnings on certain Mysqli objects if screaming is enabled.
36: */
37: class MysqliPlugin extends Plugin
38: {
39: // These 'properties' are actually globals
40: protected $always_readable = array(
41: 'client_version' => true,
42: 'connect_errno' => true,
43: 'connect_error' => true,
44: );
45:
46: // These are readable on empty mysqli objects, but not on failed connections
47: protected $empty_readable = array(
48: 'client_info' => true,
49: 'errno' => true,
50: 'error' => true,
51: );
52:
53: // These are only readable on connected mysqli objects
54: protected $connected_readable = array(
55: 'affected_rows' => true,
56: 'error_list' => true,
57: 'field_count' => true,
58: 'host_info' => true,
59: 'info' => true,
60: 'insert_id' => true,
61: 'server_info' => true,
62: 'server_version' => true,
63: 'stat' => true,
64: 'sqlstate' => true,
65: 'protocol_version' => true,
66: 'thread_id' => true,
67: 'warning_count' => true,
68: );
69:
70: public function getTypes()
71: {
72: return array('object');
73: }
74:
75: public function getTriggers()
76: {
77: return Parser::TRIGGER_COMPLETE;
78: }
79:
80: public function parse(&$var, BasicObject &$o, $trigger)
81: {
82: if (!$var instanceof Mysqli) {
83: return;
84: }
85:
86: $connected = false;
87: $empty = false;
88:
89: if (\is_string(@$var->sqlstate)) {
90: $connected = true;
91: } elseif (\is_string(@$var->client_info)) {
92: $empty = true;
93: }
94:
95: foreach ($o->value->contents as $key => $obj) {
96: if (isset($this->connected_readable[$obj->name])) {
97: if (!$connected) {
98: continue;
99: }
100: } elseif (isset($this->empty_readable[$obj->name])) {
101: if (!$connected && !$empty) {
102: continue;
103: }
104: } elseif (!isset($this->always_readable[$obj->name])) {
105: continue;
106: }
107:
108: if ('null' !== $obj->type) {
109: continue;
110: }
111:
112: $param = $var->{$obj->name};
113:
114: if (null === $param) {
115: continue;
116: }
117:
118: $base = BasicObject::blank($obj->name, $obj->access_path);
119:
120: $base->depth = $obj->depth;
121: $base->owner_class = $obj->owner_class;
122: $base->operator = $obj->operator;
123: $base->access = $obj->access;
124: $base->reference = $obj->reference;
125:
126: $o->value->contents[$key] = $this->parser->parse($param, $base);
127: }
128: }
129: }
130: