PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / system / class-sharedmemory.php

class-sharedmemory.php in DecaLog 4.4.0, at includes/system/class-sharedmemory.php

191 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * shmop handling
4 *
5 * Handles all shmop operations and detection.
6 *
7 * @package System
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.0.0
10 */
11
12 namespace Decalog\System;
13
14 use Decalog\System\Option;
15 use Decalog\System\File;
16 use Decalog\Logger;
17
18 /**
19 * Define the shmop functionality.
20 *
21 * Handles all shmop operations and detection.
22 *
23 * @package System
24 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
25 * @since 1.0.0
26 */
27 class SharedMemory {
28
29 /**
30 * Is shmop module available?
31 *
32 * @since 2.0.0
33 * @var boolean $available Maintains availability of shmop module.
34 */
35 public static $available = false;
36
37 /**
38 * The system V id.
39 *
40 * @since 2.0.0
41 * @var integer $id Maintains the system V id.
42 */
43 private $id;
44
45 /**
46 * The opened resource.
47 *
48 * @since 2.0.0
49 * @var \Shmop|null $shmid Maintains the opened resource.
50 */
51 private $shmid = null;
52
53 /**
54 * The permissions to access the memory block.
55 *
56 * @since 2.0.0
57 * @var integer $id Maintains the octal permission mask.
58 */
59 private $perms = 0666;
60
61 /**
62 * Init the class.
63 *
64 * @since 1.0.0
65 */
66 public static function init() {
67 self::$available = ( function_exists( 'shmop_open' ) && function_exists( 'shmop_read' ) && function_exists( 'shmop_write' ) && function_exists( 'shmop_delete' ) );
68 }
69
70 /**
71 * Initialize the class and set its properties.
72 *
73 * @param string $id The system V id.
74 * @since 2.0.0
75 */
76 public function __construct( $id ) {
77 $this->id = $id;
78 }
79
80 /**
81 * Acquire a ressource.
82 *
83 * @param string $flags Optional. The flags for opening.
84 * @param integer $mode Optional. The permissions needed.
85 * @param integer $size Optional. The size of opening.
86 * @return \Shmop|null The opened resource, or null if it's not possible.
87 * @since 2.0.0
88 */
89 private function acquire( $flags = 'a', $mode = 0, $size = 0 ) {
90 if ( ! self::$available ) {
91 return null;
92 }
93 // phpcs:ignore
94 set_error_handler( null );
95 // phpcs:ignore
96 $this->shmid = @shmop_open( $this->id, $flags, $mode, $size );
97 // phpcs:ignore
98 restore_error_handler();
99 return $this->shmid;
100 }
101
102 /**
103 * Check if block exists.
104 *
105 * @return boolean True if the block already exists, false otherwise.
106 * @since 2.0.0
107 */
108 private function exists() {
109 return decalog_is_shmop_resource( $this->acquire() );
110 }
111 /**
112 * Writes an array.
113 *
114 * @param array $data The data to write.
115 * @return false|int TThe number of written bytes, false if something went wrong.
116 * @since 2.0.0
117 */
118 public function write( $data ) {
119 if ( ! self::$available || ! is_array( $data ) ) {
120 return 0;
121 }
122 $data = wp_json_encode( $data, true );
123 $size = mb_strlen( $data, 'UTF-8' );
124 if ( $this->exists() ) {
125 $cpt = 0;
126 while ( 20 > $cpt ) {
127 $this->shmid = $this->acquire( 'w', $this->perms, 0 );
128 if ( decalog_is_shmop_resource( $this->shmid ) ) {
129 break;
130 } else {
131 $cpt++;
132 usleep( 100 );
133 }
134 }
135 if ( decalog_is_shmop_resource( $this->shmid ) ) {
136 shmop_delete( $this->shmid );
137 } else {
138 return false;
139 }
140 }
141 $this->shmid = $this->acquire( 'c', $this->perms, $size );
142 if ( decalog_is_shmop_resource( $this->shmid ) ) {
143 $result = shmop_write( $this->shmid, $data, 0 );
144 return $result;
145 }
146 return false;
147 }
148
149 /**
150 * Reads an array.
151 *
152 * @return array The read data.
153 * @since 2.0.0
154 */
155 public function read() {
156 if ( ! self::$available ) {
157 return [];
158 }
159 $data = '';
160 if ( $this->exists() ) {
161 $cpt = 0;
162 while ( 20 > $cpt ) {
163 $this->shmid = $this->acquire( 'w', $this->perms, 0 );
164 if ( decalog_is_shmop_resource( $this->shmid ) ) {
165 break;
166 } else {
167 $cpt++;
168 usleep( 100 );
169 }
170 }
171 if ( decalog_is_shmop_resource( $this->shmid ) ) {
172 $size = shmop_size( $this->shmid );
173 $data = shmop_read( $this->shmid, 0, $size );
174 } else {
175 return [];
176 }
177 }
178 if ( '' === (string) $data ) {
179 $data = '{}';
180 }
181 $data = \json_decode( $data, true );
182 if ( ! is_array( $data ) ) {
183 $data = [];
184 }
185 return $data;
186 }
187
188 }
189
190 SharedMemory::init();
191