PluginProbe
SQLite Object Cache / 1.5.6
SQLite Object Cache v1.5.6
1.6.5 trunk 0.1.7 1.0.0 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.4.0 1.4.1 1.5.1 1.5.4 1.5.5 1.5.6 1.5.7 All 30 releases
sqlite-object-cache / includes / lib / class-file.php

class-file.php in SQLite Object Cache 1.5.6, at includes/lib/class-file.php

154 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 /**
7 * Read and write an existing file.
8 */
9 class SQLite_Object_Cache_File {
10
11 private $lines;
12 private $filename;
13
14 public function __construct( $filename ) {
15
16 $this->filename = $filename;
17 $this->lines = self::read_lines( $this->filename );
18
19 }
20
21 public function remove( $item ) {
22 $result = array();
23 $found = false;
24 foreach ( $this->lines as $line ) {
25 if ( ! str_contains( $line, $item ) ) {
26 $result[] = $line;
27 } else {
28 $found = true;
29 }
30 }
31 if ( $found ) {
32 $this->lines = $result;
33 }
34 return $found;
35 }
36
37 /**
38 * @throws Exception if no opening php tag was found.
39 */
40 public function insert_before( $item, $before = "That's all, stop editing!" ) {
41 $result = array();
42 $found = false;
43 foreach ( $this->lines as $line ) {
44 if ( ! $found && str_contains( $line, $before ) ) {
45 $result[] = $item;
46 $found = true;
47 }
48 $result[] = $line;
49 }
50 if ( $found ) {
51 $this->lines = $result;
52 return true;
53 } else {
54 return $this->insert_after( $item );
55 }
56 }
57
58 /**
59 * @throws Exception
60 */
61 public function insert_after( $item, $after = '<?php' ) {
62 $result = array();
63 $found = false;
64 foreach ( $this->lines as $line ) {
65 $result[] = $line;
66 if ( ! $found && str_contains( $line, $after ) ) {
67 $result[] = $item;
68 $found = true;
69 }
70 }
71 if ( ! $found ) {
72 throw new \Exception( 'No opening php tag in ', $this->filename );
73 }
74 $this->lines = $result;
75 return true;
76 }
77
78 /**
79 * Read data into line-by-line array.
80 *
81 * @param string $filename
82 *
83 * @return array
84 */
85 public static function read_lines( $filename ) {
86 $str = self::read( $filename );
87 $result = explode( "\n", $str );
88 $result = array_map( function ( $line ) {
89 return ltrim( $line, "\r" );
90 }, $result );
91 return $result;
92 }
93
94 public static function write_lines( $filename, $lines ) {
95 $lines = array_filter( $lines );
96 self::save( $filename, implode( "\n", $lines ) );
97 }
98
99
100 /**
101 * Read data from file.
102 *
103 * @param string $filename The pathname of the file to read.
104 *
105 * @return bool|string The file's contents. Empty string if the file doesn't already exist. false if it is not readable.
106 */
107 public static function read( $filename ) {
108 if ( ! file_exists( $filename ) ) {
109 return '';
110 }
111 if ( ! is_readable( $filename ) ) {
112 return false;
113 }
114 $content = file_get_contents( $filename );
115 return self::remove_zero_space( $content );
116 }
117
118 /**
119 * Save data to file.
120 */
121 public function save() {
122
123 $data = implode( "\n", $this->lines );
124 $data = self::remove_zero_space( $data );
125 file_put_contents( $this->filename, $data, LOCK_EX );
126 }
127
128 /**
129 * Remove Unicode zero-width spaced <200b><200c> and BOMs
130 *
131 * @param array|string $content
132 *
133 * @return array|string|string[]
134 */
135 public static function remove_zero_space( $content ) {
136 if ( is_array( $content ) ) {
137 $content = array_map( __CLASS__ . '::remove_zero_space', $content );
138 return $content;
139 }
140
141 // Remove UTF-8 BOM if present
142 if ( substr( $content, 0, 3 ) === "\xEF\xBB\xBF" ) {
143 $content = substr( $content, 3 );
144 }
145
146 $content = str_replace( "\xe2\x80\x8b", '', $content );
147 $content = str_replace( "\xe2\x80\x8c", '', $content );
148 $content = str_replace( "\xe2\x80\x8d", '', $content );
149
150 return $content;
151 }
152
153 }
154