diff --git a/src/TgLog/Log.php b/src/TgLog/Log.php index 21c4b5b..4b3d72a 100644 --- a/src/TgLog/Log.php +++ b/src/TgLog/Log.php @@ -102,10 +102,10 @@ public function __construct($logLevel = self::INFO, $appName = NULL) { protected function log($sev, $s, $o = null) { if (!is_string($s)) $s = json_encode($s, JSON_PRETTY_PRINT); if ($o != null) { - if ($o instanceof \Exception) { - $s .= get_class($o).' "'.$o->getCode().' - '.$o->getMessage()."\" at\n".$o->getTraceAsString(); + if ($o instanceof \Throwable) { + $s .= ': '.get_class($o).' "'.$o->getCode().' - '.$o->getMessage()."\" at\n".$o->getTraceAsString(); } else { - $s .= json_encode($o, JSON_PRETTY_PRINT); + $s .= ': '.json_encode($o, JSON_PRETTY_PRINT); } } $this->messages[$sev][] = $s; diff --git a/src/TgLog/LogWrapper.php b/src/TgLog/LogWrapper.php new file mode 100644 index 0000000..5af5e56 --- /dev/null +++ b/src/TgLog/LogWrapper.php @@ -0,0 +1,66 @@ +log = $log; + } + + /** + * @see \TgLog\Logger::warn() + */ + public function warn($s, $object = NULL) { + $this->log->logWarn($s, $object); + } + + /** + * @see \TgLog\Logger::debug() + */ + public function debug($s, $object = NULL) { + $this->log->logDebug($s, $object); + } + + /** + * @see \TgLog\Logger::error() + */ + public function error($s, $object = NULL) { + $this->log->logError($s, $object); + } + + /** + * @see \TgLog\Logger::info() + */ + public function info($s, $object = NULL) { + $this->log->logInfo($s, $object); + } +} + diff --git a/src/TgLog/Logger.php b/src/TgLog/Logger.php new file mode 100644 index 0000000..fd1835e --- /dev/null +++ b/src/TgLog/Logger.php @@ -0,0 +1,38 @@ +method = $_SERVER['REQUEST_METHOD']; + $this->headers = getallheaders(); + $this->protocol = $this->initProtocol(); + $this->httpHost = $_SERVER['HTTP_HOST']; + $this->host = $this->initHost(); if (isset($_SERVER['REQUEST_URI'])) { - $this->uri = $_SERVER['REQUEST_URI']; + $this->uri = $_SERVER['REQUEST_URI']; } else { - $this->uri = Request::DEFAULT_REQUEST_URI; + $this->uri = Request::DEFAULT_REQUEST_URI; } - $uri_parts = explode('?', $this->uri, 2); - $this->path = $uri_parts[0]; - $this->params = count($uri_parts) > 1 ? $uri_parts[1] : ''; - $this->langCode = 'en'; - $this->postParams = null; - $this->startTime = time(); - } + $uri_parts = explode('?', $this->uri, 2); + $this->path = $uri_parts[0]; + $this->pathElements = $this->initPathElements(); + $this->params = count($uri_parts) > 1 ? $uri_parts[1] : ''; + $this->getParams = $this->initGetParams(); + $this->postParams = NULL; + $this->body = NULL; + $this->documentRoot = $this->initDocumentRoot(); + $this->webRoot = $this->initWebRoot(TRUE); + $this->localWebRoot = $this->initWebRoot(FALSE); + $this->webRootUri = $this->initWebRootUri(); + $this->appRoot = $this->documentRoot; + $this->relativeAppPath = ''; - /** Create the instance from global vars */ - public static function createFromGlobals() { - return new Request(); + $this->startTime = time(); + + // Will be deprecated + $this->langCode = 'en'; } /** @@ -67,30 +108,22 @@ public static function createFromGlobals() { * host is returned then.
* @return string the Host requested by the user. */ - public function getHost() { + protected function initHost() { if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { $forwarded = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']); return trim($forwarded[count($forwarded)-1]); } - return $this->getHttpHost(); - } - - /** - * Returns the hostname as given in HTTP_HOST. - * @return string the HTTP_HOST variable. - */ - public function getHttpHost() { - return $_SERVER['HTTP_HOST']; + return $this->httpHost; } /** * Returns the protocol (http, https) being used by the user. *The protocol can be switched at reverse proxies, that's * why the HTTP_X_FORWARDED_PROTO variable is checked. - * Otherwise t will be the REQUEST_SCHEME.
+ * Otherwise it will be the REQUEST_SCHEME. * @return string the protocol as used by the user. */ - public function getProtocol() { + protected function initProtocol() { if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { return $_SERVER['HTTP_X_FORWARDED_PROTO']; } @@ -102,7 +135,7 @@ public function getProtocol() { *E.g. /my/path/index.html will return three elements: my, path and index.
* @return array the path elements. */ - public function getPathElements() { + protected function initPathElements() { $path = substr($this->path, 1); if (substr($path, strlen($path)-5) == '.html') $path = substr($path, 0, strlen($path)-5); if (substr($path, strlen($path)-1) == '/') $path = substr($path, 0, strlen($path)-1); @@ -116,7 +149,7 @@ public function getPathElements() { * @return TRUE when parameter was set. */ public function hasGetParam($key) { - $params = $this->getParams(); + $params = $this->getParams; return isset($params[$key]); } @@ -127,7 +160,7 @@ public function hasGetParam($key) { * @return mixed the parameter value or its default. */ public function getGetParam($key, $default = NULL) { - $params = $this->getParams(); + $params = $this->getParams; return isset($params[$key]) ? $params[$key] : $default; } @@ -135,19 +168,8 @@ public function getGetParam($key, $default = NULL) { * Returns the parameters as an array. * @return array array of parameters. */ - public function getParams() { - if ($this->paramsArray == null) { - $this->paramsArray = self::parseQueryString($this->params); - } - return $this->paramsArray; - } - - /** - * Returns the request method, e.g. HEAD, GET, POST. - * @param string the request method. - */ - public function getMethod() { - return $_SERVER['REQUEST_METHOD']; + protected function initGetParams() { + return self::parseQueryString($this->params); } /** @@ -175,9 +197,9 @@ public function getPostParam($key, $default = NULL) { * @return array post parameters */ public function getPostParams() { - if ($this->postParams == null) { + if ($this->postParams == NULL) { $this->postParams = array(); - $headers = getallheaders(); + $headers = $this->headers; // Check that we have content-length if (isset($headers['Content-Length'])) { $len = intval($headers['Content-Length']); @@ -197,8 +219,8 @@ public function getPostParams() { * @return string the request body. */ public function getBody() { - if (in_array($this->getMethod(), array('POST', 'PUT'))) { - if ($this->body == null) { + if (in_array($this->method, array('POST', 'PUT'))) { + if ($this->body == NULL) { $this->body = file_get_contents('php://input'); } } @@ -222,9 +244,8 @@ public static function parseQueryString($s) { * @return string the value of the header. */ public function getHeader($key) { - $headers = getallheaders(); - if (isset($headers[$key])) return $headers[$key]; - return null; + if (isset($this->headers[$key])) return $this->headers[$key]; + return NULL; } /** @@ -250,5 +271,65 @@ public function getParam($key, $default = NULL, $getPrecedes = true) { public function getElapsedTime() { return time() - $this->startTime; } + + /** + * Returns the document root - this is the real path name of the web root. + * @return string the document root or context document root if available. + */ + protected function initDocumentRoot() { + if (isset($_SERVER['CONTEXT_DOCUMENT_ROOT'])) { + return $_SERVER['CONTEXT_DOCUMENT_ROOT']; + } + return $_SERVER['DOCUMENT_ROOT']; + } + + /** + * Returns the web root - that is the web path where the current + * script is rooted and usually the base path for an application. + *$_SERVER['PHP_SELF'] or $_SERVER['SCRIPT_NAME']
will + * be misleading as they would not tell the real document root. + * @return string the presumed web root. + */ + protected function initWebRoot($considerForwarding = TRUE) { + if ($considerForwarding) { + $rootDef = $_SERVER['HTTP_X_FORWARDED_ROOT']; + if ($rootDef) { + $arr = explode(',', $rootDef); + return $arr[1]; + } + } + $docRoot = $this->documentRoot; + $fileDir = dirname($_SERVER['SCRIPT_FILENAME']); + $webRoot = substr($fileDir, strlen($docRoot)); + if (isset($_SERVER['CONTEXT'])) { + $webRoot = $_SERVER['CONTEXT'].$webRoot; + } + return $webRoot; + } + + /** + * Returns the full URL of the web root. + * @return string the URL to the root dir. + */ + protected function initWebRootUri() { + $protocol = $this->protocol; + $host = $this->host; + return $protocol.'://'.$host.$this->webRoot; + } + + /** + * Initializes the appRoot and relativeAppPath according to the root of the app. + * The appRoot can differ from document root as it can be installed in a subdir. + * @param string $appRoot - the application root directory (absolute path) + */ + public function setAppRoot($appRoot) { + $appRootLen = strlen($appRoot); + $docRootLen = strlen($this->documentRoot); + if (($docRootLen < $appRootLen) && (strpos($appRoot, $this->documentRoot) === 0)) { + $this->relativeAppPath = substr($appRoot, $docRootLen); + } else { + $this->relativeAppPath = ''; + } + } }