@@ -69,12 +69,48 @@ public function put(string $data, bool $lock = false)
6969
7070 /**
7171 * Get the contents of a file.
72+ *
73+ * @param bool $lock Acquire an exclusive lock on the file while proceeding to the reading.
7274 *
7375 * @return string|false The file contents or false on failure.
7476 */
75- public function get ()
77+ public function get ($ lock = false )
7678 {
77- $ contents = file_get_contents ($ this ->path );
79+ if ($ this ->isFile ($ this ->path )) {
80+ $ contents = $ lock ? $ this ->sharedGet () : file_get_contents ($ this ->path );
81+ }
82+
83+ if ($ contents === false ) {
84+ return false ;
85+ }
86+
87+ return $ contents ;
88+ }
89+
90+ /**
91+ * Get contents of a file with shared access.
92+ *
93+ * @return string|false The file contents or false on failure.
94+ */
95+ public function sharedGet ()
96+ {
97+ $ contents = false ;
98+
99+ $ handle = fopen ($ this ->path , 'rb ' );
100+
101+ if ($ handle ) {
102+ try {
103+ if (flock ($ handle , LOCK_SH )) {
104+ clearstatcache (true , $ this ->path );
105+
106+ $ contents = fread ($ handle , $ this ->size ($ this ->path ) ?: 1 );
107+
108+ flock ($ handle , LOCK_UN );
109+ }
110+ } finally {
111+ fclose ($ handle );
112+ }
113+ }
78114
79115 if ($ contents === false ) {
80116 return false ;
@@ -111,6 +147,19 @@ public function append(string $data)
111147 return file_put_contents ($ this ->path , $ data , FILE_APPEND );
112148 }
113149
150+ /**
151+ * Replace the value with the string in a given file.
152+ *
153+ * @param string $search Search
154+ * @param mixed $replace Replace
155+ *
156+ * @return void
157+ */
158+ public function replace (string $ search , $ replace ): void
159+ {
160+ file_put_contents ($ this ->path , str_replace ($ search , $ replace , file_get_contents ($ this ->path )));
161+ }
162+
114163 /**
115164 * Delete the file at a given path.
116165 *
0 commit comments