From 19e77a6a83e5e1b233285225cc9cda4eedff921e Mon Sep 17 00:00:00 2001 From: Olivier Paroz Date: Sun, 26 Apr 2015 03:07:31 +0200 Subject: Remove utility folder --- utility/normalizer.php | 185 --------------------------------------------- utility/smarterlogger.php | 187 ---------------------------------------------- 2 files changed, 372 deletions(-) delete mode 100644 utility/normalizer.php delete mode 100644 utility/smarterlogger.php (limited to 'utility') diff --git a/utility/normalizer.php b/utility/normalizer.php deleted file mode 100644 index 0aa975f1..00000000 --- a/utility/normalizer.php +++ /dev/null @@ -1,185 +0,0 @@ - - * @author Jordi Boggiano - * - * @copyright Olivier Paroz 2015 - * @copyright Jordi Boggiano 2014-2015 - */ - -namespace OCA\GalleryPlus\Utility; - -/** - * Lets us call the main logger without having to add the context at every - * request - * - * @package OCA\GalleryPlus\Utility - */ -class Normalizer { - - /** - * Converts Objects, Arrays and Exceptions to String - * - * @param $data - * @param int $depth - * - * @return string|array - */ - public function normalize($data, $depth = 0) { - $scalar = $this->normalizeScalar($data); - if (!is_array($scalar)) { - return $scalar; - } - $decisionArray = [ - $this->normalizeTraversable($data, $depth), - $this->normalizeObject($data, $depth), - $this->normalizeResource($data), - ]; - - foreach ($decisionArray as $dataType) { - if ($dataType !== null) { - return $dataType; - } - } - - return '[unknown(' . gettype($data) . ')]'; - } - - /** - * Returns various, filtered, scalar elements - * - * We're returning an array here to detect failure because null is a scalar and so is false - * - * @param $data - * - * @return mixed - */ - private function normalizeScalar($data) { - if (null === $data || is_scalar($data)) { - /*// utf8_encode only works for Latin1 so we rely on mbstring - if (is_string($data)) { - $data = mb_convert_encoding($data, "UTF-8"); - }*/ - - return $data; - } - - return []; - } - - /** - * Returns an array containing normalized elements - * - * @param $data - * @param int $depth - * - * @return array|null - */ - private function normalizeTraversable($data, $depth = 0) { - if (is_array($data) || $data instanceof \Traversable) { - return $this->normalizeTraversableElement($data, $depth); - } - - return null; - } - - /** - * Converts each element of a traversable variable to String - * - * @param $data - * @param int $depth - * - * @return array - */ - private function normalizeTraversableElement($data, $depth) { - $maxArrayRecursion = 20; - $count = 1; - $normalized = []; - foreach ($data as $key => $value) { - if ($count++ >= $maxArrayRecursion) { - $normalized['...'] = - 'Over ' . $maxArrayRecursion . ' items, aborting normalization'; - break; - } - $normalized[$key] = $this->normalize($value, $depth); - } - - return $normalized; - } - - /** - * Converts an Object to String - * - * @param mixed $data - * @param int $depth - * - * @return array|null - */ - private function normalizeObject($data, $depth) { - if (is_object($data)) { - if ($data instanceof \Exception) { - return $this->normalizeException($data); - } - // We don't need to go too deep in the recursion - $maxObjectRecursion = 2; - $response = $data; - $arrayObject = new \ArrayObject($data); - $serializedObject = $arrayObject->getArrayCopy(); - if ($depth < $maxObjectRecursion) { - $depth++; - $response = $this->normalize($serializedObject, $depth); - } - - // Don't convert to json here as we would double encode - return [sprintf("[object] (%s)", get_class($data)), $response]; - } - - return null; - } - - /** - * Converts an Exception to String - * - * @param \Exception $exception - * - * @return string[] - */ - private function normalizeException(\Exception $exception) { - $data = [ - 'class' => get_class($exception), - 'message' => $exception->getMessage(), - 'code' => $exception->getCode(), - 'file' => $exception->getFile() . ':' . $exception->getLine(), - ]; - $trace = $exception->getTraceAsString(); - $data['trace'][] = $trace; - - $previous = $exception->getPrevious(); - if ($previous) { - $data['previous'] = $this->normalizeException($previous); - } - - return $data; - } - - /** - * Converts a resource to a String - * - * @param $data - * - * @return string|null - */ - private function normalizeResource($data) { - if (is_resource($data)) { - return "[resource] " . substr((string)$data, 0, 40); - } - - return null; - } - -} diff --git a/utility/smarterlogger.php b/utility/smarterlogger.php deleted file mode 100644 index 0f571ae8..00000000 --- a/utility/smarterlogger.php +++ /dev/null @@ -1,187 +0,0 @@ - - * - * @copyright Olivier Paroz 2015 - */ - -namespace OCA\GalleryPlus\Utility; - -use OCP\ILogger; - -/** - * Lets us call the main logger without having to add the context at every - * request - * - * @package OCA\GalleryPlus\Utility - */ -class SmarterLogger implements ILogger { - - /** - * @var ILogger - */ - private $logger; - /** - * @var Normalizer - */ - private $normalizer; - - /*** - * Constructor - * - * @param string $appName - * @param ILogger $logger - * @param Normalizer $normalizer - */ - public function __construct( - $appName, - ILogger $logger, - Normalizer $normalizer - ) { - $this->appName = $appName; - $this->logger = $logger; - $this->normalizer = $normalizer; - } - - /** - * System is unusable. - * - * @param string $message - * @param array $context - * - * @return null - */ - public function emergency($message, array $context = []) { - $this->log(\OCP\Util::FATAL, $message, $context); - } - - /** - * Action must be taken immediately. - * - * @param string $message - * @param array $context - * - * @return null - */ - public function alert($message, array $context = []) { - $this->log(\OCP\Util::ERROR, $message, $context); - } - - /** - * Critical conditions. - * - * @param string $message - * @param array $context - * - * @return null - */ - public function critical($message, array $context = []) { - $this->log(\OCP\Util::ERROR, $message, $context); - } - - /** - * Runtime errors that do not require immediate action but should typically - * be logged and monitored. - * - * @param string $message - * @param array $context - * - * @return null - */ - public function error($message, array $context = []) { - $this->log(\OCP\Util::ERROR, $message, $context); - } - - /** - * Exceptional occurrences that are not errors. - * - * @param string $message - * @param array $context - * - * @return null - */ - public function warning($message, array $context = []) { - $this->log(\OCP\Util::WARN, $message, $context); - } - - /** - * Normal but significant events. - * - * @param string $message - * @param array $context - * - * @return null - */ - public function notice($message, array $context = []) { - $this->log(\OCP\Util::INFO, $message, $context); - } - - /** - * Interesting events. - * - * @param string $message - * @param array $context - * - * @return null - */ - public function info($message, array $context = []) { - $this->log(\OCP\Util::INFO, $message, $context); - } - - /** - * Detailed debug information. - * - * @param string $message - * @param array $context - * - * @return null - */ - public function debug($message, array $context = []) { - $this->log(\OCP\Util::DEBUG, $message, $context); - } - - /** - * Converts the received log message to string before sending it to the - * ownCloud logger - * - * @param mixed $level - * @param string $message - * @param array $context - * - * @return mixed - */ - public function log($level, $message, array $context = []) { - array_walk($context, [$this, 'contextNormalizer']); - - if (!isset($context['app'])) { - $context['app'] = $this->appName; - } - - $this->logger->log($level, $message, $context); - } - - /** - * Normalises the context parameters and JSON encodes and cleans up the - * result - * - * @todo: could maybe do a better job removing slashes - * - * @param array $data - * - * @return string|null - */ - private function contextNormalizer(&$data) { - $data = $this->normalizer->normalize($data); - if (!is_string($data)) { - $data = @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); - // Removing null byte and double slashes from object properties - $data = str_replace(['\\u0000', '\\\\'], ["", "\\"], $data); - } - } - -} -- cgit v1.2.3