Chris@0: )(\w+)$/'; Chris@0: const INSTANCE_STATIC = '/^\$(\w+)::\$(\w+)$/'; Chris@0: const SUPERGLOBAL = '/^\$(GLOBALS|_(?:SERVER|ENV|FILES|COOKIE|POST|GET|SESSION))$/'; Chris@0: Chris@0: /** Chris@0: * Context instance (for ContextAware interface). Chris@0: * Chris@0: * @var Context Chris@0: */ Chris@0: protected $context; Chris@0: Chris@0: /** Chris@0: * ContextAware interface. Chris@0: * Chris@0: * @param Context $context Chris@0: */ Chris@0: public function setContext(Context $context) Chris@0: { Chris@0: $this->context = $context; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the target for a value. Chris@0: * Chris@0: * @throws \InvalidArgumentException when the value specified can't be resolved Chris@0: * Chris@0: * @param string $valueName Function, class, variable, constant, method or property name Chris@0: * @param bool $classOnly True if the name should only refer to a class, function or instance Chris@0: * Chris@0: * @return array (class or instance name, member name, kind) Chris@0: */ Chris@0: protected function getTarget($valueName, $classOnly = false) Chris@0: { Chris@0: $valueName = trim($valueName); Chris@0: $matches = array(); Chris@0: switch (true) { Chris@0: case preg_match(self::SUPERGLOBAL, $valueName, $matches): Chris@0: // @todo maybe do something interesting with these at some point? Chris@0: if (array_key_exists($matches[1], $GLOBALS)) { Chris@0: throw new RuntimeException('Unable to inspect a non-object'); Chris@0: } else { Chris@0: throw new RuntimeException('Unknown target: ' . $valueName); Chris@0: } Chris@0: Chris@0: case preg_match(self::CLASS_OR_FUNC, $valueName, $matches): Chris@0: return array($this->resolveName($matches[0], true), null, 0); Chris@0: Chris@0: case preg_match(self::INSTANCE, $valueName, $matches): Chris@0: return array($this->resolveInstance($matches[1]), null, 0); Chris@0: Chris@0: case !$classOnly && preg_match(self::CLASS_MEMBER, $valueName, $matches): Chris@0: return array($this->resolveName($matches[1]), $matches[2], Mirror::CONSTANT | Mirror::METHOD); Chris@0: Chris@0: case !$classOnly && preg_match(self::CLASS_STATIC, $valueName, $matches): Chris@0: return array($this->resolveName($matches[1]), $matches[2], Mirror::STATIC_PROPERTY | Mirror::PROPERTY); Chris@0: Chris@0: case !$classOnly && preg_match(self::INSTANCE_MEMBER, $valueName, $matches): Chris@0: if ($matches[2] === '->') { Chris@0: $kind = Mirror::METHOD | Mirror::PROPERTY; Chris@0: } else { Chris@0: $kind = Mirror::CONSTANT | Mirror::METHOD; Chris@0: } Chris@0: Chris@0: return array($this->resolveInstance($matches[1]), $matches[3], $kind); Chris@0: Chris@0: case !$classOnly && preg_match(self::INSTANCE_STATIC, $valueName, $matches): Chris@0: return array($this->resolveInstance($matches[1]), $matches[2], Mirror::STATIC_PROPERTY); Chris@0: Chris@0: default: Chris@0: throw new RuntimeException('Unknown target: ' . $valueName); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resolve a class or function name (with the current shell namespace). Chris@0: * Chris@0: * @param string $name Chris@0: * @param bool $includeFunctions (default: false) Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: protected function resolveName($name, $includeFunctions = false) Chris@0: { Chris@0: if (substr($name, 0, 1) === '\\') { Chris@0: return $name; Chris@0: } Chris@0: Chris@0: if ($namespace = $this->getApplication()->getNamespace()) { Chris@0: $fullName = $namespace . '\\' . $name; Chris@0: Chris@0: if (class_exists($fullName) || interface_exists($fullName) || ($includeFunctions && function_exists($fullName))) { Chris@0: return $fullName; Chris@0: } Chris@0: } Chris@0: Chris@0: return $name; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get a Reflector and documentation for a function, class or instance, constant, method or property. Chris@0: * Chris@0: * @param string $valueName Function, class, variable, constant, method or property name Chris@0: * @param bool $classOnly True if the name should only refer to a class, function or instance Chris@0: * Chris@0: * @return array (value, Reflector) Chris@0: */ Chris@0: protected function getTargetAndReflector($valueName, $classOnly = false) Chris@0: { Chris@0: list($value, $member, $kind) = $this->getTarget($valueName, $classOnly); Chris@0: Chris@0: return array($value, Mirror::get($value, $member, $kind)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return a variable instance from the current scope. Chris@0: * Chris@0: * @throws \InvalidArgumentException when the requested variable does not exist in the current scope Chris@0: * Chris@0: * @param string $name Chris@0: * Chris@0: * @return mixed Variable instance Chris@0: */ Chris@0: protected function resolveInstance($name) Chris@0: { Chris@0: $value = $this->getScopeVariable($name); Chris@0: if (!is_object($value)) { Chris@0: throw new RuntimeException('Unable to inspect a non-object'); Chris@0: } Chris@0: Chris@0: return $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get a variable from the current shell scope. Chris@0: * Chris@0: * @param string $name Chris@0: * Chris@0: * @return mixed Chris@0: */ Chris@0: protected function getScopeVariable($name) Chris@0: { Chris@0: return $this->context->get($name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get all scope variables from the current shell scope. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: protected function getScopeVariables() Chris@0: { Chris@0: return $this->context->getAll(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Given a Reflector instance, set command-scope variables in the shell Chris@0: * execution context. This is used to inject magic $__class, $__method and Chris@0: * $__file variables (as well as a handful of others). Chris@0: * Chris@0: * @param \Reflector $reflector Chris@0: */ Chris@0: protected function setCommandScopeVariables(\Reflector $reflector) Chris@0: { Chris@0: $vars = array(); Chris@0: Chris@0: switch (get_class($reflector)) { Chris@0: case 'ReflectionClass': Chris@0: case 'ReflectionObject': Chris@0: $vars['__class'] = $reflector->name; Chris@0: if ($reflector->inNamespace()) { Chris@0: $vars['__namespace'] = $reflector->getNamespaceName(); Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'ReflectionMethod': Chris@0: $vars['__method'] = sprintf('%s::%s', $reflector->class, $reflector->name); Chris@0: $vars['__class'] = $reflector->class; Chris@0: $classReflector = $reflector->getDeclaringClass(); Chris@0: if ($classReflector->inNamespace()) { Chris@0: $vars['__namespace'] = $classReflector->getNamespaceName(); Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'ReflectionFunction': Chris@0: $vars['__function'] = $reflector->name; Chris@0: if ($reflector->inNamespace()) { Chris@0: $vars['__namespace'] = $reflector->getNamespaceName(); Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'ReflectionGenerator': Chris@0: $funcReflector = $reflector->getFunction(); Chris@0: $vars['__function'] = $funcReflector->name; Chris@0: if ($funcReflector->inNamespace()) { Chris@0: $vars['__namespace'] = $funcReflector->getNamespaceName(); Chris@0: } Chris@0: if ($fileName = $reflector->getExecutingFile()) { Chris@0: $vars['__file'] = $fileName; Chris@0: $vars['__line'] = $reflector->getExecutingLine(); Chris@0: $vars['__dir'] = dirname($fileName); Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'ReflectionProperty': Chris@0: case 'Psy\Reflection\ReflectionConstant': Chris@0: $classReflector = $reflector->getDeclaringClass(); Chris@0: $vars['__class'] = $classReflector->name; Chris@0: if ($classReflector->inNamespace()) { Chris@0: $vars['__namespace'] = $classReflector->getNamespaceName(); Chris@0: } Chris@0: // no line for these, but this'll do Chris@0: if ($fileName = $reflector->getDeclaringClass()->getFileName()) { Chris@0: $vars['__file'] = $fileName; Chris@0: $vars['__dir'] = dirname($fileName); Chris@0: } Chris@0: break; Chris@0: } Chris@0: Chris@0: if ($reflector instanceof \ReflectionClass || $reflector instanceof \ReflectionFunctionAbstract) { Chris@0: if ($fileName = $reflector->getFileName()) { Chris@0: $vars['__file'] = $fileName; Chris@0: $vars['__line'] = $reflector->getStartLine(); Chris@0: $vars['__dir'] = dirname($fileName); Chris@0: } Chris@0: } Chris@0: Chris@0: $this->context->setCommandScopeVariables($vars); Chris@0: } Chris@0: }