Skip to content

Commit 25f2cdb

Browse files
committed
Porting more PHP classes to Zephir
1 parent 3adfdda commit 25f2cdb

11 files changed

+334
-79
lines changed

scale/http/http/controllerfactory.zep

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Scale\Http\HTTP;
2+
3+
use Scale\Kernel\Interfaces\BuilderInterface;
4+
use Scale\Kernel\Core\Container;
5+
6+
class ControllerFactory extends Container implements BuilderInterface
7+
{
8+
public function factory($name)
9+
{
10+
if (class_exists($name)) {
11+
12+
return this->constructInject($name);
13+
}
14+
}
15+
}

scale/http/http/html/controller.zep

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace Scale\Http\HTTP\HTML;
2+
3+
/**
4+
* Controller
5+
*
6+
*/
7+
class Controller
8+
{
9+
protected view;
10+
11+
/**
12+
*
13+
*/
14+
public function __construct(<\Closure> view)
15+
{
16+
let this->view = view;
17+
}
18+
19+
/**
20+
*
21+
* @param string $name
22+
* @param array $params
23+
*/
24+
public function renderView($name, $params = [], $ret = false)
25+
{
26+
var $view;
27+
let $view = this->view->__invoke($name, $params);
28+
29+
if ($ret) {
30+
return $view->render(true);
31+
}
32+
33+
$view->render();
34+
}
35+
}
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
namespace Scale\Http\HTTP\IO\Provider;
2+
3+
use Scale\Http\HTTP\IO\RequestInterface;
4+
use Scale\Kernel\Core\Environment;
5+
use Scale\Kernel\Core\RuntimeException;
6+
7+
class Request implements RequestInterface
8+
{
9+
protected $env;
10+
protected $uri;
11+
protected $params;
12+
protected $method;
13+
protected $body;
14+
15+
/**
16+
*
17+
* @param Environment $env
18+
*/
19+
public function __construct(<Environment> $env)
20+
{
21+
let this->env = $env;
22+
23+
this->setup();
24+
}
25+
26+
/**
27+
*
28+
*/
29+
public function setup()
30+
{
31+
let this->uri = strtok(this->env->getServer("REQUEST_URI"), '?');
32+
33+
let this->method = this->env->getServer("REQUEST_METHOD");
34+
35+
let this->body = file_get_contents("php://input");
36+
}
37+
38+
/**
39+
*
40+
* @param string $name
41+
* @return string
42+
* @throws RuntimeException
43+
*/
44+
public function param(string $name)
45+
{
46+
if (this->method === "POST") {
47+
return filter_input(INPUT_POST, $name, FILTER_SANITIZE_STRING);
48+
} elseif (this->method === "GET") {
49+
return filter_input(INPUT_GET, $name, FILTER_SANITIZE_STRING);
50+
} else {
51+
throw new RuntimeException("Invalid Param Request");
52+
}
53+
}
54+
55+
public function uri()
56+
{
57+
return this->uri;
58+
}
59+
60+
/**
61+
*
62+
* @return array
63+
* @throws RuntimeException
64+
*/
65+
public function params()
66+
{
67+
var $input;
68+
if (this->method === "POST") {
69+
let $input = INPUT_POST;
70+
} elseif (this->method === "GET") {
71+
let $input = INPUT_GET;
72+
} else {
73+
throw new RuntimeException("Invalid Param Request");
74+
}
75+
return filter_input_array($input, FILTER_SANITIZE_STRING);
76+
}
77+
}

scale/http/http/io/requestfactory.zep

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Scale\Http\HTTP\IO;
2+
3+
use Scale\Kernel\Core\Environment;
4+
use Scale\Http\HTTP\IO\Provider\Request;
5+
6+
class RequestFactory
7+
{
8+
public function factory(<Environment> $env)
9+
{
10+
return new Request($env);
11+
}
12+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Scale\Http\HTTP\IO;
2+
3+
interface RequestInterface
4+
{
5+
public function params();
6+
}

scale/http/http/router.zep

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
namespace Scale\Http\HTTP;
2+
3+
use Closure;
4+
use Scale\Kernel\Interfaces\ExecutorInterface;
5+
use Scale\Kernel\Core\Builders;
6+
use Scale\Kernel\Core\RuntimeException;
7+
use Scale\Http\HTTP\IO\RequestInterface;
8+
9+
class Router implements ExecutorInterface
10+
{
11+
protected request;
12+
protected controller;
13+
protected route;
14+
15+
/**
16+
*
17+
* @param string $uri
18+
* @param array $params
19+
* @param RequestInterface $request
20+
* @param Closure $controller
21+
*/
22+
public function __construct(<RequestInterface> $request = null, <\Closure> $controller = null)
23+
{
24+
let this->request = $request;
25+
let this->controller = $controller;
26+
}
27+
28+
/**
29+
*
30+
* @return Router
31+
*/
32+
public function prepare()
33+
{
34+
// Find route in config
35+
let this->route = this->getRoute(this->request->uri(), this->request->params());
36+
37+
// Create a new instance of the task
38+
let this->controller = this->controller->__invoke(this->route["controller"]);
39+
40+
return this;
41+
}
42+
43+
/**
44+
*
45+
* @param string $uri
46+
* @param array $params
47+
* @return array
48+
* @throws RuntimeException
49+
*/
50+
public function getRoute(string $uri, var $params)
51+
{
52+
var $routes, $route, $param;
53+
array $input;
54+
let route = null;
55+
let $routes = apc_fetch("routes");
56+
if (!$routes)
57+
{
58+
let $routes = (array) require dirname(dirname(_SERVER["DOCUMENT_ROOT"]))."/etc/routes.php";
59+
apc_store("routes", $routes);
60+
}
61+
62+
if (isset($routes[$uri])) {
63+
let $route = $routes[$uri];
64+
} else {
65+
throw new RuntimeException("404");
66+
}
67+
68+
let $input = [];
69+
70+
for $param in $route["params"] {
71+
let $input[$param] = isset($params[$param]) ? $params[$param] : null;
72+
}
73+
74+
let $route["input"] = $input;
75+
return $route;
76+
}
77+
78+
/**
79+
* Executes the controller action
80+
*/
81+
public function execute()
82+
{
83+
return call_user_func_array(
84+
[this->controller, this->route["action"]],
85+
this->route["input"]
86+
);
87+
}
88+
}

scale/kernel/core/application.zep

-5
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,4 @@ class Application
3131
{
3232
this->executor->prepare()->execute();
3333
}
34-
35-
public function say(var text)
36-
{
37-
echo text;
38-
}
3934
}

0 commit comments

Comments
 (0)