mirror of
https://github.com/processwire/processwire.git
synced 2025-08-07 15:26:54 +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:
@@ -1,42 +1,58 @@
|
||||
<?php namespace ProcessWire;
|
||||
|
||||
/**
|
||||
* ProcessWire ConfigurableModule Interface
|
||||
* ProcessWire ConfigurableModule and ConfigModule Interfaces
|
||||
*
|
||||
* Provides the base interfaces required by modules.
|
||||
*
|
||||
* This file is licensed under the MIT license
|
||||
* 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
|
||||
*
|
||||
*
|
||||
* About the ConfigurableModule interface
|
||||
* ======================================
|
||||
* ConfigurableModule is an interface that indicates the module is configurable by providing
|
||||
* __get() and __set() methods for getting and setting config values. Modules implementing
|
||||
* this interface are assumed to also implement the 'Module' interface.
|
||||
* `__get()` and `__set()` methods for getting and setting config values. Modules implementing
|
||||
* this interface are assumed to also implement the `Module` interface.
|
||||
*
|
||||
* The module must also provide one (1) of the following:
|
||||
*
|
||||
* 1. A getModuleConfigInputfields([$data]) method (static or non-static); 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.
|
||||
* 1. A `getModuleConfigInputfields([$data])` method (static or non-static); 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.
|
||||
*
|
||||
* For more details about the above options, see the commented methods within
|
||||
* the interface.
|
||||
*
|
||||
* When you use this as an interface, you MUST also use 'Module' as an interface,
|
||||
* i.e. "class Something implements Module, ConfigurableModule"
|
||||
* When you use this as an interface, you MUST also use `Module` as an interface,
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* 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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
@@ -288,6 +288,7 @@ class Modules extends WireArray {
|
||||
'permissions',
|
||||
'searchable',
|
||||
'page',
|
||||
// 'languages',
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -2929,6 +2930,8 @@ class Modules extends WireArray {
|
||||
'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
|
||||
'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:
|
||||
// 'nav' => array(), // navigation definition: see Process.php
|
||||
@@ -3446,10 +3449,9 @@ class Modules extends WireArray {
|
||||
if(is_array($this->configData[$id])) {
|
||||
$data = $this->configData[$id];
|
||||
} else {
|
||||
// first verify that module doesn't have a config file
|
||||
$configurable = $this->isConfigurable($className);
|
||||
if(!$configurable) return $emptyReturn;
|
||||
$database = $this->wire('database');
|
||||
$configable = $this->isConfigable($className);
|
||||
if(!$configable) return $emptyReturn;
|
||||
$database = $this->wire()->database;
|
||||
$query = $database->prepare("SELECT data FROM modules WHERE id=:id", "modules.getConfig($className)"); // QA
|
||||
$query->bindValue(":id", (int) $id, \PDO::PARAM_INT);
|
||||
$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
|
||||
* how it is configurable.
|
||||
@@ -3837,6 +3839,34 @@ class Modules extends WireArray {
|
||||
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
|
||||
*
|
||||
@@ -3865,7 +3895,7 @@ class Modules extends WireArray {
|
||||
*/
|
||||
protected function setModuleConfigData(Module $module, $data = null, $extraData = null) {
|
||||
|
||||
$configurable = $this->isConfigurable($module);
|
||||
$configurable = $this->isConfigable($module);
|
||||
if(!$configurable) return false;
|
||||
if(!is_array($data)) $data = $this->getConfig($module);
|
||||
if($extraData !== null && is_array($extraData)) $data = array_merge($data, $extraData);
|
||||
|
Reference in New Issue
Block a user