ExecutionLoop.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy;
  11. use Psy\Exception\ErrorException;
  12. /**
  13. * The Psy Shell execution loop.
  14. */
  15. class ExecutionLoop
  16. {
  17. /**
  18. * Run the execution loop.
  19. *
  20. * @throws ThrowUpException if thrown by the `throw-up` command
  21. *
  22. * @param Shell $shell
  23. */
  24. public function run(Shell $shell)
  25. {
  26. $this->loadIncludes($shell);
  27. $closure = new ExecutionLoopClosure($shell);
  28. $closure->execute();
  29. }
  30. /**
  31. * Load user-defined includes.
  32. *
  33. * @param Shell $shell
  34. */
  35. protected function loadIncludes(Shell $shell)
  36. {
  37. // Load user-defined includes
  38. $load = function (Shell $__psysh__) {
  39. \set_error_handler([$__psysh__, 'handleError']);
  40. foreach ($__psysh__->getIncludes() as $__psysh_include__) {
  41. try {
  42. include $__psysh_include__;
  43. } catch (\Error $_e) {
  44. $__psysh__->writeException(ErrorException::fromError($_e));
  45. } catch (\Exception $_e) {
  46. $__psysh__->writeException($_e);
  47. }
  48. }
  49. \restore_error_handler();
  50. unset($__psysh_include__);
  51. // Override any new local variables with pre-defined scope variables
  52. \extract($__psysh__->getScopeVariables(false));
  53. // ... then add the whole mess of variables back.
  54. $__psysh__->setScopeVariables(\get_defined_vars());
  55. };
  56. $load($shell);
  57. }
  58. }