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 | 2 | public static function install_extract ($target_directory, $source_phar) { |
|
43 | /** |
||
44 | * @param array $fs |
||
45 | * @param string $tmp_dir |
||
46 | * @param string $target_directory |
||
47 | * |
||
48 | * @return bool |
||
49 | */ |
||
50 | 2 | protected static function generic_extract ($fs, $tmp_dir, $target_directory) { |
|
51 | 2 | $extracted = array_filter( |
|
52 | 2 | array_map( |
|
53 | function ($index, $file) use ($tmp_dir, $target_directory) { |
||
54 | if ( |
||
55 | 2 | !@mkdir(dirname("$target_directory/$file"), 0770, true) && |
|
56 | 2 | !is_dir(dirname("$target_directory/$file")) |
|
57 | ) { |
||
58 | return false; |
||
59 | } |
||
60 | 2 | return copy("$tmp_dir/fs/$index", "$target_directory/$file"); |
|
61 | 2 | }, |
|
62 | 2 | $fs, |
|
63 | 2 | array_keys($fs) |
|
64 | ) |
||
65 | ); |
||
66 | 2 | return count($extracted) === count($fs); |
|
67 | } |
||
68 | /** |
||
69 | * Generic extraction of files from phar distributive for CleverStyle Framework (system and components update) |
||
70 | * |
||
71 | * @param string $target_directory |
||
72 | * @param string $source_phar Will be removed after extraction |
||
73 | * @param null|string $fs_location_directory Defaults to `$target_directory` |
||
74 | * @param null|string $meta_location_directory Defaults to `$target_directory` |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | 2 | public static function update_extract ($target_directory, $source_phar, $fs_location_directory = null, $meta_location_directory = null) { |
|
79 | 2 | $fs_location_directory = $fs_location_directory ?: $target_directory; |
|
80 | 2 | $meta_location_directory = $meta_location_directory ?: $target_directory; |
|
81 | /** |
||
82 | * Backup some necessary information about current version |
||
83 | */ |
||
84 | 2 | copy("$fs_location_directory/fs.json", "$fs_location_directory/fs_backup.json"); |
|
85 | 2 | copy("$meta_location_directory/meta.json", "$meta_location_directory/meta_backup.json"); |
|
86 | /** |
||
87 | * Extracting new versions of files |
||
88 | */ |
||
89 | 2 | $tmp_dir = "phar://$source_phar"; |
|
90 | 2 | $fs = file_get_json("$tmp_dir/fs.json"); |
|
91 | 2 | $extracted = static::generic_extract($fs, $tmp_dir, $target_directory); |
|
92 | 2 | if (!$extracted) { |
|
93 | 2 | return false; |
|
94 | } |
||
95 | 2 | unlink($source_phar); |
|
96 | 2 | $fs = array_keys($fs); |
|
97 | /** |
||
98 | * Removing of old unnecessary files and directories |
||
99 | */ |
||
100 | foreach ( |
||
101 | 2 | array_diff( |
|
102 | 2 | file_get_json("$fs_location_directory/fs_backup.json"), |
|
103 | 2 | $fs |
|
104 | ) as $file |
||
105 | ) { |
||
106 | 2 | $file = "$target_directory/$file"; |
|
107 | 2 | if (is_writable($file)) { |
|
108 | 2 | unlink($file); |
|
109 | // Recursively remove all empty parent directories |
||
110 | 2 | while (!get_files_list($file = dirname($file), false, 'fd')) { |
|
111 | 2 | rmdir($file); |
|
112 | } |
||
113 | } |
||
114 | } |
||
115 | 2 | unset($file, $dir); |
|
116 | 2 | file_put_json("$fs_location_directory/fs.json", $fs); |
|
117 | 2 | clearstatcache(true); |
|
118 | 2 | if (function_exists('opcache_reset')) { |
|
119 | 2 | opcache_reset(); |
|
120 | } |
||
121 | 2 | return true; |
|
122 | } |
||
123 | /** |
||
124 | * Generic update for CleverStyle Framework (system and components), runs PHP scripts and does DB migrations after extracting of new distributive |
||
125 | * |
||
126 | * @param string $target_directory |
||
127 | * @param string $old_version |
||
128 | * @param array|null $db_array `$Config->components['modules'][$module]['db']` if module or system |
||
129 | * |
||
130 | * @throws \cs\ExitException |
||
131 | */ |
||
132 | 2 | public static function update_php_sql ($target_directory, $old_version, $db_array = null) { |
|
148 | /** |
||
149 | * @param string $target_directory |
||
150 | * |
||
151 | * @return string[] |
||
152 | */ |
||
153 | 2 | protected static function get_update_versions ($target_directory) { |
|
154 | 2 | $update_versions = _mb_substr(get_files_list("$target_directory/meta/update"), 0, -4) ?: []; |
|
155 | 2 | foreach (get_files_list("$target_directory/meta/update_db", false, 'd') ?: [] as $db) { |
|
156 | /** @noinspection SlowArrayOperationsInLoopInspection */ |
||
157 | 2 | $update_versions = array_merge( |
|
158 | 2 | $update_versions, |
|
159 | 2 | get_files_list("$target_directory/meta/update_db/$db", false, 'd') ?: [] |
|
160 | ); |
||
161 | } |
||
162 | 2 | $update_versions = array_unique($update_versions); |
|
163 | 2 | usort($update_versions, 'version_compare'); |
|
164 | 2 | return $update_versions; |
|
165 | } |
||
166 | /** |
||
167 | * @param string $directory Base path to SQL files |
||
168 | * @param array $db_configuration Array in form [$db_name => $index] |
||
169 | * @param string $version In case when we are working with update script we might have version subdirectory |
||
170 | * |
||
171 | * @throws \cs\ExitException |
||
172 | */ |
||
173 | 2 | public static function execute_sql_from_directory ($directory, $db_configuration, $version = '') { |
|
197 | } |
||
198 |