(no commit message)
[shipsimu.git] / inc / classes / main / output / class_ConsoleOutput.php
1 <?php
2 /**
3  * This class simply puts HTML code / JavaScript code or CSS code out to the
4  * browser
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0
8  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
9  * @license             GNU GPL 3.0 or any newer version
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class ConsoleOutput extends BaseFrameworkSystem implements OutputStreamer {
25         /**
26          * The instance for the singleton design pattern
27          */
28         private static $consoleInstance = null;
29
30         /**
31          * Assigned variables
32          */
33         private $vars = array();
34
35         /**
36          * Private constructor
37          *
38          * @return      void
39          */
40         private function __construct () {
41                 // Call parent constructor
42                 parent::constructor(__CLASS__);
43
44                 // Set description
45                 $this->setPartDescr("Console-Ausgabe-Handler");
46
47                 // Create an unique ID
48                 $this->createUniqueID();
49         }
50
51         /**
52          * Create a new web output system and set the content type
53          *
54          * @param               $contentType            A valid content-type
55          * @return      $debugInstance          An instance of this middleware class
56          */
57         public final static function createConsoleOutput ($contentType) {
58                 // Cast the content-type to string
59                 $contentType = (string) $contentType;
60                 $contentType = trim($contentType);
61
62                 // Get instance
63                 self::$consoleInstance = new ConsoleOutput();
64
65                 // Set the content type
66                 if (!empty($contentType)) {
67                         @header(sprintf("Content-type: %s",
68                                 $contentType
69                         ));
70                 }
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 final static function getInstance() {
82                 if (is_null(self::$consoleInstance)) {
83                         $contentType = FrameworkConfiguration::getInstance()->readConfig("web_content_type");
84                         self::$consoleInstance = ConsoleOutput::createConsoleOutput($contentType);
85                 }
86                 return self::$consoleInstance;
87         }
88
89         /**
90          * Output the code
91          *
92          * @param               $outStream      Something we shall sent to the console
93          * @return      void
94          */
95         public final function output ($outStream=false) {
96                 if ($outStream === false) {
97                         // Output something here...
98                         foreach ($this->vars as $var=>$value) {
99                                 $this->output("var=".$var.", value=".$value."");
100                         }
101                 } else {
102                         // Output it to the console
103                         printf("%s\n", trim(html_entity_decode(strip_tags($outStream))));
104                 }
105         }
106
107         /**
108          * Assigns a variable for output
109          *
110          * @param               $var            The variable we shall assign
111          * @param               $value  The value to store in the variable
112          * @return      void
113          */
114         public function assignVariable ($var, $value) {
115                 $this->vars[$var] = $value;
116         }
117 }
118
119 // [EOF]
120 ?>