You are here

final public static function kintParser::factory in Devel 8.2

Same name and namespace in other branches
  1. 8 kint/kint/inc/kintParser.class.php \kintParser::factory()

* the only public entry point to return a parsed representation of a variable * * @static * *

Parameters

$variable: * @param null $name * * @throws Exception * @return \kintParser

7 calls to kintParser::factory()
Kint::dump in kint/kint/Kint.class.php
* Dump information about variables, accepts any number of parameters, supports modifiers: * * clean up any output before kint and place the dump at the top of page: * - Kint::dump() * ***** * expand all nodes on display: * !…
kintParser::_parse_array in kint/kint/inc/kintParser.class.php
kintParser::_parse_object in kint/kint/inc/kintParser.class.php
Kint_Decorators_Plain::decorateTrace in kint/kint/decorators/plain.php
Kint_Decorators_Rich::decorateTrace in kint/kint/decorators/rich.php

... See full list

File

kint/kint/inc/kintParser.class.php, line 61

Class

kintParser

Code

public static final function factory(&$variable, $name = null) {
  isset(self::$_customDataTypes) or self::_init();

  # save internal data to revert after dumping to properly handle recursions etc
  $revert = array(
    'level' => self::$_level,
    'objects' => self::$_objects,
  );
  self::$_level++;
  $varData = new kintVariableData();
  $varData->name = $name;

  # first parse the variable based on its type
  $varType = gettype($variable);
  $varType === 'unknown type' and $varType = 'unknown';

  # PHP 5.4 inconsistency
  $methodName = '_parse_' . $varType;

  # objects can be presented in a different way altogether, INSTEAD, not ALONGSIDE the generic parser
  if ($varType === 'object') {
    foreach (self::$_objectParsers as $parserClass) {
      $className = 'Kint_Objects_' . $parserClass;

      /** @var $object KintObject */
      $object = new $className();
      if (($alternativeTabs = $object
        ->parse($variable)) !== false) {
        self::$_skipAlternatives = true;
        $alternativeDisplay = new kintVariableData();
        $alternativeDisplay->type = $object->name;
        $alternativeDisplay->value = $object->value;
        $alternativeDisplay->name = $name;
        foreach ($alternativeTabs as $name => $values) {
          $alternative = kintParser::factory($values);
          $alternative->type = $name;
          if (Kint::enabled() === Kint::MODE_RICH) {
            empty($alternative->value) and $alternative->value = $alternative->extendedValue;
            $alternativeDisplay->_alternatives[] = $alternative;
          }
          else {
            $alternativeDisplay->extendedValue[] = $alternative;
          }
        }
        self::$_skipAlternatives = false;
        self::$_level = $revert['level'];
        self::$_objects = $revert['objects'];
        return $alternativeDisplay;
      }
    }
  }

  # base type parser returning false means "stop processing further": e.g. recursion
  if (self::$methodName($variable, $varData) === false) {
    self::$_level--;
    return $varData;
  }
  if (Kint::enabled() === Kint::MODE_RICH && !self::$_skipAlternatives) {

    # if an alternative returns something that can be represented in an alternative way, don't :)
    self::$_skipAlternatives = true;

    # now check whether the variable can be represented in a different way
    foreach (self::$_customDataTypes as $parserClass) {
      $className = 'Kint_Parsers_' . $parserClass;

      /** @var $parser kintParser */
      $parser = new $className();
      $parser->name = $name;

      # the parser may overwrite the name value, so set it first
      if ($parser
        ->_parse($variable) !== false) {
        $varData->_alternatives[] = $parser;
      }
    }

    # if alternatives exist, push extendedValue to their front and display it as one of alternatives
    if (!empty($varData->_alternatives) && isset($varData->extendedValue)) {
      $_ = new kintVariableData();
      $_->value = $varData->extendedValue;
      $_->type = 'contents';
      $_->size = null;
      array_unshift($varData->_alternatives, $_);
      $varData->extendedValue = null;
    }
    self::$_skipAlternatives = false;
  }
  self::$_level = $revert['level'];
  self::$_objects = $revert['objects'];
  if (strlen($varData->name) > 80) {
    $varData->name = self::_substr($varData->name, 0, 37) . '...' . self::_substr($varData->name, -38, null);
  }
  return $varData;
}