PluginProbe
Code Snippets / 3.10.1
Code Snippets v3.10.1
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 3.0.1 All 64 releases
code-snippets / php / Flat_Files / Flat_File_Config_Repository.php

Flat_File_Config_Repository.php in Code Snippets 3.10.1, at php/Flat_Files/Flat_File_Config_Repository.php

104 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets\Flat_Files;
4
5 use Code_Snippets\Flat_Files\Interfaces\Filesystem_Adapter;
6 use Code_Snippets\Flat_Files\Interfaces\Snippet_Config_Repository;
7 use Code_Snippets\Model\Snippet;
8
9 /**
10 * Snippet configuration repository implementation.
11 */
12 class Flat_File_Config_Repository implements Snippet_Config_Repository {
13
14 /**
15 * Name of file to store configuration.
16 */
17 private const CONFIG_FILE_NAME = 'index.php';
18
19 /**
20 * Adapter to use for connecting to the filesystem.
21 *
22 * @var Filesystem_Adapter
23 */
24 private Filesystem_Adapter $fs;
25
26 /**
27 * Constructor.
28 *
29 * @param Filesystem_Adapter $fs Adapter to use for connecting to the filesystem.
30 */
31 public function __construct( Filesystem_Adapter $fs ) {
32 $this->fs = $fs;
33 }
34
35 /**
36 * Load configuration from a directory.
37 *
38 * @param string $base_dir Full filesystem path to directory.
39 *
40 * @return array Loaded configuration.
41 */
42 public function load( string $base_dir ): array {
43 $config_file_path = trailingslashit( $base_dir ) . static::CONFIG_FILE_NAME;
44
45 if ( is_file( $config_file_path ) ) {
46 if ( function_exists( 'opcache_invalidate' ) ) {
47 opcache_invalidate( $config_file_path, true );
48 }
49 return require $config_file_path;
50 }
51 return [];
52 }
53
54 /**
55 * Store configuration.
56 *
57 * @param string $base_dir Full filesystem path to configuration directory.
58 * @param array $active_snippets List of active snippets.
59 *
60 * @return void
61 *
62 * phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_var_export
63 */
64 public function save( string $base_dir, array $active_snippets ): void {
65 $config_file_path = trailingslashit( $base_dir ) . static::CONFIG_FILE_NAME;
66
67 ksort( $active_snippets );
68
69 $file_content = sprintf(
70 "<?php\n\nif ( ! defined( 'ABSPATH' ) ) { return; }\n\nreturn %s;\n",
71 var_export( $active_snippets, true )
72 );
73
74 $this->fs->put_contents( $config_file_path, $file_content, FS_CHMOD_FILE );
75
76 if ( is_file( $config_file_path ) ) {
77 if ( function_exists( 'opcache_invalidate' ) ) {
78 opcache_invalidate( $config_file_path, true );
79 }
80 }
81 }
82
83 /**
84 * Update stored configuration for a snippet.
85 *
86 * @param string $base_dir Full filesystem path to configuration directory.
87 * @param Snippet $snippet Snippet to update.
88 * @param bool|null $remove Whether to remove the snippet from the configuration.
89 *
90 * @return void
91 */
92 public function update( string $base_dir, Snippet $snippet, ?bool $remove = false ): void {
93 $active_snippets = $this->load( $base_dir );
94
95 if ( $remove ) {
96 unset( $active_snippets[ $snippet->id ] );
97 } else {
98 $active_snippets[ $snippet->id ] = $snippet->get_fields();
99 }
100
101 $this->save( $base_dir, $active_snippets );
102 }
103 }
104