3 namespace PhpXmlRpc\Helper
;
6 * @todo implement an interface
7 * @todo make constructor private to force users to go through `instance()` ?
11 protected static $instance = null;
14 * This class can be used as singleton, so that later we can move to DI patterns (ish...)
18 public static function instance()
20 if (self
::$instance === null) {
21 self
::$instance = new self();
24 return self
::$instance;
27 // *** Implement the same interface as PSR/LOG, for the sake of interoperability ***
30 * NB: unlike other "traditional" loggers, this one echoes to screen the debug messages instead of logging them.
32 * @param string $message
33 * @param array $context known key: 'encoding'
36 public function debug($message, $context = array())
38 if (isset($context['encoding'])) {
39 $this->debugMessage($message, $context['encoding']);
41 $this->debugMessage($message);
46 * Following the general principle of 'never break stdout', the default behaviour
48 * @param string $message
52 public function warning($message, $context = array())
54 $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Warning: ', $message));
58 * Triggers the writing of a message to php's error log
60 * @param string $message
61 * @param array $context
64 public function error($message, $context = array())
66 $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Error: ', $message));
72 * Echoes a debug message, taking care of escaping it when not in console mode.
73 * NB: if the encoding of the message is not known or wrong, and we are working in web mode, there is no guarantee
74 * of 100% accuracy, which kind of defeats the purpose of debugging
76 * @param string $message
77 * @param string $encoding deprecated
80 * @internal left in purely for BC
82 public function debugMessage($message, $encoding = null)
84 // US-ASCII is a warning for PHP and a fatal for HHVM
85 if ($encoding == 'US-ASCII') {
89 if (PHP_SAPI
!= 'cli') {
91 // avoid warnings on php < 5.4...
92 if (defined('ENT_HTML401')) {
93 $flags = $flags | ENT_HTML401
;
95 if (defined('ENT_SUBSTITUTE')) {
96 $flags = $flags | ENT_SUBSTITUTE
;
98 if ($encoding != null) {
99 print "<PRE>\n".htmlentities($message, $flags, $encoding)."\n</PRE>";
101 print "<PRE>\n".htmlentities($message, $flags)."\n</PRE>";
104 print "\n$message\n";
107 // let the user see this now in case there's a time-out later...
112 * Writes a message to the error log.
114 * @param string $message
117 * @internal left in purely for BC
119 public function errorLog($message)