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