]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/System.php
New function to exit the program
[friendica.git] / src / Core / System.php
index 16bc2360ef2767edb8984209bcac62b07b7437dd..10fc5c7d4e335e6cdc725affb1672f93c572ea8d 100644 (file)
@@ -295,7 +295,7 @@ class System
                DI::apiResponse()->addContent(XML::fromArray(["result" => $result], $xml));
                DI::page()->exit(DI::apiResponse()->generate());
 
-               exit();
+               self::exit();
        }
 
        /**
@@ -306,25 +306,42 @@ class System
         * @param string  $content Response body. Optional.
         * @throws \Exception
         */
-       public static function httpExit($val, $message = '', $content = '')
+       public static function httpError($httpCode, $message = '', $content = '')
        {
-               if ($val >= 400) {
-                       Logger::debug('Exit with error', ['code' => $val, 'message' => $message, 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
+               if ($httpCode >= 400) {
+                       Logger::debug('Exit with error', ['code' => $httpCode, 'message' => $message, 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
                }
-               DI::apiResponse()->setStatus($val, $message);
+               DI::apiResponse()->setStatus($httpCode, $message);
                DI::apiResponse()->addContent($content);
                DI::page()->exit(DI::apiResponse()->generate());
 
-               exit();
+               self::exit();
        }
 
-       public static function jsonError($httpCode, $data, $content_type = 'application/json')
+       /**
+        * This function adds the content and a content-teype HTTP header to the output.
+        * After finishing the process is getting killed.
+        *
+        * @param string $content
+        * @param [type] $responce
+        * @param string|null $content_type
+        * @return void
+        */
+       public static function httpExit(string $content, string $responce = Response::TYPE_HTML, ?string $content_type = null) {
+               DI::apiResponse()->setType($responce, $content_type);
+               DI::apiResponse()->addContent($content);
+               DI::page()->exit(DI::apiResponse()->generate());
+
+               self::exit();
+       }
+
+       public static function jsonError($httpCode, $content, $content_type = 'application/json')
        {
                if ($httpCode >= 400) {
                        Logger::debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
                }
                DI::apiResponse()->setStatus($httpCode);
-               self::jsonExit($data, $content_type);
+               self::jsonExit($content, $content_type);
        }
 
        /**
@@ -334,15 +351,25 @@ class System
         * and adds an application/json HTTP header to the output.
         * After finishing the process is getting killed.
         *
-        * @param mixed   $x            The input content
+        * @param mixed   $content      The input content
         * @param string  $content_type Type of the input (Default: 'application/json')
         * @param integer $options      JSON options
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function jsonExit($x, $content_type = 'application/json', int $options = 0) {
+       public static function jsonExit($content, $content_type = 'application/json', int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) {
                DI::apiResponse()->setType(Response::TYPE_JSON, $content_type);
-               DI::apiResponse()->addContent(json_encode($x, $options));
+               DI::apiResponse()->addContent(json_encode($content, $options));
                DI::page()->exit(DI::apiResponse()->generate());
+
+               self::exit();
+       }
+
+       /**
+        * Exit the program execution.
+        */
+       public static function exit()
+       {
+               DI::page()->logRuntime();
                exit();
        }
 
@@ -432,8 +459,7 @@ class System
                        case 307:
                                throw new TemporaryRedirectException();
                }
-
-               exit();
+               self::exit();
        }
 
        /**
@@ -506,7 +532,7 @@ class System
                echo str_replace("\t", "       ", $o);
                echo "</section>";
                echo "</body></html>\r\n";
-               exit();
+               self::exit();
        }
 
        /**