PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Nette/Tools.php

https://bitbucket.org/rampa/aukce
PHP | 189 lines | 76 code | 48 blank | 65 comment | 11 complexity | 6d432936a259ad33d32233d7bb2bcdc8 MD5 | raw file
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette
  10. */
  11. namespace Nette;
  12. use Nette;
  13. /**
  14. * Tools library.
  15. *
  16. * @copyright Copyright (c) 2004, 2010 David Grudl
  17. * @package Nette
  18. */
  19. final class Tools
  20. {
  21. /** minute in seconds */
  22. const MINUTE = 60;
  23. /** hour in seconds */
  24. const HOUR = 3600;
  25. /** day in seconds */
  26. const DAY = 86400;
  27. /** week in seconds */
  28. const WEEK = 604800;
  29. /** average month in seconds */
  30. const MONTH = 2629800;
  31. /** average year in seconds */
  32. const YEAR = 31557600;
  33. /**
  34. * Static class - cannot be instantiated.
  35. */
  36. final public function __construct()
  37. {
  38. throw new \LogicException("Cannot instantiate static class " . get_class($this));
  39. }
  40. /**
  41. * DateTime object factory.
  42. * @param string|int|DateTime
  43. * @return DateTime
  44. */
  45. public static function createDateTime($time)
  46. {
  47. if ($time instanceof \DateTime) {
  48. return clone $time;
  49. } elseif (is_numeric($time)) {
  50. if ($time <= self::YEAR) {
  51. $time += time();
  52. }
  53. return new \DateTime(date('Y-m-d H:i:s', $time));
  54. } else { // textual or NULL
  55. return new \DateTime($time);
  56. }
  57. }
  58. /**
  59. * Gets the boolean value of a configuration option.
  60. * @param string configuration option name
  61. * @return bool
  62. */
  63. public static function iniFlag($var)
  64. {
  65. $status = strtolower(ini_get($var));
  66. return $status === 'on' || $status === 'true' || $status === 'yes' || $status % 256;
  67. }
  68. /**
  69. * Initializes variable with $default value.
  70. *
  71. * @param mixed variable
  72. * @param mixed default value
  73. * @return void
  74. */
  75. public static function defaultize(&$var, $default)
  76. {
  77. if ($var === NULL) $var = $default;
  78. }
  79. /**
  80. * Recursive glob(). Finds pathnames matching a pattern.
  81. * @param string
  82. * @param int
  83. * @return array
  84. */
  85. public static function glob($pattern, $flags = 0)
  86. {
  87. // TODO: replace by RecursiveDirectoryIterator
  88. $files = glob($pattern, $flags);
  89. if (!is_array($files)) {
  90. $files = array();
  91. }
  92. $dirs = glob(dirname($pattern) . '/*', $flags | GLOB_ONLYDIR);
  93. if (is_array($dirs)) {
  94. $mask = basename($pattern);
  95. foreach ($dirs as $dir) {
  96. $files = array_merge($files, self::glob($dir . '/' . $mask, $flags));
  97. }
  98. }
  99. return $files;
  100. }
  101. /********************* errors and warnings catching ****************d*g**/
  102. /** @var string */
  103. private static $errorMsg;
  104. /**
  105. * Starts catching potential errors/warnings.
  106. *
  107. * @return void
  108. */
  109. public static function tryError($level = E_ALL)
  110. {
  111. set_error_handler(array(__CLASS__, '_errorHandler'), $level);
  112. self::$errorMsg = NULL;
  113. }
  114. /**
  115. * Returns catched error/warning message.
  116. *
  117. * @param string catched message
  118. * @return bool
  119. */
  120. public static function catchError(& $message)
  121. {
  122. restore_error_handler();
  123. $message = self::$errorMsg;
  124. self::$errorMsg = NULL;
  125. return $message !== NULL;
  126. }
  127. /**
  128. * Internal error handler. Do not call directly.
  129. * @internal
  130. */
  131. public static function _errorHandler($code, $message)
  132. {
  133. if (ini_get('html_errors')) {
  134. $message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
  135. }
  136. if (($a = strpos($message, ': ')) !== FALSE) {
  137. $message = substr($message, $a + 2);
  138. }
  139. self::$errorMsg = $message;
  140. }
  141. }