8000 initial commit · reactjs/react-php-v8js@7d1d5d0 · GitHub
[go: up one dir, main page]

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

Commit 7d1d5d0

Browse files
committed
initial commit
0 parents  commit 7d1d5d0

File tree

6 files changed

+377
-0
lines changed

6 files changed

+377
-0
lines changed

LICENSE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
BSD License for React-PHP-V8Js
2+
3+
Copyright (c) 2014, Facebook, Inc. All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification,
6+
are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
* Neither the name Facebook nor the names of its contributors may be used to
16+
endorse or promote products derived from this software without specific
17+
prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PATENTS

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Additional Grant of Patent Rights
2+
3+
“Software” means the React-PHP-V8Js software distributed by Facebook, Inc.
4+
5+
Facebook hereby grants you a perpetual, worldwide, royalty-free, non-exclusive,
6+
irrevocable (subject to the termination provision below) license under any
7+
rights in any patent claims owned by Facebook, to make, have made, use, sell,
8+
offer to sell, import, and otherwise transfer the Software. For avoidance of
9+
doubt, no license is granted under Facebook’s rights in any patent claims that
10+
are infringed by (i) modifications to the Software made by you or a third party,
11+
or (ii) the Software in combination with any software or other technology
12+
provided by you or a third party.
13+
14+
The license granted hereunder will terminate, automatically and without notice,
15+
for anyone that makes any claim (including by filing any lawsuit, assertion or
16+
other action) alleging (a) direct, indirect, or contributory infringement or
17+
inducement to infringe any patent: (i) by Facebook or any of its subsidiaries or
18+
affiliates, whether or not such claim is related to the Software, (ii) by any
19+
party if such claim arises in whole or in part from any software, product or
20+
service of Facebook or any of its subsidiaries or affiliates, whether or not
21+
such claim is related to the Software, or (iii) by any party relating to the
22+
Software; or (b) that any right in any patent claim of Facebook is invalid or
23+
unenforceable.

README

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
React-PHP-V8Js
2+
===========
3+
4+
React-PHP-V8Js is an experimental library that uses the power of Facebook's
5+
[React](http://facebook.github.io/react/) library to render UI components
6+
on the server-side with PHP as well as on the client.
7+
8+
If you want to render on the server-side with Node.js please see the
9+
[react-page project](https://github.com/facebook/react-page)
10+
11+
Prerequisites
12+
===========
13+
* Server running PHP 5.3.3+
14+
* [V8Js PHP extension](http://php.net/v8js)
15+
16+
For a walkthrough how to setup the requirements on a vanilla MacOS, [see
17+
here](http://www.phpied.com/installing-v8js-for-php-on-a-mac/)
18+
19+
Usage
20+
===========
21+
```php
22+
// the library
23+
$react_source = file_get_contents('/path/to/build/react.js');
24+
// all custom code concatenated
25+
$app_source = file_get_contents('/path/to/custom/components.js');
26+
27+
$rjs = new ReactJS($react_source, $app_source);
28+
$rjs->setComponent('MyComponent', array(
29+
'any': 1,
30+
'props': 2));
31+
32+
/// ...
33+
34+
// print rendered markup
35+
echo '<div id="here">' . $rjs->getMarkup() . '</div>';
36+
37+
/// ...
38+
39+
// load JavaScript somehow - concatenated, from CDN, etc
40+
// including react.js and custom/components.js
41+
42+
// init client
43+
echo '<script>' . $rjs->getJS("#here") . '</script>';
44+
45+
/// ...
46+
47+
// repeat setComponent(), getMarkup(), getJS() as necessary
48+
// to render more components
49+
```
50+
51+
License
52+
=======
53+
BSD License for React-PHP-V8Js
54+
55+
Copyright (c) 2014, Facebook, Inc. All rights reserved.
56+
57+
Redistribution and use in source and binary forms, with or without modification,
58+
are permitted provided that the following conditions are met:
59+
60+
* Redistributions of source code must retain the above copyright notice, this
61+
list of conditions and the following disclaimer.
62+
* Redistributions in binary form must reproduce the above copyright notice,
63+
this list of conditions and the following disclaimer in the documentation
64+
and/or other materials provided with the distribution.
65+
* Neither the name Facebook nor the names of its contributors may be used to
66+
endorse or promote products derived from this software without specific
67+
prior written permission.
68+
69+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
70+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
71+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
73+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
74+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
75+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
76+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
77+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
78+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
79+

ReactJS.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
/**!
3+
* Copyright (c) 2014, Facebook, Inc.
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under the BSD-style license found in the
7+
* LICENSE file in the root directory of this source tree. An additional grant
8+
* of patent rights can be found in the PATENTS file in the same directory.
9+
*/
10+
11+
/**
12+
* A PHP class to render React components on the server
13+
* and then hook the components on the client side.
14+
* Requires V8JS PHP extension: http://php.net/v8js
15+
*/
16+
class ReactJS {
17+
18+
private
19+
/**
20+
* Concatenated React.js + Application code + boilerplate
21+
* @var string
22+
*/
23+
$react,
24+
25+
/**
26+
* Name of the component to render
27+
* @var string
28+
*/
29+
$component,
30+
31+
/**
32+
* Properties that go along with the component
33+
* @var mixed
34+
*/
35+
$data = null,
36+
37+
/**
38+
* Instance of V8Js class
39+
* @var object
40+
*/
41+
$v8,
42+
43+
/**
44+
* Custom error handler
45+
* @var callable
46+
*/
47+
$errorHandler;
48+
49+
/**
50+
* Initialize by passing JS code as a string.
51+
* The application source code is concatenated string
52+
* of all custom components and app code
53+
*
54+
* @param string $libsrc React's source code
55+
* @param string $appsrc Application source code
56+
*/
57+
function __construct($libsrc, $appsrc) {
58+
$react = array();
59+
// stubs, react
60+
$react[] = "var console = {warn: function(){}, error: print}";
61+
$react[] = "var global = {}";
62+
$react[] = $libsrc;
63+
$react[] = "var React = global.React";
64+
// app's components
65+
$react[] = $appsrc;
66+
$react[] = ';';
67+
$this->react = implode(";\n", $react);
68+
69+
$this->v8 = new V8Js();
70+
}
71+
72+
/**
73+
* Which components is to be rendered along with it's data
74+
* E.g.
75+
* $rjs->setComponent('MyTable', array('content' => $q4_results));
76+
*
77+
* @param string $component Component name
78+
* @param mixed $data Any type of data to be passed as params
79+
* when initializing the component. Optional.
80+
* @return object $this instance
81+
*/
82+
function setComponent($component, $data = null) {
83+
$this->component = $component;
84+
$this->data = json_encode($data);
85+
return $this;
86+
}
87+
88+
/**
89+
* Custom error handler. The default one var_dumps the exception
90+
* and die()s.
91+
*
92+
* @param callable $err Callback passed to call_user_func()
93+
* @return object $this instance
94+
*/
95+
function setErrorHandler($err) {
96+
$this->errorHandler = $err;
97+
return $this;
98+
}
99+
100+
/**
101+
* Returns the markup to print to the page
102+
*
103+
* @return string HTML string
104+
*/
105+
function getMarkup() {
106+
try {
107+
$js = $this->react;
108+
$js.= sprintf(
109+
"React.renderComponentToString(%s(%s), print)",
110+
$this->component,
111+
$this->data);
112+
113+
ob_start();
114+
$this->v8->executeString($js);
115+
return ob_get_clean();
116+
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+
var_dump($e);
124+
echo "</pre>";
125+
die();
126+
}
127+
}
128+
}
129+
130+
/**
131+
* Returns JS to be inlined in the page (without <script> tags)
132+
* It instantiates the client side, once the JS arrives
133+
*
134+
* NOTE: This class makes no attempt to load files JS so you can load it
135+
* however is appropriate - from a CDN, asynchronously, etc.
136+
*
137+
* e.g. getJS('document.body');
138+
* renders in body and doesn't retain a var
139+
* e.g. getJS('#page', "GLOB");
140+
* renders in element id="page" and assigns the component to
141+
* a JavaScript variable named GLOB for later use if needed
142+
* @param string $where A reference to a DOM object, or an ID
143+
* for convenience if prefixed by a #. E.g. "#page"
144+
* It will be passed to document.getElementById('page')
145+
* @param string $return_var Optional name of JS variable to be assigned to
146+
* the rendered component
147+
* @return string JS code
148+
*/
149+
function getJS($where, $return_var = null) {
150+
// special case for IDs
151+
if (substr($where, 0, 1) === '#') {
152+
$where = sprintf(
153+
'document.getElementById("%s")',
154+
substr($where, 1)
155+
);
156+
}
157+
return
158+
($return_var ? "var $return_var = " : "") .
159+
sprintf(
160+
"React.renderComponent(%s(%s), %s);",
161+
$this->component,
162+
$this->data,
163+
$where
164+
);
165+
}
166+
}
167+

example/example.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**!
3+
* Copyright (c) 2014, Facebook, Inc.
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under the BSD-style license found in the
7+
* LICENSE file in the root directory of this source tree. An additional grant
8+
* of patent rights can be found in the PATENTS file in the same directory.
9+
*/
10+
11+
/**
12+
* Example of using the ReactJS class
13+
*/
14+
include '../ReactJS.php';
15+
16+
$rjs = new ReactJS(
17+
file_get_contents('../../react/build/react.js'), // location of React's code
18+
file_get_contents('table.js')); // app code
19+
20+
// data to be passed to the component
21+
$data =
22+
array('data' => array(
23+
array(1, 2, 3),
24+
array(4, 5, 6),
25+
array(7, 8, 9)
26+
));
27+
28+
// set the component and its data
29+
// after this you can call getMarkup()/getJS()
30+
// Then you can set another component to render
31+
// and do that as many times as the components you need
32+
// all the while reusing the $rjs instance
33+
$rjs->setComponent('Table', $data);
34+
?>
35+
<!doctype html>
36+
<html>
37+
<head>
38+
<title>React page</title>
39+
<!-- css and stuff -->
40+
</head>
41+
<body>
42+
43+
<!-- render server content here -->
44+
<div id="page"><?php echo $rjs->getMarkup(); ?></div>
45+
46+
<!-- load react and app code -->
47+
<script src="react/build/react.min.js"></script>
48+
<script src="react/build/table.js"></script>
49+
50+
<script>
51+
// client init/render
52+
// this is a straight echo of the JS because the JS resources
53+
// were loaded synchronously
54+
// You may want to load JS async and wrap the return of getJS()
55+
// in a function you can call later
56+
<?php echo $rjs->getJS('#page', "GLOB"); ?>
57+
</script>
58+
</body>
59+
</html>
60+

example/table.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**!
2+
* Copyright (c) 2014, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
var Table = React.createClass({
10+
render: function () {
11+
return (
12+
React.DOM.table(null, React.DOM.tbody(null,
13+
this.props.data.map(function (row) {
14+
return (
15+
React.DOM.tr(null,
16+
row.map(function (cell) {
17+
return React.DOM.td(null, cell);
18+
})));
19+
}))));
20+
}});

0 commit comments

Comments
 (0)
0