Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Prevent code execution in constructor #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions ReactJS.php
Original file line number Diff line number Diff line change
@@ -38,7 +38,19 @@ class ReactJS {
* Custom error handler
* @var callable
*/
$errorHandler;
$errorHandler,

/**
* JS Code to execute only once
* @var string
*/
$bootstrap,

/**
* Has the bootstrap JS code been executed ?
* @var bool
*/
$booted = false;

/**
* Initialize by passing JS code as a string.
@@ -59,10 +71,9 @@ function __construct($libsrc, $appsrc) {
$react[] = $appsrc;
$react[] = ';';

$concatenated = implode(";\n", $react);
$this->bootstrap = implode(";\n", $react);

$this->v8 = new V8Js();
$this->executeJS($concatenated);
}

/**
@@ -152,6 +163,7 @@ function getJS($where, $return_var = null) {
*/
private function executeJS($js) {
try {
$this->bootIfRequired();
ob_start();
$this->v8->executeString($js);
return ob_get_clean();
@@ -168,4 +180,13 @@ private function executeJS($js) {
}
}

/**
* Execute the bootstrap code if it hasn't been yet
*/
private function bootIfRequired() {
if ($this->booted === false) {
$this->v8->executeString($this->bootstrap);
$this->booted = true;
}
}
}