Initial import of current development status
[shipsimu.git] / ship-simu / 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 class ConsoleOutput extends BaseFrameworkSystem implements OutputStreamer {
7         /**
8          * The instance for the singleton design pattern
9          */
10         private static $consoleInstance = null;
11
12         /**
13          * Assigned variables
14          */
15         private $vars = array();
16
17         /**
18          * Private constructor
19          *
20          * @return      void
21          */
22         private function __construct () {
23                 // Call parent constructor
24                 parent::constructor(__CLASS__);
25
26                 // Set description
27                 $this->setPartDescr("Console-Ausgabe-Handler");
28
29                 // Create an unique ID
30                 $this->createUniqueID();
31         }
32
33         /**
34          * Create a new web output system and set the content type
35          *
36          * @param               $contentType            A valid content-type
37          * @return      $debugInstance          An instance of this middleware class
38          */
39         public final static function createConsoleOutput ($contentType) {
40                 // Cast the content-type to string
41                 $contentType = (string) $contentType;
42                 $contentType = trim($contentType);
43
44                 // Get instance
45                 self::$consoleInstance = new ConsoleOutput();
46
47                 // Set the content type
48                 if (!empty($contentType)) {
49                         @header(sprintf("Content-type: %s",
50                                 $contentType
51                         ));
52                 }
53
54                 // Return instance
55                 return self::$consoleInstance;
56         }
57
58         /**
59          * Getter for an instance of this class
60          *
61          * @return      $consoleInstance                An instance of this class
62          */
63         public final static function getInstance() {
64                 if (is_null(self::$consoleInstance)) {
65                         $contentType = FrameworkConfiguration::getInstance()->readConfig("web_content_type");
66                         self::$consoleInstance = ConsoleOutput::createConsoleOutput($contentType);
67                 }
68                 return self::$consoleInstance;
69         }
70
71         /**
72          * Output the code
73          *
74          * @param               $outStream      Something we shall sent to the console
75          * @return      void
76          */
77         public final function output ($outStream=false) {
78                 if ($outStream === false) {
79                         // Output something here...
80                         foreach ($this->vars as $var=>$value) {
81                                 $this->output("var=".$var.", value=".$value."");
82                         }
83                 } else {
84                         // Output it to the console
85                         printf("%s\n", trim(html_entity_decode(strip_tags($outStream))));
86                 }
87         }
88
89         /**
90          * Assigns a variable for output
91          *
92          * @param               $var            The variable we shall assign
93          * @param               $value  The value to store in the variable
94          * @return      void
95          */
96         public function assignVariable ($var, $value) {
97                 $this->vars[$var] = $value;
98         }
99 }
100
101 // [EOF]
102 ?>