@@ -13,16 +13,7 @@ public function __construct(
1313 private string $ storageType = 'filesystem ' ,
1414 private string $ json_path = 'json/verify.json '
1515 ) {
16- $ this ->civToken = $ civToken ;
17- $ this ->storageType = $ storageType ;
18-
1916 if ($ this ->storageType !== 'filesystem ' ) {
20- $ env = self ::loadEnvConfig ();
21- $ dsn = $ env ['DB_DSN ' ];
22- $ username = $ env ['DB_USERNAME ' ];
23- $ password = $ env ['DB_PASSWORD ' ];
24- $ options = isset ($ env ['DB_OPTIONS ' ]) ? json_decode ($ env ['DB_OPTIONS ' ], true ) : [];
25- $ this ->pdo = new \PDO ($ dsn , $ username , $ password , $ options );
2617 $ this ->initializeDatabase ();
2718 }
2819 }
@@ -47,6 +38,13 @@ public function __construct(
4738 */
4839 private function initializeDatabase (): void
4940 {
41+ $ env = self ::loadEnvConfig ();
42+ $ this ->pdo = new \PDO (
43+ $ env ['DB_DSN ' ],
44+ $ env ['DB_USERNAME ' ],
45+ $ env ['DB_PASSWORD ' ],
46+ isset ($ env ['DB_OPTIONS ' ]) ? json_decode ($ env ['DB_OPTIONS ' ], true ) : null
47+ );
5048 if (strpos ($ this ->pdo ->getAttribute (\PDO ::ATTR_DRIVER_NAME ), 'mysql ' ) !== false ) {
5149 if ($ this ->pdo ->exec ("CREATE TABLE IF NOT EXISTS verify_list (
5250 id INT AUTO_INCREMENT PRIMARY KEY,
@@ -80,9 +78,9 @@ private function initializeDatabase(): void
8078 *
8179 * @throws \PDOException If there is an error executing the query or fetching the data from the database.
8280 */
83- public function getVerifyList (): array
81+ public function getVerifyList (bool $ getLocalCache = false ): array
8482 {
85- if ($ this ->storageType === 'filesystem ' ) {
83+ if ($ this ->storageType === 'filesystem ' || $ getLocalCache ) {
8684 return isset ($ this ->verifyList )
8785 ? $ this ->verifyList
8886 : $ this ->verifyList = self ::loadVerifyFile ($ this ->getJsonPath ());
@@ -97,7 +95,7 @@ public function getVerifyList(): array
9795 $ errorInfo = $ this ->pdo ->errorInfo ();
9896 throw new \PDOException ("Failed to fetch data: " . implode (", " , $ errorInfo ));
9997 }
100- return $ result ;
98+ return $ this -> verifyList = $ result ;
10199 }
102100
103101 /**
@@ -109,29 +107,26 @@ public function getVerifyList(): array
109107 *
110108 * @param array $list The list of verification items to be set. Each item in the list should be an associative array
111109 * with keys 'ss13', 'discord', and 'create_time'.
110+ * @param bool $write Whether to write the list to the database. Default is true.
112111 *
113112 * @throws \PDOException If there is an error deleting from the verify_list table, preparing the insert statement,
114113 * or executing the insert statement.
115114 */
116- public function setVerifyList (array $ list ): void
115+ public function setVerifyList (array $ list, bool $ write = true ): void
117116 {
118- if ($ this ->storageType === 'filesystem ' ) {
119- $ this ->verifyList = $ list ;
120- return ;
121- }
122- if ($ this ->pdo ->exec ("DELETE FROM verify_list " ) === false ) {
123- $ errorInfo = $ this ->pdo ->errorInfo ();
124- throw new \PDOException ("Failed to delete from verify_list: " . implode (", " , $ errorInfo ));
125- }
126- $ stmt = $ this ->pdo ->prepare ("INSERT INTO verify_list (ss13, discord, create_time) VALUES (:ss13, :discord, :create_time) " );
127- if ($ stmt === false ) {
128- throw new \PDOException ("Failed to prepare statement. " );
129- }
130- foreach ($ list as $ item ) {
131- if (!$ stmt ->execute ($ item )) {
117+ if ($ write && $ this ->storageType !== 'filesystem ' ) {
118+ if ($ this ->pdo ->exec ("DELETE FROM verify_list " ) === false ) {
119+ throw new \PDOException ("Failed to delete from verify_list: " . implode (", " , $ this ->pdo ->errorInfo ()));
120+ }
121+ $ stmt = $ this ->pdo ->prepare ("INSERT INTO verify_list (ss13, discord, create_time) VALUES (:ss13, :discord, :create_time) " );
122+ if ($ stmt === false ) {
123+ throw new \PDOException ("Failed to prepare statement. " );
124+ }
125+ foreach ($ list as $ item ) if (!$ stmt ->execute ($ item )) {
132126 throw new \PDOException ("Failed to execute statement. " );
133127 }
134128 }
129+ $ this ->verifyList = $ list ;
135130 }
136131
137132 /**
@@ -145,27 +140,13 @@ public function getToken(): string
145140 }
146141
147142 /**
148- * Loads the verification data from the "verify.json" file.
149- * If the file does not exist, it creates an empty JSON array file.
150- * If the file cannot be read, it throws an exception.
143+ * Retrieves the JSON path associated with the persistent state.
151144 *
152- * @return array|null The decoded JSON data as an associative array, or an empty array if the file is empty or invalid.
153- * @throws \Exception If the file cannot be read.
145+ * @return string The file path to the JSON data.
154146 */
155- public static function loadVerifyFile ( string $ json_path ): ? array
147+ public function getJsonPath ( ): string
156148 {
157- $ directory = dirname ($ json_path );
158- if (!is_dir ($ directory )) {
159- mkdir ($ directory , 0777 , true );
160- }
161- if (!file_exists ($ json_path )) {
162- file_put_contents ($ json_path , "[] " );
163- }
164- $ data = file_get_contents ($ json_path );
165- if ($ data === false ) {
166- throw new \Exception ("Failed to read {$ json_path }" );
167- }
168- return json_decode ($ data , true ) ?: [];
149+ return getcwd () . '/ ' . $ this ->json_path ;
169150 }
170151
171152 /**
@@ -232,23 +213,37 @@ public static function loadEnvConfig(): array
232213 }
233214
234215 /**
235- * Writes the given data to a file in JSON format.
216+ * Loads the verification data from the "verify.json" file.
217+ * If the file does not exist, it creates an empty JSON array file.
218+ * If the file cannot be read, it throws an exception.
236219 *
237- * @param string $file The path to the file where the JSON data will be written .
238- * @param mixed $data The data to be encoded as JSON and written to the file .
220+ * @return array|null The decoded JSON data as an associative array, or an empty array if the file is empty or invalid .
221+ * @throws \Exception If the file cannot be read .
239222 */
240- public static function writeJson (string $ file , $ data ): void
223+ public static function loadVerifyFile (string $ json_path ): ? array
241224 {
242- file_put_contents ($ file , json_encode ($ data , JSON_PRETTY_PRINT ));
225+ $ directory = dirname ($ json_path );
226+ if (!is_dir ($ directory )) {
227+ mkdir ($ directory , 0777 , true );
228+ }
229+ if (!file_exists ($ json_path )) {
230+ file_put_contents ($ json_path , "[] " );
231+ }
232+ $ data = file_get_contents ($ json_path );
233+ if ($ data === false ) {
234+ throw new \Exception ("Failed to read {$ json_path }" );
235+ }
236+ return json_decode ($ data , true ) ?: [];
243237 }
244238
245239 /**
246- * Retrieves the JSON path associated with the persistent state .
240+ * Writes the given data to a file in JSON format .
247241 *
248- * @return string The file path to the JSON data.
242+ * @param string $file The path to the file where the JSON data will be written.
243+ * @param mixed $data The data to be encoded as JSON and written to the file.
249244 */
250- public function getJsonPath ( ): string
245+ public static function writeJson ( string $ file , array $ data ): void
251246 {
252- return getcwd () . $ this -> json_path ;
247+ file_put_contents ( $ file , json_encode ( $ data , JSON_PRETTY_PRINT )) ;
253248 }
254249}
0 commit comments