|
16 | 16 | class ReactJS {
|
17 | 17 |
|
18 | 18 | private
|
19 |
| - /** |
20 |
| - * Concatenated React.js + Application code + boilerplate |
21 |
| - * @var string |
22 |
| - */ |
23 |
| - $react, |
24 |
| - |
25 | 19 | /**
|
26 | 20 | * Name of the component to render
|
27 | 21 | * @var string
|
@@ -64,9 +58,11 @@ function __construct($libsrc, $appsrc) {
|
64 | 58 | // app's components
|
65 | 59 | $react[] = $appsrc;
|
66 | 60 | $react[] = ';';
|
67 |
| - $this->react = implode(";\n", $react); |
68 |
| - |
| 61 | + |
| 62 | + $concatenated = implode(";\n", $react); |
| 63 | + |
69 | 64 | $this->v8 = new V8Js();
|
| 65 | + $this->executeJS($concatenated); |
70 | 66 | }
|
71 | 67 |
|
72 | 68 | /**
|
@@ -103,28 +99,14 @@ function setErrorHandler($err) {
|
103 | 99 | * @return string HTML string
|
104 | 100 | */
|
105 | 101 | function getMarkup() {
|
106 |
| - try { |
107 |
| - $js = $this->react; |
108 |
| - $js.= sprintf( |
109 |
| - "print(React.renderToString(React.createElement(%s, %s)))", |
110 |
| - $this->component, |
111 |
| - $this->data); |
112 |
| - |
113 |
| - ob_start(); |
114 |
| - $this->v8->executeString($js); |
115 |
| - return ob_get_clean(); |
| 102 | + $js = sprintf( |
| 103 | + "print(React.renderToString(React.createElement(%s, %s)))", |
| 104 | + $this->component, |
| 105 | + $this->data); |
116 | 106 |
|
117 |
| - } catch (V8JsException $e) { |
118 |
| - if ($this->errorHandler) { |
119 |
| - call_user_func($this->errorHandler, $e); |
120 |
| - } else { |
121 |
| - // default error handler blows up bad |
122 |
| - echo "<pre>"; |
123 |
| - echo $e->getMessage(); |
124 |
| - echo "</pre>"; |
125 |
| - die(); |
126 |
| - } |
127 |
| - } |
| 107 | + ob_start(); |
| 108 | + $this->executeJS($js); |
| 109 | + return ob_get_clean(); |
128 | 110 | }
|
129 | 111 |
|
130 | 112 | /**
|
@@ -163,5 +145,28 @@ function getJS($where, $return_var = null) {
|
163 | 145 | $where
|
164 | 146 | );
|
165 | 147 | }
|
| 148 | + |
| 149 | + /** |
| 150 | + * Executes Javascript using V8JS, with primitive exception handling |
| 151 | + * |
| 152 | + * @param string $js JS code to be executed |
| 153 | + * @return mixed |
| 154 | + */ |
| 155 | + private function executeJS($js) { |
| 156 | + try { |
| 157 | + return $this->v8->executeString($js); |
| 158 | + } catch (V8JsException $e) { |
| 159 | + if ($this->errorHandler) { |
| 160 | + call_user_func($this->errorHandler, $e); |
| 161 | + } else { |
| 162 | + // default error handler blows up bad |
| 163 | + echo "<pre>"; |
| 164 | + echo $e->getMessage(); |
| 165 | + echo "</pre>"; |
| 166 | + die(); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
166 | 171 | }
|
167 | 172 |
|
0 commit comments