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: use Kint\Object\Representation\Representation;
31: use ReflectionClass;
32: use ReflectionProperty;
33:
34: class ClassStaticsPlugin extends Plugin
35: {
36: private static $cache = array();
37:
38: public function getTypes()
39: {
40: return array('object');
41: }
42:
43: public function getTriggers()
44: {
45: return Parser::TRIGGER_SUCCESS;
46: }
47:
48: public function parse(&$var, BasicObject &$o, $trigger)
49: {
50: $class = \get_class($var);
51: $reflection = new ReflectionClass($class);
52:
53: // Constants
54: // TODO: PHP 7.1 allows private consts but reflection doesn't have a way to check them yet
55: if (!isset(self::$cache[$class])) {
56: $consts = array();
57:
58: foreach ($reflection->getConstants() as $name => $val) {
59: $const = BasicObject::blank($name, '\\'.$class.'::'.$name);
60: $const->const = true;
61: $const->depth = $o->depth + 1;
62: $const->owner_class = $class;
63: $const->operator = BasicObject::OPERATOR_STATIC;
64: $const = $this->parser->parse($val, $const);
65:
66: $consts[] = $const;
67: }
68:
69: self::$cache[$class] = $consts;
70: }
71:
72: $statics = new Representation('Static class properties', 'statics');
73: $statics->contents = self::$cache[$class];
74:
75: foreach ($reflection->getProperties(ReflectionProperty::IS_STATIC) as $static) {
76: $prop = new BasicObject();
77: $prop->name = '$'.$static->getName();
78: $prop->depth = $o->depth + 1;
79: $prop->static = true;
80: $prop->operator = BasicObject::OPERATOR_STATIC;
81: $prop->owner_class = $static->getDeclaringClass()->name;
82:
83: $prop->access = BasicObject::ACCESS_PUBLIC;
84: if ($static->isProtected()) {
85: $prop->access = BasicObject::ACCESS_PROTECTED;
86: } elseif ($static->isPrivate()) {
87: $prop->access = BasicObject::ACCESS_PRIVATE;
88: }
89:
90: if ($this->parser->childHasPath($o, $prop)) {
91: $prop->access_path = '\\'.$prop->owner_class.'::'.$prop->name;
92: }
93:
94: $static->setAccessible(true);
95: $static = $static->getValue();
96: $statics->contents[] = $this->parser->parse($static, $prop);
97: }
98:
99: if (empty($statics->contents)) {
100: return;
101: }
102:
103: \usort($statics->contents, array('Kint\\Parser\\ClassStaticsPlugin', 'sort'));
104:
105: $o->addRepresentation($statics);
106: }
107:
108: private static function sort(BasicObject $a, BasicObject $b)
109: {
110: $sort = ((int) $a->const) - ((int) $b->const);
111: if ($sort) {
112: return $sort;
113: }
114:
115: $sort = BasicObject::sortByAccess($a, $b);
116: if ($sort) {
117: return $sort;
118: }
119:
120: return InstanceObject::sortByHierarchy($a->owner_class, $b->owner_class);
121: }
122: }
123: