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