Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php

/* vim: set shiftwidth=2 expandtab softtabstop=2: */

namespace Boris;

/**
 * Boris is a tiny REPL for PHP.
 */
class Boris {
  const VERSION = "1.0.8";

  private $_prompt;
  private $_historyFile;
  private $_exports = array();
  private $_startHooks = array();
  private $_failureHooks = array();
  private $_inspector;

  /**
   * Create a new REPL, which consists of an evaluation worker and a readline client.
   *
   * @param string $prompt, optional
   * @param string $historyFile, optional
   */
  public function __construct($prompt = 'boris> ', $historyFile = null) {
    $this->setPrompt($prompt);
    $this->_historyFile = $historyFile
      ? $historyFile
      : sprintf('%s/.boris_history', getenv('HOME'))
      ;
    $this->_inspector = new ColoredInspector();
  }

  /**
   * Add a new hook to run in the context of the REPL when it starts.
   *
   * @param mixed $hook
   *
   * The hook is either a string of PHP code to eval(), or a Closure accepting
   * the EvalWorker object as its first argument and the array of defined
   * local variables in the second argument.
   *
   * If the hook is a callback and needs to set any local variables in the
   * REPL's scope, it should invoke $worker->setLocal($var_name, $value) to
   * do so.
   *
   * Hooks are guaranteed to run in the order they were added and the state
   * set by each hook is available to the next hook (either through global
   * resources, such as classes and interfaces, or through the 2nd parameter
   * of the callback, if any local variables were set.
   *
   * @example Contrived example where one hook sets the date and another
   *          prints it in the REPL.
   *
   *   $boris->onStart(function($worker, $vars){
   *     $worker->setLocal('date', date('Y-m-d'));
   *   });
   *
   *   $boris->onStart('echo "The date is $date\n";');
   */
  public function onStart($hook) {
    $this->_startHooks[] = $hook;
  }

  /**
   * Add a new hook to run in the context of the REPL when a fatal error occurs.
   *
   * @param mixed $hook
   *
   * The hook is either a string of PHP code to eval(), or a Closure accepting
   * the EvalWorker object as its first argument and the array of defined
   * local variables in the second argument.
   *
   * If the hook is a callback and needs to set any local variables in the
   * REPL's scope, it should invoke $worker->setLocal($var_name, $value) to
   * do so.
   *
   * Hooks are guaranteed to run in the order they were added and the state
   * set by each hook is available to the next hook (either through global
   * resources, such as classes and interfaces, or through the 2nd parameter
   * of the callback, if any local variables were set.
   *
   * @example An example if your project requires some database connection cleanup:
   *
   *   $boris->onFailure(function($worker, $vars){
   *     DB::reset();
   *   });
   */
  public function onFailure($hook){
    $this->_failureHooks[] = $hook;
  }

  /**
   * Set a local variable, or many local variables.
   *
   * @example Setting a single variable
   *   $boris->setLocal('user', $bob);
   *
   * @example Setting many variables at once
   *   $boris->setLocal(array('user' => $bob, 'appContext' => $appContext));
   *
   * This method can safely be invoked repeatedly.
   *
   * @param array|string $local
   * @param mixed $value, optional
   */
  public function setLocal($local, $value = null) {
    if (!is_array($local)) {
      $local = array($local => $value);
    }

    $this->_exports = array_merge($this->_exports, $local);
  }

  /**
   * Sets the Boris prompt text
   *
   * @param string $prompt
   */
  public function setPrompt($prompt) {
    $this->_prompt = $prompt;
  }

  /**
   * Set an Inspector object for Boris to output return values with.
   *
   * @param object $inspector any object the responds to inspect($v)
   */
  public function setInspector($inspector) {
    $this->_inspector = $inspector;
  }

  /**
   * Start the REPL (display the readline prompt).
   *
   * This method never returns.
   */
  public function start() {
    declare(ticks = 1);
    pcntl_signal(SIGINT, SIG_IGN, true);

    if (!$pipes = stream_socket_pair(
      STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP)) {
      throw new \RuntimeException('Failed to create socket pair');
    }

    $pid = pcntl_fork();

    if ($pid > 0) {
      if (function_exists('setproctitle')) {
        setproctitle('boris (master)');
      }

      fclose($pipes[0]);
      $client = new ReadlineClient($pipes[1]);
      $client->start($this->_prompt, $this->_historyFile);
    } elseif ($pid < 0) {
      throw new \RuntimeException('Failed to fork child process');
    } else {
      if (function_exists('setproctitle')) {
        setproctitle('boris (worker)');
      }

      fclose($pipes[1]);
      $worker = new EvalWorker($pipes[0]);
      $worker->setLocal($this->_exports);
      $worker->setStartHooks($this->_startHooks);
      $worker->setFailureHooks($this->_failureHooks);
      $worker->setInspector($this->_inspector);
      $worker->start();
    }
  }
}

Commits for Nextrek/Aiba_backup/vendor/d11wtq/boris/lib/Boris/Boris.php

Diff revisions: vs.
Revision Author Commited Message
1464 MOliva picture MOliva Tue 13 Oct, 2020 11:16:56 +0000