|
14 | 14 | * @method static string|Page|NullPage page($source, $args = []) Static getter (factory) method for Pages. |
15 | 15 | * @method static string|null partial(string $partial_name, array $args = []) Static getter (factory) method for Partials. |
16 | 16 | * |
17 | | - * @version 0.29.2 |
| 17 | + * @version 0.30.0 |
18 | 18 | * @author Teppo Koivula <teppo@wireframe-framework.com> |
19 | 19 | * @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/ |
20 | 20 | */ |
@@ -1145,4 +1145,86 @@ public function getController(?Page $page = null, ?string $template_name = null) |
1145 | 1145 | return null; |
1146 | 1146 | } |
1147 | 1147 |
|
| 1148 | + /** |
| 1149 | + * Create directories |
| 1150 | + * |
| 1151 | + * This method is used for creating directories used by Wireframe. It's called from the module configuration screen, |
| 1152 | + * but can also be called in code if needed. |
| 1153 | + * |
| 1154 | + * @param array|null $keys Optional array of keys to create directories for, leave blank to create all directories. |
| 1155 | + * @return array Array of existing directories. |
| 1156 | + * |
| 1157 | + * @throws WireException if no directories are found. |
| 1158 | + * @throws WireException if an invalid path key is provided. |
| 1159 | + * @throws WireException if a directory can't be created. |
| 1160 | + */ |
| 1161 | + public function createDirectories(?array $keys = null): array { |
| 1162 | + |
| 1163 | + $paths = $this->getDirectoryPaths(); |
| 1164 | + if (empty($paths)) { |
| 1165 | + throw new WireException( |
| 1166 | + $this->_('No directories found, possible configuration error. Please check your site config.') |
| 1167 | + ); |
| 1168 | + } |
| 1169 | + |
| 1170 | + $keys = $keys ?? array_keys((array) $paths); |
| 1171 | + $dirs = []; |
| 1172 | + |
| 1173 | + foreach ($keys as $key) { |
| 1174 | + $path = $paths->$key ?? null; |
| 1175 | + if ($path === null) { |
| 1176 | + throw new WireException(sprintf( |
| 1177 | + $this->_('Invalid path key: %s'), |
| 1178 | + $key |
| 1179 | + )); |
| 1180 | + } |
| 1181 | + if (substr($path, 0, 2) === '@ ') { |
| 1182 | + $path = substr($path, 2); |
| 1183 | + } |
| 1184 | + if (\is_dir($path)) { |
| 1185 | + $dirs[$key] = $path; |
| 1186 | + } else { |
| 1187 | + $parent_dir = \dirname($path); |
| 1188 | + if (\is_writable($parent_dir)) { |
| 1189 | + \ProcessWire\wireMkdir($path); |
| 1190 | + if (\is_dir($path)) { |
| 1191 | + $dirs[$key] = $path; |
| 1192 | + } |
| 1193 | + } else { |
| 1194 | + throw new WireException(sprintf( |
| 1195 | + $this->_('Unable to create directory: %s (parent directory not writable)'), |
| 1196 | + $path |
| 1197 | + )); |
| 1198 | + } |
| 1199 | + } |
| 1200 | + } |
| 1201 | + |
| 1202 | + return $dirs; |
| 1203 | + } |
| 1204 | + |
| 1205 | + /** |
| 1206 | + * Get paths for the create directories feature |
| 1207 | + * |
| 1208 | + * @return \stdClass |
| 1209 | + */ |
| 1210 | + protected function getDirectoryPaths(): \stdClass { |
| 1211 | + |
| 1212 | + // get paths object |
| 1213 | + $paths = $this->paths; |
| 1214 | + |
| 1215 | + // append relative URLs |
| 1216 | + $urls = $this->getConfig()['urls'] ?? []; |
| 1217 | + if (!empty($urls)) { |
| 1218 | + foreach ($urls as $key => $url) { |
| 1219 | + if (strpos($url, '/') !== 0) { |
| 1220 | + // not a local path, skip |
| 1221 | + continue; |
| 1222 | + } |
| 1223 | + $paths->$key = '@ ' . rtrim($this->wire('config')->paths->root, '/') . $url; |
| 1224 | + } |
| 1225 | + } |
| 1226 | + |
| 1227 | + return $paths; |
| 1228 | + } |
| 1229 | + |
1148 | 1230 | } |
0 commit comments