PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Nette/Utils/Tools.php

https://code.google.com/
PHP | 215 lines | 94 code | 56 blank | 65 comment | 15 complexity | 5ad6de991677b55ffa0416dedbcb83a9 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework.
  4. *
  5. * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license", and/or
  8. * GPL license. For more information please see http://nette.org
  9. */
  10. namespace Nette;
  11. use Nette;
  12. /**
  13. * Tools library.
  14. *
  15. * @author David Grudl
  16. */
  17. final class Tools
  18. {
  19. /** minute in seconds */
  20. const MINUTE = 60;
  21. /** hour in seconds */
  22. const HOUR = 3600;
  23. /** day in seconds */
  24. const DAY = 86400;
  25. /** week in seconds */
  26. const WEEK = 604800;
  27. /** average month in seconds */
  28. const MONTH = 2629800;
  29. /** average year in seconds */
  30. const YEAR = 31557600;
  31. /**
  32. * Static class - cannot be instantiated.
  33. */
  34. final public function __construct()
  35. {
  36. throw new \LogicException("Cannot instantiate static class " . get_class($this));
  37. }
  38. /**
  39. * DateTime object factory.
  40. * @param string|int|DateTime
  41. * @return DateTime
  42. */
  43. public static function createDateTime($time)
  44. {
  45. if ($time instanceof \DateTime) {
  46. return clone $time;
  47. } elseif (is_numeric($time)) {
  48. if ($time <= self::YEAR) {
  49. $time += time();
  50. }
  51. return new \DateTime(date('Y-m-d H:i:s', $time));
  52. } else { // textual or NULL
  53. return new \DateTime($time);
  54. }
  55. }
  56. /**
  57. * Gets the boolean value of a configuration option.
  58. * @param string configuration option name
  59. * @return bool
  60. */
  61. public static function iniFlag($var)
  62. {
  63. $status = strtolower(ini_get($var));
  64. return $status === 'on' || $status === 'true' || $status === 'yes' || $status % 256;
  65. }
  66. /**
  67. * Initializes variable with $default value.
  68. * @param mixed variable
  69. * @param mixed default value
  70. * @return void
  71. */
  72. public static function defaultize(&$var, $default)
  73. {
  74. if ($var === NULL) $var = $default;
  75. }
  76. /**
  77. * Recursive glob(). Finds pathnames matching a pattern.
  78. * @param string
  79. * @param int
  80. * @return array
  81. */
  82. public static function glob($pattern, $flags = 0)
  83. {
  84. // TODO: replace by RecursiveDirectoryIterator
  85. $files = glob($pattern, $flags);
  86. if (!is_array($files)) {
  87. $files = array();
  88. }
  89. $dirs = glob(dirname($pattern) . '/*', $flags | GLOB_ONLYDIR);
  90. if (is_array($dirs)) {
  91. $mask = basename($pattern);
  92. foreach ($dirs as $dir) {
  93. $files = array_merge($files, self::glob($dir . '/' . $mask, $flags));
  94. }
  95. }
  96. return $files;
  97. }
  98. /**
  99. * Returns the MIME content type of file.
  100. * @param string
  101. * @return string
  102. */
  103. public static function detectMimeType($file)
  104. {
  105. if (!is_file($file)) {
  106. throw new \FileNotFoundException("File '$file' not found.");
  107. }
  108. $info = @getimagesize($file); // @ - files smaller than 12 bytes causes read error
  109. if (isset($info['mime'])) {
  110. return $info['mime'];
  111. } elseif (extension_loaded('fileinfo')) {
  112. $type = preg_replace('#[\s;].*$#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
  113. } elseif (function_exists('mime_content_type')) {
  114. $type = mime_content_type($file);
  115. }
  116. return isset($type) && preg_match('#^\S+/\S+$#', $type) ? $type : 'application/octet-stream';
  117. }
  118. /********************* errors and warnings catching ****************d*g**/
  119. /** @var string */
  120. private static $errorMsg;
  121. /**
  122. * Starts catching potential errors/warnings.
  123. * @return void
  124. */
  125. public static function tryError($level = E_ALL)
  126. {
  127. set_error_handler(array(__CLASS__, '_errorHandler'), $level);
  128. self::$errorMsg = NULL;
  129. }
  130. /**
  131. * Returns catched error/warning message.
  132. * @param string catched message
  133. * @return bool
  134. */
  135. public static function catchError(& $message)
  136. {
  137. restore_error_handler();
  138. $message = self::$errorMsg;
  139. self::$errorMsg = NULL;
  140. return $message !== NULL;
  141. }
  142. /**
  143. * Internal error handler. Do not call directly.
  144. * @internal
  145. */
  146. public static function _errorHandler($severity, $message)
  147. {
  148. if (($severity & error_reporting()) !== $severity) {
  149. return NULL;
  150. }
  151. if (ini_get('html_errors')) {
  152. $message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
  153. }
  154. if (($a = strpos($message, ': ')) !== FALSE) {
  155. $message = substr($message, $a + 2);
  156. }
  157. self::$errorMsg = $message;
  158. }
  159. }