| 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 |
|