Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

v1.0.1 #5

Merged
merged 2 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
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
25 changes: 17 additions & 8 deletions src/ReduxReactJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@ final class ReduxReactJs implements ReduxReactJsInterface
private $v8;

/**
* @param string $reactBundleSrc Bundled code include React, ReactDom, and Redux
* @param string $appBundleSrc Bundled application code
* @param ExceptionHandler $handler V8Js exception handler
* @param string $reactBundleSrc
* @param string $appBundleSrc
* @param ExceptionHandlerInterface $handler
*/
public function __construct(string $reactBundleSrc, string $appBundleSrc, ExceptionHandler $handler, V8Js $v8Js)

/**
* ReduxReactJs constructor.
*
* @param string $reactBundleSrc Bundled code include React, ReactDom, and Redux
* @param string $appBundleSrc Bundled application code
* @param ExceptionHandlerInterface $handler
* @param V8Js $v8Js V8Js exception handler
*/
public function __construct(string $reactBundleSrc, string $appBundleSrc, ExceptionHandlerInterface $handler, V8Js $v8Js)
{
$this->reactBundleSrc = $reactBundleSrc;
$this->appBundleSrc = $appBundleSrc;
Expand All @@ -53,10 +62,10 @@ public function __invoke(string $rootContainer, array $store, string $id) : View
$view = new View;
try {
$v8 = $this->v8->executeString($code);
$view->html = $v8->html;
$view->markup = $v8->markup;
} catch (\V8JsScriptException $e) {
$this->handler->__invoke($e);
$view->html = '';
$view->markup = '';
}
$view->js = "ReactDOM.render(React.createElement(Provider,{store:configureStore($storeJson)},React.createElement(App)),document.getElementById('{$id}'));";

Expand All @@ -73,8 +82,8 @@ private function getServerSideRenderingCode(string $rootContainer, string $store
var React = global.React, ReactDOM = global.ReactDOM, ReactDOMServer = global.ReactDOMServer;
{$this->appBundleSrc}
var Provider = global.Provider, configureStore = global.configureStore, App = global.{$rootContainer};
var html = ReactDOMServer.renderToString(React.createElement(Provider, { store: configureStore({$storeJson}) }, React.createElement(App)));
tmp = {html: html};
var markup = ReactDOMServer.renderToString(React.createElement(Provider, { store: configureStore({$storeJson}) }, React.createElement(App)));
tmp = {markup: markup};
EOT;

return $code;
Expand Down
6 changes: 3 additions & 3 deletions tests/ReduxReactJsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function testInvoke()
{
$state = ['hello' => ['message' => 'Hello SSR !']];
$view = $this->ssr->__invoke('App', $state, 'root');
$this->assertStringStartsWith('<div data-reactroot=', $view->html);
$this->assertContains('<h1 data-reactid="3">Hello SSR !</h1>', $view->html);
$this->assertStringStartsWith('<div data-reactroot=', $view->markup);
$this->assertContains('<h1 data-reactid="3">Hello SSR !</h1>', $view->markup);
$this->assertSame('ReactDOM.render(React.createElement(Provider,{store:configureStore({"hello":{"message":"Hello SSR !"}})},React.createElement(App)),document.getElementById(\'root\'));', $view->js);
}

Expand All @@ -47,6 +47,6 @@ public function testInvalidValue()
{
$state = ['hello' => ['__INVALID__' => 'Hello SSR !']];
$view = @$this->ssr->__invoke('_INVALID_', $state, 'root');
$this->assertSame('', $view->html);
$this->assertSame('', $view->markup);
}
}