@@ -1,5 +1,5 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | function app_content($request, $args) { |
4 | - return ['data'=>['names'=>["John","Jane","Jack"]]]; |
|
4 | + return ['data'=>['names'=>["John", "Jane", "Jack"]]]; |
|
5 | 5 | } |
6 | 6 | \ No newline at end of file |
@@ -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, |
@@ -31,40 +31,40 @@ discard block |
||
31 | 31 | $this->app = new \Slim\App($this->container); |
32 | 32 | } |
33 | 33 | |
34 | - public function run_route($request, $response, $route, $action='main', $return_type='html', $args = []) |
|
34 | + public function run_route($request, $response, $route, $action = 'main', $return_type = 'html', $args = []) |
|
35 | 35 | { |
36 | - $controller = $this->config['base_dir'] . '/apps/' . $this->config['app_name'] .'/controllers/'. $route . '/' . $action . '.php'; |
|
36 | + $controller = $this->config['base_dir'] . '/apps/' . $this->config['app_name'] . '/controllers/' . $route . '/' . $action . '.php'; |
|
37 | 37 | $template = '/apps/' . $this->config['app_name'] . '/templates/' . $route . '/' . $action . '.html'; |
38 | 38 | |
39 | - if(file_exists($controller)){ |
|
39 | + if (file_exists($controller)) { |
|
40 | 40 | |
41 | 41 | require_once($controller); |
42 | 42 | |
43 | - if(!function_exists('app_content')){ |
|
44 | - return $this->not_found($request, $response, $return_type, 'Controller '. $route . '/' . $action." has not app_content function"); |
|
43 | + if (!function_exists('app_content')) { |
|
44 | + return $this->not_found($request, $response, $return_type, 'Controller ' . $route . '/' . $action . " has not app_content function"); |
|
45 | 45 | } |
46 | - if( $return_type == 'json' ){ |
|
46 | + if ($return_type == 'json') { |
|
47 | 47 | $status = 500; |
48 | 48 | $function_output = app_content($request, $args); |
49 | - if(!is_array( $function_output) ){ |
|
49 | + if (!is_array($function_output)) { |
|
50 | 50 | $function_output = ["status" => 500, "error" => "Internal Server Error"]; |
51 | 51 | } |
52 | - else{ |
|
53 | - if(!isset($function_output['status'])){ |
|
54 | - $function_output['status']=200; |
|
52 | + else { |
|
53 | + if (!isset($function_output['status'])) { |
|
54 | + $function_output['status'] = 200; |
|
55 | 55 | } |
56 | 56 | $status = (int) $function_output['status']; |
57 | 57 | } |
58 | - $response->getBody()->write( json_encode( $function_output ) ); |
|
59 | - $newResponse = $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By',"reformo/rslim")->withStatus($status); |
|
58 | + $response->getBody()->write(json_encode($function_output)); |
|
59 | + $newResponse = $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By', "reformo/rslim")->withStatus($status); |
|
60 | 60 | } |
61 | - else{ |
|
62 | - if (!file_exists($this->config['base_dir'].$template)){ |
|
63 | - throw new \Exception("<strong>Template file not found!</strong> ".$route.'/'.$action." needs a template file at:".$template ); |
|
61 | + else { |
|
62 | + if (!file_exists($this->config['base_dir'] . $template)) { |
|
63 | + throw new \Exception("<strong>Template file not found!</strong> " . $route . '/' . $action . " needs a template file at:" . $template); |
|
64 | 64 | } |
65 | 65 | $function_output = app_content($request, $args); |
66 | - if (!isset($function_output['data'])){ |
|
67 | - $function_output['data']=[]; |
|
66 | + if (!isset($function_output['data'])) { |
|
67 | + $function_output['data'] = []; |
|
68 | 68 | } |
69 | 69 | $loader = new \Twig_Loader_Filesystem($this->config['base_dir']); |
70 | 70 | $this->twig = new \Twig_Environment($loader, [ |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | ]); |
75 | 75 | $filter = new \Twig_SimpleFunction( |
76 | 76 | 'rwidget_*_*', |
77 | - function ($widget_name, $widget_action, $args=[]) { |
|
77 | + function($widget_name, $widget_action, $args = []) { |
|
78 | 78 | |
79 | 79 | $widget_file = $this->config['base_dir'] . '/apps/' . $this->config['app_name'] . '/widgets/' . $widget_name . '/' . $widget_action . ".php"; |
80 | 80 | $widget_template = '/apps/' . $this->config['app_name'] . '/templates/_widgets/' . $widget_name . '/' . $widget_action . '.html'; |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | if (is_string($widget_content)) { |
90 | 90 | return $widget_content; |
91 | 91 | } |
92 | - else{ |
|
92 | + else { |
|
93 | 93 | throw new \Exception("<strong>Widget should return string:</strong> " . $widget_name . '/' . $widget_action . "!"); |
94 | 94 | } |
95 | 95 | } else { |
@@ -104,70 +104,70 @@ discard block |
||
104 | 104 | $this->twig->addFunction($filter); |
105 | 105 | $this->twig->addGlobal('runtime_config', $this->config); |
106 | 106 | $this->twig->addGlobal('url_params', $request->getParams()); |
107 | - $function_output['app_content']=$this->twig->render($template, $function_output['data']); |
|
107 | + $function_output['app_content'] = $this->twig->render($template, $function_output['data']); |
|
108 | 108 | $main_template_name = 'default'; |
109 | 109 | if (isset($function_output['app_main_template'])) { |
110 | 110 | $main_template_name = $function_output['app_main_template']; |
111 | 111 | } |
112 | - $main_template = '/apps/'.$this->config['app_name'] . '/templates/_' . $main_template_name . '.html'; |
|
112 | + $main_template = '/apps/' . $this->config['app_name'] . '/templates/_' . $main_template_name . '.html'; |
|
113 | 113 | |
114 | - if (!file_exists($this->config['base_dir'] . $main_template)){ |
|
115 | - throw new \Exception("<strong>Main emplate file not found!</strong> ".$route.'/'.$action." needs a main template file at:".$main_template ); |
|
114 | + if (!file_exists($this->config['base_dir'] . $main_template)) { |
|
115 | + throw new \Exception("<strong>Main emplate file not found!</strong> " . $route . '/' . $action . " needs a main template file at:" . $main_template); |
|
116 | 116 | } |
117 | - $app_content = $this->twig->render( $main_template, $function_output); |
|
118 | - $newResponse=$response->withHeader('X-Powered-By',"reformo/rslim"); |
|
117 | + $app_content = $this->twig->render($main_template, $function_output); |
|
118 | + $newResponse = $response->withHeader('X-Powered-By', "reformo/rslim"); |
|
119 | 119 | $newResponse->write($app_content); |
120 | 120 | } |
121 | 121 | return $newResponse; |
122 | 122 | } |
123 | - else{ |
|
124 | - return $this->not_found($request, $response, $return_type, 'Controller '. $route . '/' . $action." not found"); |
|
123 | + else { |
|
124 | + return $this->not_found($request, $response, $return_type, 'Controller ' . $route . '/' . $action . " not found"); |
|
125 | 125 | } |
126 | 126 | } |
127 | 127 | |
128 | - public function not_found($request, $response, $return_type='html',$message=""){ |
|
128 | + public function not_found($request, $response, $return_type = 'html', $message = "") { |
|
129 | 129 | |
130 | - $not_found_template = '/apps/'.$this->config['app_name'] . '/templates/_404.html'; |
|
131 | - if($return_type == 'json'){ |
|
132 | - $response->getBody()->write( json_encode( ['status'=>404, 'message'=>$message] ) ); |
|
133 | - return $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By',"reformo/rslim")->withStatus(404); |
|
130 | + $not_found_template = '/apps/' . $this->config['app_name'] . '/templates/_404.html'; |
|
131 | + if ($return_type == 'json') { |
|
132 | + $response->getBody()->write(json_encode(['status'=>404, 'message'=>$message])); |
|
133 | + return $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By', "reformo/rslim")->withStatus(404); |
|
134 | 134 | } |
135 | - else{ |
|
135 | + else { |
|
136 | 136 | return $response->withStatus(404) |
137 | - ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By',"reformo/rslim") |
|
137 | + ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By', "reformo/rslim") |
|
138 | 138 | ->write($this->twig->render($not_found_template, ['message'=>$message])); |
139 | 139 | } |
140 | 140 | } |
141 | 141 | |
142 | - public function register($request_method, $pattern, $controller, $return_type='html') |
|
142 | + public function register($request_method, $pattern, $controller, $return_type = 'html') |
|
143 | 143 | { |
144 | - $this->app->map([strtoupper($request_method)], $pattern, function (Request $req, Response $res, $args ){ |
|
145 | - list($route,$action) = explode("/",$args['controller']); |
|
144 | + $this->app->map([strtoupper($request_method)], $pattern, function(Request $req, Response $res, $args) { |
|
145 | + list($route, $action) = explode("/", $args['controller']); |
|
146 | 146 | return $args['RSlim']->run_route($req, $res, $route, $action, $args['return_type'], $args); |
147 | 147 | })->setArguments(['controller'=>$controller, 'return_type'=>$return_type, 'RSlim'=>$this]); |
148 | 148 | } |
149 | 149 | |
150 | 150 | public function run() |
151 | 151 | { |
152 | - $this->container['notFoundHandler'] = function ($c) { |
|
153 | - return function (Request $req, Response $res) use ($c) { |
|
152 | + $this->container['notFoundHandler'] = function($c) { |
|
153 | + return function(Request $req, Response $res) use ($c) { |
|
154 | 154 | return $c['response'] |
155 | 155 | ->withStatus(404) |
156 | - ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By',"reformo/rslim") |
|
156 | + ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By', "reformo/rslim") |
|
157 | 157 | ->write('<h1>404 - Requested URL not found</h1>'); |
158 | 158 | }; |
159 | 159 | }; |
160 | - if ($this->config['bypass_error_handlers'] === true ) { |
|
161 | - $this->container['errorHandler'] = function ($container) { |
|
162 | - return function ($request, $response, $exception) use ($container) { |
|
160 | + if ($this->config['bypass_error_handlers'] === true) { |
|
161 | + $this->container['errorHandler'] = function($container) { |
|
162 | + return function($request, $response, $exception) use ($container) { |
|
163 | 163 | $response->getBody()->rewind(); |
164 | 164 | return $response->withStatus(500) |
165 | 165 | ->withHeader('Content-Type', 'text/html') |
166 | 166 | ->write($exception->getMessage()); |
167 | 167 | }; |
168 | 168 | }; |
169 | - $this->container['phpErrorHandler'] = function ($container) { |
|
170 | - return function ($request, $response, $error) use ($container) { |
|
169 | + $this->container['phpErrorHandler'] = function($container) { |
|
170 | + return function($request, $response, $error) use ($container) { |
|
171 | 171 | $response->getBody()->rewind(); |
172 | 172 | return $response->withStatus(500) |
173 | 173 | ->withHeader('Content-Type', 'text/html') |
@@ -48,8 +48,7 @@ discard block |
||
48 | 48 | $function_output = app_content($request, $args); |
49 | 49 | if(!is_array( $function_output) ){ |
50 | 50 | $function_output = ["status" => 500, "error" => "Internal Server Error"]; |
51 | - } |
|
52 | - else{ |
|
51 | + } else{ |
|
53 | 52 | if(!isset($function_output['status'])){ |
54 | 53 | $function_output['status']=200; |
55 | 54 | } |
@@ -57,8 +56,7 @@ discard block |
||
57 | 56 | } |
58 | 57 | $response->getBody()->write( json_encode( $function_output ) ); |
59 | 58 | $newResponse = $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By',"reformo/rslim")->withStatus($status); |
60 | - } |
|
61 | - else{ |
|
59 | + } else{ |
|
62 | 60 | if (!file_exists($this->config['base_dir'].$template)){ |
63 | 61 | throw new \Exception("<strong>Template file not found!</strong> ".$route.'/'.$action." needs a template file at:".$template ); |
64 | 62 | } |
@@ -88,15 +86,13 @@ discard block |
||
88 | 86 | if (!file_exists($this->config['base_dir'] . $widget_template)) { |
89 | 87 | if (is_string($widget_content)) { |
90 | 88 | return $widget_content; |
91 | - } |
|
92 | - else{ |
|
89 | + } else{ |
|
93 | 90 | throw new \Exception("<strong>Widget should return string:</strong> " . $widget_name . '/' . $widget_action . "!"); |
94 | 91 | } |
95 | 92 | } else { |
96 | 93 | return $this->twig->render($widget_template, $widget_content); |
97 | 94 | } |
98 | - } |
|
99 | - else { |
|
95 | + } else { |
|
100 | 96 | throw new \Exception("<strong>Widget function not found!</strong> " . $widget_name . '/' . $widget_action . "!"); |
101 | 97 | } |
102 | 98 | }, |
@@ -119,8 +115,7 @@ discard block |
||
119 | 115 | $newResponse->write($app_content); |
120 | 116 | } |
121 | 117 | return $newResponse; |
122 | - } |
|
123 | - else{ |
|
118 | + } else{ |
|
124 | 119 | return $this->not_found($request, $response, $return_type, 'Controller '. $route . '/' . $action." not found"); |
125 | 120 | } |
126 | 121 | } |
@@ -131,8 +126,7 @@ discard block |
||
131 | 126 | if($return_type == 'json'){ |
132 | 127 | $response->getBody()->write( json_encode( ['status'=>404, 'message'=>$message] ) ); |
133 | 128 | return $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By',"reformo/rslim")->withStatus(404); |
134 | - } |
|
135 | - else{ |
|
129 | + } else{ |
|
136 | 130 | return $response->withStatus(404) |
137 | 131 | ->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By',"reformo/rslim") |
138 | 132 | ->write($this->twig->render($not_found_template, ['message'=>$message])); |