1414use \Psr \Container \ContainerInterface ;
1515use \App \App ;
1616use \App \Site \Routing \RouteInfo ;
17+ use \App \Site \Models \RequestLog ;
1718use \Symfony \Component \HttpFoundation \Response ;
1819use \Symfony \Component \HttpFoundation \JsonResponse ;
1920use \Exception ;
@@ -39,7 +40,7 @@ public function __construct(ContainerInterface $container)
3940 *
4041 * @return array
4142 */
42- public function getRouteVerbs ()
43+ public static function getRouteVerbs ()
4344 {
4445 return ['DELETE ' , 'GET ' , 'HEAD ' , 'OPTIONS ' , 'PATCH ' , 'POST ' , 'PUT ' ];
4546 }
@@ -56,7 +57,9 @@ protected function beforeRender()
5657 return $ return ;
5758 }
5859
59- if ($ this ->getRequest ()->getContentType () != 'application/json ' ) {
60+ if ($ this ->getRequest ()->headers ->get ('Content-Type ' ) != 'application/json ' &&
61+ $ this ->getRequest ()->getContentType () != 'json '
62+ ) {
6063 return $ this ->getUtils ()->errorPage (403 );
6164 }
6265
@@ -97,14 +100,23 @@ protected function loadObject($id)
97100 */
98101 public function process (RouteInfo $ route_info = null , $ route_data = [])
99102 {
100- $ return = parent ::process ($ route_info , $ route_data );
101-
102103 if (!empty ($ data = json_decode ($ this ->getRequest ()->getContent (), true ))) {
103104 if (isset ($ data ['id ' ])) {
104105 unset($ data ['id ' ]);
105106 }
106107 }
107108
109+ try {
110+ $ log = $ this ->getContainer ()->make (RequestLog::class);
111+ $ log ->fillWithRequest ($ this ->getRequest (), $ this );
112+ $ log ->persist ();
113+ } catch (Exception $ e ) {
114+ $ this ->getUtils ()->logException ($ e , "Can't write RequestLog " );
115+ if ($ this ->getEnv ('DEBUG ' )) {
116+ return $ this ->getUtils ()->errorException ($ e );
117+ }
118+ }
119+
108120 $ object = $ this ->getContainer ()->call ([$ this ->getObjectClass (), 'new ' ]);
109121 if (in_array ($ this ->getVerb (), ['GET ' , 'PUT ' , 'DELETE ' ])) {
110122 $ object = $ this ->loadObject ($ route_data ['id ' ]);
@@ -125,6 +137,16 @@ public function process(RouteInfo $route_info = null, $route_data = [])
125137 break ;
126138 case 'GET ' :
127139 // Read
140+
141+ if ($ object ->id == null ) {
142+ return $ this
143+ ->getResponse ()
144+ ->prepare ($ this ->getRequest ())
145+ ->setData (array_map (function ($ object ) {
146+ return $ object ->getData ();
147+ }, $ this ->getContainer ()->call ([$ this ->getObjectClass (), 'all ' ])));
148+ }
149+
128150 return $ this
129151 ->getResponse ()
130152 ->prepare ($ this ->getRequest ())
0 commit comments