Skip to content

Commit

Permalink
Merge pull request #1 from bearsunday/wip
Browse files Browse the repository at this point in the history
ReduxModule
  • Loading branch information
koriym authored Oct 25, 2016
2 parents 8e61456 + c0af4a7 commit f77a7d0
Show file tree
Hide file tree
Showing 23 changed files with 966 additions and 1,694 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ nbproject
.idea
.project
.settings
build/
node_modules/
vendor/
composer.lock
84 changes: 42 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,79 @@
composer require bear/reactjs-module
```

### Redux React Application

You need two js bundled file. The one is an application bundled file `{app_name}.bundle.js` and react bundled js file `react.bundle.js`.
For instance, you named application simply 'app'. You need `app.bundle.js` and `react.bundle.js`

### Module Install

```php
$baseDir = dirname(dirname(__DIR__));
$reactLibSrc = $baseDir . '/var/www/build/react.bundle.js';
$reactAppSrc = $baseDir . '/var/www/build/app.bundle.js';
$this->install(new ReactJsModule($reactLibSrc, $reactAppSrc));
$baseDir = dirname(__DIR__, 2);
$this->install(new ReduxModule($baseDir, 'app');
```

### Redux React Application

You need two js code. One is a [library](https://github.com/koriym/Koriym.ReduxReactSsr/blob/1.x/example/webpack.config.js#L7) ([react.js](https://github.com/koriym/Koriym.ReduxReactSsr/blob/1.x/example/common/react.js)) and the other one is [concatenated all custom code](https://github.com/koriym/Koriym.ReduxReactSsr/blob/1.x/example/webpack.config.js#L8) ([app.js](https://github.com/koriym/Koriym.ReduxReactSsr/blob/1.x/example/common/app.js)). See [example](https://github.com/koriym/Koriym.ReduxReactSsr/tree/1.x/example) application code and [how to install](https://github.com/koriym/Koriym.ReduxReactSsr#run-example) it.

### ResourceOjbect

Use server-side renderer with `ReduxSsr `trait.
Set `Redux Server Side Renderer` with named binding. The biding name format is `redux_{app_name}`.

```php
use BEAR\ReactJsModule\ReduxSsr;
use BEAR\Resource\Annotation\Embed;

use BEAR\Resource\RenderInterface;
use BEAR\Resource\ResourceObject;
use Ray\Di\Di\Inject;
use Ray\Di\Di\Named;

class Restx extends ResourceObject
class Greeting extends ResourceObject
{
use ReduxSsr;
/**
* @Inject
* @Named("redux_app")
*/
public function setRenderer(RenderInterface $renderer)
{
$this->renderer = $renderer;
}

public function onGet(string $id): ResourceObject
public function onGet()
{
// set initial state to $body property
// ...
$this->body = [
'user' => $userState,
'entries' => $entriesState,
'comments' => $commentState,
'filter' => $filterState
'title' => 'Greeting',
'hello' => ['message' => 'konichiwa']
];

return $this;
}
}


```

### Template

We need php template code. For exapmle, `Index.php` page resource needs `Index.html.php` template file.

You can get the value of body by `escape()` or `raw()`.

```php
/** @var $ssr \BEAR\ReactJsModule\ReduxSsrInterface */
/** @var $ro \BEAR\Resource\ResourceObject*/

$title = htmlspecialchars($ro->body['title'], ENT_QUOTES, 'UTF-8');
<?php

// render with initial state
$state = $ro->body;
list($html, $js) = $ssr('App', $state);
/* @var $ssr \BEAR\ReactJsModule\Ssr */
list($markup, $script) = $ssr->render(['hello']);

return <<<"EOD"
<!DOCTYPE html>
return <<<"EOT"
<!doctype>
<html>
<head>
<title>{$title}</title>
</head>
<body>
<!-- rendered markup -- >
<div id="root">{$html}</div>

<!-- init client -- >
{$js}
<script src="build/react.bundle.js"></script>
<script src="build/client.bundle.js"></script>
</body>
<head>
<title>{$ssr->escape('title')}</title>
</head>
<body>
<div id="root">{$markup}</div>
<script src="build/react.bundle.js"></script>
<script src="build/app.bundle.js"></script>
<script>{$script}</script>
</body>
</html>
EOD;
EOT;

```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"require": {
"php": ">=7.0.0",
"bear/resource": "^1.2",
"koriym/redux-react-ssr": "^0.1"
"koriym/redux-react-ssr": "^0.2"
},
"require-dev": {
"phpunit/phpunit": "^5.3"
Expand Down
20 changes: 20 additions & 0 deletions docs/demo/Greeting.html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use BEAR\ReactJsModule\Ssr;
/* @var $ssr Ssr */
list($markup, $script) = $ssr->render(['hello']);

return <<<"EOT"
<!doctype>
<html>
<head>
<title>{$ssr->escape('title')}</title>
</head>
<body>
<div id="root">{$markup}</div>
<script src="build/react.bundle.js"></script>
<script src="build/app.bundle.js"></script>
<script>{$script}</script>
</body>
</html>
EOT;
28 changes: 28 additions & 0 deletions docs/demo/Greeting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use BEAR\Resource\RenderInterface;
use BEAR\Resource\ResourceObject;
use Ray\Di\Di\Inject;
use Ray\Di\Di\Named;

class Greeting extends ResourceObject
{
/**
* @Inject
* @Named("redux_app")
*/
public function setRenderer(RenderInterface $renderer)
{
$this->renderer = $renderer;
}

public $body = [
'title' => 'Greeting',
'hello' => ['message' => 'konichiwa']
];

public function onGet()
{
return $this;
}
}
5 changes: 5 additions & 0 deletions docs/demo/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"autoload": {
"files": ["Greeting.php"]
}
}
14 changes: 14 additions & 0 deletions docs/demo/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require __DIR__ . '/vendor/autoload.php';
require dirname(__DIR__, 2) . '/vendor/autoload.php';

use BEAR\ReactJsModule\ReduxModule;
use BEAR\Resource\ResourceObject;
use Ray\Di\Injector;

$injector = new Injector(new ReduxModule(__DIR__ . '/build', 'app'));
$index = $injector->getInstance(Greeting::class);
/* @var $index ResourceObject */

echo $index;
11 changes: 11 additions & 0 deletions src/Exception/BodyKeyNotExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* This file is part of the BEAR\ReactJsModule package
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace BEAR\ReactJsModule\Exception;

class BodyKeyNotExistsException extends LogicException
{
}
11 changes: 11 additions & 0 deletions src/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* This file is part of the BEAR\ReactJsModule package
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace BEAR\ReactJsModule\Exception;

class LogicException extends \RuntimeException
{
}
3 changes: 3 additions & 0 deletions src/ReactJsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Ray\Di\AbstractModule;
use Ray\Di\Scope;

/**
* @deprecated
*/
class ReactJsModule extends AbstractModule
{
/**
Expand Down
53 changes: 53 additions & 0 deletions src/ReduxModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* This file is part of the BEAR\ReactJsModule package
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace BEAR\ReactJsModule;

use BEAR\Resource\RenderInterface;
use Ray\Di\AbstractModule;

class ReduxModule extends AbstractModule
{
/**
* @var string
*/
private $name;

/**
* @var string
*/
private $reactBundleSrc;

/**
* @var string
*/
private $appBundleSrc;

/**
* @param string $buildPath
* @param string $name
*/
public function __construct(string $buildPath, string $name, AbstractModule $module = null)
{
$this->name = $name;
$this->buildPath = $buildPath;
$this->reactBundleSrc = file_get_contents("{$buildPath}/react.bundle.js");
$this->appBundleSrc = file_get_contents("{$buildPath}/{$name}.bundle.js");
parent::__construct($module);
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->bind()->annotatedWith('react_bundle_src')->toInstance($this->reactBundleSrc);
$this->bind()->annotatedWith('app_bundle_src_' . $this->name)->toInstance($this->appBundleSrc);
$this->bind()->annotatedWith('redux_app_name_' . $this->name)->toInstance($this->name);
$name = "appName=redux_app_name_{$this->name},reactBundleSrc=react_bundle_src,appBundleSrc=app_bundle_src_{$this->name}";
$this->bind(RenderInterface::class)->annotatedWith('redux_' . $this->name)->toConstructor(ReduxRenderer::class, $name);
}
}
55 changes: 55 additions & 0 deletions src/ReduxRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);

/**
* This file is part of the BEAR\ReactJsModule package
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace BEAR\ReactJsModule;

use BEAR\Resource\RenderInterface;
use BEAR\Resource\ResourceObject;
use Koriym\ReduxReactSsr\ReduxReactJs;
use Ray\Aop\WeavedInterface;

final class ReduxRenderer implements RenderInterface
{
/**
* @var ReduxReactJs
*/
private $redux;

/**
* @var string
*/
private $appName;

/**
* ReduxRenderer constructor.
*
* @param string $appName redux application name
* @param string $reactBundleSrc redux-lib bundled source
* @param string $appBundleSrc redux-app bundled source
*/
public function __construct(string $appName, string $reactBundleSrc, string $appBundleSrc)
{
$this->appName = $appName;
$this->reactLibsrc = $reactBundleSrc;
$this->reactAppSrc = $appBundleSrc;
$this->redux = new ReduxReactJs($reactBundleSrc, $appBundleSrc);
}

/**
* {@inheritdoc}
*/
public function render(ResourceObject $ro)
{
$ssr = new Ssr($ro, $this->redux, $this->appName);
$file = $ro instanceof WeavedInterface ? (new \ReflectionClass($ro))->getParentClass()->getFileName() : (new \ReflectionClass($ro))->getFileName();
$template = substr($file, 0, -4) . '.html.php';
$ro->view = require $template;

return $ro->view;
}
}
4 changes: 4 additions & 0 deletions src/ReduxServerSideRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use Koriym\ReduxReactSsr\ReduxSsrInterface;
use Ray\Aop\WeavedInterface;

/**
* @deprecated
* @codeCoverageIgnore
*/
final class ReduxServerSideRenderer implements RenderInterface
{
/**
Expand Down
6 changes: 6 additions & 0 deletions src/ReduxSsr.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* This file is part of the BEAR\ReactJsModule package
*
Expand All @@ -8,6 +10,10 @@

use BEAR\Resource\RenderInterface;

/**
* @deprecated
* @codeCoverageIgnore
*/
trait ReduxSsr
{
/**
Expand Down
Loading

0 comments on commit f77a7d0

Please sign in to comment.