-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bearsunday/wip
ReduxModule
- Loading branch information
Showing
23 changed files
with
966 additions
and
1,694 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ nbproject | |
.idea | ||
.project | ||
.settings | ||
build/ | ||
node_modules/ | ||
vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"autoload": { | ||
"files": ["Greeting.php"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.