ddec2cc00de4176bc011c270be9fc114f3c935ff
[core.git] / inc / main / classes / output / console / class_ConsoleOutput.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Output;
4
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7
8 /**
9  * This class simply puts text without any HTML code out. This class is suiable
10  * for console output
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
14  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15  * @license             GNU GPL 3.0 or any newer version
16  * @link                http://www.shipsimu.org
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  */
31 class ConsoleOutput extends BaseOutput implements OutputStreamer {
32         /**
33          * The instance for the singleton design pattern
34          */
35         private static $consoleInstance = NULL;
36
37         /**
38          * Protected constructor
39          *
40          * @return      void
41          */
42         protected function __construct () {
43                 // Call parent constructor
44                 parent::__construct(__CLASS__);
45         }
46
47         /**
48          * Create a new web output system and set the content type
49          *
50          * @param       $contentType    A valid content-type
51          * @return      $debugInstance  An instance of this middleware class
52          */
53         public static final function createConsoleOutput ($contentType) {
54                 // Cast the content-type to string
55                 $contentType = (string) $contentType;
56                 $contentType = trim($contentType);
57
58                 // Get instance
59                 self::$consoleInstance = new ConsoleOutput();
60
61                 // Set the content type
62                 // @TODO Need to rewrite this to $requestInstance->addHeader()
63                 if (!empty($contentType)) {
64                         @header(sprintf('Content-type: %s',
65                                 $contentType
66                         ));
67                 } // END - if
68
69                 // Return instance
70                 return self::$consoleInstance;
71         }
72
73         /**
74          * Getter for an instance of this class
75          *
76          * @return      $consoleInstance        An instance of this class
77          */
78         public static final function getInstance() {
79                 // Is the self-instance already set?
80                 if (is_null(self::$consoleInstance)) {
81                         $contentType = FrameworkConfiguration::getSelfInstance()->getConfigEntry('web_content_type');
82                         self::$consoleInstance = ConsoleOutput::createConsoleOutput($contentType);
83                 } // END - if
84
85                 // Return the instance
86                 return self::$consoleInstance;
87         }
88
89         /**
90          * Output the code
91          *
92          * @param       $outStream      Something we shall sent to the console
93          * @param       $stripTags      Whether HTML tags shall be stripped out
94          * @return      void
95          */
96         public final function output ($outStream = FALSE, $stripTags = FALSE) {
97                 print trim($outStream) . PHP_EOL;
98         }
99
100         /**
101          * Determines seek position
102          *
103          * @return      $seekPosition   Current seek position
104          * @throws      UnsupportedOperationException   If this method is called
105          */
106         public function determineSeekPosition () {
107                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
108         }
109
110         /**
111          * Seek to given offset (default) or other possibilities as fseek() gives.
112          *
113          * @param       $offset         Offset to seek to (or used as "base" for other seeks)
114          * @param       $whence         Added to offset (default: only use offset to seek to)
115          * @return      $status         Status of file seek: 0 = success, -1 = failed
116          * @throws      UnsupportedOperationException   If this method is called
117          */
118         public function seek ($offset, $whence = SEEK_SET) {
119                 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] offset=' . $offset . ',whence=' . $whence);
120                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
121         }
122
123         /**
124          * Size of file stack
125          *
126          * @return      $size   Size (in bytes) of file
127          * @throws      UnsupportedOperationException   If this method is called
128          */
129         public function size () {
130                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
131         }
132 }
133
134 // [EOF]
135 ?>