1 | <?php |
||
19 | class Packages_manipulation { |
||
20 | /** |
||
21 | * Generic extraction of files from phar distributive for CleverStyle Framework (components installation) |
||
22 | * |
||
23 | * @param string $target_directory |
||
24 | * @param string $source_phar Will be removed after extraction |
||
25 | * |
||
26 | * @return bool |
||
27 | */ |
||
28 | public static function install_extract ($target_directory, $source_phar) { |
||
57 | /** |
||
58 | * Generic extraction of files from phar distributive for CleverStyle Framework (system and components update) |
||
59 | * |
||
60 | * @param string $target_directory |
||
61 | * @param string $source_phar Will be removed after extraction |
||
62 | * @param null|string $fs_location_directory Defaults to `$target_directory` |
||
63 | * @param null|string $meta_location_directory Defaults to `$target_directory` |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | public static function update_extract ($target_directory, $source_phar, $fs_location_directory = null, $meta_location_directory = null) { |
||
68 | $fs_location_directory = $fs_location_directory ?: $target_directory; |
||
69 | $meta_location_directory = $meta_location_directory ?: $target_directory; |
||
70 | /** |
||
71 | * Backup some necessary information about current version |
||
72 | */ |
||
73 | copy("$fs_location_directory/fs.json", "$fs_location_directory/fs_backup.json"); |
||
74 | copy("$meta_location_directory/meta.json", "$meta_location_directory/meta_backup.json"); |
||
75 | /** |
||
76 | * Extracting new versions of files |
||
77 | */ |
||
78 | $tmp_dir = "phar://$source_phar"; |
||
79 | $fs = file_get_json("$tmp_dir/fs.json"); |
||
80 | $extracted = array_filter( |
||
81 | array_map( |
||
82 | function ($index, $file) use ($tmp_dir, $target_directory) { |
||
83 | if ( |
||
84 | !@mkdir(dirname("$target_directory/$file"), 0770, true) && |
||
85 | !is_dir(dirname("$target_directory/$file")) |
||
86 | ) { |
||
87 | return false; |
||
88 | } |
||
89 | return copy("$tmp_dir/fs/$index", "$target_directory/$file"); |
||
90 | }, |
||
91 | $fs, |
||
92 | array_keys($fs) |
||
93 | ) |
||
94 | ); |
||
95 | unlink($source_phar); |
||
96 | unset($tmp_dir); |
||
97 | if (count($extracted) !== count($fs)) { |
||
98 | return false; |
||
99 | } |
||
100 | unset($extract); |
||
101 | $fs = array_keys($fs); |
||
102 | /** |
||
103 | * Removing of old unnecessary files and directories |
||
104 | */ |
||
105 | foreach ( |
||
106 | array_diff( |
||
107 | file_get_json("$fs_location_directory/fs_backup.json"), |
||
108 | $fs |
||
109 | ) as $file |
||
110 | ) { |
||
111 | $file = "$target_directory/$file"; |
||
112 | if (is_writable($file)) { |
||
113 | unlink($file); |
||
114 | // Recursively remove all empty parent directories |
||
115 | while (!get_files_list($file = dirname($file), false, 'fd')) { |
||
116 | rmdir($file); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | unset($file, $dir); |
||
121 | file_put_json("$fs_location_directory/fs.json", $fs); |
||
122 | clearstatcache(true); |
||
123 | if (function_exists('opcache_reset')) { |
||
124 | opcache_reset(); |
||
125 | } |
||
126 | return true; |
||
127 | } |
||
128 | /** |
||
129 | * Generic update for CleverStyle Framework (system and components), runs PHP scripts and does DB migrations after extracting of new distributive |
||
130 | * |
||
131 | * @param string $target_directory |
||
132 | * @param string $old_version |
||
133 | * @param array|null $db_array `$Config->components['modules'][$module]['db']` if module or system |
||
134 | * |
||
135 | * @throws \cs\ExitException |
||
136 | */ |
||
137 | public static function update_php_sql ($target_directory, $old_version, $db_array = null) { |
||
153 | /** |
||
154 | * @param string $target_directory |
||
155 | * |
||
156 | * @return string[] |
||
157 | */ |
||
158 | protected static function get_update_versions ($target_directory) { |
||
171 | /** |
||
172 | * @param string $directory Base path to SQL files |
||
173 | * @param array $db_configuration Array in form [$db_name => $index] |
||
174 | * @param string $version In case when we are working with update script we might have version subdirectory |
||
175 | * |
||
176 | * @throws \cs\ExitException |
||
177 | */ |
||
178 | public static function execute_sql_from_directory ($directory, $db_configuration, $version = '') { |
||
202 | } |
||
203 |