\WPConfigTransformer

Transforms a wp-config.php file.

Summary

Methods
Properties
Constants
__construct()
exists()
get_value()
add()
update()
remove()
No public properties found
ANCHOR_EOF
format_value()
normalize()
parse_wp_config()
save()
$wp_config_path
$wp_config_src
$wp_configs
N/A
No private methods found
No private properties found
N/A

Constants

ANCHOR_EOF

ANCHOR_EOF = 'EOF'

Append to end of file

Properties

$wp_config_path

$wp_config_path : string

Path to the wp-config.php file.

Type

normal

$wp_config_src

$wp_config_src : string

Original source of the wp-config.php file.

Type

normal

$wp_configs

$wp_configs

Array of parsed configs.

Methods

__construct()

__construct(string  $wp_config_path, mixed  $read_only = false) : mixed

Instantiates the class with a valid wp-config.php.

Parameters

string $wp_config_path

Path to a wp-config.php file.

mixed $read_only

Throws

\Exception

If the wp-config.php file is missing.

\Exception

If the wp-config.php file is not writable.

Returns

mixed

exists()

exists(string  $type, string  $name) : bool

Checks if a config exists in the wp-config.php file.

Parameters

string $type

Config type (constant or variable).

string $name

Config name.

Throws

\Exception

If the wp-config.php file is empty.

\Exception

If the requested config type is invalid.

Returns

bool

get_value()

get_value(string  $type, string  $name) : string|null

Get the value of a config in the wp-config.php file.

Parameters

string $type

Config type (constant or variable).

string $name

Config name.

Throws

\Exception

If the wp-config.php file is empty.

\Exception

If the requested config type is invalid.

Returns

string|null

add()

add(string  $type, string  $name, string  $value, array<string,mixed>  $options = array()) : bool

Adds a config to the wp-config.php file.

Parameters

string $type

Config type (constant or variable).

string $name

Config name.

string $value

Config value.

array $options

(optional) Array of special behavior options.

Throws

\Exception

If the config value provided is not a string.

\Exception

If the config placement anchor could not be located.

Returns

bool

update()

update(string  $type, string  $name, string  $value, array<string,mixed>  $options = array()) : bool

Updates an existing config in the wp-config.php file.

Parameters

string $type

Config type (constant or variable).

string $name

Config name.

string $value

Config value.

array $options

(optional) Array of special behavior options.

Throws

\Exception

If the config value provided is not a string.

Returns

bool

remove()

remove(string  $type, string  $name) : bool

Removes a config from the wp-config.php file.

Parameters

string $type

Config type (constant or variable).

string $name

Config name.

Returns

bool

format_value()

format_value(string  $value, bool  $raw) : bool|float|int|string|null

Applies formatting to a config value.

Parameters

string $value

Config value.

bool $raw

Display value in raw format without quotes.

Throws

\Exception

When a raw value is requested for an empty string.

Returns

bool|float|int|string|null

normalize()

normalize(string  $type, string  $name, bool|float|int|string|null  $value) : string

Normalizes the source output for a name/value pair.

Parameters

string $type

Config type (constant or variable).

string $name

Config name.

bool|float|int|string|null $value

Config value.

Throws

\Exception

If the requested config type does not support normalization.

Returns

string

parse_wp_config()

parse_wp_config(string  $src) : mixed

Parses the source of a wp-config.php file.

Parameters

string $src

Config file source.

Returns

mixed

save()

save(string  $contents) : bool

Saves new contents to the wp-config.php file.

Parameters

string $contents

New config contents.

Throws

\Exception

If the config file content provided is empty.

\Exception

If there is a failure when saving the wp-config.php file.

Returns

bool