@@ -1,5 +1,5 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -function app_content($request, $args){ |
|
3 | +function app_content($request, $args) { |
|
4 | 4 | return ['data'=>null]; |
5 | 5 | } |
6 | 6 | \ No newline at end of file |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -function app_content($request, $args){ |
|
3 | +function app_content($request, $args) { |
|
4 | 4 | |
5 | 5 | return ['data'=>null]; |
6 | 6 | } |
7 | 7 | \ No newline at end of file |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -function app_content($request, $args){ |
|
3 | +function app_content($request, $args) { |
|
4 | 4 | |
5 | - return ['data'=>['name'=>$request->getParam('name')],'status'=>200]; |
|
5 | + return ['data'=>['name'=>$request->getParam('name')], 'status'=>200]; |
|
6 | 6 | } |
7 | 7 | \ No newline at end of file |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -function app_content($request, $args){ |
|
3 | +function app_content($request, $args) { |
|
4 | 4 | |
5 | - return ['data'=>['name'=>$args['name']],'status'=>200]; |
|
5 | + return ['data'=>['name'=>$args['name']], 'status'=>200]; |
|
6 | 6 | } |
7 | 7 | \ No newline at end of file |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -$config =[ |
|
3 | +$config = [ |
|
4 | 4 | 'base_dir' => dirname(dirname(__DIR__)), |
5 | 5 | 'base_url' => 'http://www.reformo.dev', |
6 | 6 | 'app_name' => basename(__DIR__), |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | use \Psr\Http\Message\RequestInterface as Request; |
6 | 6 | use \Psr\Http\Message\ResponseInterface as Response; |
7 | 7 | |
8 | -class RSlim{ |
|
8 | +class RSlim { |
|
9 | 9 | |
10 | 10 | public $config = [ |
11 | 11 | 'base_dir' => null, |
@@ -30,40 +30,40 @@ discard block |
||
30 | 30 | $this->app = new \Slim\App($this->container); |
31 | 31 | } |
32 | 32 | |
33 | - public function run_route($request, $response, $route, $action='main', $return_type='html', $args = []) |
|
33 | + public function run_route($request, $response, $route, $action = 'main', $return_type = 'html', $args = []) |
|
34 | 34 | { |
35 | - $controller = $this->config['base_dir'] . '/apps/' . $this->config['app_name'] .'/controllers/'. $route . '/' . $action . '.php'; |
|
35 | + $controller = $this->config['base_dir'] . '/apps/' . $this->config['app_name'] . '/controllers/' . $route . '/' . $action . '.php'; |
|
36 | 36 | $template = '/apps/' . $this->config['app_name'] . '/templates/' . $route . '/' . $action . '.html'; |
37 | 37 | |
38 | - if(file_exists($controller)){ |
|
38 | + if (file_exists($controller)) { |
|
39 | 39 | |
40 | 40 | require_once($controller); |
41 | 41 | |
42 | - if(!function_exists('app_content')){ |
|
43 | - return $this->not_found($request, $response, $return_type, 'Controller '. $route . '/' . $action." has not app_content function"); |
|
42 | + if (!function_exists('app_content')) { |
|
43 | + return $this->not_found($request, $response, $return_type, 'Controller ' . $route . '/' . $action . " has not app_content function"); |
|
44 | 44 | } |
45 | - if( $return_type == 'json' ){ |
|
45 | + if ($return_type == 'json') { |
|
46 | 46 | $status = 500; |
47 | 47 | $function_output = app_content($request, $args); |
48 | - if(!is_array( $function_output) ){ |
|
48 | + if (!is_array($function_output)) { |
|
49 | 49 | $function_output = ["status" => 500, "error" => "Internal Server Error"]; |
50 | 50 | } |
51 | - else{ |
|
52 | - if(!isset($function_output['status'])){ |
|
53 | - $function_output['status']=200; |
|
51 | + else { |
|
52 | + if (!isset($function_output['status'])) { |
|
53 | + $function_output['status'] = 200; |
|
54 | 54 | } |
55 | 55 | $status = (int) $function_output['status']; |
56 | 56 | } |
57 | - $response->getBody()->write( json_encode( $function_output ) ); |
|
58 | - $newResponse = $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By',"reformo/rslim")->withStatus($status); |
|
57 | + $response->getBody()->write(json_encode($function_output)); |
|
58 | + $newResponse = $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By', "reformo/rslim")->withStatus($status); |
|
59 | 59 | } |
60 | - else{ |
|
61 | - if(!file_exists($this->config['base_dir'].$template)){ |
|
62 | - throw new \Exception("<strong>Template file not found!</strong> ".$route.'/'.$action." needs a template file at:".$template ); |
|
60 | + else { |
|
61 | + if (!file_exists($this->config['base_dir'] . $template)) { |
|
62 | + throw new \Exception("<strong>Template file not found!</strong> " . $route . '/' . $action . " needs a template file at:" . $template); |
|
63 | 63 | } |
64 | 64 | $function_output = app_content($request, $args); |
65 | - if(!isset($function_output['data'])){ |
|
66 | - $function_output['data']=[]; |
|
65 | + if (!isset($function_output['data'])) { |
|
66 | + $function_output['data'] = []; |
|
67 | 67 | } |
68 | 68 | $loader = new \Twig_Loader_Filesystem($this->config['base_dir']); |
69 | 69 | $this->twig = new \Twig_Environment($loader, [ |
@@ -72,69 +72,69 @@ discard block |
||
72 | 72 | 'auto_reload' => 1 |
73 | 73 | ]); |
74 | 74 | $this->twig->addGlobal('runtime_config', $this->config); |
75 | - $function_output['app_content']=$this->twig->render($template,$function_output['data']); |
|
75 | + $function_output['app_content'] = $this->twig->render($template, $function_output['data']); |
|
76 | 76 | $main_template_name = 'default'; |
77 | - if( isset( $function_output['app_main_template'] ) ){ |
|
77 | + if (isset($function_output['app_main_template'])) { |
|
78 | 78 | $main_template_name = $function_output['app_main_template']; |
79 | 79 | } |
80 | - $main_template = '/apps/'.$this->config['app_name'] . '/templates/_' . $main_template_name . '.html'; |
|
80 | + $main_template = '/apps/' . $this->config['app_name'] . '/templates/_' . $main_template_name . '.html'; |
|
81 | 81 | |
82 | - if(!file_exists($this->config['base_dir'].$main_template)){ |
|
83 | - throw new \Exception("<strong>Main emplate file not found!</strong> ".$route.'/'.$action." needs a main template file at:".$main_template ); |
|
82 | + if (!file_exists($this->config['base_dir'] . $main_template)) { |
|
83 | + throw new \Exception("<strong>Main emplate file not found!</strong> " . $route . '/' . $action . " needs a main template file at:" . $main_template); |
|
84 | 84 | } |
85 | - $app_content = $this->twig->render( $main_template, $function_output); |
|
86 | - $newResponse=$response->withHeader('X-Powered-By',"reformo/rslim"); |
|
85 | + $app_content = $this->twig->render($main_template, $function_output); |
|
86 | + $newResponse = $response->withHeader('X-Powered-By', "reformo/rslim"); |
|
87 | 87 | $newResponse->write($app_content); |
88 | 88 | } |
89 | 89 | return $newResponse; |
90 | 90 | } |
91 | - else{ |
|
92 | - return $this->not_found($request, $response, $return_type, 'Controller '. $route . '/' . $action." not found"); |
|
91 | + else { |
|
92 | + return $this->not_found($request, $response, $return_type, 'Controller ' . $route . '/' . $action . " not found"); |
|
93 | 93 | } |
94 | 94 | } |
95 | 95 | |
96 | - public function not_found($request, $response, $return_type='html',$message=""){ |
|
96 | + public function not_found($request, $response, $return_type = 'html', $message = "") { |
|
97 | 97 | |
98 | - $not_found_template = '/apps/'.$this->config['app_name'] . '/templates/_404.html'; |
|
99 | - if($return_type == 'json'){ |
|
100 | - $response->getBody()->write( json_encode( ['status'=>404, 'message'=>$message] ) ); |
|
101 | - return $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By',"reformo/rslim")->withStatus(404); |
|
98 | + $not_found_template = '/apps/' . $this->config['app_name'] . '/templates/_404.html'; |
|
99 | + if ($return_type == 'json') { |
|
100 | + $response->getBody()->write(json_encode(['status'=>404, 'message'=>$message])); |
|
101 | + return $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By', "reformo/rslim")->withStatus(404); |
|
102 | 102 | } |
103 | - else{ |
|
103 | + else { |
|
104 | 104 | return $response->withStatus(404) |
105 | - ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By',"reformo/rslim") |
|
105 | + ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By', "reformo/rslim") |
|
106 | 106 | ->write($this->twig->render($not_found_template, ['message'=>$message])); |
107 | 107 | } |
108 | 108 | } |
109 | 109 | |
110 | - public function register($request_method, $pattern, $controller, $return_type='html') |
|
110 | + public function register($request_method, $pattern, $controller, $return_type = 'html') |
|
111 | 111 | { |
112 | - $this->app->map([strtoupper($request_method)], $pattern, function (Request $req, Response $res, $args ){ |
|
113 | - list($route,$action) = explode("/",$args['controller']); |
|
112 | + $this->app->map([strtoupper($request_method)], $pattern, function(Request $req, Response $res, $args) { |
|
113 | + list($route, $action) = explode("/", $args['controller']); |
|
114 | 114 | return $args['RSlim']->run_route($req, $res, $route, $action, $args['return_type'], $args); |
115 | 115 | })->setArguments(['controller'=>$controller, 'return_type'=>$return_type, 'RSlim'=>$this]); |
116 | 116 | } |
117 | 117 | |
118 | 118 | public function run() |
119 | 119 | { |
120 | - $this->container['notFoundHandler'] = function ($c) { |
|
121 | - return function (Request $req, Response $res) use ($c) { |
|
120 | + $this->container['notFoundHandler'] = function($c) { |
|
121 | + return function(Request $req, Response $res) use ($c) { |
|
122 | 122 | return $c['response'] |
123 | 123 | ->withStatus(404) |
124 | - ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By',"reformo/rslim") |
|
124 | + ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By', "reformo/rslim") |
|
125 | 125 | ->write('<h1>404 - Requested URL not found</h1>'); |
126 | 126 | }; |
127 | 127 | }; |
128 | - $this->container['errorHandler'] = function ($container) { |
|
129 | - return function ($request, $response, $exception) use ($container) { |
|
128 | + $this->container['errorHandler'] = function($container) { |
|
129 | + return function($request, $response, $exception) use ($container) { |
|
130 | 130 | $response->getBody()->rewind(); |
131 | 131 | return $response->withStatus(500) |
132 | 132 | ->withHeader('Content-Type', 'text/html') |
133 | 133 | ->write($exception->getMessage()); |
134 | 134 | }; |
135 | 135 | }; |
136 | - $this->container['phpErrorHandler'] = function ($container) { |
|
137 | - return function ($request, $response, $error) use ($container) { |
|
136 | + $this->container['phpErrorHandler'] = function($container) { |
|
137 | + return function($request, $response, $error) use ($container) { |
|
138 | 138 | $response->getBody()->rewind(); |
139 | 139 | return $response->withStatus(500) |
140 | 140 | ->withHeader('Content-Type', 'text/html') |
@@ -47,8 +47,7 @@ discard block |
||
47 | 47 | $function_output = app_content($request, $args); |
48 | 48 | if(!is_array( $function_output) ){ |
49 | 49 | $function_output = ["status" => 500, "error" => "Internal Server Error"]; |
50 | - } |
|
51 | - else{ |
|
50 | + } else{ |
|
52 | 51 | if(!isset($function_output['status'])){ |
53 | 52 | $function_output['status']=200; |
54 | 53 | } |
@@ -56,8 +55,7 @@ discard block |
||
56 | 55 | } |
57 | 56 | $response->getBody()->write( json_encode( $function_output ) ); |
58 | 57 | $newResponse = $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By',"reformo/rslim")->withStatus($status); |
59 | - } |
|
60 | - else{ |
|
58 | + } else{ |
|
61 | 59 | if(!file_exists($this->config['base_dir'].$template)){ |
62 | 60 | throw new \Exception("<strong>Template file not found!</strong> ".$route.'/'.$action." needs a template file at:".$template ); |
63 | 61 | } |
@@ -87,8 +85,7 @@ discard block |
||
87 | 85 | $newResponse->write($app_content); |
88 | 86 | } |
89 | 87 | return $newResponse; |
90 | - } |
|
91 | - else{ |
|
88 | + } else{ |
|
92 | 89 | return $this->not_found($request, $response, $return_type, 'Controller '. $route . '/' . $action." not found"); |
93 | 90 | } |
94 | 91 | } |
@@ -99,8 +96,7 @@ discard block |
||
99 | 96 | if($return_type == 'json'){ |
100 | 97 | $response->getBody()->write( json_encode( ['status'=>404, 'message'=>$message] ) ); |
101 | 98 | return $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By',"reformo/rslim")->withStatus(404); |
102 | - } |
|
103 | - else{ |
|
99 | + } else{ |
|
104 | 100 | return $response->withStatus(404) |
105 | 101 | ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By',"reformo/rslim") |
106 | 102 | ->write($this->twig->render($not_found_template, ['message'=>$message])); |