55use Pckg \Concept \Reflect ;
66use Pckg \Framework \Helper \Lazy ;
77use Pckg \Framework \Request \Data \Cookie ;
8- use Pckg \Framework \Request \Data \Get ;
98use Pckg \Framework \Request \Data \Post ;
109use Pckg \Framework \Request \Data \Server ;
11- use Pckg \Framework \Request \Data \Session ;
12- use Pckg \Framework \Request \Data \Request as DataRequest ;
1310use Pckg \Framework \Request \Message ;
1411use Psr \Http \Message \RequestInterface ;
1512use Psr \Http \Message \ServerRequestInterface ;
@@ -56,21 +53,55 @@ class Request extends Message implements RequestInterface, ServerRequestInterfac
5653 */
5754 protected $ uri ;
5855
59- public function __construct ()
56+ public function __construct ($ input = [] )
6057 {
6158 Reflect::method ($ this , 'initDependencies ' );
6259
63- $ this ->server = (new Server ())->setFromGlobals ();
64- $ this ->request = (new DataRequest ())->setFromGlobals ();
65- $ this ->post = (new Post ())->setFromGlobals ();
66- $ this ->get = (new Get ())->setFromGlobals ();
67- $ this ->cookie = (new Cookie ())->setFromGlobals ();
68- $ this ->files = (new Lazy ());
60+ if (!$ input ) {
61+ $ input = file_get_contents ('php://input ' );
62+ if ($ input && (strpos ($ input , '{ ' ) === 0 && strrpos ($ input , '} ' ) === strlen ($ input ) - 1 )) {
63+ $ input = json_decode ($ input , true );
64+ } elseif ($ input ) {
65+ parse_str ($ input , $ input );
66+ } else {
67+ $ input = [];
68+ }
69+ }
70+
71+ if (!$ input && $ _POST ) {
72+ /**
73+ * Why is php input sometimes empty?
74+ */
75+ $ input = $ _POST ;
76+ }
77+
78+ $ this ->setPost ($ input );
79+ $ this ->get = new Lazy ($ _GET );
80+ $ this ->server = new Server ($ _SERVER );
81+ $ this ->files = new Lazy ($ _FILES );
82+ $ this ->cookie = new Cookie ($ _COOKIE );
83+ $ this ->request = new Lazy ($ _REQUEST );
84+
85+ $ this ->fetchUrl ();
86+
6987 $ this ->headers = collect (getallheaders ())->groupBy (function ($ value , $ key ) {
7088 return $ key ;
7189 })->all ();
90+ }
91+
92+ public function setConstructs ($ post , $ get , $ server , $ files , $ cookie , $ request , $ headers = [])
93+ {
94+ $ this ->setPost ($ post );
95+ $ this ->get = new Lazy ($ get );
96+ $ this ->server = new Lazy ($ server );
97+ $ this ->files = new Lazy ($ files );
98+ $ this ->cookie = new Cookie ($ cookie );
99+ $ this ->request = new Lazy ($ request );
100+ $ this ->headers = $ headers ;
72101
73102 $ this ->fetchUrl ();
103+
104+ return $ this ;
74105 }
75106
76107 /**
@@ -155,77 +186,46 @@ public function post($key = null, $default = [])
155186 return $ this ->post ->get ($ key , $ default );
156187 }
157188
158- /**
159- * @param $object
160- * @param null $key
161- * @param array $default
162- * @return Lazy|mixed
163- */
164- private function getOrFull ($ object , $ key = null , $ default = [])
165- {
166- return is_null ($ key )
167- ? $ object
168- : $ object ->get ($ key , $ default );
169- }
170-
171- /**
172- * @param null $key
173- * @param array $default
174- * @return Get|mixed
175- */
176189 public function get ($ key = null , $ default = [])
177190 {
178- return $ this ->getOrFull ($ this ->get , $ key , $ default );
191+ return is_null ($ key )
192+ ? $ this ->get
193+ : $ this ->get ->get ($ key , $ default );
179194 }
180195
181- /**
182- * @param null $key
183- * @param array $default
184- * @return Server|mixed
185- */
186196 public function server ($ key = null , $ default = [])
187197 {
188- return $ this ->getOrFull ($ this ->server , $ key , $ default );
198+ return is_null ($ key )
199+ ? $ this ->server
200+ : $ this ->server ->get ($ key , $ default );
189201 }
190202
191- /**
192- * @param null $key
193- * @param array $default
194- * @return Cookie|mixed
195- */
196203 public function cookie ($ key = null , $ default = [])
197204 {
198- return $ this ->getOrFull ($ this ->cookie , $ key , $ default );
205+ return is_null ($ key )
206+ ? $ this ->cookie
207+ : $ this ->cookie ->get ($ key , $ default );
199208 }
200209
201- /**
202- * @param null $key
203- * @param array $default
204- * @return Session|mixed
205- */
206210 public function session ($ key = null , $ default = [])
207211 {
208- return $ this ->getOrFull ($ this ->session , $ key , $ default );
212+ return is_null ($ key )
213+ ? $ this ->session
214+ : $ this ->session ->get ($ key , $ default );
209215 }
210216
211- /**
212- * @param null $key
213- * @param array $default
214- * @return \Pckg\Htmlbuilder\Datasource\Method\Request|mixed
215- */
216217 public function request ($ key = null , $ default = [])
217218 {
218- return $ this ->getOrFull ($ this ->request , $ key , $ default );
219+ return is_null ($ key )
220+ ? $ this ->request
221+ : $ this ->request ->get ($ key , $ default );
219222 }
220223
221- /**
222- * @param null $key
223- * @param array $default
224- * @return Lazy|mixed
225- */
226- public function files ($ key = null , $ default = [])
224+ public function files ($ key = null )
227225 {
228- return $ this ->getOrFull ($ this ->files , $ key , $ default );
226+ return is_null ($ key )
227+ ? $ this ->files
228+ : $ this ->files ->get ($ key );
229229 }
230230
231231 public function isMethod ($ method )
@@ -530,7 +530,7 @@ public function withParsedBody($data)
530530 */
531531 public function getAttributes ()
532532 {
533- return $ this -> post ()-> all () ;
533+ return [] ;
534534 }
535535
536536 /**
@@ -540,7 +540,6 @@ public function getAttributes()
540540 */
541541 public function getAttribute ($ name , $ default = null )
542542 {
543- return $ this ->post ()->get ($ name , $ default );
544543 return $ this ->attributes [$ name ] ?? $ default ;
545544 }
546545
@@ -551,8 +550,7 @@ public function getAttribute($name, $default = null)
551550 */
552551 public function withAttribute ($ name , $ value )
553552 {
554- $ this ->post ()->set ($ name , $ value );
555- // $this->attributes[$name] = $value;
553+ $ this ->attributes [$ name ] = $ value ;
556554
557555 return $ this ;
558556 }
@@ -563,8 +561,7 @@ public function withAttribute($name, $value)
563561 */
564562 public function withoutAttribute ($ name )
565563 {
566- unset($ this ->post ()[$ name ]);
567- //unset($this->attributes[$name]);
564+ unset($ this ->attributes [$ name ]);
568565
569566 return $ this ;
570567 }
0 commit comments