ReadOnlyStream.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace RocketTheme\Toolbox\StreamWrapper;
  3. use RocketTheme\Toolbox\ResourceLocator\ResourceLocatorInterface;
  4. /**
  5. * Implements Read Only Streams.
  6. *
  7. * @package RocketTheme\Toolbox\StreamWrapper
  8. * @author RocketTheme
  9. * @license MIT
  10. */
  11. class ReadOnlyStream extends Stream implements StreamInterface
  12. {
  13. /**
  14. * @var ResourceLocatorInterface
  15. */
  16. protected static $locator;
  17. public function stream_open($uri, $mode, $options, &$opened_url)
  18. {
  19. if (!in_array($mode, ['r', 'rb', 'rt'])) {
  20. if ($options & STREAM_REPORT_ERRORS) {
  21. trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
  22. }
  23. return false;
  24. }
  25. $path = $this->getPath($uri);
  26. if (!$path) {
  27. return false;
  28. }
  29. $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
  30. return (bool) $this->handle;
  31. }
  32. public function stream_lock($operation)
  33. {
  34. // Disallow exclusive lock or non-blocking lock requests
  35. if (!in_array($operation, [LOCK_SH, LOCK_UN, LOCK_SH | LOCK_NB])) {
  36. trigger_error(
  37. 'stream_lock() exclusive lock operations not supported for read-only stream wrappers',
  38. E_USER_WARNING
  39. );
  40. return false;
  41. }
  42. return flock($this->handle, $operation);
  43. }
  44. public function stream_write($data)
  45. {
  46. throw new \BadMethodCallException('stream_write() not supported for read-only stream wrappers');
  47. }
  48. public function unlink($uri)
  49. {
  50. throw new \BadMethodCallException('unlink() not supported for read-only stream wrappers');
  51. }
  52. public function rename($from_uri, $to_uri)
  53. {
  54. throw new \BadMethodCallException('rename() not supported for read-only stream wrappers');
  55. }
  56. public function mkdir($uri, $mode, $options)
  57. {
  58. throw new \BadMethodCallException('mkdir() not supported for read-only stream wrappers');
  59. }
  60. public function rmdir($uri, $options)
  61. {
  62. throw new \BadMethodCallException('rmdir() not supported for read-only stream wrappers');
  63. }
  64. }