1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: final class Tools
24: {
25:
26: const MINUTE = 60;
27:
28:
29: const HOUR = 3600;
30:
31:
32: const DAY = 86400;
33:
34:
35: const WEEK = 604800;
36:
37:
38: const MONTH = 2629800;
39:
40:
41: const YEAR = 31557600;
42:
43:
44: private static $criticalSections;
45:
46:
47:
48: 49: 50:
51: final public function __construct()
52: {
53: throw new \LogicException("Cannot instantiate static class " . get_class($this));
54: }
55:
56:
57:
58: 59: 60: 61: 62:
63: public static function createDateTime($time)
64: {
65: if ($time instanceof \DateTime) {
66: return clone $time;
67:
68: } elseif (is_numeric($time)) {
69: if ($time <= self::YEAR) {
70: $time += time();
71: }
72: return new \DateTime(date('Y-m-d H:i:s', $time));
73:
74: } else { 75: return new \DateTime($time);
76: }
77: }
78:
79:
80:
81: 82: 83: 84: 85:
86: public static function iniFlag($var)
87: {
88: $status = strtolower(ini_get($var));
89: return $status === 'on' || $status === 'true' || $status === 'yes' || (int) $status;
90: }
91:
92:
93:
94: 95: 96: 97: 98: 99:
100: public static function defaultize(&$var, $default)
101: {
102: if ($var === NULL) $var = $default;
103: }
104:
105:
106:
107: 108: 109: 110: 111: 112:
113: public static function compare($l, $operator, $r)
114: {
115: switch ($operator) {
116: case '>':
117: return $l > $r;
118: case '>=':
119: return $l >= $r;
120: case '<':
121: return $l < $r;
122: case '<=':
123: return $l <= $r;
124: case '=':
125: case '==':
126: return $l == $r;
127: case '!':
128: case '!=':
129: case '<>':
130: return $l != $r;
131: }
132: throw new \InvalidArgumentException("Unknown operator $operator.");
133: }
134:
135:
136:
137: 138: 139: 140: 141:
142: public static function detectMimeType($file)
143: {
144: if (!is_file($file)) {
145: throw new \FileNotFoundException("File '$file' not found.");
146: }
147:
148: $info = @getimagesize($file); 149: if (isset($info['mime'])) {
150: return $info['mime'];
151:
152: } elseif (extension_loaded('fileinfo')) {
153: $type = preg_replace('#[\s;].*$#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
154:
155: } elseif (function_exists('mime_content_type')) {
156: $type = mime_content_type($file);
157: }
158:
159: return isset($type) && preg_match('#^\S+/\S+$#', $type) ? $type : 'application/octet-stream';
160: }
161:
162:
163:
164:
165:
166:
167:
168: 169: 170: 171:
172: public static function enterCriticalSection()
173: {
174: if (self::$criticalSections) {
175: throw new \InvalidStateException('Critical section has already been entered.');
176: }
177: $handle = fopen((defined('TEMP_DIR') ? TEMP_DIR : __DIR__) . '/criticalSection.lock', 'w');
178: if (!$handle) {
179: throw new \InvalidStateException('Unable initialize critical section.');
180: }
181: flock(self::$criticalSections = $handle, LOCK_EX);
182: }
183:
184:
185:
186: 187: 188: 189:
190: public static function leaveCriticalSection()
191: {
192: if (!self::$criticalSections) {
193: throw new \InvalidStateException('Critical section has not been initialized.');
194: }
195: fclose(self::$criticalSections);
196: self::$criticalSections = NULL;
197: }
198:
199: }