Completed
Push — master ( b1baf8...059e51 )
by David
05:01
created
src/includes/cache/class-wordlift-file-cache-service.php 2 patches
Indentation   +211 added lines, -211 removed lines patch added patch discarded remove patch
@@ -16,231 +16,231 @@
 block discarded – undo
16 16
  */
17 17
 class Wordlift_File_Cache_Service implements Wordlift_Cache_Service {
18 18
 
19
-	/**
20
-	 * The cache directory.
21
-	 *
22
-	 * @since  3.16.0
23
-	 * @access private
24
-	 * @var string $cache_dir The root cache directory (ending with a trailing slash).
25
-	 */
26
-	private $cache_dir;
27
-
28
-	/**
29
-	 * The file extension for cache files (e.g. `.wlcache`).
30
-	 *
31
-	 * @since  3.16.0
32
-	 * @access private
33
-	 * @var string $file_extension The file extension for cache files (e.g. `.wlcache`).
34
-	 */
35
-	private $file_extension;
36
-
37
-	/**
38
-	 * A {@link Wordlift_Log_Service} instance.
39
-	 *
40
-	 * @since  3.16.0
41
-	 * @access private
42
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
43
-	 */
44
-	private $log;
45
-
46
-	/**
47
-	 * The {@link Wordlift_File_Cache_Service} registered instances.
48
-	 *
49
-	 * Each {@link Wordlift_File_Cache_Service} adds itself to the registered
50
-	 * instances.
51
-	 *
52
-	 * @since  3.16.3
53
-	 * @access private
54
-	 * @var array $instances An array of {@link Wordlift_File_Cache_Service} instances.
55
-	 */
56
-	private static $instances = array();
57
-
58
-	private static $instance;
59
-
60
-	/**
61
-	 * Create a {@link Wordlift_File_Cache_Service} instance.
62
-	 *
63
-	 * The File Cache Service requires a base cache directory (to which a unique
64
-	 * id for the current site will be appended) and a file extension for cache
65
-	 * files (by default `.wlcache`) is used.
66
-	 *
67
-	 * @param string $cache_dir The base cache directory.
68
-	 * @param string $file_extension The file extension, by default `.wlcache`.
69
-	 *
70
-	 * @since 3.16.0
71
-	 *
72
-	 */
73
-	public function __construct( $cache_dir, $file_extension = '.wlcache' ) {
74
-
75
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
76
-
77
-		// Set the cache directory using the base directory provided by the caller
78
-		// and appending a hash for the unique site id.
79
-		$this->cache_dir      = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/';
80
-		$this->file_extension = $file_extension;
81
-
82
-		// Create the cache dir.
83
-		if ( ! file_exists( $this->cache_dir ) ) {
84
-			@mkdir( $this->cache_dir, 0755, true );
85
-		}
86
-
87
-		// Add ourselves to the list of instances.
88
-		self::$instances[] = $this;
89
-
90
-		// Initialize the singleton and the ajax method.
91
-		if ( ! isset( self::$instance ) ) {
92
-			self::$instance = $this;
93
-
94
-			add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) );
95
-		}
96
-
97
-		$this->log->debug( "File Cache service initialized on $this->cache_dir." );
98
-
99
-	}
100
-
101
-	/**
102
-	 * Get the cached response for the specified `id`.
103
-	 *
104
-	 * @param int $id The cache `id`.
105
-	 *
106
-	 * @return mixed|false The cached contents or false if the cache isn't found.
107
-	 * @since 3.16.0
108
-	 *
109
-	 */
110
-	function get_cache( $id ) {
111
-
112
-		// Bail out if we don't have the cache.
113
-		if ( ! $this->has_cache( $id ) ) {
114
-			return false;
115
-		}
116
-
117
-		// Get the filename.
118
-		$filename = $this->get_filename( $id );
119
-
120
-		$this->log->trace( "Trying to get cache contents for $id from $filename..." );
121
-
122
-		// Try to decode the contents.
123
-		$contents = json_decode( file_get_contents( $filename ), true );
124
-
125
-		// Return false if decoding failed, otherwise the decoded contents.
126
-		return $contents ?: false;
127
-	}
128
-
129
-	/**
130
-	 * Set the cache contents for the specified `id`.
131
-	 *
132
-	 * @param int $id The cache id.
133
-	 *
134
-	 * @return bool True if the `id` has a cache.
135
-	 * @since 3.16.0
136
-	 *
137
-	 */
138
-	function has_cache( $id ) {
139
-
140
-		// Get the filename.
141
-		$filename = $this->get_filename( $id );
142
-
143
-		// Bail out if the file doesn't exist.
144
-		return file_exists( $filename );
145
-	}
146
-
147
-	/**
148
-	 * @inheritdoc
149
-	 */
150
-	function set_cache( $id, $contents ) {
151
-
152
-		$filename = $this->get_filename( $id );
153
-
154
-		$this->log->trace( "Writing cache contents for $id to $filename..." );
155
-
156
-		@file_put_contents( $filename, wp_json_encode( $contents ) );
157
-
158
-	}
159
-
160
-	/**
161
-	 * Delete the cache for the specified `id`.
162
-	 *
163
-	 * @param int $id The cache `id`.
164
-	 *
165
-	 * @since 3.16.0
166
-	 *
167
-	 */
168
-	function delete_cache( $id ) {
169
-
170
-		$filename = $this->get_filename( $id );
19
+    /**
20
+     * The cache directory.
21
+     *
22
+     * @since  3.16.0
23
+     * @access private
24
+     * @var string $cache_dir The root cache directory (ending with a trailing slash).
25
+     */
26
+    private $cache_dir;
27
+
28
+    /**
29
+     * The file extension for cache files (e.g. `.wlcache`).
30
+     *
31
+     * @since  3.16.0
32
+     * @access private
33
+     * @var string $file_extension The file extension for cache files (e.g. `.wlcache`).
34
+     */
35
+    private $file_extension;
36
+
37
+    /**
38
+     * A {@link Wordlift_Log_Service} instance.
39
+     *
40
+     * @since  3.16.0
41
+     * @access private
42
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
43
+     */
44
+    private $log;
45
+
46
+    /**
47
+     * The {@link Wordlift_File_Cache_Service} registered instances.
48
+     *
49
+     * Each {@link Wordlift_File_Cache_Service} adds itself to the registered
50
+     * instances.
51
+     *
52
+     * @since  3.16.3
53
+     * @access private
54
+     * @var array $instances An array of {@link Wordlift_File_Cache_Service} instances.
55
+     */
56
+    private static $instances = array();
57
+
58
+    private static $instance;
59
+
60
+    /**
61
+     * Create a {@link Wordlift_File_Cache_Service} instance.
62
+     *
63
+     * The File Cache Service requires a base cache directory (to which a unique
64
+     * id for the current site will be appended) and a file extension for cache
65
+     * files (by default `.wlcache`) is used.
66
+     *
67
+     * @param string $cache_dir The base cache directory.
68
+     * @param string $file_extension The file extension, by default `.wlcache`.
69
+     *
70
+     * @since 3.16.0
71
+     *
72
+     */
73
+    public function __construct( $cache_dir, $file_extension = '.wlcache' ) {
74
+
75
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
76
+
77
+        // Set the cache directory using the base directory provided by the caller
78
+        // and appending a hash for the unique site id.
79
+        $this->cache_dir      = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/';
80
+        $this->file_extension = $file_extension;
81
+
82
+        // Create the cache dir.
83
+        if ( ! file_exists( $this->cache_dir ) ) {
84
+            @mkdir( $this->cache_dir, 0755, true );
85
+        }
86
+
87
+        // Add ourselves to the list of instances.
88
+        self::$instances[] = $this;
89
+
90
+        // Initialize the singleton and the ajax method.
91
+        if ( ! isset( self::$instance ) ) {
92
+            self::$instance = $this;
93
+
94
+            add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) );
95
+        }
96
+
97
+        $this->log->debug( "File Cache service initialized on $this->cache_dir." );
98
+
99
+    }
100
+
101
+    /**
102
+     * Get the cached response for the specified `id`.
103
+     *
104
+     * @param int $id The cache `id`.
105
+     *
106
+     * @return mixed|false The cached contents or false if the cache isn't found.
107
+     * @since 3.16.0
108
+     *
109
+     */
110
+    function get_cache( $id ) {
111
+
112
+        // Bail out if we don't have the cache.
113
+        if ( ! $this->has_cache( $id ) ) {
114
+            return false;
115
+        }
116
+
117
+        // Get the filename.
118
+        $filename = $this->get_filename( $id );
119
+
120
+        $this->log->trace( "Trying to get cache contents for $id from $filename..." );
121
+
122
+        // Try to decode the contents.
123
+        $contents = json_decode( file_get_contents( $filename ), true );
124
+
125
+        // Return false if decoding failed, otherwise the decoded contents.
126
+        return $contents ?: false;
127
+    }
128
+
129
+    /**
130
+     * Set the cache contents for the specified `id`.
131
+     *
132
+     * @param int $id The cache id.
133
+     *
134
+     * @return bool True if the `id` has a cache.
135
+     * @since 3.16.0
136
+     *
137
+     */
138
+    function has_cache( $id ) {
139
+
140
+        // Get the filename.
141
+        $filename = $this->get_filename( $id );
142
+
143
+        // Bail out if the file doesn't exist.
144
+        return file_exists( $filename );
145
+    }
146
+
147
+    /**
148
+     * @inheritdoc
149
+     */
150
+    function set_cache( $id, $contents ) {
151
+
152
+        $filename = $this->get_filename( $id );
153
+
154
+        $this->log->trace( "Writing cache contents for $id to $filename..." );
155
+
156
+        @file_put_contents( $filename, wp_json_encode( $contents ) );
157
+
158
+    }
159
+
160
+    /**
161
+     * Delete the cache for the specified `id`.
162
+     *
163
+     * @param int $id The cache `id`.
164
+     *
165
+     * @since 3.16.0
166
+     *
167
+     */
168
+    function delete_cache( $id ) {
169
+
170
+        $filename = $this->get_filename( $id );
171 171
 
172
-		$this->log->trace( "Deleting cache contents for $id, file $filename..." );
172
+        $this->log->trace( "Deleting cache contents for $id, file $filename..." );
173 173
 
174
-		if ( file_exists( $filename ) ) {
175
-			@unlink( $filename );
176
-		}
177
-
178
-	}
174
+        if ( file_exists( $filename ) ) {
175
+            @unlink( $filename );
176
+        }
177
+
178
+    }
179 179
 
180
-	/**
181
-	 * Flush the whole cache.
182
-	 *
183
-	 * @since 3.16.0
184
-	 */
185
-	function flush() {
180
+    /**
181
+     * Flush the whole cache.
182
+     *
183
+     * @since 3.16.0
184
+     */
185
+    function flush() {
186 186
 
187
-		// Bail out if the cache dir isn't set.
188
-		if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) {
189
-			return;
190
-		}
187
+        // Bail out if the cache dir isn't set.
188
+        if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) {
189
+            return;
190
+        }
191 191
 
192
-		$this->log->trace( "Flushing cache contents from $this->cache_dir..." );
192
+        $this->log->trace( "Flushing cache contents from $this->cache_dir..." );
193 193
 
194
-		$handle = @opendir( $this->cache_dir );
194
+        $handle = @opendir( $this->cache_dir );
195 195
 
196
-		// Bail out if the directory can't be opened.
197
-		if ( false === $handle ) {
198
-			return;
199
-		}
200
-
201
-		// Calculate the file extension length for matching file names.
202
-		$file_extension_length = strlen( $this->file_extension );
203
-
204
-		// Loop into the directory to delete files.
205
-		while ( false !== ( $entry = readdir( $handle ) ) ) {
206
-			if ( substr( $entry, - $file_extension_length ) === $this->file_extension
207
-			     && file_exists( $this->cache_dir . $entry ) ) {
208
-				$this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." );
196
+        // Bail out if the directory can't be opened.
197
+        if ( false === $handle ) {
198
+            return;
199
+        }
200
+
201
+        // Calculate the file extension length for matching file names.
202
+        $file_extension_length = strlen( $this->file_extension );
203
+
204
+        // Loop into the directory to delete files.
205
+        while ( false !== ( $entry = readdir( $handle ) ) ) {
206
+            if ( substr( $entry, - $file_extension_length ) === $this->file_extension
207
+                 && file_exists( $this->cache_dir . $entry ) ) {
208
+                $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." );
209 209
 
210
-				@unlink( $this->cache_dir . $entry );
211
-			}
212
-		}
210
+                @unlink( $this->cache_dir . $entry );
211
+            }
212
+        }
213 213
 
214
-		// Finally closed the directory.
215
-		closedir( $handle );
214
+        // Finally closed the directory.
215
+        closedir( $handle );
216 216
 
217
-	}
217
+    }
218 218
 
219
-	public static function flush_all() {
219
+    public static function flush_all() {
220 220
 
221
-		foreach ( self::$instances as $instance ) {
222
-			$instance->flush();
223
-		}
221
+        foreach ( self::$instances as $instance ) {
222
+            $instance->flush();
223
+        }
224 224
 
225
-		if ( defined( 'DOING_AJAX' ) && DOING_AJAX
226
-		     && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) {
227
-			wp_send_json_success();
228
-		}
229
-
230
-	}
225
+        if ( defined( 'DOING_AJAX' ) && DOING_AJAX
226
+             && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) {
227
+            wp_send_json_success();
228
+        }
229
+
230
+    }
231 231
 
232
-	/**
233
-	 * Get the filename holding the cache contents for the specified `id`.
234
-	 *
235
-	 * @param int $id The cache `id`.
236
-	 *
237
-	 * @return string The filename.
238
-	 * @since 3.16.0
239
-	 *
240
-	 */
241
-	private function get_filename( $id ) {
232
+    /**
233
+     * Get the filename holding the cache contents for the specified `id`.
234
+     *
235
+     * @param int $id The cache `id`.
236
+     *
237
+     * @return string The filename.
238
+     * @since 3.16.0
239
+     *
240
+     */
241
+    private function get_filename( $id ) {
242 242
 
243
-		return $this->cache_dir . md5( $id ) . $this->file_extension;
244
-	}
243
+        return $this->cache_dir . md5( $id ) . $this->file_extension;
244
+    }
245 245
 
246 246
 }
Please login to merge, or discard this patch.
Spacing   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -70,31 +70,31 @@  discard block
 block discarded – undo
70 70
 	 * @since 3.16.0
71 71
 	 *
72 72
 	 */
73
-	public function __construct( $cache_dir, $file_extension = '.wlcache' ) {
73
+	public function __construct($cache_dir, $file_extension = '.wlcache') {
74 74
 
75
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
75
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
76 76
 
77 77
 		// Set the cache directory using the base directory provided by the caller
78 78
 		// and appending a hash for the unique site id.
79
-		$this->cache_dir      = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/';
79
+		$this->cache_dir      = trailingslashit($cache_dir).md5(get_site_url()).'/';
80 80
 		$this->file_extension = $file_extension;
81 81
 
82 82
 		// Create the cache dir.
83
-		if ( ! file_exists( $this->cache_dir ) ) {
84
-			@mkdir( $this->cache_dir, 0755, true );
83
+		if ( ! file_exists($this->cache_dir)) {
84
+			@mkdir($this->cache_dir, 0755, true);
85 85
 		}
86 86
 
87 87
 		// Add ourselves to the list of instances.
88 88
 		self::$instances[] = $this;
89 89
 
90 90
 		// Initialize the singleton and the ajax method.
91
-		if ( ! isset( self::$instance ) ) {
91
+		if ( ! isset(self::$instance)) {
92 92
 			self::$instance = $this;
93 93
 
94
-			add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) );
94
+			add_action('wp_ajax_wl_file_cache__flush_all', array('Wordlift_File_Cache_Service', 'flush_all'));
95 95
 		}
96 96
 
97
-		$this->log->debug( "File Cache service initialized on $this->cache_dir." );
97
+		$this->log->debug("File Cache service initialized on $this->cache_dir.");
98 98
 
99 99
 	}
100 100
 
@@ -107,20 +107,20 @@  discard block
 block discarded – undo
107 107
 	 * @since 3.16.0
108 108
 	 *
109 109
 	 */
110
-	function get_cache( $id ) {
110
+	function get_cache($id) {
111 111
 
112 112
 		// Bail out if we don't have the cache.
113
-		if ( ! $this->has_cache( $id ) ) {
113
+		if ( ! $this->has_cache($id)) {
114 114
 			return false;
115 115
 		}
116 116
 
117 117
 		// Get the filename.
118
-		$filename = $this->get_filename( $id );
118
+		$filename = $this->get_filename($id);
119 119
 
120
-		$this->log->trace( "Trying to get cache contents for $id from $filename..." );
120
+		$this->log->trace("Trying to get cache contents for $id from $filename...");
121 121
 
122 122
 		// Try to decode the contents.
123
-		$contents = json_decode( file_get_contents( $filename ), true );
123
+		$contents = json_decode(file_get_contents($filename), true);
124 124
 
125 125
 		// Return false if decoding failed, otherwise the decoded contents.
126 126
 		return $contents ?: false;
@@ -135,25 +135,25 @@  discard block
 block discarded – undo
135 135
 	 * @since 3.16.0
136 136
 	 *
137 137
 	 */
138
-	function has_cache( $id ) {
138
+	function has_cache($id) {
139 139
 
140 140
 		// Get the filename.
141
-		$filename = $this->get_filename( $id );
141
+		$filename = $this->get_filename($id);
142 142
 
143 143
 		// Bail out if the file doesn't exist.
144
-		return file_exists( $filename );
144
+		return file_exists($filename);
145 145
 	}
146 146
 
147 147
 	/**
148 148
 	 * @inheritdoc
149 149
 	 */
150
-	function set_cache( $id, $contents ) {
150
+	function set_cache($id, $contents) {
151 151
 
152
-		$filename = $this->get_filename( $id );
152
+		$filename = $this->get_filename($id);
153 153
 
154
-		$this->log->trace( "Writing cache contents for $id to $filename..." );
154
+		$this->log->trace("Writing cache contents for $id to $filename...");
155 155
 
156
-		@file_put_contents( $filename, wp_json_encode( $contents ) );
156
+		@file_put_contents($filename, wp_json_encode($contents));
157 157
 
158 158
 	}
159 159
 
@@ -165,14 +165,14 @@  discard block
 block discarded – undo
165 165
 	 * @since 3.16.0
166 166
 	 *
167 167
 	 */
168
-	function delete_cache( $id ) {
168
+	function delete_cache($id) {
169 169
 
170
-		$filename = $this->get_filename( $id );
170
+		$filename = $this->get_filename($id);
171 171
 
172
-		$this->log->trace( "Deleting cache contents for $id, file $filename..." );
172
+		$this->log->trace("Deleting cache contents for $id, file $filename...");
173 173
 
174
-		if ( file_exists( $filename ) ) {
175
-			@unlink( $filename );
174
+		if (file_exists($filename)) {
175
+			@unlink($filename);
176 176
 		}
177 177
 
178 178
 	}
@@ -185,45 +185,45 @@  discard block
 block discarded – undo
185 185
 	function flush() {
186 186
 
187 187
 		// Bail out if the cache dir isn't set.
188
-		if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) {
188
+		if (empty($this->cache_dir) || '/' === $this->cache_dir) {
189 189
 			return;
190 190
 		}
191 191
 
192
-		$this->log->trace( "Flushing cache contents from $this->cache_dir..." );
192
+		$this->log->trace("Flushing cache contents from $this->cache_dir...");
193 193
 
194
-		$handle = @opendir( $this->cache_dir );
194
+		$handle = @opendir($this->cache_dir);
195 195
 
196 196
 		// Bail out if the directory can't be opened.
197
-		if ( false === $handle ) {
197
+		if (false === $handle) {
198 198
 			return;
199 199
 		}
200 200
 
201 201
 		// Calculate the file extension length for matching file names.
202
-		$file_extension_length = strlen( $this->file_extension );
202
+		$file_extension_length = strlen($this->file_extension);
203 203
 
204 204
 		// Loop into the directory to delete files.
205
-		while ( false !== ( $entry = readdir( $handle ) ) ) {
206
-			if ( substr( $entry, - $file_extension_length ) === $this->file_extension
207
-			     && file_exists( $this->cache_dir . $entry ) ) {
208
-				$this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." );
205
+		while (false !== ($entry = readdir($handle))) {
206
+			if (substr($entry, - $file_extension_length) === $this->file_extension
207
+			     && file_exists($this->cache_dir.$entry)) {
208
+				$this->log->trace("Deleting file {$this->cache_dir}{$entry}...");
209 209
 
210
-				@unlink( $this->cache_dir . $entry );
210
+				@unlink($this->cache_dir.$entry);
211 211
 			}
212 212
 		}
213 213
 
214 214
 		// Finally closed the directory.
215
-		closedir( $handle );
215
+		closedir($handle);
216 216
 
217 217
 	}
218 218
 
219 219
 	public static function flush_all() {
220 220
 
221
-		foreach ( self::$instances as $instance ) {
221
+		foreach (self::$instances as $instance) {
222 222
 			$instance->flush();
223 223
 		}
224 224
 
225
-		if ( defined( 'DOING_AJAX' ) && DOING_AJAX
226
-		     && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) {
225
+		if (defined('DOING_AJAX') && DOING_AJAX
226
+		     && isset($_REQUEST['action']) && 'wl_file_cache__flush_all' === $_REQUEST['action']) {
227 227
 			wp_send_json_success();
228 228
 		}
229 229
 
@@ -238,9 +238,9 @@  discard block
 block discarded – undo
238 238
 	 * @since 3.16.0
239 239
 	 *
240 240
 	 */
241
-	private function get_filename( $id ) {
241
+	private function get_filename($id) {
242 242
 
243
-		return $this->cache_dir . md5( $id ) . $this->file_extension;
243
+		return $this->cache_dir.md5($id).$this->file_extension;
244 244
 	}
245 245
 
246 246
 }
Please login to merge, or discard this patch.