Skip to content

Commit

Permalink
EnumPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Aug 27, 2022
1 parent ef914da commit 0518ee9
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 0 deletions.
Binary file modified build/kint.phar
Binary file not shown.
1 change: 1 addition & 0 deletions src/Kint.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class Kint
'Kint\\Parser\\ClosurePlugin',
'Kint\\Parser\\ColorPlugin',
'Kint\\Parser\\DateTimePlugin',
'Kint\\Parser\\EnumPlugin',
'Kint\\Parser\\FsPathPlugin',
'Kint\\Parser\\IteratorPlugin',
'Kint\\Parser\\JsonPlugin',
Expand Down
6 changes: 6 additions & 0 deletions src/Parser/ClassStaticsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Kint\Zval\Value;
use ReflectionClass;
use ReflectionProperty;
use UnitEnum;

class ClassStaticsPlugin extends Plugin
{
Expand All @@ -56,6 +57,11 @@ public function parse(&$var, Value &$o, $trigger)
$consts = [];

foreach ($reflection->getConstants() as $name => $val) {
// Skip enum constants
if ($var instanceof UnitEnum && $val instanceof UnitEnum && $o->classname == \get_class($val)) {
continue;
}

$const = Value::blank($name, '\\'.$class.'::'.$name);
$const->const = true;
$const->depth = $o->depth + 1;
Expand Down
86 changes: 86 additions & 0 deletions src/Parser/EnumPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt ([email protected]), Rokas Šleinius ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace Kint\Parser;

use BackedEnum;
use Kint\Zval\EnumValue;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use UnitEnum;

class EnumPlugin extends Plugin
{
private static $cache = [];

public function getTypes()
{
return ['object'];
}

public function getTriggers()
{
if (!KINT_PHP81) {
return Parser::TRIGGER_NONE;
}

return Parser::TRIGGER_SUCCESS;
}

public function parse(&$var, Value &$o, $trigger)
{
if (!$var instanceof UnitEnum) {
return;
}

$class = \get_class($var);

if (!isset(self::$cache[$class])) {
$cases = new Representation('Enum values', 'enum');
$cases->contents = [];

foreach ($var->cases() as $case) {
$base_obj = Value::blank($class.'::'.$case->name, '\\'.$class.'::'.$case->name);
$base_obj->depth = $o->depth + 1;

if ($var instanceof BackedEnum) {
$c = $case->value;
$cases->contents[] = $this->parser->parse($c, $base_obj);
} else {
$cases->contents[] = $base_obj;
}
}

self::$cache[$class] = $cases;
}

$object = new EnumValue($var);
$object->transplant($o);

$object->addRepresentation(self::$cache[$class], 0);

$o = $object;
}
}
44 changes: 44 additions & 0 deletions src/Renderer/Text/EnumPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt ([email protected]), Rokas Šleinius ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace Kint\Renderer\Text;

use Kint\Zval\Value;

class EnumPlugin extends Plugin
{
public function render(Value $o)
{
$out = '';

if (0 == $o->depth) {
$out .= $this->renderer->colorTitle($this->renderer->renderTitle($o)).PHP_EOL;
}

$out .= $this->renderer->renderHeader($o).PHP_EOL;

return $out;
}
}
2 changes: 2 additions & 0 deletions src/Renderer/TextRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TextRenderer extends Renderer
'microtime' => 'Kint\\Renderer\\Text\\MicrotimePlugin',
'recursion' => 'Kint\\Renderer\\Text\\RecursionPlugin',
'trace' => 'Kint\\Renderer\\Text\\TracePlugin',
'enum' => 'Kint\\Renderer\\Text\\EnumPlugin',
];

/**
Expand All @@ -55,6 +56,7 @@ class TextRenderer extends Renderer
'Kint\\Parser\\MicrotimePlugin',
'Kint\\Parser\\StreamPlugin',
'Kint\\Parser\\TracePlugin',
'Kint\\Parser\\EnumPlugin',
];

/**
Expand Down
62 changes: 62 additions & 0 deletions src/Zval/EnumValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt ([email protected]), Rokas Šleinius ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace Kint\Zval;

use BackedEnum;
use UnitEnum;

class EnumValue extends InstanceValue
{
public $enumval;

public $hints = ['object', 'enum'];

public function __construct(UnitEnum $enumval)
{
$this->enumval = $enumval;
}

public function getValueShort()
{
if ($this->enumval instanceof BackedEnum) {
if (\is_string($this->enumval->value)) {
return '"'.$this->enumval->value.'"';
}
if (\is_int($this->enumval->value)) {
return (string) $this->enumval->value;
}
}
}

public function getType()
{
return $this->classname.'::'.$this->enumval->name;
}

public function getSize()
{
}
}

0 comments on commit 0518ee9

Please sign in to comment.