ConfigDb
extends BaseModel
in package
uses
CacheFileTrait
Config DB model.
Tags
Table of Contents
Properties
- $FileSystem : FileSystem
- $storageData : mixed
- $storageFile : string
- $storagePath : string
- $configPrefix : string
- $storageFileTemplate : string
Methods
- __construct() : mixed
- {@inheritDoc}
- add() : mixed
- Add a config data.
- buildCacheContent() : string
- Get DB result and build cache content.
- get() : array<string|int, mixed>|string
- Get config value(s).
- resetCacheProperties() : mixed
- Reset cache properties.
- resetGetData() : mixed
- Reset some properties that was used while get the data using `loadCacheData()`.
- update() : bool
- Update configuration DB.
- updateMultipleValues() : bool
- Update multiple values.
- updateValue() : bool
- Update configuration value only.
- beginCacheFileTrait() : mixed
- Trait initialize method.
- buildCacheContentFromResult() : string
- Buld cache content from result that got from `PDOStatement::fetchAll()`.
- deleteCachedFile() : mixed
- Delete the file that were built and cached.
- getConfigPrefix() : string
- Get config prefix from config name.
- getMultiple() : array<string|int, mixed>
- Get config values.
- getRow() : mixed
- Get config data as object (DB row) by name condition.
- loadCacheData() : mixed
- Load the cache file data into class property. (`storageData`).
- bindValuesForUpdateMultipleVals() : mixed
- Call to `bindValue()` from `$Sth` argument.
- buildCacheFile() : bool
- Build the cache file.
- buildPlaceholdersAndBindValuesDescForUpdateMultipleVals() : mixed
- Build/alter the variables for description column for use in `updateMultipleValues()` method.
- buildPlaceholdersAndBindValuesForUpdateMultipleVals() : mixed
- Build/alter the variables for use in `updateMultipleValues()` method.
- getCacheFilePath() : string
- Get full path to cache file.
- isNeedRebuildCache() : bool
- Check if it is needed to rebuild the cache file.
Properties
$FileSystem
protected
FileSystem
$FileSystem
$storageData
protected
mixed
$storageData
The data that was generated from DB. This property is depend on how you set $content
argument in buildCacheFile()
method.
$storageFile
protected
string
$storageFile
File name with extension for cache. Example: 'tablename-db.php'
.
$storagePath
protected
string
$storagePath
Full path to storage folder and specific name without trailing slash.
Example STORAGE_PATH . '/cache/tablename'
for store data from selected table in DB.
Only set this if you want to use cache.
$configPrefix
private
string
$configPrefix
Config prefix which is module name.
$storageFileTemplate
private
string
$storageFileTemplate
= 'config-module-%MODULENAME%.php'
Config cache file name for replacement.
Methods
__construct()
{@inheritDoc}
public
__construct(Container $Container) : mixed
Parameters
- $Container : Container
add()
Add a config data.
public
add(array<string|int, mixed> $data) : mixed
Parameters
- $data : array<string|int, mixed>
-
The associative array where key is column name and value is its value.
Return values
mixed —Return inserted ID if successfully inserted, return 0
(zero), or false
if failed to insert.
buildCacheContent()
Get DB result and build cache content.
public
buildCacheContent() : string
Return values
string —Return generated data in php language that is ready to use as cache.
get()
Get config value(s).
public
get(array<string|int, mixed>|string $name[, array<string|int, mixed>|string $default = '' ]) : array<string|int, mixed>|string
Example for get single config name:
$ConfigDb->get('config_name', 'default_value');
Example for get multiple config name:
$names = ['config_name1', 'config_name2']; $defaults = ['default_value1', 'default_value2']; $ConfigDb->get($names, $defaults);
Parameters
- $name : array<string|int, mixed>|string
-
The config name can be string (name) or array (names).
- $default : array<string|int, mixed>|string = ''
-
The default config value if this configuration is not exists or have no value. This can be any types but if name is array then this will always be array.
Tags
Return values
array<string|int, mixed>|string —Return config value.
If name is array then it will always return the array with name in the key. If config was not found then it will return default value matched in the array index.
If name is string then it will data type as value contain in db. If config was not found then it will return $default
argument.
resetCacheProperties()
Reset cache properties.
public
resetCacheProperties() : mixed
This is useful when you call to any methods that have called to loadCacheData()
method on this trait.
This method will be reset storageData
, storageFile
, storagePath
properties.
Tags
resetGetData()
Reset some properties that was used while get the data using `loadCacheData()`.
public
resetGetData() : mixed
This is useful when you call to extended class that is get a cache data row using loadCacheData()
and you want to call it again or it is in the loop.
This method will be reset storageData
, storageFile
properties.
Tags
update()
Update configuration DB.
public
update(array<string|int, mixed> $data, array<string|int, mixed> $where) : bool
This will be add if the data is not exists.
Parameters
- $data : array<string|int, mixed>
-
The associative array where its key is column name and value is its value to update.
- $where : array<string|int, mixed>
-
The associative array where its key is column name and value is its value.
Return values
bool —Return true
on success update, false
for otherwise.
updateMultipleValues()
Update multiple values.
public
updateMultipleValues(array<string|int, mixed> $data[, array<string|int, mixed> $dataDesc = [] ]) : bool
Parameters
- $data : array<string|int, mixed>
-
Associative array where key is match
config_name
column and value is matchconfig_value
column. - $dataDesc : array<string|int, mixed> = []
-
Associative array of config description where array key is the
config_name
column and its value is matchconfig_description
column.
Tags
Return values
bool —Return true
if all data have been updated, false
for otherwise.
updateValue()
Update configuration value only.
public
updateValue(mixed $value, string $name) : bool
Parameters
- $value : mixed
-
The config value to update.
- $name : string
-
The config name to search for.
Return values
bool —Return true
on success update, false
for otherwise.
beginCacheFileTrait()
Trait initialize method.
protected
beginCacheFileTrait(Container $Container) : mixed
This method must be called before it can be working.
Parameters
- $Container : Container
-
The DI container class.
buildCacheContentFromResult()
Buld cache content from result that got from `PDOStatement::fetchAll()`.
protected
buildCacheContentFromResult(mixed $result[, string $formatMethod = 'raw' ]) : string
Parameters
- $result : mixed
-
The result from
PDOStatement::fetchAll()
. - $formatMethod : string = 'raw'
-
The format option. Value can be 'array', 'json_array', 'replace_setstate', 'serialize', 'raw'. Default is 'raw'.
Return values
string —Return generated content ready for cache.
deleteCachedFile()
Delete the file that were built and cached.
protected
deleteCachedFile() : mixed
This method was called from buildCacheFile()
.
Tags
getConfigPrefix()
Get config prefix from config name.
protected
getConfigPrefix(string $name) : string
Basically the config prefix is module name, get the prefix for use with build cache in smaller size.
This method also set configPrefix
property.
Parameters
- $name : string
-
Config name.
Return values
string —Return prefix of config.
getMultiple()
Get config values.
protected
getMultiple(array<string|int, mixed> $names[, array<string|int, mixed> $defaults = [] ]) : array<string|int, mixed>
This method was called from get()
method.
Parameters
- $names : array<string|int, mixed>
-
The config names.
- $defaults : array<string|int, mixed> = []
-
The default values that must be matched the array key of
$names
.
Tags
Return values
array<string|int, mixed> —Always return array with the name in key.
getRow()
Get config data as object (DB row) by name condition.
protected
getRow(string $name[, mixed $default = '' ]) : mixed
This method was called from get()
, getMultiple()
methods.
Parameters
- $name : string
-
The name of config to search.
- $default : mixed = ''
-
The default config value if this configuration is not exists.
Return values
mixed —Return object if found, return $default
if not found.
loadCacheData()
Load the cache file data into class property. (`storageData`).
protected
loadCacheData(callable $BuildContent) : mixed
Also build it if it is not exists or too old (expired).
You can access cache data via storageData
property.
Parameters
- $BuildContent : callable
-
The callback array to build content if needed. Example:
[$this, 'buildCacheContent']
. The callback must return content string of generated php data for write to cache.
bindValuesForUpdateMultipleVals()
Call to `bindValue()` from `$Sth` argument.
private
bindValuesForUpdateMultipleVals(PDOStatement $Sth, array<string|int, mixed> $bindValues) : mixed
This is for work with updateMultipleValues()
method.
Parameters
- $Sth : PDOStatement
- $bindValues : array<string|int, mixed>
-
The array of bind values where key is placeholder and value is its value.
Tags
buildCacheFile()
Build the cache file.
private
buildCacheFile(string $content) : bool
This method was called from getCacheFilePath()
.
Parameters
- $content : string
-
The file content to build cache. This content should be array or object from db in php data that is ready to access.
Return values
bool —Return true on success build cache file.
buildPlaceholdersAndBindValuesDescForUpdateMultipleVals()
Build/alter the variables for description column for use in `updateMultipleValues()` method.
private
buildPlaceholdersAndBindValuesDescForUpdateMultipleVals(array<string|int, mixed> $dataDesc, array<string|int, mixed> $configNamesToCheck, array<string|int, mixed> &$cfdPlaceholders, array<string|int, mixed> &$cfdBindValues) : mixed
Parameters
- $dataDesc : array<string|int, mixed>
-
The input data description.
- $configNamesToCheck : array<string|int, mixed>
-
The list of config names to check input name (config name) that must be matched. For example config for check with exists (will update), or not exists (will insert).
- $cfdPlaceholders : array<string|int, mixed>
-
The config description placeholders to be altered.
- $cfdBindValues : array<string|int, mixed>
-
The config description for use with bind values to be altered.
Tags
buildPlaceholdersAndBindValuesForUpdateMultipleVals()
Build/alter the variables for use in `updateMultipleValues()` method.
private
buildPlaceholdersAndBindValuesForUpdateMultipleVals(array<string|int, mixed> $data, array<string|int, mixed> $configNamesToCheck, array<string|int, mixed> &$configPrefixes, array<string|int, mixed> &$cfnPlaceholders, array<string|int, mixed> &$cfnBindValues, array<string|int, mixed> &$cfvPlaceholders, array<string|int, mixed> &$cfvBindValues) : mixed
Parameters
- $data : array<string|int, mixed>
-
The input data.
- $configNamesToCheck : array<string|int, mixed>
-
The list of config names to check input name (config name) that must be matched. For example config for check with exists (will update), or not exists (will insert).
- $configPrefixes : array<string|int, mixed>
-
The config prefixes to use later with clear cache. This value will be alter.
- $cfnPlaceholders : array<string|int, mixed>
-
The config name placeholders to be altered.
- $cfnBindValues : array<string|int, mixed>
-
The config name for use with bind values to be altered.
- $cfvPlaceholders : array<string|int, mixed>
-
The config value placeholders to be altered.
- $cfvBindValues : array<string|int, mixed>
-
The config value for use with bind values to be altered.
Tags
getCacheFilePath()
Get full path to cache file.
private
getCacheFilePath(callable $BuildContent) : string
This method was called from loadCacheData()
.
Parameters
- $BuildContent : callable
-
The callback array to build content if needed.
Return values
string —Return full path to cache file. Return empty string if cache file was not found and unable to build.
isNeedRebuildCache()
Check if it is needed to rebuild the cache file.
private
isNeedRebuildCache() : bool
This method was called from getCacheFilePath()
, MetaFieldsTrait->listObjectsFields()
.
Return values
bool —Return true
if it is need to build, rebuild the cache file, return false
if it is not.