PluginProbe
Code Snippets / 3.9.6
Code Snippets v3.9.6
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / flat-files / classes / class-config-repository.php

class-config-repository.php in Code Snippets 3.9.6, at php/flat-files/classes/class-config-repository.php

56 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Code_Snippets;
3
4 class Snippet_Config_Repository implements Snippet_Config_Repository_Interface {
5
6 const CONFIG_FILE_NAME = 'index.php';
7
8 private File_System_Interface $fs;
9
10 public function __construct( File_System_Interface $fs ) {
11 $this->fs = $fs;
12 }
13
14 public function load( string $base_dir ): array {
15 $config_file_path = trailingslashit( $base_dir ) . static::CONFIG_FILE_NAME;
16
17 if ( is_file( $config_file_path ) ) {
18 if ( function_exists( 'opcache_invalidate' ) ) {
19 opcache_invalidate( $config_file_path, true );
20 }
21 return require $config_file_path;
22 }
23 return [];
24 }
25
26 public function save( string $base_dir, array $active_snippets ): void {
27 $config_file_path = trailingslashit( $base_dir ) . static::CONFIG_FILE_NAME;
28
29 ksort( $active_snippets );
30
31 $file_content = "<?php\n\nif ( ! defined( 'ABSPATH' ) ) { return; }\n\nreturn " .
32 var_export( $active_snippets, true ) .
33 ";\n";
34
35 $this->fs->put_contents( $config_file_path, $file_content, FS_CHMOD_FILE );
36
37 if ( is_file( $config_file_path ) ) {
38 if ( function_exists( 'opcache_invalidate' ) ) {
39 opcache_invalidate( $config_file_path, true );
40 }
41 }
42 }
43
44 public function update( string $base_dir, Snippet $snippet, ?bool $remove = false ): void {
45 $active_snippets = $this->load( $base_dir );
46
47 if ( $remove ) {
48 unset( $active_snippets[ $snippet->id ] );
49 } else {
50 $active_snippets[ $snippet->id ] = $snippet->get_fields();
51 }
52
53 $this->save( $base_dir, $active_snippets );
54 }
55 }
56