Skip to content

Commit

Permalink
Merge pull request #51 from juvenn/feature/leanengine
Browse files Browse the repository at this point in the history
添加支持 Laravel 的中间件 LaravelEngine
  • Loading branch information
juvenn committed May 13, 2016
2 parents 6a6e8fe + caacf05 commit b3c23a7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
54 changes: 54 additions & 0 deletions src/LeanCloud/Engine/LaravelEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace LeanCloud\Engine;

/**
* LeanEngine as Laravel middleware
*
* Add LaravelEngine::class to Laravel middleware stack:
*
* ```php
* // in app/Http/Kernel.php
* $middleware = [
* \LeanCloud\Engine\LaravelEngine::class
* ];
* ```
*
* @link https://laravel.com/docs/5.1/middleware
*/
class LaravelEngine extends LeanEngine {

/**
* Get request header value
*
* @param string $key Header key
* @return string
*/
protected function getHeaderLine($key) {
return $this->request->header($key);
}

/**
* Get request body string
*
* @return string
*/
protected function getBody() {
return $this->request->getContent();
}

/**
* Laravel middleware entry point
*
* @param \Illuminate\Http\Reuqest $request Laravel request
* @param \Closure $next Laravel closure
* @return mixed
*/
public function handle($request, $next) {
$this->request = $request;
$this->dispatch($request->method(),
$request->url());
return $next($this->request);
}
}

32 changes: 30 additions & 2 deletions src/LeanCloud/Engine/LeanEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,47 +152,73 @@ private function retrieveHeader($keys) {

/**
* Extract variant headers into env
*
* PHP prepends `HTTP_` to user-defined headers, so `X-MY-VAR`
* would be populated as `HTTP_X_MY_VAR`. But 3rd party frameworks
* (e.g. Laravel) may overwrite the behavior, and populate it as
* cleaner `X_MY_VAR`. So we try to retrieve header value from both
* versions.
*
*/
private function parseHeaders() {
$this->env["ORIGIN"] = $this->retrieveHeader(array(
"ORIGIN",
"HTTP_ORIGIN"
));
$this->env["CONTENT_TYPE"] = $this->retrieveHeader(array(
"CONTENT_TYPE",
"HTTP_CONTENT_TYPE"
));
$this->env["REMOTE_ADDR"] = $this->retrieveHeader(array(
"X_REAL_IP",
"HTTP_X_REAL_IP",
"X_FORWARDED_FOR",
"HTTP_X_FORWARDED_FOR",
"REMOTE_ADDR"
));

$this->env["LC_ID"] = $this->retrieveHeader(array(
"X_LC_ID",
"HTTP_X_LC_ID",
"X_AVOSCLOUD_APPLICATION_ID",
"HTTP_X_AVOSCLOUD_APPLICATION_ID",
"X_ULURU_APPLICATION_ID",
"HTTP_X_ULURU_APPLICATION_ID"
));
$this->env["LC_KEY"] = $this->retrieveHeader(array(
"X_LC_KEY",
"HTTP_X_LC_KEY",
"X_AVOSCLOUD_APPLICATION_KEY",
"HTTP_X_AVOSCLOUD_APPLICATION_KEY",
"X_ULURU_APPLICATION_KEY",
"HTTP_X_ULURU_APPLICATION_KEY"
));
$this->env["LC_MASTER_KEY"] = $this->retrieveHeader(array(
"X_AVOSCLOUD_MASTER_KEY",
"HTTP_X_AVOSCLOUD_MASTER_KEY",
"X_ULURU_MASTER_KEY",
"HTTP_X_ULURU_MASTER_KEY"
));
$this->env["LC_SESSION"] = $this->retrieveHeader(array(
"X_LC_SESSION",
"HTTP_X_LC_SESSION",
"X_AVOSCLOUD_SESSION_TOKEN",
"HTTP_X_AVOSCLOUD_SESSION_TOKEN",
"X_ULURU_SESSION_TOKEN",
"HTTP_X_ULURU_SESSION_TOKEN"
));
$this->env["LC_SIGN"] = $this->retrieveHeader(array(
"X_LC_SIGN",
"HTTP_X_LC_SIGN",
"X_AVOSCLOUD_REQUEST_SIGN",
"HTTP_X_AVOSCLOUD_REQUEST_SIGN"
));
$prod = $this->retrieveHeader(array(
"X_LC_PROD",
"HTTP_X_LC_PROD",
"X_AVOSCLOUD_APPLICATION_PRODUCTION",
"HTTP_X_AVOSCLOUD_APPLICATION_PRODUCTION",
"X_ULURU_APPLICATION_PRODUCTION",
"HTTP_X_ULURU_APPLICATION_PRODUCTION"
));
$this->env["useProd"] = true;
Expand Down Expand Up @@ -229,8 +255,10 @@ private function parsePlainBody($body) {
true;
$this->env["useMaster"] = false;
// remove internal fields set by API
// note we need to preserve `__type` field for object decoding
// see #61
forEach($data as $key) {
if ($key[0] === "_") {
if ($key[0] === "_" && $key[1] !== "_") {
unset($data[$key]);
}
}
Expand Down Expand Up @@ -348,7 +376,7 @@ protected function dispatch($method, $url) {
$this->processSession();
if (strpos($pathParts["extra"], "/_ops/metadatas") === 0) {
if ($this->env["useMaster"]) {
$this->renderJSON(Cloud::getKeys());
$this->renderJSON(array("result" => Cloud::getKeys()));
} else {
$this->renderError("Unauthorized.", 401, 401);
}
Expand Down
2 changes: 1 addition & 1 deletion src/LeanCloud/Engine/SlimEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function getHeaderLine($key) {
* @return string
*/
protected function getBody() {
return $this->response->getBody()->getContents();
return $this->request->getBody()->getContents();
}

/*
Expand Down
2 changes: 1 addition & 1 deletion tests/engine/LeanEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testPingEngine() {

public function testGetFuncitonMetadata() {
$resp = $this->request("/1/functions/_ops/metadatas", "GET");
$this->assertContains("hello", $resp);
$this->assertContains("hello", $resp["result"]);
}

public function testCloudFunctionHello() {
Expand Down

0 comments on commit b3c23a7

Please sign in to comment.