Browse Source

refracto; content type header

develop
Robin Thoni 8 years ago
parent
commit
b13a96e9fd

+ 12
- 5
src/Utils/Controller/LuticateApplication.php View File

83
         $this->_router->setup();
83
         $this->_router->setup();
84
     }
84
     }
85
     
85
     
86
-    public function runHttp()
86
+    private function dispatch($httpMethod, $url, $parameters)
87
     {
87
     {
88
-        $httpMethod = $_SERVER['REQUEST_METHOD'];
89
-        $url = $_SERVER['REQUEST_URI'];
90
-
91
-        $parameters = array_merge($_GET, $_POST);
92
         $r = new LuRequestDbo();
88
         $r = new LuRequestDbo();
93
         $r->setVersion($this->getVersion());
89
         $r->setVersion($this->getVersion());
94
 
90
 
109
             $r->setStatusCode(500);
105
             $r->setStatusCode(500);
110
             $r->setMessage("Internal Error");
106
             $r->setMessage("Internal Error");
111
         }
107
         }
108
+        return $r;
109
+    }
110
+    
111
+    public function runHttp()
112
+    {
113
+        $httpMethod = $_SERVER['REQUEST_METHOD'];
114
+        $url = $_SERVER['REQUEST_URI'];
115
+        $parameters = array_merge($_GET, $_POST);
116
+        
117
+        $r = $this->dispatch($httpMethod, $url, $parameters);
118
+        header("content-type", "application/json");
112
         http_response_code($r->getStatusCode());
119
         http_response_code($r->getStatusCode());
113
         echo $r->__toString();
120
         echo $r->__toString();
114
     }
121
     }

+ 11
- 14
src/Utils/Dbo/LuRequestDbo.php View File

10
 
10
 
11
 class LuRequestDbo extends LuDbo
11
 class LuRequestDbo extends LuDbo
12
 {
12
 {
13
-    /**
14
-     * Specify data which should be serialized to JSON
15
-     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
16
-     * @return mixed data which can be serialized by <b>json_encode</b>,
17
-     * which is a value of any type other than a resource.
18
-     * @since 5.4.0
19
-     */
20
     function jsonSerialize()
13
     function jsonSerialize()
21
     {
14
     {
22
         return array(
15
         return array(
23
             "Version" => $this->_version,
16
             "Version" => $this->_version,
24
             "Data" => $this->_data,
17
             "Data" => $this->_data,
25
-            "Message" => $this->_message
18
+            "Message" => $this->_message,
19
+            "StatusCode" => $this->_statusCode
26
         );
20
         );
27
     }
21
     }
28
 
22
 
38
         if (isset($json["Message"])) {
32
         if (isset($json["Message"])) {
39
             $dbo->setMessage($json["Message"]);
33
             $dbo->setMessage($json["Message"]);
40
         }
34
         }
35
+        if (isset($json["StatusCode"])) {
36
+            $dbo->setStatusCode($json["StatusCode"]);
37
+        }
41
         return $dbo;
38
         return $dbo;
42
     }
39
     }
43
 
40
 
44
     private $_version;
41
     private $_version;
45
     /**
42
     /**
46
-     * @return mixed
43
+     * @return float
47
      */
44
      */
48
     public function getVersion()
45
     public function getVersion()
49
     {
46
     {
50
         return $this->_version;
47
         return $this->_version;
51
     }
48
     }
52
     /**
49
     /**
53
-     * @param mixed $version
50
+     * @param float $version
54
      */
51
      */
55
     public function setVersion($version)
52
     public function setVersion($version)
56
     {
53
     {
75
 
72
 
76
     private $_message;
73
     private $_message;
77
     /**
74
     /**
78
-     * @return mixed
75
+     * @return string
79
      */
76
      */
80
     public function getMessage()
77
     public function getMessage()
81
     {
78
     {
82
         return $this->_message;
79
         return $this->_message;
83
     }
80
     }
84
     /**
81
     /**
85
-     * @param mixed $message
82
+     * @param string $message
86
      */
83
      */
87
     public function setMessage($message)
84
     public function setMessage($message)
88
     {
85
     {
92
     private $_statusCode;
89
     private $_statusCode;
93
 
90
 
94
     /**
91
     /**
95
-     * @return mixed
92
+     * @return int
96
      */
93
      */
97
     public function getStatusCode()
94
     public function getStatusCode()
98
     {
95
     {
100
     }
97
     }
101
 
98
 
102
     /**
99
     /**
103
-     * @param mixed $statusCode
100
+     * @param int $statusCode
104
      */
101
      */
105
     public function setStatusCode($statusCode)
102
     public function setStatusCode($statusCode)
106
     {
103
     {

+ 0
- 25
src/Utils/LuHandler.php View File

1
-<?php
2
-
3
-namespace Luticate\Utils;
4
-
5
-use Exception;
6
-use Symfony\Component\HttpKernel\Exception\HttpException;
7
-use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
8
-
9
-class LuHandler extends ExceptionHandler {
10
-
11
-    public function render($request, Exception $e)
12
-    {
13
-        LuLog::log($e);
14
-        $data = "Internal error";
15
-        $code = 500;
16
-
17
-        if ($e instanceof HttpException)
18
-        {
19
-            $data = $e->getMessage();
20
-            $code = $e->getStatusCode();
21
-        }
22
-        return response(LuOutputFormatter::formatError($code, $data), $code);
23
-    }
24
-
25
-}

+ 0
- 27
src/Utils/LuOutputFormatter.php View File

1
-<?php
2
-
3
-namespace Luticate\Utils;
4
-
5
-
6
-class LuOutputFormatter
7
-{
8
-    public static $APP_VERSION = 1.0;
9
-
10
-    public static function formatSuccess($data)
11
-    {
12
-        $r = new LuRequestDbo();
13
-        $r->setVersion(self::$APP_VERSION);
14
-        $r->setData($data);
15
-        $r->setStatusCode(200);
16
-        return $r;
17
-    }
18
-
19
-    public static function formatError($code, $message)
20
-    {
21
-        $r = new LuRequestDbo();
22
-        $r->setVersion(self::$APP_VERSION);
23
-        $r->setMessage($message);
24
-        $r->setStatusCode($code);
25
-        return $r;
26
-    }
27
-}

Loading…
Cancel
Save