/** * Get global key's value. * An expression to access array elements or a function can be given. * @param string $key Expression key, can have parenthesis or square brackets operator. * @return mixed|callable|array If you access a function without the * parenthesis operator or an array without square brackets operator * then this method will return the callable or the whole array respectively. * @example * ```php * Globals::get('myVarible'); * * Globals::get('rand-boolean()'); * * Globals::get('rand-integer(10)'); //A random integer from 0 to 10 * * Globals::get('myArray[1]'); //Get second array element * ``` * @throws \Exception When expression key is invalid. * @throws \Phramework\Exceptions\NotFoundException When key is not found. */ public static function get($key = null, $operators = null) { if (static::$globals === null) { static::initializeGlobals(); } if ($key !== null) { $parsed = Expression::parse($key); if ($parsed === null) { throw new \Exception(sprintf('Invalid key "%s"', $key)); } if (!static::exists($parsed->key)) { throw new UnsetGlobalException($parsed->key); } $global = static::$globals->{$parsed->key}; switch ($parsed->mode) { case Globals::KEY_FUNCTION: $functionParameters = []; if (property_exists($parsed, 'parameters')) { $functionParameters = Globals::handleFunctionVariable($parsed->parameters); } return call_user_func_array($global, $functionParameters); case Globals::KEY_ARRAY: return $global[(int) Globals::handleArrayVariable($parsed->index)]; case Globals::KEY_VARIABLE: default: return $global; } } return static::$globals; }