Fr3nch13/CakePHP Utilities

JsonConfig
in package
implements ConfigEngineInterface uses FileConfigTrait

JSON engine allows Configure to load configuration values from files containing JSON strings.

An example JSON file would look like::

{
    "debug": false,
    "App": {
        "namespace": "MyApp"
    },
    "Security": {
        "salt": "its-secret"
    }
}

Table of Contents

Interfaces

ConfigEngineInterface
An interface for creating objects compatible with Configure::load()

Properties

$_extension  : string
File extension.
$_path  : string
The path this engine finds files on.

Methods

__construct()  : mixed
Constructor for JSON Config file reading.
dump()  : bool
Converts the provided $data into a JSON string that can be used saved into a file and loaded later.
read()  : array<string|int, mixed>
Read a config file and return its contents.
_getFilePath()  : string
Get file path

Properties

$_extension

File extension.

protected string $_extension = '.json'

$_path

The path this engine finds files on.

protected string $_path = ''

Methods

__construct()

Constructor for JSON Config file reading.

public __construct([string|null $path = null ]) : mixed
Parameters
$path : string|null = null

The path to read config files from. Defaults to CONFIG.

dump()

Converts the provided $data into a JSON string that can be used saved into a file and loaded later.

public dump(string $key, array<string|int, mixed> $data) : bool
Parameters
$key : string

The identifier to write to. If the key has a . it will be treated as a plugin prefix.

$data : array<string|int, mixed>

Data to dump.

Return values
bool

Success

read()

Read a config file and return its contents.

public read(string $key) : array<string|int, mixed>

Files with . in the name will be treated as values in plugins. Instead of reading from the initialized path, plugin keys will be located using Plugin::path().

Parameters
$key : string

The identifier to read from. If the key has a . it will be treated as a plugin prefix.

Tags
throws
CakeException

When files don't exist or when files contain '..' (as this could lead to abusive reads) or when there is an error parsing the JSON string.

Return values
array<string|int, mixed>

Parsed configuration values.

_getFilePath()

Get file path

protected _getFilePath(string $key[, bool $checkExists = false ]) : string
Parameters
$key : string

The identifier to write to. If the key has a . it will be treated as a plugin prefix.

$checkExists : bool = false

Whether to check if file exists. Defaults to false.

Tags
throws
CakeException

When files don't exist or when files contain '..' as this could lead to abusive reads.

Return values
string

Full file path


        
On this page

Search results