Block.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Class Block
  4. * https://github.com/klaussilveira/SimpleSHM/blob/1.0.0/src/Simple/SHM/Block.php
  5. */
  6. class Block
  7. {
  8. /**
  9. * Holds the system id for the shared memory block
  10. *
  11. * @var int
  12. * @access protected
  13. */
  14. protected $id;
  15. /**
  16. * Holds the shared memory block id returned by shmop_open
  17. *
  18. * @var int
  19. * @access protected
  20. */
  21. protected $shmid = false;
  22. /**
  23. * Holds the default permission (octal) that will be used in created memory blocks
  24. *
  25. * @var int
  26. * @access protected
  27. */
  28. protected $perms = 0644;
  29. /**
  30. * Shared memory block instantiation
  31. *
  32. * In the constructor we'll check if the block we're going to manipulate
  33. * already exists or needs to be created. If it exists, let's open it.
  34. *
  35. * @access public
  36. * @param string $id (optional) ID of the shared memory block you want to manipulate
  37. */
  38. public function __construct($id = null)
  39. {
  40. if($id === null) {
  41. $this->id = $this->generateID();
  42. } else {
  43. $this->id = $id;
  44. if($this->exists($this->id)) {
  45. $this->shmid = shmop_open($this->id, "w", 0, 0);
  46. }
  47. }
  48. }
  49. /**
  50. * Generates a random ID for a shared memory block
  51. *
  52. * @access protected
  53. * @return int Randomly generated ID, between 1 and 65535
  54. */
  55. protected function generateID()
  56. {
  57. $id = mt_rand(1, 65535);
  58. return $id;
  59. }
  60. /**
  61. * Checks if a shared memory block with the provided id exists or not
  62. *
  63. * In order to check for shared memory existance, we have to open it with
  64. * reading access. If it doesn't exist, warnings will be cast, therefore we
  65. * suppress those with the @ operator.
  66. *
  67. * @access public
  68. * @param string $id ID of the shared memory block you want to check
  69. * @return boolean True if the block exists, false if it doesn't
  70. */
  71. public function exists($id)
  72. {
  73. $status = @shmop_open($id, "a", 0, 0);
  74. return $status;
  75. }
  76. /**
  77. * Writes on a shared memory block
  78. *
  79. * First we check for the block existance, and if it doesn't, we'll create it. Now, if the
  80. * block already exists, we need to delete it and create it again with a new byte allocation that
  81. * matches the size of the data that we want to write there. We mark for deletion, close the semaphore
  82. * and create it again.
  83. *
  84. * @access public
  85. * @param string $data The data that you wan't to write into the shared memory block
  86. */
  87. public function write($data)
  88. {
  89. $size = strlen($data);
  90. if(extension_loaded('mb_strlen')){
  91. $size = mb_strlen($data, 'UTF-8');
  92. }
  93. if($this->exists($this->id)) {
  94. shmop_delete($this->shmid);
  95. shmop_close($this->shmid);
  96. $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
  97. shmop_write($this->shmid, $data, 0);
  98. } else {
  99. $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
  100. shmop_write($this->shmid, $data, 0);
  101. }
  102. }
  103. /**
  104. * Reads from a shared memory block
  105. *
  106. * @access public
  107. * @return string The data read from the shared memory block
  108. */
  109. public function read()
  110. {
  111. if(!$this->shmid){
  112. return null;
  113. }
  114. $size = shmop_size($this->shmid);
  115. $data = shmop_read($this->shmid, 0, $size);
  116. return $data;
  117. }
  118. /**
  119. * Mark a shared memory block for deletion
  120. *
  121. * @access public
  122. */
  123. public function delete()
  124. {
  125. shmop_delete($this->shmid);
  126. }
  127. /**
  128. * Gets the current shared memory block id
  129. *
  130. * @access public
  131. */
  132. public function getId()
  133. {
  134. return $this->id;
  135. }
  136. /**
  137. * Gets the current shared memory block permissions
  138. *
  139. * @access public
  140. */
  141. public function getPermissions()
  142. {
  143. return $this->perms;
  144. }
  145. /**
  146. * Sets the default permission (octal) that will be used in created memory blocks
  147. *
  148. * @access public
  149. * @param string $perms Permissions, in octal form
  150. */
  151. public function setPermissions($perms)
  152. {
  153. $this->perms = $perms;
  154. }
  155. /**
  156. * Closes the shared memory block and stops manipulation
  157. *
  158. * @access public
  159. */
  160. public function __destruct()
  161. {
  162. if(!$this->shmid){
  163. return null;
  164. }
  165. shmop_close($this->shmid);
  166. }
  167. }