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 Kint\Object\InstanceObject;
30:
31: class BlacklistPlugin extends Plugin
32: {
33: /**
34: * List of classes and interfaces to blacklist.
35: *
36: * @var array
37: */
38: public static $blacklist = array();
39:
40: /**
41: * List of classes and interfaces to blacklist except when dumped directly.
42: *
43: * @var array
44: */
45: public static $shallow_blacklist = array();
46:
47: /**
48: * Maximum size of arrays before blacklisting.
49: *
50: * @var int
51: */
52: public static $array_limit = 10000;
53:
54: /**
55: * Maximum size of arrays before blacklisting except when dumped directly.
56: *
57: * @var int
58: */
59: public static $shallow_array_limit = 1000;
60:
61: public function getTypes()
62: {
63: return array('object', 'array');
64: }
65:
66: public function getTriggers()
67: {
68: return Parser::TRIGGER_BEGIN;
69: }
70:
71: public function parse(&$var, BasicObject &$o, $trigger)
72: {
73: if (\is_object($var)) {
74: return $this->parseObject($var, $o);
75: }
76: if (\is_array($var)) {
77: return $this->parseArray($var, $o);
78: }
79: }
80:
81: protected function parseObject(&$var, BasicObject &$o)
82: {
83: foreach (self::$blacklist as $class) {
84: if ($var instanceof $class) {
85: return $this->blacklistObject($var, $o);
86: }
87: }
88:
89: if ($o->depth <= 0) {
90: return;
91: }
92:
93: foreach (self::$shallow_blacklist as $class) {
94: if ($var instanceof $class) {
95: return $this->blacklistObject($var, $o);
96: }
97: }
98: }
99:
100: protected function blacklistObject(&$var, BasicObject &$o)
101: {
102: $object = new InstanceObject();
103: $object->transplant($o);
104: $object->classname = \get_class($var);
105: $object->hash = \spl_object_hash($var);
106: $object->clearRepresentations();
107: $object->value = null;
108: $object->size = null;
109: $object->hints[] = 'blacklist';
110:
111: $o = $object;
112:
113: $this->parser->haltParse();
114: }
115:
116: protected function parseArray(array &$var, BasicObject &$o)
117: {
118: if (\count($var) > self::$array_limit) {
119: return $this->blacklistArray($var, $o);
120: }
121:
122: if ($o->depth <= 0) {
123: return;
124: }
125:
126: if (\count($var) > self::$shallow_array_limit) {
127: return $this->blacklistArray($var, $o);
128: }
129: }
130:
131: protected function blacklistArray(array &$var, BasicObject &$o)
132: {
133: $object = new BasicObject();
134: $object->transplant($o);
135: $object->value = null;
136: $object->size = \count($var);
137: $object->hints[] = 'blacklist';
138:
139: $o = $object;
140:
141: $this->parser->haltParse();
142: }
143: }
144: