/** * This function removes the top of the stacktrace and formats the trace to a wellformed * string stracktrace. * @param string The original backtrace * @return string The stacktrace */ private static final function getFormattedTraceAsString($traces) { array_shift($traces); $outputString = ''; foreach ($traces as $index => $trace) { $outputString .= '#' . $index . ' '; if (isset($trace['file'])) { $outputString .= $trace['file'] . '('; if (isset($trace['line'])) { $outputString .= $trace['line']; } $outputString .= '): '; } else { $outputString .= '[internal]: '; } if (isset($trace['object'])) { $outputString .= $trace['object'] . $trace['type']; } $outputString .= $trace['function'] . '('; $params = array(); if (isset($trace['args'])) { foreach ($trace['args'] as $arg) { $type = \System\Type::getType($arg); switch ($type) { case \System\Type::TYPE_BOOLEAN: case \System\Type::TYPE_DOUBLE: case \System\Type::TYPE_INTEGER: case \System\Type::TYPE_NULL: $params[] = $type . '{' . \System\Type::getValue($arg) . '}'; break; case \System\Type::TYPE_STRING: $params[] = $type . '{"' . \System\Type::getValue($arg) . '"}'; break; default: $params[] = $type; } } } $outputString .= implode(', ', $params); $outputString .= ')'; $outputString .= "\r\n"; } return $outputString; }
/** * Implements the function result value caching. This function is called by the __call * function and generates a key from the given parameters. It performs a simple and fast lookup * in an internal array and returns the value if needed. Otherwise the value is appended to the * lut by executing the function and storing its result. * Note: it is not possible to call functions that require parameters by reference * @param string The name of the function being called * @param array The arguments passed to the function * @return mixed The result of the function */ private final function functionCache($name, array $arguments) { //first we need a lut key from the given params $key = spl_object_hash($this) . '_' . $name . '('; //serialize the parameters foreach ($arguments as $arg) { $key .= \System\Type::getValue($arg); } $key .= ')'; //because we dont have a constructor, we must check the type of the cache and create it //if needed if ($this->functionCache === null) { $this->functionCache = new \System\Collection\Map(); } //lookup the key in the functionCache for fast calling and store the function result if (!isset($this->functionCache[$key])) { if (is_callable(array($this, $name))) { $this->functionCache[$key] = call_user_func_array(array($this, $name), $arguments); } else { throw new \System\Error\Exception\InvalidMethodException('Cannot access given method. Is it public or protected?: ' . $name); } } return $this->functionCache[$key]; }
/** * Adds class constants to the given code block. * @param string The string to append to. By reference. * @param array Constants in an array, the key is their name; the value its value. */ private static final function addClassConstants(&$code, array $constants) { foreach ($constants as $constantName => $constantValue) { $str = ''; switch (\System\Type::getType($constantValue)) { case \System\Type::TYPE_ARRAY: $str = 'array()'; break; case \System\Type::TYPE_STRING: $str = '\'' . $constantValue . '\''; break; case \System\Type::TYPE_INTEGER: case \System\Type::TYPE_DOUBLE: case \System\Type::TYPE_NULL: case \System\Type::TYPE_BOOLEAN: $str = \System\Type::getValue($constantValue); break; default: $str = '\'undefined\''; break; } $code .= 'const ' . $constantName . ' = ' . $str . ';' . "\r\n"; } }