X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fpsy%2Fpsysh%2Fsrc%2FCommand%2FReflectingCommand.php;fp=vendor%2Fpsy%2Fpsysh%2Fsrc%2FCommand%2FReflectingCommand.php;h=0bf8327c919a52d088249b8cf87e9057996a9f12;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/vendor/psy/psysh/src/Command/ReflectingCommand.php b/vendor/psy/psysh/src/Command/ReflectingCommand.php new file mode 100644 index 000000000..0bf8327c9 --- /dev/null +++ b/vendor/psy/psysh/src/Command/ReflectingCommand.php @@ -0,0 +1,277 @@ +)(\w+)$/'; + + /** + * Context instance (for ContextAware interface). + * + * @var Context + */ + protected $context; + + /** + * ContextAware interface. + * + * @param Context $context + */ + public function setContext(Context $context) + { + $this->context = $context; + } + + /** + * Get the target for a value. + * + * @throws \InvalidArgumentException when the value specified can't be resolved + * + * @param string $valueName Function, class, variable, constant, method or property name + * + * @return array (class or instance name, member name, kind) + */ + protected function getTarget($valueName) + { + $valueName = trim($valueName); + $matches = []; + switch (true) { + case preg_match(self::CLASS_OR_FUNC, $valueName, $matches): + return [$this->resolveName($matches[0], true), null, 0]; + + case preg_match(self::CLASS_MEMBER, $valueName, $matches): + return [$this->resolveName($matches[1]), $matches[2], Mirror::CONSTANT | Mirror::METHOD]; + + case preg_match(self::CLASS_STATIC, $valueName, $matches): + return [$this->resolveName($matches[1]), $matches[2], Mirror::STATIC_PROPERTY | Mirror::PROPERTY]; + + case preg_match(self::INSTANCE_MEMBER, $valueName, $matches): + if ($matches[2] === '->') { + $kind = Mirror::METHOD | Mirror::PROPERTY; + } else { + $kind = Mirror::CONSTANT | Mirror::METHOD; + } + + return [$this->resolveObject($matches[1]), $matches[3], $kind]; + + default: + return [$this->resolveObject($valueName), null, 0]; + } + } + + /** + * Resolve a class or function name (with the current shell namespace). + * + * @param string $name + * @param bool $includeFunctions (default: false) + * + * @return string + */ + protected function resolveName($name, $includeFunctions = false) + { + if (substr($name, 0, 1) === '\\') { + return $name; + } + + if ($namespace = $this->getApplication()->getNamespace()) { + $fullName = $namespace . '\\' . $name; + + if (class_exists($fullName) || interface_exists($fullName) || ($includeFunctions && function_exists($fullName))) { + return $fullName; + } + } + + return $name; + } + + /** + * Get a Reflector and documentation for a function, class or instance, constant, method or property. + * + * @param string $valueName Function, class, variable, constant, method or property name + * + * @return array (value, Reflector) + */ + protected function getTargetAndReflector($valueName) + { + list($value, $member, $kind) = $this->getTarget($valueName); + + return [$value, Mirror::get($value, $member, $kind)]; + } + + /** + * Resolve code to a value in the current scope. + * + * @throws RuntimeException when the code does not return a value in the current scope + * + * @param string $code + * + * @return mixed Variable value + */ + protected function resolveCode($code) + { + try { + $value = $this->getApplication()->execute($code, true); + } catch (\Exception $e) { + // Swallow all exceptions? + } + + if (!isset($value) || $value instanceof NoReturnValue) { + throw new RuntimeException('Unknown target: ' . $code); + } + + return $value; + } + + /** + * Resolve code to an object in the current scope. + * + * @throws RuntimeException when the code resolves to a non-object value + * + * @param string $code + * + * @return object Variable instance + */ + private function resolveObject($code) + { + $value = $this->resolveCode($code); + + if (!is_object($value)) { + throw new RuntimeException('Unable to inspect a non-object'); + } + + return $value; + } + + /** + * @deprecated Use `resolveCode` instead + * + * @param string $name + * + * @return mixed Variable instance + */ + protected function resolveInstance($name) + { + @trigger_error('`resolveInstance` is deprecated; use `resolveCode` instead.', E_USER_DEPRECATED); + + return $this->resolveCode($name); + } + + /** + * Get a variable from the current shell scope. + * + * @param string $name + * + * @return mixed + */ + protected function getScopeVariable($name) + { + return $this->context->get($name); + } + + /** + * Get all scope variables from the current shell scope. + * + * @return array + */ + protected function getScopeVariables() + { + return $this->context->getAll(); + } + + /** + * Given a Reflector instance, set command-scope variables in the shell + * execution context. This is used to inject magic $__class, $__method and + * $__file variables (as well as a handful of others). + * + * @param \Reflector $reflector + */ + protected function setCommandScopeVariables(\Reflector $reflector) + { + $vars = []; + + switch (get_class($reflector)) { + case 'ReflectionClass': + case 'ReflectionObject': + $vars['__class'] = $reflector->name; + if ($reflector->inNamespace()) { + $vars['__namespace'] = $reflector->getNamespaceName(); + } + break; + + case 'ReflectionMethod': + $vars['__method'] = sprintf('%s::%s', $reflector->class, $reflector->name); + $vars['__class'] = $reflector->class; + $classReflector = $reflector->getDeclaringClass(); + if ($classReflector->inNamespace()) { + $vars['__namespace'] = $classReflector->getNamespaceName(); + } + break; + + case 'ReflectionFunction': + $vars['__function'] = $reflector->name; + if ($reflector->inNamespace()) { + $vars['__namespace'] = $reflector->getNamespaceName(); + } + break; + + case 'ReflectionGenerator': + $funcReflector = $reflector->getFunction(); + $vars['__function'] = $funcReflector->name; + if ($funcReflector->inNamespace()) { + $vars['__namespace'] = $funcReflector->getNamespaceName(); + } + if ($fileName = $reflector->getExecutingFile()) { + $vars['__file'] = $fileName; + $vars['__line'] = $reflector->getExecutingLine(); + $vars['__dir'] = dirname($fileName); + } + break; + + case 'ReflectionProperty': + case 'Psy\Reflection\ReflectionConstant': + $classReflector = $reflector->getDeclaringClass(); + $vars['__class'] = $classReflector->name; + if ($classReflector->inNamespace()) { + $vars['__namespace'] = $classReflector->getNamespaceName(); + } + // no line for these, but this'll do + if ($fileName = $reflector->getDeclaringClass()->getFileName()) { + $vars['__file'] = $fileName; + $vars['__dir'] = dirname($fileName); + } + break; + } + + if ($reflector instanceof \ReflectionClass || $reflector instanceof \ReflectionFunctionAbstract) { + if ($fileName = $reflector->getFileName()) { + $vars['__file'] = $fileName; + $vars['__line'] = $reflector->getStartLine(); + $vars['__dir'] = dirname($fileName); + } + } + + $this->context->setCommandScopeVariables($vars); + } +}