88 * Wireframe is an output framework with MVC inspired architecture for ProcessWire CMS/CMF.
99 * See README.md or https://wireframe-framework.com for more details.
1010 *
11- * @version 0.2.1
11+ * @version 0.3.0
1212 * @author Teppo Koivula <teppo@wireframe-framework.com>
1313 * @license Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/
1414 */
15- class Wireframe extends WireData implements Module {
15+ class Wireframe extends WireData implements Module, ConfigurableModule {
1616
1717 /**
1818 * Config settings
@@ -63,6 +63,36 @@ class Wireframe extends WireData implements Module {
6363 */
6464 protected $ data = [];
6565
66+ /**
67+ * Create directories automatically?
68+ *
69+ * This property is only used by the module configuration screen. Contains an array of
70+ * directories to create automatically.
71+ *
72+ * @var array
73+ */
74+ protected $ create_directories = [];
75+
76+ /**
77+ * Return inputfields necessary to configure the module
78+ *
79+ * @param array $data Data array.
80+ * @return InputfieldWrapper
81+ */
82+ public function getModuleConfigInputfields (array $ data ) {
83+
84+ // init necessary parts of Wireframe
85+ $ this ->setConfig ();
86+ $ this ->setPaths ();
87+ $ this ->addNamespaces ();
88+
89+ // instantiate Wireframe Config and get all config inputfields
90+ $ config = new \Wireframe \Config ($ this ->wire (), $ this );
91+ $ fields = $ config ->getAllFields ();
92+
93+ return $ fields ;
94+ }
95+
6696 /**
6797 * Initialize Wireframe
6898 *
@@ -73,8 +103,8 @@ class Wireframe extends WireData implements Module {
73103 */
74104 public function ___init (array $ settings = []): Wireframe {
75105
76- // initialize config settings
77- $ this ->initConfig ();
106+ // set config settings
107+ $ this ->setConfig ();
78108
79109 // set any additional settings
80110 $ this ->setArray ($ settings );
@@ -85,9 +115,11 @@ public function ___init(array $settings = []): Wireframe {
85115 throw new WireException ('No valid Page object found ' );
86116 }
87117
88- // store paths and template extension as local variables
89- $ this ->paths = (object ) $ this ->config ['paths ' ];
90- $ this ->ext = ". " . $ this ->wire ('config ' )->templateExtension ;
118+ // store paths locally
119+ $ this ->setPaths ();
120+
121+ // store template extension locally
122+ $ this ->setExt ();
91123
92124 // add Wireframe namespaces to ProcessWire's classLoader
93125 $ this ->addNamespaces ();
@@ -114,11 +146,12 @@ public function ___init(array $settings = []): Wireframe {
114146 }
115147
116148 /**
117- * Initialize config settings
149+ * Define runtime config settings
118150 *
119151 * @param array $config Optional configuration settings array
152+ * @return Wireframe Self-reference
120153 */
121- public function ___initConfig (array $ config = []) {
154+ public function ___setConfig (array $ config = []): Wireframe {
122155
123156 // default config settings; if you need to customize or override any of
124157 // these, copy this array to /site/config.php as $config->wireframe
@@ -166,6 +199,29 @@ public function ___initConfig(array $config = []) {
166199 $ this ->wire ('config ' )->urls ->set ($ key , $ value );
167200 }
168201
202+ return $ this ;
203+ }
204+
205+ /**
206+ * Store paths in a class property
207+ *
208+ * @param array $paths Paths array for overriding the default value.
209+ * @return Wireframe Self-reference
210+ */
211+ public function setPaths (array $ paths = []): Wireframe {
212+ $ this ->paths = (object ) $ this ->config ['paths ' ];
213+ return $ this ;
214+ }
215+
216+ /**
217+ * Store template extension in a class property
218+ *
219+ * @param string|null $ext Extension string for overriding the default value.
220+ * @return Wireframe Self-reference
221+ */
222+ public function setExt (string $ ext = null ): Wireframe {
223+ $ this ->ext = ". " . ($ ext ?? $ this ->wire ('config ' )->templateExtension );
224+ return $ this ;
169225 }
170226
171227 /**
@@ -229,33 +285,42 @@ protected function addHooks() {
229285 */
230286 public function ___checkRedirects () {
231287
232- // params
233- $ config = $ this ->config ;
288+ // redirect fields from Wireframe runtime configuration
289+ $ redirect_fields = $ this ->config ['redirect_fields ' ] ?? null ;
290+ if (empty ($ redirect_fields )) return ;
291+
292+ // current Page object
234293 $ page = $ this ->page ;
235294
236- if (!empty ($ config ['redirect_fields ' ])) {
237- foreach ($ config ['redirect_fields ' ] as $ field => $ options ) {
238- if (is_int ($ field ) && is_string ($ options )) {
239- $ field = $ options ;
295+ foreach ($ redirect_fields as $ field => $ options ) {
296+
297+ // redirect_fields may be an indexed array
298+ if (is_int ($ field ) && is_string ($ options )) {
299+ $ field = $ options ;
300+ }
301+
302+ // get URL from a page field
303+ $ url = $ page ->get ($ field );
304+ if (empty ($ url )) continue ;
305+
306+ // default to non-permanent redirect (302)
307+ $ permanent = false ;
308+
309+ // if options is an array, read contained settings
310+ if (is_array ($ options )) {
311+ if (!empty ($ options ['property ' ])) {
312+ $ url = $ url ->$ options ['property ' ];
240313 }
241- if ($ page ->$ field ) {
242- $ url = $ page ->$ field ;
243- $ permanent = false ;
244- if (is_array ($ options )) {
245- if (isset ($ options ['property ' ])) {
246- $ url = $ url ->$ options ['property ' ];
247- }
248- if (isset ($ options ['permanent ' ])) {
249- $ permanent = (bool ) $ options ['permanent ' ];
250- }
251- }
252- if (is_string ($ url ) && $ url != $ page ->url && $ this ->wire ('sanitizer ' )->url ($ url )) {
253- $ this ->redirect ($ url , $ permanent );
254- }
314+ if (!empty ($ options ['permanent ' ])) {
315+ $ permanent = (bool ) $ options ['permanent ' ];
255316 }
256317 }
257- }
258318
319+ // if target URL is valid and doesn't belong to current page, perform a redirect
320+ if (is_string ($ url ) && $ url != $ page ->url && $ this ->wire ('sanitizer ' )->url ($ url )) {
321+ $ this ->redirect ($ url , $ permanent );
322+ }
323+ }
259324 }
260325
261326 /**
@@ -298,7 +363,6 @@ public function ___initView(): \Wireframe\View {
298363 $ this ->wire ('view ' , $ view );
299364
300365 return $ view ;
301-
302366 }
303367
304368 /**
@@ -329,7 +393,6 @@ public function ___initController(): ?\Wireframe\Controller {
329393 $ this ->controller = $ controller ;
330394
331395 return $ controller ;
332-
333396 }
334397
335398 /**
@@ -385,7 +448,6 @@ public function ___setView() {
385448 }
386449 }
387450 }
388-
389451 }
390452
391453 /**
@@ -413,23 +475,21 @@ public function ___render(array $data = []): ?string {
413475 }
414476
415477 // render output
416- $ out = null ;
478+ $ output = null ;
417479 if ($ view ->filename || $ view ->layout ) {
418- $ out = $ view ->render ();
480+ $ output = $ view ->render ();
419481 if ($ filename = basename ($ view ->layout )) {
420482 // layouts make it possible to define a common base structure for
421483 // multiple otherwise separate template and view files (DRY)
422484 $ view ->setFilename ($ paths ->layouts . $ filename . $ ext );
423485 if (!$ view ->placeholders ->default ) {
424- $ view ->placeholders ->default = $ out ;
486+ $ view ->placeholders ->default = $ output ;
425487 }
426- $ out = $ view ->render ();
488+ $ output = $ view ->render ();
427489 }
428490 }
429491
430- // return rendered output
431- return $ out ;
432-
492+ return $ output ;
433493 }
434494
435495 /**
@@ -438,6 +498,7 @@ public function ___render(array $data = []): ?string {
438498 * Example: <?= $page->layout('default')->render() ?>
439499 *
440500 * @param HookEvent $event
501+ *
441502 * @see addHooks() for the code that attaches Wireframe hooks
442503 */
443504 public function pageLayout (HookEvent $ event ) {
@@ -455,6 +516,7 @@ public function pageLayout(HookEvent $event) {
455516 * Example: <?= $page->view('json')->render() ?>
456517 *
457518 * @param HookEvent $event
519+ *
458520 * @see addHooks() for the code that attaches Wireframe hooks
459521 */
460522 public function pageView (HookEvent $ event ) {
@@ -492,7 +554,7 @@ public function __set($key, $value): Wireframe {
492554 }
493555
494556 /**
495- * Getter method for specific class properties
557+ * Getter method for specific class properties
496558 *
497559 * Note that this differs notably from the parent class' get() method: unlike
498560 * in WireData, here we limit the scope of the method to specific, predefined
@@ -527,32 +589,48 @@ public function get($key) {
527589 * @throws WireException if trying to set invalid value to a property
528590 */
529591 public function set ($ key , $ value ): Wireframe {
592+
593+ // value is invalid until proven valid
530594 $ invalid_value = true ;
595+
531596 switch ($ key ) {
532597 case 'data ' :
533598 if (is_array ($ value )) {
534- $ this ->$ key = $ value ;
535599 $ invalid_value = false ;
600+ $ this ->$ key = $ value ;
536601 }
537602 break ;
538603 case 'page ' :
539604 if ($ value instanceof Page && $ value ->id ) {
540- $ this ->$ key = $ value ;
541605 $ invalid_value = false ;
606+ $ this ->$ key = $ value ;
542607 }
543608 break ;
609+ case 'create_directories ' :
610+ // module config (saved values)
611+ $ invalid_value = false ;
612+ $ this ->$ key = $ value ;
613+ break ;
614+ case 'uninstall ' :
615+ case 'submit_save_module ' :
616+ // module config (skipped values)
617+ $ invalid_value = false ;
618+ break ;
544619 default :
545620 throw new WireException (sprintf (
546621 'Unable to set value for unrecognized property "%s" ' ,
547622 $ key
548623 ));
549624 }
625+
626+ // if value is invalid, throw an exception
550627 if ($ invalid_value ) {
551628 throw new WireException (sprintf (
552629 'Invalid value provided for "%s" ' ,
553630 $ key
554631 ));
555632 }
633+
556634 return $ this ;
557635 }
558636
0 commit comments