Skip to content

Commit

Permalink
✅ Improved solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eval committed Sep 10, 2018
1 parent 4f60982 commit ac1660c
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 50 deletions.
8 changes: 1 addition & 7 deletions src/kaleido/http/Capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class Capture extends Worker
{
public static $lock = [];
public $activity = ['history', 'current'];
public $leancloud = [];
public $apiServer;
Expand Down Expand Up @@ -94,13 +93,8 @@ private function activity($activity = null) {
$this->switchHandle('request');
$this->setTiming();
new Sender(parent::getItem('request'));
$this->lockClass();
parent::lockItem();
new Decoder(Sender::response(false));
}
}

private function lockClass() {
self::$lock = self::$item;
parent::resetClass();
}
}
18 changes: 6 additions & 12 deletions src/kaleido/http/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class Decoder extends Worker
{
private static $handleItem = [];
private static $lock;
public $error = false;
public $errorCode = 0;
public $respType;
Expand All @@ -23,7 +22,7 @@ public function __construct(array $response) {
parent::unpackItem($response);
parent::matchTaskId();
$this->handle();
$this->lockClass();
parent::lockItem(__CLASS__);
}

private function handle() {
Expand All @@ -42,11 +41,6 @@ private function checkError() {
}
}

private function lockClass() {
self::$lock = self::$item;
self::resetClass();
}

public static function getHandle($item = null) {
return self::$handleItem[$item] ?? null;
}
Expand All @@ -57,14 +51,14 @@ private function setHandle() {
}

public static function class($encode) {
return $encode ? json_encode(self::$lock)
: (array)self::$lock;
return $encode ? json_encode(parent::$lock[__CLASS__])
: (array)parent::$lock[__CLASS__];
}

public static function getBody() {
\is_string(self::$lock['body']) ?: new HttpException(
parent::getError('0x02'), 500);
return self::$lock['body'];
\is_string(parent::$lock[__CLASS__]['body'])
?: new HttpException(parent::getError('0x02'), 500);
return parent::$lock[__CLASS__]['body'];
}

private function setResponseDate() {
Expand Down
14 changes: 4 additions & 10 deletions src/kaleido/http/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class Encoder extends Worker
public $allow = ['post', 'put', 'head', 'options', 'search', 'patch', 'delete'];
public $method;
public $host;
private static $lock = [];

/**
* Encoder constructor.
Expand All @@ -21,7 +20,7 @@ public function __construct($taskId, $url) {
parent::matchTaskId($taskId);
$this->check($url);
$this->handle($taskId, $url);
$this->lockClass();
parent::lockItem(__CLASS__);
}

private function check($url) {
Expand All @@ -35,14 +34,9 @@ private function handle($taskId, $url) {
->setCookie()->setHeader()->setMaxSize();
}

private function lockClass() {
self::$lock = self::$item;
self::resetClass();
}

public static function class($encode) {
return $encode ? json_encode(self::$lock)
: self::$lock;
public static function payload($encode) {
return $encode ? json_encode(parent::$lock[__CLASS__])
: (array)parent::$lock[__CLASS__];
}

private function getHost($url = null) :string {
Expand Down
2 changes: 1 addition & 1 deletion src/kaleido/http/LeanCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function setApiServer($apiServer) {
}

public function setRequest(LeanObject $class) :LeanObject {
return $class->set('request', Encoder::class(false));
return $class->set('request', Encoder::payload(false));
}

public function setResponse(LeanObject $class) :LeanObject {
Expand Down
14 changes: 4 additions & 10 deletions src/kaleido/http/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class Loader extends Worker
{
public static $lock = [];
public $allow = ['dynamic'];
public $expire = [];
public $hashName;
Expand All @@ -25,7 +24,7 @@ public function __construct() {
$this->getEnv('database');
$this->unpackItem('loadInfo');
$this->handle();
$this->lockClass();
parent::lockItem(__CLASS__);
}

public function __toString() {
Expand Down Expand Up @@ -95,8 +94,8 @@ private function loadDatabase() {
}

public static function fetch() {
return \is_string(self::$lock['fetch'])
? self::$lock['fetch'] : 'error_fetch';
return \is_string(self::$lock[__CLASS__]['fetch'])
? self::$lock[__CLASS__]['fetch'] : 'error_fetch';
}

private function local($setName, $fileName) {
Expand Down Expand Up @@ -134,7 +133,7 @@ private function setLoadCache($name, $value = null) {
*/
public function listenHttp($taskId, $url) :string {
new Encoder($taskId, $url);
new Sender(Encoder::class(false));
new Sender(Encoder::payload(false));
new Decoder(Sender::response(false));
new Capture();
return Decoder::getBody();
Expand Down Expand Up @@ -165,11 +164,6 @@ private function setConsole() {
: error_log('redisExpire: '. (parent::getItem('expire') - time()));
}

private function lockClass() {
self::$lock = self::$item;
self::resetClass();
}

private function isJson($data = null) {
if (!\is_object(json_decode($data))) {
new HttpException(
Expand Down
12 changes: 3 additions & 9 deletions src/kaleido/http/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class Sender extends Worker
{
private static $lock;
public $protocol = ['get', 'post', 'put', 'head', 'options', 'search', 'patch', 'delete'];
public $maxSize;
public $url;
Expand All @@ -25,7 +24,7 @@ public function __construct(array $payload) {
parent::unpackItem($payload);
$this->check();
$this->handle();
$this->lockClass();
parent::lockItem(__CLASS__);
}

public function check() {
Expand All @@ -52,14 +51,9 @@ public function handle() {
: $this->setBody($curl->response, $curl->responseHeaders);
}

private function lockClass() {
self::$lock = self::$item;
self::resetClass();
}

public static function response($encode) {
return $encode ? json_encode(self::$lock)
: (array)self::$lock;
return $encode ? json_encode(self::$lock[__CLASS__])
: (array)self::$lock[__CLASS__];
}

private function setTaskId() {
Expand Down
4 changes: 3 additions & 1 deletion src/kaleido/http/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class Worker
{
protected static $lock = [];
public static $loader;
public static $timing = [];
public static $item = [];
Expand Down Expand Up @@ -124,7 +125,8 @@ protected static function getError($id = null) :string {
});
}

protected static function resetClass() :array {
protected function lockItem($className = null) :array {
self::$lock[$className] = self::$item;
return self::$item = [];
}
}

0 comments on commit ac1660c

Please sign in to comment.