comparison vendor/psy/psysh/src/Shell.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 5fb285c0d0e3
children 129ea1e6d783
comparison
equal deleted inserted replaced
15:e200cb7efeb3 16:c2387f117808
45 * 45 *
46 * @author Justin Hileman <justin@justinhileman.info> 46 * @author Justin Hileman <justin@justinhileman.info>
47 */ 47 */
48 class Shell extends Application 48 class Shell extends Application
49 { 49 {
50 const VERSION = 'v0.9.3'; 50 const VERSION = 'v0.9.6';
51 51
52 const PROMPT = '>>> '; 52 const PROMPT = '>>> ';
53 const BUFF_PROMPT = '... '; 53 const BUFF_PROMPT = '... ';
54 const REPLAY = '--> '; 54 const REPLAY = '--> ';
55 const RETVAL = '=> '; 55 const RETVAL = '=> ';
116 * Invoke a Psy Shell from the current context. 116 * Invoke a Psy Shell from the current context.
117 * 117 *
118 * @see Psy\debug 118 * @see Psy\debug
119 * @deprecated will be removed in 1.0. Use \Psy\debug instead 119 * @deprecated will be removed in 1.0. Use \Psy\debug instead
120 * 120 *
121 * @param array $vars Scope variables from the calling context (default: array()) 121 * @param array $vars Scope variables from the calling context (default: array())
122 * @param object $boundObject Bound object ($this) value for the shell 122 * @param object|string $bindTo Bound object ($this) or class (self) value for the shell
123 * 123 *
124 * @return array Scope variables from the debugger session 124 * @return array Scope variables from the debugger session
125 */ 125 */
126 public static function debug(array $vars = [], $boundObject = null) 126 public static function debug(array $vars = [], $bindTo = null)
127 { 127 {
128 return \Psy\debug($vars, $boundObject); 128 return \Psy\debug($vars, $bindTo);
129 } 129 }
130 130
131 /** 131 /**
132 * Adds a command object. 132 * Adds a command object.
133 * 133 *
390 throw new BreakException('Ctrl+D'); 390 throw new BreakException('Ctrl+D');
391 } 391 }
392 } 392 }
393 393
394 // handle empty input 394 // handle empty input
395 if (trim($input) === '') { 395 if (trim($input) === '' && !$this->codeBufferOpen) {
396 continue; 396 continue;
397 } 397 }
398 398
399 $input = $this->onInput($input); 399 $input = $this->onInput($input);
400 400
401 if ($this->hasCommand($input)) { 401 // If the input isn't in an open string or comment, check for commands to run.
402 if ($this->hasCommand($input) && !$this->inputInOpenStringOrComment($input)) {
402 $this->addHistory($input); 403 $this->addHistory($input);
403 $this->runCommand($input); 404 $this->runCommand($input);
404 405
405 continue; 406 continue;
406 } 407 }
407 408
408 $this->addCode($input); 409 $this->addCode($input);
409 } while (!$this->hasValidCode()); 410 } while (!$this->hasValidCode());
411 }
412
413 /**
414 * Check whether the code buffer (plus current input) is in an open string or comment.
415 *
416 * @param string $input current line of input
417 *
418 * @return bool true if the input is in an open string or comment
419 */
420 private function inputInOpenStringOrComment($input)
421 {
422 if (!$this->hasCode()) {
423 return;
424 }
425
426 $code = $this->codeBuffer;
427 array_push($code, $input);
428 $tokens = @token_get_all('<?php ' . implode("\n", $code));
429 $last = array_pop($tokens);
430
431 return $last === '"' || $last === '`' ||
432 (is_array($last) && in_array($last[0], [T_ENCAPSED_AND_WHITESPACE, T_START_HEREDOC, T_COMMENT]));
410 } 433 }
411 434
412 /** 435 /**
413 * Run execution loop listeners before the shell session. 436 * Run execution loop listeners before the shell session.
414 */ 437 */
583 * @return object|null 606 * @return object|null
584 */ 607 */
585 public function getBoundObject() 608 public function getBoundObject()
586 { 609 {
587 return $this->context->getBoundObject(); 610 return $this->context->getBoundObject();
611 }
612
613 /**
614 * Set the bound class (self) for the interactive shell.
615 *
616 * @param string|null $boundClass
617 */
618 public function setBoundClass($boundClass)
619 {
620 $this->context->setBoundClass($boundClass);
621 }
622
623 /**
624 * Get the bound class (self) for the interactive shell.
625 *
626 * @return string|null
627 */
628 public function getBoundClass()
629 {
630 return $this->context->getBoundClass();
588 } 631 }
589 632
590 /** 633 /**
591 * Add includes, to be parsed and executed before running the interactive shell. 634 * Add includes, to be parsed and executed before running the interactive shell.
592 * 635 *
943 $message = sprintf('%s with message \'%s\'', get_class($e), $message); 986 $message = sprintf('%s with message \'%s\'', get_class($e), $message);
944 } 987 }
945 } 988 }
946 989
947 $message = preg_replace( 990 $message = preg_replace(
948 "#(\\w:)?(/\\w+)*/src/ExecutionClosure.php\(\d+\) : eval\(\)'d code#", 991 "#(\\w:)?(/\\w+)*/src/Execution(?:Loop)?Closure.php\(\d+\) : eval\(\)'d code#",
949 "eval()'d code", 992 "eval()'d code",
950 str_replace('\\', '/', $message) 993 str_replace('\\', '/', $message)
951 ); 994 );
952 995
953 $message = str_replace(" in eval()'d code", ' in Psy Shell code', $message); 996 $message = str_replace(" in eval()'d code", ' in Psy Shell code', $message);
1087 * 1130 *
1088 * @return bool True if the shell has a command for the given input 1131 * @return bool True if the shell has a command for the given input
1089 */ 1132 */
1090 protected function hasCommand($input) 1133 protected function hasCommand($input)
1091 { 1134 {
1092 $input = new StringInput($input); 1135 if (preg_match('/([^\s]+?)(?:\s|$)/A', ltrim($input), $match)) {
1093 if ($name = $input->getFirstArgument()) { 1136 return $this->has($match[1]);
1094 return $this->has($name);
1095 } 1137 }
1096 1138
1097 return false; 1139 return false;
1098 } 1140 }
1099 1141