Skip to content

Commit 249f352

Browse files
committed
Add Wireframe::createDirectories() method
1 parent 2b32f99 commit 249f352

File tree

3 files changed

+91
-29
lines changed

3 files changed

+91
-29
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.30.0] - 2024-12-01
11+
12+
### Added
13+
- New method Wireframe::createDirectories() for adding Wireframe directories in code.
14+
1015
## [0.29.2] - 2024-05-30
1116

1217
### Fixed

Wireframe.module.php

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @method static string|Page|NullPage page($source, $args = []) Static getter (factory) method for Pages.
1515
* @method static string|null partial(string $partial_name, array $args = []) Static getter (factory) method for Partials.
1616
*
17-
* @version 0.29.2
17+
* @version 0.30.0
1818
* @author Teppo Koivula <teppo@wireframe-framework.com>
1919
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/
2020
*/
@@ -1145,4 +1145,86 @@ public function getController(?Page $page = null, ?string $template_name = null)
11451145
return null;
11461146
}
11471147

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+
11481230
}

lib/Config.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Configuration helper for the Wireframe module
1111
*
12-
* @version 0.3.1
12+
* @version 0.3.2
1313
* @author Teppo Koivula <teppo@wireframe-framework.com>
1414
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/
1515
*/
@@ -87,7 +87,7 @@ protected function getCreateDirectoriesField(): InputfieldCheckboxes {
8787
protected function processCreateDirectoriesField(InputfieldCheckboxes $field): InputfieldCheckboxes {
8888

8989
// get an array of paths and bail out early if the paths array is empty
90-
$paths = $this->getPaths();
90+
$paths = $this->wireframe->getDirectoryPaths();
9191
if (empty($paths)) {
9292
$field->notes = $this->_('No directories found, possible configuration error. Please check your site config.');
9393
return $field;
@@ -123,7 +123,7 @@ protected function processCreateDirectoriesField(InputfieldCheckboxes $field): I
123123
if (\is_array($this->wire('input')->post->create_directories) && \in_array($key, $this->wire('input')->post->create_directories)) {
124124

125125
// attempt to create a directory
126-
$path_created = \ProcessWire\wireMkDir($real_path);
126+
$path_created = \ProcessWire\wireMkdir($real_path);
127127

128128
if ($path_created) {
129129

@@ -162,29 +162,4 @@ protected function processCreateDirectoriesField(InputfieldCheckboxes $field): I
162162
return $field;
163163
}
164164

165-
/**
166-
* Get paths for the create directories feature
167-
*
168-
* @return \stdClass
169-
*/
170-
protected function getPaths(): \stdClass {
171-
172-
// get paths object from Wireframe
173-
$paths = $this->wireframe->paths;
174-
175-
// append relative URLs
176-
$urls = $this->wireframe->getConfig()['urls'] ?? [];
177-
if (!empty($urls)) {
178-
foreach ($urls as $key => $url) {
179-
if (strpos($url, '/') !== 0) {
180-
// not a local path, skip
181-
continue;
182-
}
183-
$paths->$key = '@ ' . rtrim($this->wire('config')->paths->root, '/') . $url;
184-
}
185-
}
186-
187-
return $paths;
188-
}
189-
190165
}

0 commit comments

Comments
 (0)