|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Cache for autoload. |
| 4 | + * |
| 5 | + * @package wppunk/wpautoload |
| 6 | + * @author WPPunk |
| 7 | + * @link https://github.com/wppunk/wpautoload/ |
| 8 | + * @copyright Copyright (c) 2020 |
| 9 | + * @license GPL-2.0+ |
| 10 | + */ |
| 11 | + |
| 12 | +namespace WPPunk\Autoload; |
| 13 | + |
| 14 | +/** |
| 15 | + * @codeCoverageIgnore |
| 16 | + */ |
| 17 | +if ( class_exists( '\WPPunk\Autoload\Cache' ) ) { |
| 18 | + return; |
| 19 | +} |
| 20 | +/** |
| 21 | + * @codingStandardsIgnoreEnd |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * Class Cache |
| 26 | + * |
| 27 | + * @package wppunk/wpautoload |
| 28 | + */ |
| 29 | +class Cache { |
| 30 | + |
| 31 | + /** |
| 32 | + * Classmap file |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + private $map_file; |
| 37 | + /** |
| 38 | + * Classmap |
| 39 | + * |
| 40 | + * @var array |
| 41 | + */ |
| 42 | + private $map; |
| 43 | + /** |
| 44 | + * Has the cache been updated |
| 45 | + * |
| 46 | + * @var bool |
| 47 | + */ |
| 48 | + private $has_been_update = false; |
| 49 | + |
| 50 | + /** |
| 51 | + * Cache constructor. |
| 52 | + */ |
| 53 | + public function __construct() { |
| 54 | + $this->map_file = __DIR__ . '/../cache/classmap.php'; |
| 55 | + if ( file_exists( $this->map_file ) ) { |
| 56 | + include $this->map_file; |
| 57 | + } |
| 58 | + $this->map = is_array( $this->map ) ? $this->map : []; |
| 59 | + register_shutdown_function( [ $this, 'save' ] ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get path for class |
| 64 | + * |
| 65 | + * @param string $class Class name. |
| 66 | + * |
| 67 | + * @return string |
| 68 | + */ |
| 69 | + public function get( $class ) { |
| 70 | + return isset( $this->map[ $class ] ) ? $this->map[ $class ] : ''; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Update cache |
| 75 | + * |
| 76 | + * @param string $class Class name. |
| 77 | + * @param string $path Path to file. |
| 78 | + */ |
| 79 | + public function update( $class, $path ) { |
| 80 | + $this->has_been_update = true; |
| 81 | + $this->map[ $class ] = $path; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Save cache |
| 86 | + */ |
| 87 | + public function save() { |
| 88 | + if ( ! $this->has_been_update ) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $this->create_dir(); |
| 93 | + |
| 94 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
| 95 | + file_put_contents( $this->map_file, '<?php return [' . $this->create_map() . '];', LOCK_EX ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Create cache directory. |
| 100 | + */ |
| 101 | + private function create_dir() { |
| 102 | + if ( ! file_exists( dirname( $this->map_file ) ) ) { |
| 103 | + mkdir( dirname( $this->map_file ), 0755, true ); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Create class map. |
| 109 | + * |
| 110 | + * @return string |
| 111 | + */ |
| 112 | + private function create_map() { |
| 113 | + $map = ''; |
| 114 | + $last = end( $this->map ); |
| 115 | + foreach ( $this->map as $key => $value ) { |
| 116 | + $map .= "'$key' => '$value'"; |
| 117 | + if ( $value !== $last ) { |
| 118 | + $map .= ",\n"; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return $map; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Clear garbage classes |
| 127 | + */ |
| 128 | + public function clear_garbage() { |
| 129 | + foreach ( $this->map as $key => $file ) { |
| 130 | + $this->clear_class( $key, $file ); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Clear class |
| 136 | + * |
| 137 | + * @param string $class_name Class name. |
| 138 | + * @param string $path Path to file. |
| 139 | + */ |
| 140 | + private function clear_class( $class_name, $path ) { |
| 141 | + $path = realpath( $path ); |
| 142 | + if ( ! file_exists( $path ) ) { |
| 143 | + $this->has_been_update = true; |
| 144 | + unset( $this->map[ $class_name ] ); |
| 145 | + |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + $this->map[ $class_name ] = $path; |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments