PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/libraries/gantry5/vendor/rockettheme/toolbox/StreamWrapper/src/Stream.php

https://gitlab.com/ricardosanchez/prueba
PHP | 239 lines | 162 code | 55 blank | 22 comment | 19 complexity | f6145f85e92edfff77b62c8b4815ff3b MD5 | raw file
  1. <?php
  2. namespace RocketTheme\Toolbox\StreamWrapper;
  3. use RocketTheme\Toolbox\ResourceLocator\ResourceLocatorInterface;
  4. /**
  5. * Implements Read/Write Streams.
  6. *
  7. * @package RocketTheme\Toolbox\StreamWrapper
  8. * @author RocketTheme
  9. * @license MIT
  10. */
  11. class Stream implements StreamInterface
  12. {
  13. /**
  14. * A generic resource handle.
  15. *
  16. * @var Resource
  17. */
  18. protected $handle = null;
  19. /**
  20. * @var ResourceLocatorInterface
  21. */
  22. protected static $locator;
  23. /**
  24. * @param ResourceLocatorInterface $locator
  25. */
  26. public static function setLocator(ResourceLocatorInterface $locator)
  27. {
  28. static::$locator = $locator;
  29. }
  30. public function stream_open($uri, $mode, $options, &$opened_url)
  31. {
  32. $path = $this->getPath($uri, $mode);
  33. if (!$path) {
  34. return false;
  35. }
  36. $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
  37. return (bool) $this->handle;
  38. }
  39. public function stream_close()
  40. {
  41. return fclose($this->handle);
  42. }
  43. public function stream_lock($operation)
  44. {
  45. if (in_array($operation, [LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB])) {
  46. return flock($this->handle, $operation);
  47. }
  48. return false;
  49. }
  50. public function stream_metadata($uri, $option, $value)
  51. {
  52. switch ($option) {
  53. case STREAM_META_TOUCH:
  54. list ($time, $atime) = $value;
  55. return touch($uri, $time, $atime);
  56. case STREAM_META_OWNER_NAME:
  57. case STREAM_META_OWNER:
  58. return chown($uri, $value);
  59. case STREAM_META_GROUP_NAME:
  60. case STREAM_META_GROUP:
  61. return chgrp($uri, $value);
  62. case STREAM_META_ACCESS:
  63. return chmod($uri, $value);
  64. }
  65. return false;
  66. }
  67. public function stream_read($count)
  68. {
  69. return fread($this->handle, $count);
  70. }
  71. public function stream_write($data)
  72. {
  73. return fwrite($this->handle, $data);
  74. }
  75. public function stream_eof()
  76. {
  77. return feof($this->handle);
  78. }
  79. public function stream_seek($offset, $whence)
  80. {
  81. // fseek returns 0 on success and -1 on a failure.
  82. return !fseek($this->handle, $offset, $whence);
  83. }
  84. public function stream_flush()
  85. {
  86. return fflush($this->handle);
  87. }
  88. public function stream_tell()
  89. {
  90. return ftell($this->handle);
  91. }
  92. public function stream_stat()
  93. {
  94. return fstat($this->handle);
  95. }
  96. public function unlink($uri)
  97. {
  98. $path = $this->getPath($uri);
  99. if (!$path) {
  100. return false;
  101. }
  102. return unlink($path);
  103. }
  104. public function rename($fromUri, $toUri)
  105. {
  106. $fromPath = $this->getPath($fromUri);
  107. $toPath = $this->getPath($toUri);
  108. if (!($fromPath && $toPath)) {
  109. return false;
  110. }
  111. return rename($fromPath, $toPath);
  112. }
  113. public function mkdir($uri, $mode, $options)
  114. {
  115. $recursive = (bool) ($options & STREAM_MKDIR_RECURSIVE);
  116. $path = $this->getPath($uri, $recursive ? $mode : null);
  117. if (!$path) {
  118. return false;
  119. }
  120. return ($options & STREAM_REPORT_ERRORS) ? mkdir($path, $mode, $recursive) : @mkdir($path, $mode, $recursive);
  121. }
  122. public function rmdir($uri, $options)
  123. {
  124. $path = $this->getPath($uri);
  125. if (!$path) {
  126. return false;
  127. }
  128. return ($options & STREAM_REPORT_ERRORS) ? rmdir($path) : @rmdir($path);
  129. }
  130. public function url_stat($uri, $flags)
  131. {
  132. $path = $this->getPath($uri);
  133. if (!$path) {
  134. return false;
  135. }
  136. // Suppress warnings if requested or if the file or directory does not
  137. // exist. This is consistent with PHP's plain filesystem stream wrapper.
  138. return ($flags & STREAM_URL_STAT_QUIET || !file_exists($path)) ? @stat($path) : stat($path);
  139. }
  140. public function dir_opendir($uri, $options)
  141. {
  142. $path = $this->getPath($uri);
  143. if (!$path) {
  144. return false;
  145. }
  146. $this->handle = opendir($path);
  147. return (bool) $this->handle;
  148. }
  149. public function dir_readdir()
  150. {
  151. return readdir($this->handle);
  152. }
  153. public function dir_rewinddir()
  154. {
  155. rewinddir($this->handle);
  156. return true;
  157. }
  158. public function dir_closedir()
  159. {
  160. closedir($this->handle);
  161. return true;
  162. }
  163. protected function getPath($uri, $mode = null)
  164. {
  165. $path = $this->findPath($uri);
  166. if ($mode == null || !$path || file_exists($path)) {
  167. return $path;
  168. }
  169. if ($mode[0] == 'r') {
  170. return false;
  171. }
  172. // We are either opening a file or creating directory.
  173. list($scheme, $target) = explode('://', $uri, 2);
  174. $path = $this->findPath($scheme . '://' . dirname($target));
  175. if (!$path) {
  176. return false;
  177. }
  178. return $path . '/' . basename($uri);
  179. }
  180. protected function findPath($uri)
  181. {
  182. return static::$locator && static::$locator->isStream($uri) ? static::$locator->findResource($uri) : false;
  183. }
  184. }