|
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,12 @@ 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 | + return $this->executeJS($js); |
128 | 108 | } |
129 | 109 |
|
130 | 110 | /** |
@@ -163,5 +143,30 @@ function getJS($where, $return_var = null) { |
163 | 143 | $where |
164 | 144 | ); |
165 | 145 | } |
| 146 | + |
| 147 | + /** |
| 148 | + * Executes Javascript using V8JS, with primitive exception handling |
| 149 | + * |
| 150 | + * @param string $js JS code to be executed |
| 151 | + * @return string The execution response |
| 152 | + */ |
| 153 | + private function executeJS($js) { |
| 154 | + try { |
| 155 | + ob_start(); |
| 156 | + $this->v8->executeString($js); |
| 157 | + return ob_get_clean(); |
| 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