PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / trunk
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution vtrunk
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Filesystem / Filesystem.php

Filesystem.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution trunk, at src/Performance/Filesystem/Filesystem.php

134 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The Filesystem component.
4 *
5 * @package SolidWP\Performance
6 */
7
8 declare( strict_types=1 );
9
10 namespace SolidWP\Performance\Filesystem;
11
12 use SolidWP\Performance\Filesystem\Exceptions\IOException;
13 use SolidWP\Performance\Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
14 use SolidWP\Performance\Symfony\Component\Filesystem\Exception\IOException as SymfonyException;
15 use Throwable;
16
17 /**
18 * The Filesystem component.
19 *
20 * @package SolidWP\Performance
21 */
22 class Filesystem {
23
24 /**
25 * @var SymfonyFilesystem
26 */
27 private SymfonyFilesystem $filesystem;
28
29 /**
30 * @param SymfonyFilesystem $filesystem The Symfony filesystem component.
31 */
32 public function __construct(
33 SymfonyFilesystem $filesystem
34 ) {
35 $this->filesystem = $filesystem;
36 }
37
38 /**
39 * Acquire a read lock and read the contents of a file.
40 *
41 * @param string $path The path to the file.
42 *
43 * @throws IOException When we are unable to open or lock the file for reading.
44 *
45 * @return string
46 */
47 public function read( string $path ): string {
48 // Open in binary mode to ensure consistent behavior by preventing automatic newline conversions.
49 $handle = @fopen( $path, 'rb' );
50
51 if ( ! $handle ) {
52 throw new IOException(
53 sprintf(
54 'Failed to open the "%s" file for reading.',
55 $path
56 ),
57 0,
58 null,
59 $path
60 );
61 }
62
63 try {
64 if ( ! flock( $handle, LOCK_SH | LOCK_NB ) ) {
65 throw new IOException(
66 sprintf(
67 'Failed to acquire a shared lock on the "%s" file.',
68 $path
69 ),
70 0,
71 null,
72 $path
73 );
74 }
75
76 clearstatcache( true, $path );
77
78 $content = stream_get_contents( $handle );
79
80 if ( $content === false ) {
81 throw new IOException(
82 sprintf(
83 'Failed to read content from "%s" file.',
84 $path,
85 ),
86 0,
87 null,
88 $path
89 );
90 }
91
92 return $content;
93 } catch ( Throwable $e ) {
94 throw new IOException( $e->getMessage(), $e->getCode(), $e, $path );
95 } finally {
96 flock( $handle, LOCK_UN );
97 fclose( $handle );
98 }
99 }
100
101 /**
102 * Atomically dumps content into a file.
103 *
104 * @param string $path The path to the file to write.
105 * @param string|resource $content The data to write to the file.
106 *
107 * @throws IOException If writing to the file fails.
108 */
109 public function write( string $path, $content ) {
110 try {
111 $this->filesystem->dumpFile( $path, $content );
112 } catch ( SymfonyException $e ) {
113 throw new IOException( $e->getMessage(), $e->getCode(), $e, $e->getPath() );
114 }
115 }
116
117 /**
118 * Remove files or directories.
119 *
120 * @param string|iterable $paths A path or an array of paths to remove.
121 *
122 * @throws IOException When removal fails.
123 *
124 * @return void
125 */
126 public function remove( $paths ) {
127 try {
128 $this->filesystem->remove( $paths );
129 } catch ( SymfonyException $e ) {
130 throw new IOException( $e->getMessage(), $e->getCode(), $e, $e->getPath() );
131 }
132 }
133 }
134