1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-07 07:16:51 +02:00

Add ConfigModule interface as alternative to ConfigurableModule interface for modules that want to maintain non-interactive configuration data

This commit is contained in:
Ryan Cramer
2021-05-18 12:49:36 -04:00
parent e028d9f7b6
commit 335ad1cefe
2 changed files with 77 additions and 18 deletions

View File

@@ -1,41 +1,57 @@
<?php namespace ProcessWire; <?php namespace ProcessWire;
/** /**
* ProcessWire ConfigurableModule Interface * ProcessWire ConfigurableModule and ConfigModule Interfaces
* *
* Provides the base interfaces required by modules. * Provides the base interfaces required by modules.
* *
* This file is licensed under the MIT license * This file is licensed under the MIT license
* https://processwire.com/about/license/mit/ * https://processwire.com/about/license/mit/
* *
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer * ProcessWire 3.x, Copyright 2021 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* *
* About the ConfigurableModule interface * About the ConfigurableModule interface
* ====================================== * ======================================
* ConfigurableModule is an interface that indicates the module is configurable by providing * ConfigurableModule is an interface that indicates the module is configurable by providing
* __get() and __set() methods for getting and setting config values. Modules implementing * `__get()` and `__set()` methods for getting and setting config values. Modules implementing
* this interface are assumed to also implement the 'Module' interface. * this interface are assumed to also implement the `Module` interface.
* *
* The module must also provide one (1) of the following: * The module must also provide one (1) of the following:
* *
* 1. A getModuleConfigInputfields([$data]) method (static or non-static); OR * 1. A `getModuleConfigInputfields([$data])` method (static or non-static); OR
* 2. A separate ModuleName.config.php file that just populates $config array; OR * 2. A separate `ModuleName.config.php` file that just populates $config array; OR
* 3. A separate ModuleNameConfig.php file that contains a ModuleConfig class. * 3. A separate `ModuleNameConfig.php` file that contains a ModuleConfig class.
* *
* For more details about the above options, see the commented methods within * For more details about the above options, see the commented methods within
* the interface. * the interface.
* *
* When you use this as an interface, you MUST also use 'Module' as an interface, * When you use this as an interface, you MUST also use `Module` as an interface,
* i.e. "class Something implements Module, ConfigurableModule" * i.e. `class Something implements Module, ConfigurableModule`
* *
* Hint: Make your ConfigurableModule classes inherit from WireData, which already has * Hint: Make your ConfigurableModule classes inherit from `WireData`, which already has
* the get/set required methods. * the get/set required methods.
* *
* You may optionally specify a handler method for configuration data: setConfigData(). * You may optionally specify a handler method for configuration data: `setConfigData()`.
* If present, it will be used. See commented function reference in the interface below. * If present, it will be used. See commented function reference in the interface below.
* *
*
* About the ConfigModule interface (3.0.179+)
* ===========================================
* This interface indicates the module can receive config settings, but is not
* interactively configurable. Use this for modules where configuration will
* only be managed from the API/code side. Config settings must be saved using
* `$modules->saveConfig()`. Settings will be automatically populated to the module
* when it is loaded, or may be retrieved with `$modules->getConfig()`.
*
* Beyond the difference mentioned above, this interface is identical to the
* ConfigurableModule interface except that it needs no getModuleConfigInputfields()
* method nor will it use a configuration php or json file.
*
* A module *must not* contain both the ConfigModule and ConfigurableModule interfaces
* in their implements definition at the same time, so choose just one.
*
* *
*/ */
interface ConfigurableModule { interface ConfigurableModule {
@@ -152,3 +168,16 @@ interface ConfigurableModule {
*/ */
} }
/**
* ProcessWire ConfigModule interface
*
* See notes about this interface and its differences in the ConfigurableModule documentation.
*
* @since 3.0.179
*
*/
interface ConfigModule {
public function __get($key);
public function __set($key, $value);
}

View File

@@ -288,6 +288,7 @@ class Modules extends WireArray {
'permissions', 'permissions',
'searchable', 'searchable',
'page', 'page',
// 'languages',
); );
/** /**
@@ -2928,7 +2929,9 @@ class Modules extends WireArray {
// verbose mode only: this is set to the module filename (from PW installation root), false when it can't be found, null when it hasn't been determined // verbose mode only: this is set to the module filename (from PW installation root), false when it can't be found, null when it hasn't been determined
'file' => null, 'file' => null,
// verbose mode only: this is set to true when the module is a core module, false when it's not, and null when it's not determined // verbose mode only: this is set to true when the module is a core module, false when it's not, and null when it's not determined
'core' => null, 'core' => null,
// verbose mode only: any translations supplied with the module
// 'languages' => null,
// other properties that may be present, but are optional, for Process modules: // other properties that may be present, but are optional, for Process modules:
// 'nav' => array(), // navigation definition: see Process.php // 'nav' => array(), // navigation definition: see Process.php
@@ -3446,10 +3449,9 @@ class Modules extends WireArray {
if(is_array($this->configData[$id])) { if(is_array($this->configData[$id])) {
$data = $this->configData[$id]; $data = $this->configData[$id];
} else { } else {
// first verify that module doesn't have a config file $configable = $this->isConfigable($className);
$configurable = $this->isConfigurable($className); if(!$configable) return $emptyReturn;
if(!$configurable) return $emptyReturn; $database = $this->wire()->database;
$database = $this->wire('database');
$query = $database->prepare("SELECT data FROM modules WHERE id=:id", "modules.getConfig($className)"); // QA $query = $database->prepare("SELECT data FROM modules WHERE id=:id", "modules.getConfig($className)"); // QA
$query->bindValue(":id", (int) $id, \PDO::PARAM_INT); $query->bindValue(":id", (int) $id, \PDO::PARAM_INT);
$query->execute(); $query->execute();
@@ -3602,7 +3604,7 @@ class Modules extends WireArray {
} }
/** /**
* Is the given module configurable? * Is the given module interactively configurable?
* *
* This method can be used to simply determine if a module is configurable (yes or no), or more specifically * This method can be used to simply determine if a module is configurable (yes or no), or more specifically
* how it is configurable. * how it is configurable.
@@ -3836,6 +3838,34 @@ class Modules extends WireArray {
return $result; return $result;
} }
/**
* Indicates whether module accepts config settings, whether interactively or API only
*
* - Returns false if module does not accept config settings.
* - Returns integer `30` if module accepts config settings but is not interactively configurable.
* - Returns true, int or string if module is interactively configurable, see `Modules::isConfigurable()` return values.
*
* @param string|Module $class
* @param bool $useCache
* @return bool|int|string
* @since 3.0.179
*
*/
public function isConfigable($class, $useCache = true) {
if(is_object($class)) {
if($class instanceof ConfigModule) {
$result = 30;
} else {
$result = $this->isConfigurable($class, $useCache);
}
} else {
$result = $this->isConfigurable($class, $useCache);
if(!$result && wireInstanceOf($class, 'ConfigModule')) $result = 30;
}
return $result;
}
/** /**
* Alias of isConfigurable() for backwards compatibility * Alias of isConfigurable() for backwards compatibility
@@ -3865,7 +3895,7 @@ class Modules extends WireArray {
*/ */
protected function setModuleConfigData(Module $module, $data = null, $extraData = null) { protected function setModuleConfigData(Module $module, $data = null, $extraData = null) {
$configurable = $this->isConfigurable($module); $configurable = $this->isConfigable($module);
if(!$configurable) return false; if(!$configurable) return false;
if(!is_array($data)) $data = $this->getConfig($module); if(!is_array($data)) $data = $this->getConfig($module);
if($extraData !== null && is_array($extraData)) $data = array_merge($data, $extraData); if($extraData !== null && is_array($extraData)) $data = array_merge($data, $extraData);