Skip to content

Commit 9a35b6c

Browse files
committed
state update via new file
1 parent 83714da commit 9a35b6c

File tree

4 files changed

+45
-32
lines changed

4 files changed

+45
-32
lines changed

Component.php

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,21 @@ abstract class Component extends React{
2525
*/
2626
private static $queue = []; //queue of components
2727

28-
/**
29-
* A flag that indicates when the queue is already set
30-
*
31-
* @var bool
32-
*/
33-
private static $isQueued = false;
3428

3529
/**
3630
* the post object when state is changes
3731
*
3832
* @var object
3933
*/
40-
private static $post;
34+
private static $isSetState = false;
4135

4236
/**
4337
* Setup the javascript of handling state
4438
*
4539
*/
4640
static function setup(): void {
47-
self::$post = json_decode(@$_POST['phpreact']);
48-
4941
//script tag to setup setState function
50-
self::import('phpreact.min.js');
42+
self::import('phpreact.js', '1.3');
5143
}
5244

5345
/**
@@ -56,7 +48,31 @@ static function setup(): void {
5648
* @return bool
5749
*/
5850
static function isSetState(): bool{
59-
return !!self::$post;
51+
return self::$isSetState;
52+
}
53+
54+
static function renderState(){
55+
$post = json_decode(@$_POST['phpreact']);
56+
if(!$post) return;
57+
self::$isSetState = true;
58+
self::$queue = $post->components;
59+
$component = self::decode($post->current);
60+
$oldState = $component->state;
61+
$component->state = (object)array_merge((array)$oldState, (array)$post->state);
62+
$component->componentDidUpdate($oldState, $component->state);
63+
return $component->handleRender();
64+
}
65+
66+
private static function decode(string $encode){
67+
$arr = json_decode(base64_decode($encode));
68+
include_once $arr->file;
69+
return unserialize($arr->component);
70+
}
71+
72+
private function encode(){
73+
$ref = $this instanceof Func ? new \ReflectionFunction(get_class($this)) : new \ReflectionClass($this);
74+
$file = $ref->getFileName();
75+
return base64_encode(json_encode(['file'=> $file, 'component'=> serialize($this)]));
6076
}
6177

6278
/**
@@ -66,7 +82,7 @@ static function isSetState(): bool{
6682
*/
6783
private function getQueueComponent(){
6884
$encode = array_shift(self::$queue);
69-
return $encode ? unserialize(base64_decode($encode)) : null;
85+
return $encode ? self::decode($encode) : null;
7086
}
7187

7288
/**
@@ -75,22 +91,8 @@ private function getQueueComponent(){
7591
* @return string
7692
*/
7793
private function stateManager(): string {
78-
$component = null;
79-
80-
if(!self::$isQueued){
81-
$post = self::$post;
82-
self::$queue = $post->components;
83-
self::$isQueued = true;
84-
$component = $this->getQueueComponent();
85-
$oldState = $component->state;
86-
$component->state = (object)array_merge((array)$oldState, (array)$post->state);
87-
$component->componentDidUpdate($oldState, $component->state);
88-
}else{
89-
$component = $this->getQueueComponent();
90-
}
91-
94+
$component = $this->getQueueComponent();
9295
if(!$component) $component = $this;
93-
9496
return $component->handleRender();
9597
}
9698

@@ -104,7 +106,8 @@ private function handleRender(): string {
104106
$components = $this->render();
105107

106108
if((array)$this->state && $components instanceof Tag){
107-
$componentProps = ['component'=> base64_encode(serialize($this)), 'component-state'=> $this->state];
109+
// $componentProps = ['component'=> base64_encode(serialize($this)), 'component-state'=> $this->state];
110+
$componentProps = ['component'=> $this->encode(), 'component-state'=> $this->state];
108111
$components->props = (object)array_merge((array)$components->props,$componentProps);
109112
}
110113

React.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private static function setup(): void {
6969
* @param string $class_name
7070
*/
7171
static function register_class($class_name){
72+
self::setup();
7273
$isTag = Tag::isValid($class_name);
7374
if(!$isTag && !function_exists($class_name)) return;
7475

@@ -187,8 +188,6 @@ private function isProps(array $array): bool{
187188
*
188189
*/
189190
function __construct($args = [], $children = []){
190-
self::setup();
191-
192191
if(!is_array($args)) $args = [$args];
193192
if(!is_array($children)) $children = [$children];
194193

phpreact.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
! function(t, e) {
2+
var url = e.currentScript.src.replace(/(\.min)?\.js/,'.php');
23
var n = function() {
34
e.querySelectorAll("[component] *,[component]").forEach(function(t) {
45
t.setState || (t.getState = c, t.setState = o)
@@ -7,7 +8,7 @@
78
o = function(t, o) {
89
var c = this.hasAttribute("component") ? this : this.closest("[component]");
910
if (c) {
10-
var i = [c.getAttribute("component")],
11+
var i = [],
1112
r = this.getAttribute("key"),
1213
s = (document.activeElement, this.value),
1314
a = this.getState();
@@ -16,6 +17,7 @@
1617
}), "function" == typeof t && (t = t(a));
1718
var u = new XMLHttpRequest,
1819
p = {
20+
current: c.getAttribute("component"),
1921
components: i,
2022
state: t
2123
};
@@ -24,7 +26,7 @@
2426
var t, i = e.createElement("div");
2527
i.innerHTML = this.responseText, r && (t = i.querySelector("[key=\'" + r + "\']")), c.replaceWith(i.childNodes[0]), t && (t.focus(), s && (t.value = "", t.value = s)), "function" == typeof o && o(), n()
2628
}
27-
}, u.open("POST", location.href, !0), u.setRequestHeader("Content-type", "application/x-www-form-urlencoded"), u.send("phpreact=" + JSON.stringify(p))
29+
}, u.open("POST", url, !0), u.setRequestHeader("Content-type", "application/x-www-form-urlencoded"), u.send("phpreact=" + JSON.stringify(p))
2830
}
2931
},
3032
c = function() {

phpreact.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
require_once 'React.php';
4+
require_once 'Component.php';
5+
require_once 'Tag.php';
6+
require_once 'Func.php';
7+
8+
9+
echo React\Component::renderState();

0 commit comments

Comments
 (0)