Generic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\File;
  36. use Hoa\Stream;
  37. /**
  38. * Class \Hoa\File\Generic.
  39. *
  40. * Describe a super-file.
  41. *
  42. * @copyright Copyright © 2007-2017 Hoa community
  43. * @license New BSD License
  44. */
  45. abstract class Generic
  46. extends Stream
  47. implements Stream\IStream\Pathable,
  48. Stream\IStream\Statable,
  49. Stream\IStream\Touchable
  50. {
  51. /**
  52. * Mode.
  53. *
  54. * @var string
  55. */
  56. protected $_mode = null;
  57. /**
  58. * Get filename component of path.
  59. *
  60. * @return string
  61. */
  62. public function getBasename()
  63. {
  64. return basename($this->getStreamName());
  65. }
  66. /**
  67. * Get directory name component of path.
  68. *
  69. * @return string
  70. */
  71. public function getDirname()
  72. {
  73. return dirname($this->getStreamName());
  74. }
  75. /**
  76. * Get size.
  77. *
  78. * @return int
  79. */
  80. public function getSize()
  81. {
  82. if (false === $this->getStatistic()) {
  83. return false;
  84. }
  85. return filesize($this->getStreamName());
  86. }
  87. /**
  88. * Get informations about a file.
  89. *
  90. * @return array
  91. */
  92. public function getStatistic()
  93. {
  94. return fstat($this->getStream());
  95. }
  96. /**
  97. * Get last access time of file.
  98. *
  99. * @return int
  100. */
  101. public function getATime()
  102. {
  103. return fileatime($this->getStreamName());
  104. }
  105. /**
  106. * Get inode change time of file.
  107. *
  108. * @return int
  109. */
  110. public function getCTime()
  111. {
  112. return filectime($this->getStreamName());
  113. }
  114. /**
  115. * Get file modification time.
  116. *
  117. * @return int
  118. */
  119. public function getMTime()
  120. {
  121. return filemtime($this->getStreamName());
  122. }
  123. /**
  124. * Get file group.
  125. *
  126. * @return int
  127. */
  128. public function getGroup()
  129. {
  130. return filegroup($this->getStreamName());
  131. }
  132. /**
  133. * Get file owner.
  134. *
  135. * @return int
  136. */
  137. public function getOwner()
  138. {
  139. return fileowner($this->getStreamName());
  140. }
  141. /**
  142. * Get file permissions.
  143. *
  144. * @return int
  145. */
  146. public function getPermissions()
  147. {
  148. return fileperms($this->getStreamName());
  149. }
  150. /**
  151. * Get file permissions as a string.
  152. * Result sould be interpreted like this:
  153. * * s: socket;
  154. * * l: symbolic link;
  155. * * -: regular;
  156. * * b: block special;
  157. * * d: directory;
  158. * * c: character special;
  159. * * p: FIFO pipe;
  160. * * u: unknown.
  161. *
  162. * @return string
  163. */
  164. public function getReadablePermissions()
  165. {
  166. $p = $this->getPermissions();
  167. if (($p & 0xC000) == 0xC000) {
  168. $out = 's';
  169. } elseif (($p & 0xA000) == 0xA000) {
  170. $out = 'l';
  171. } elseif (($p & 0x8000) == 0x8000) {
  172. $out = '-';
  173. } elseif (($p & 0x6000) == 0x6000) {
  174. $out = 'b';
  175. } elseif (($p & 0x4000) == 0x4000) {
  176. $out = 'd';
  177. } elseif (($p & 0x2000) == 0x2000) {
  178. $out = 'c';
  179. } elseif (($p & 0x1000) == 0x1000) {
  180. $out = 'p';
  181. } else {
  182. $out = 'u';
  183. }
  184. $out .=
  185. (($p & 0x0100) ? 'r' : '-') .
  186. (($p & 0x0080) ? 'w' : '-') .
  187. (($p & 0x0040) ?
  188. (($p & 0x0800) ? 's' : 'x') :
  189. (($p & 0x0800) ? 'S' : '-')) .
  190. (($p & 0x0020) ? 'r' : '-') .
  191. (($p & 0x0010) ? 'w' : '-') .
  192. (($p & 0x0008) ?
  193. (($p & 0x0400) ? 's' : 'x') :
  194. (($p & 0x0400) ? 'S' : '-')) .
  195. (($p & 0x0004) ? 'r' : '-') .
  196. (($p & 0x0002) ? 'w' : '-') .
  197. (($p & 0x0001) ?
  198. (($p & 0x0200) ? 't' : 'x') :
  199. (($p & 0x0200) ? 'T' : '-'));
  200. return $out;
  201. }
  202. /**
  203. * Check if the file is readable.
  204. *
  205. * @return bool
  206. */
  207. public function isReadable()
  208. {
  209. return is_readable($this->getStreamName());
  210. }
  211. /**
  212. * Check if the file is writable.
  213. *
  214. * @return bool
  215. */
  216. public function isWritable()
  217. {
  218. return is_writable($this->getStreamName());
  219. }
  220. /**
  221. * Check if the file is executable.
  222. *
  223. * @return bool
  224. */
  225. public function isExecutable()
  226. {
  227. return is_executable($this->getStreamName());
  228. }
  229. /**
  230. * Clear file status cache.
  231. *
  232. * @return void
  233. */
  234. public function clearStatisticCache()
  235. {
  236. clearstatcache(true, $this->getStreamName());
  237. return;
  238. }
  239. /**
  240. * Clear all files status cache.
  241. *
  242. * @return void
  243. */
  244. public static function clearAllStatisticCaches()
  245. {
  246. clearstatcache();
  247. return;
  248. }
  249. /**
  250. * Set access and modification time of file.
  251. *
  252. * @param int $time Time. If equals to -1, time() should be used.
  253. * @param int $atime Access time. If equals to -1, $time should be
  254. * used.
  255. * @return bool
  256. */
  257. public function touch($time = -1, $atime = -1)
  258. {
  259. if ($time == -1) {
  260. $time = time();
  261. }
  262. if ($atime == -1) {
  263. $atime = $time;
  264. }
  265. return touch($this->getStreamName(), $time, $atime);
  266. }
  267. /**
  268. * Copy file.
  269. * Return the destination file path if succeed, false otherwise.
  270. *
  271. * @param string $to Destination path.
  272. * @param bool $force Force to copy if the file $to already exists.
  273. * Use the \Hoa\Stream\IStream\Touchable::*OVERWRITE
  274. * constants.
  275. * @return bool
  276. */
  277. public function copy($to, $force = Stream\IStream\Touchable::DO_NOT_OVERWRITE)
  278. {
  279. $from = $this->getStreamName();
  280. if ($force === Stream\IStream\Touchable::DO_NOT_OVERWRITE &&
  281. true === file_exists($to)) {
  282. return true;
  283. }
  284. if (null === $this->getStreamContext()) {
  285. return @copy($from, $to);
  286. }
  287. return @copy($from, $to, $this->getStreamContext()->getContext());
  288. }
  289. /**
  290. * Move a file.
  291. *
  292. * @param string $name New name.
  293. * @param bool $force Force to move if the file $name already
  294. * exists.
  295. * Use the \Hoa\Stream\IStream\Touchable::*OVERWRITE
  296. * constants.
  297. * @param bool $mkdir Force to make directory if does not exist.
  298. * Use the \Hoa\Stream\IStream\Touchable::*DIRECTORY
  299. * constants.
  300. * @return bool
  301. */
  302. public function move(
  303. $name,
  304. $force = Stream\IStream\Touchable::DO_NOT_OVERWRITE,
  305. $mkdir = Stream\IStream\Touchable::DO_NOT_MAKE_DIRECTORY
  306. ) {
  307. $from = $this->getStreamName();
  308. if ($force === Stream\IStream\Touchable::DO_NOT_OVERWRITE &&
  309. true === file_exists($name)) {
  310. return false;
  311. }
  312. if (Stream\IStream\Touchable::MAKE_DIRECTORY === $mkdir) {
  313. Directory::create(
  314. dirname($name),
  315. Directory::MODE_CREATE_RECURSIVE
  316. );
  317. }
  318. if (null === $this->getStreamContext()) {
  319. return @rename($from, $name);
  320. }
  321. return @rename($from, $name, $this->getStreamContext()->getContext());
  322. }
  323. /**
  324. * Delete a file.
  325. *
  326. * @return bool
  327. */
  328. public function delete()
  329. {
  330. if (null === $this->getStreamContext()) {
  331. return @unlink($this->getStreamName());
  332. }
  333. return @unlink(
  334. $this->getStreamName(),
  335. $this->getStreamContext()->getContext()
  336. );
  337. }
  338. /**
  339. * Change file group.
  340. *
  341. * @param mixed $group Group name or number.
  342. * @return bool
  343. */
  344. public function changeGroup($group)
  345. {
  346. return chgrp($this->getStreamName(), $group);
  347. }
  348. /**
  349. * Change file mode.
  350. *
  351. * @param int $mode Mode (in octal!).
  352. * @return bool
  353. */
  354. public function changeMode($mode)
  355. {
  356. return chmod($this->getStreamName(), $mode);
  357. }
  358. /**
  359. * Change file owner.
  360. *
  361. * @param mixed $user User.
  362. * @return bool
  363. */
  364. public function changeOwner($user)
  365. {
  366. return chown($this->getStreamName(), $user);
  367. }
  368. /**
  369. * Change the current umask.
  370. *
  371. * @param int $umask Umask (in octal!). If null, given the current
  372. * umask value.
  373. * @return int
  374. */
  375. public static function umask($umask = null)
  376. {
  377. if (null === $umask) {
  378. return umask();
  379. }
  380. return umask($umask);
  381. }
  382. /**
  383. * Check if it is a file.
  384. *
  385. * @return bool
  386. */
  387. public function isFile()
  388. {
  389. return is_file($this->getStreamName());
  390. }
  391. /**
  392. * Check if it is a link.
  393. *
  394. * @return bool
  395. */
  396. public function isLink()
  397. {
  398. return is_link($this->getStreamName());
  399. }
  400. /**
  401. * Check if it is a directory.
  402. *
  403. * @return bool
  404. */
  405. public function isDirectory()
  406. {
  407. return is_dir($this->getStreamName());
  408. }
  409. /**
  410. * Check if it is a socket.
  411. *
  412. * @return bool
  413. */
  414. public function isSocket()
  415. {
  416. return filetype($this->getStreamName()) == 'socket';
  417. }
  418. /**
  419. * Check if it is a FIFO pipe.
  420. *
  421. * @return bool
  422. */
  423. public function isFIFOPipe()
  424. {
  425. return filetype($this->getStreamName()) == 'fifo';
  426. }
  427. /**
  428. * Check if it is character special file.
  429. *
  430. * @return bool
  431. */
  432. public function isCharacterSpecial()
  433. {
  434. return filetype($this->getStreamName()) == 'char';
  435. }
  436. /**
  437. * Check if it is block special.
  438. *
  439. * @return bool
  440. */
  441. public function isBlockSpecial()
  442. {
  443. return filetype($this->getStreamName()) == 'block';
  444. }
  445. /**
  446. * Check if it is an unknown type.
  447. *
  448. * @return bool
  449. */
  450. public function isUnknown()
  451. {
  452. return filetype($this->getStreamName()) == 'unknown';
  453. }
  454. /**
  455. * Set the open mode.
  456. *
  457. * @param string $mode Open mode. Please, see the child::MODE_*
  458. * constants.
  459. * @return string
  460. */
  461. protected function setMode($mode)
  462. {
  463. $old = $this->_mode;
  464. $this->_mode = $mode;
  465. return $old;
  466. }
  467. /**
  468. * Get the open mode.
  469. *
  470. * @return string
  471. */
  472. public function getMode()
  473. {
  474. return $this->_mode;
  475. }
  476. /**
  477. * Get inode.
  478. *
  479. * @return int
  480. */
  481. public function getINode()
  482. {
  483. return fileinode($this->getStreamName());
  484. }
  485. /**
  486. * Check if the system is case sensitive or not.
  487. *
  488. * @return bool
  489. */
  490. public static function isCaseSensitive()
  491. {
  492. return !(
  493. file_exists(mb_strtolower(__FILE__)) &&
  494. file_exists(mb_strtoupper(__FILE__))
  495. );
  496. }
  497. /**
  498. * Get a canonicalized absolute pathname.
  499. *
  500. * @return string
  501. */
  502. public function getRealPath()
  503. {
  504. if (false === $out = realpath($this->getStreamName())) {
  505. return $this->getStreamName();
  506. }
  507. return $out;
  508. }
  509. /**
  510. * Get file extension (if exists).
  511. *
  512. * @return string
  513. */
  514. public function getExtension()
  515. {
  516. return pathinfo(
  517. $this->getStreamName(),
  518. PATHINFO_EXTENSION
  519. );
  520. }
  521. /**
  522. * Get filename without extension.
  523. *
  524. * @return string
  525. */
  526. public function getFilename()
  527. {
  528. $file = basename($this->getStreamName());
  529. if (defined('PATHINFO_FILENAME')) {
  530. return pathinfo($file, PATHINFO_FILENAME);
  531. }
  532. if (strstr($file, '.')) {
  533. return substr($file, 0, strrpos($file, '.'));
  534. }
  535. return $file;
  536. }
  537. }