Continued:
[core.git] / framework / main / classes / output / debug / console / class_DebugConsoleOutput.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Debug\Output;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Debug\Debugger;
8 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
9 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
10 use Org\Mxchange\CoreFramework\Output\Debug\BaseDebugOutput;
11 use Org\Mxchange\CoreFramework\Registry\Registerable;
12 use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer;
13
14 /**
15  * A debug output class for the console (e.g. hub software)
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.shipsimu.org
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program. If not, see <http://www.gnu.org/licenses/>.
35  */
36 class DebugConsoleOutput extends BaseDebugOutput implements Debugger, OutputStreamer, Registerable {
37         /**
38          * Cached configuration entry 'debug_*_output_timings'
39          */
40         private $isDebugOutputTimingsEnabled = FALSE;
41
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         private function __construct () {
48                 // Call parent constructor first
49                 parent::__construct(__CLASS__);
50
51                 // Cache configuration entry
52                 $this->isDebugOutputTimingsEnabled = FrameworkBootstrap::getConfigurationInstance()->isEnabled('debug_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_output_timings');
53         }
54
55         /**
56          * Creates an instance of this class
57          *
58          * @return      $debugInstance  The prepared debug instance
59          */
60         public static final function createDebugConsoleOutput () {
61                 // Get a new instance
62                 $debugInstance = new DebugConsoleOutput();
63
64                 // Return it
65                 return $debugInstance;
66         }
67
68         /**
69          * Outputs the given data without HTML tags
70          *
71          * @param       $output         The HTML'ed output
72          * @param       $stripTags      Whether HTML tags shall be stripped out
73          * @return      void
74          */
75         public final function outputStream (string $output, bool $stripTags = false) {
76                 // Strip HTML tags out?
77                 if ($stripTags === true) {
78                         // Prepare the output without HTML tags
79                         $output = trim(html_entity_decode(strip_tags(stripslashes($output))));
80                 }
81
82                 // Are debug times enabled?
83                 if ($this->isDebugOutputTimingsEnabled) {
84                         // Output it first
85                         $output = $this->getPrintableExecutionTime() . $output;
86                 }
87
88                 // And print it out...
89                 printf('%s%s', str_replace('-&gt;', '->', $output), PHP_EOL);
90         }
91
92         /**
93          * Output the code
94          *
95          * @param       $outStream      Stream to output
96          * @param       $stripTags      Whether HTML tags shall be stripped out
97          * @return      void
98          */
99         public final function output (string $outStream = '', bool $stripTags = false) {
100                 // Empty output will be silently ignored
101                 if (!empty($outStream)) {
102                         $this->outputStream($outStream, $stripTags);
103                 }
104         }
105
106         /**
107          * Streams the data and maybe does something to it
108          *
109          * @param       $data   The data (string mostly) to "stream"
110          * @return      $data   The data (string mostly) to "stream"
111          * @throws      UnsupportedOperationException   If this method is called
112          */
113         public function streamData (string $data) {
114                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
115                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
116         }
117
118 }