Skip to content

Commit

Permalink
http route add redirect method, workersocket workerhttp add header ch…
Browse files Browse the repository at this point in the history
…eck, change response redirect method
  • Loading branch information
wazsmwazsm committed Jun 5, 2018
1 parent e59cfb9 commit 4a4ecf1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 27 deletions.
32 changes: 27 additions & 5 deletions src/WorkerF/Http/Response.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace WorkerF\Http;
use WorkerF\WorkerSocket\WorkerHttp;
use Closure;

/**
* HTTP response.
Expand All @@ -15,11 +16,11 @@
* create http response header.
*
* @param mixed $header
* @return boolean
* @return void
*/
public static function header($headers)
{
return WorkerHttp::header($headers);
WorkerHttp::header($headers);
}

/**
Expand All @@ -37,11 +38,18 @@ public static function getHeader($key)
* redirect.
*
* @param string $path
* @return void
* @return \Closure
*/
public static function redirect($path)
{
self::header("Location: $path");
{
// return Closure
// to notice bulid method the type of response
return function() use($path) {
// run Location
Response::header("Location: $path");

return "redirect";
};
}

/**
Expand All @@ -56,6 +64,20 @@ public static function bulid($data, array $conf)
{
// should be json
if(is_array($data) || is_object($data)) {
// Closure
if($data instanceof Closure) {

switch (call_user_func($data)) {
case 'redirect':
return 'link already redirected';
break;

default:
return '';
break;
}
}
// Array \ Object
self::header("Content-Type: application/json;charset=utf-8");
return self::_compress(json_encode($data), $conf['compress']);
}
Expand Down
48 changes: 33 additions & 15 deletions src/WorkerF/Http/Route.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace WorkerF\Http;
use WorkerF\Http\Requests;
use WorkerF\Http\Response;
use WorkerF\IOCContainer;
use WorkerF\Config;
use Closure;
/**
* HTTP router.
Expand Down Expand Up @@ -54,11 +56,11 @@ public static function __callstatic($method, $params)
*/
public static function setMapTree($method, $path, $content)
{
$uri = self::_uriParse(self::$_filter['prefix'].$path);
$path = self::_pathParse(self::$_filter['prefix'].$path);
$callback = is_string($content) ?
self::_namespaceParse('\\'.self::$_filter['namespace'].$content) : $content;

self::$_map_tree[$uri][strtoupper($method)] = $callback;
self::$_map_tree[$path][strtoupper($method)] = $callback;
}

/**
Expand All @@ -74,7 +76,7 @@ public static function group(array $filter, Closure $routes)
$tmp_prefix = self::$_filter['prefix'];
$tmp_namespace = self::$_filter['namespace'];

// set filter uri prefix
// set filter path prefix
if(isset($filter['prefix'])) {
self::$_filter['prefix'] .= '/'.$filter['prefix'].'/';
}
Expand All @@ -90,18 +92,18 @@ public static function group(array $filter, Closure $routes)
}

/**
* Parse uri.
* Parse path.
*
* @param string $uri
* @param string $path
* @return string
*/
protected static function _uriParse($uri)
protected static function _pathParse($path)
{
// make uri as /a/b/c mode
$uri = ($uri == '/') ? $uri : '/'.rtrim($uri, '/');
$uri = preg_replace('/\/+/', '/', $uri);
// make path as /a/b/c mode
$path = ($path == '/') ? $path : '/'.rtrim($path, '/');
$path = preg_replace('/\/+/', '/', $path);

return $uri;
return $path;
}

/**
Expand All @@ -127,19 +129,19 @@ protected static function _namespaceParse($namespace)
public static function dispatch(Requests $request)
{
// get request param
$uri = self::_uriParse(parse_url(($request->server->REQUEST_URI))['path']);
$path = self::_pathParse(parse_url(($request->server->REQUEST_URI))['path']);
$method = $request->server->REQUEST_METHOD;
// router exist or not
if( ! array_key_exists($uri, self::$_map_tree) ||
! array_key_exists($method, self::$_map_tree[$uri])
if( ! array_key_exists($path, self::$_map_tree) ||
! array_key_exists($method, self::$_map_tree[$path])
)
{
$e = new \LogicException("route rule uri: $uri <==> method : $method is not set!");
$e = new \LogicException("route rule path: $path <==> method : $method is not set!");
$e->httpCode = 404;
throw $e;
}
// get callback info
$callback = self::$_map_tree[$uri][$method];
$callback = self::$_map_tree[$path][$method];

// is class
if(is_string($callback)) {
Expand All @@ -165,4 +167,20 @@ public static function dispatch(Requests $request)
return call_user_func($callback, $request);
}
}

/**
* redirect. warning: only for get method
*
* @param string $path
* @param array $param
* @return void
*/
public static function redirect($path, $param = [])
{
$base_url = rtrim(Config::get('app.base_url'), '/');
$path = self::_pathParse($path);
$url = $base_url.$path.'?'.http_build_query($param);

return Response::redirect($url);
}
}
10 changes: 7 additions & 3 deletions src/WorkerF/WorkerSocket/WorkerHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ class WorkerHttp
* create http response header.
*
* @param mixed $header
* @return boolean
* @return void
*/
public static function header($headers)
{
if(is_array($headers)) {
// if pass array
foreach ($headers as $header) {
Http::header($header);
if(FALSE === Http::header($header)) {
throw new \InvalidArgumentException("Header $header is invalid!");
}
}
return;
}
// pass string
return Http::header($headers);
if(FALSE === Http::header($headers)) {
throw new \InvalidArgumentException("Header $headers is invalid!");
}
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/Http/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public static function cleanMapTree()
self::$_map_tree = [];
}

public static function uriParse($uri)
public static function pathParse($path)
{
return self::_uriParse($uri);
return self::_pathParse($path);
}

public static function namespaceParse($namespace)
Expand Down Expand Up @@ -49,11 +49,11 @@ public function setUp()

public function testUriParse()
{
$result = RouteFake::uriParse('usr//local///bin');
$result = RouteFake::pathParse('usr//local///bin');

$this->assertEquals('/usr/local/bin', $result);

$result = RouteFake::uriParse('/');
$result = RouteFake::pathParse('/');

$this->assertEquals('/', $result);
}
Expand Down

0 comments on commit 4a4ecf1

Please sign in to comment.