Oihana PHP System

init

Table of Contents

Functions

initConfig()  : array<string|int, mixed>
Load a toml configuration and initialize it.
initContainer()  : Container
Initialize and build a PHP-DI container for the application.
initDefaultTimezone()  : void
Initialize the application's default timezone.
initDefinitions()  : array<string|int, mixed>
Initialize all DI container definitions by loading and merging PHP definition files.
initErrors()  : void
Initialize the global PHP error settings.
initMemoryLimit()  : bool
Initialize the default memory limit of the PHP application.
setIniIfExists()  : bool
Set a PHP ini directive from a scalar or from a config array if the value exists and is not empty.

Functions

initConfig()

Load a toml configuration and initialize it.

initConfig([string $basePath = Char::EMPTY ][, string $file = 'config.toml' ][, callable(array<string|int, mixed>): array<string|int, mixed>|null $init = null ]) : array<string|int, mixed>

Delegates resolution to oihana\files\toml\resolveTomlConfig while preserving the previous behavior of returning an empty config when the file does not exist.

Parameters
$basePath : string = Char::EMPTY

Base path of config

$file : string = 'config.toml'

Config file name (with or without .toml)

$init : callable(array<string|int, mixed>): array<string|int, mixed>|null = null

Initialize the application with the config definition.

Tags
throws
TomlError
Return values
array<string|int, mixed>

The config array definition.

initContainer()

Initialize and build a PHP-DI container for the application.

initContainer(string|array<string|int, mixed>|DefinitionSource ...$definitions) : Container

This function creates a ContainerBuilder, adds one or more definition sources, and returns the built Container. Definitions can be provided as:

  • string: path to a PHP definition file (returning an array of definitions).
  • array: an associative array of definitions.
  • DefinitionSource: any PHP-DI compatible definition source.

Notes:

  • Later definition sources can override entries defined earlier.
  • To load multiple files from a directory, see initDefinitions().
Parameters
$definitions : string|array<string|int, mixed>|DefinitionSource

One or more definition sources (file path(s), array(s), or DefinitionSource instances).

Tags
throws
Exception

If the container build process fails.

see
initDefinitions()
see
ContainerBuilder
link

PHP-DI definitions documentation

example
use DI\Container;
use function oihana\init\{initContainer, initDefinitions};

// From a definitions directory (merges all PHP files returning arrays)
$definitions = initDefinitions(__DIR__ . '/../../definitions');

// Add additional inline definitions that can override previous ones
$inline =
[
    'config.timezone' => 'UTC',
];

// Build the container with multiple sources
// @var Container $container
$container = initContainer($definitions, $inline, __DIR__ . '/extra-definitions.php');

// Retrieve a service
// $logger = $container->get(App\Logger::class);
Return values
Container

The built dependency injection container.

initDefaultTimezone()

Initialize the application's default timezone.

initDefaultTimezone(string|null $timezoneId[, string $defaultTimezone = 'Europe/Paris' ]) : void

This function sets the global timezone used by all date/time functions. If $timezoneId is null, $defaultTimezone will be applied.

Notes:

  • If an invalid identifier is provided, PHP will return false and emit a warning.
  • Prefer passing null rather than an empty string to trigger the default timezone.
Parameters
$timezoneId : string|null

Timezone identifier to apply. Use null to apply $defaultTimezone.

$defaultTimezone : string = 'Europe/Paris'

Timezone to use when $timezoneId is null (default 'Europe/Paris').

Tags
see
date_default_timezone_set()
link

List of available timezones

example
use function oihana\init\initDefaultTimezone;

// Use explicit timezone
initDefaultTimezone('UTC');

// Fallback to provided default when first argument is null
initDefaultTimezone(null, 'Europe/Paris');

// Typically, you might pick from configuration
$config = ['app' => ['timezone' => 'Europe/Lisbon']];
initDefaultTimezone($config['app']['timezone'] ?? null, 'UTC');

initDefinitions()

Initialize all DI container definitions by loading and merging PHP definition files.

initDefinitions(string $basePath) : array<string|int, mixed>

This function scans $basePath recursively for .php files, requires each file, and merges the resulting arrays into a single definitions array. It is intended to assemble service definitions for a DI container.

Behavior:

  • Only files with the "php" extension are considered.
  • Files are discovered recursively in subdirectories.
  • Each file must return an array; non-array returns may cause merge issues or exceptions in requireAndMergeArrays.
Parameters
$basePath : string

Absolute or relative path to the root directory containing definition files.

Tags
throws
Exception

If a required file cannot be read/included or merging fails.

see
recursiveFilePaths()
see
requireAndMergeArrays()
see
initContainer()
example
use DI\ContainerBuilder;
use function oihana\init\initDefinitions;

// Load all container definitions from the 'definitions' directory
$definitions = initDefinitions(__DIR__ . '/../../definitions');

// Example: pass the definitions to your container builder
$containerBuilder = new ContainerBuilder() ;

$containerBuilder->addDefinitions( $definitions ) ;

$containerBuilder->build() ;
Return values
array<string|int, mixed>

Merged definitions array.

initErrors()

Initialize the global PHP error settings.

initErrors([array<string|int, mixed>|null $init = null ][, string|null $logRootPath = null ][, int $defaultErrorLevel = E_ALL ]) : void

This function sets PHP error reporting and ini directives related to error display and logging, based on the provided configuration array and optional log root path.

It applies the following settings:

  • error_reporting level (defaulting to $defaultErrorLevel if not provided)
  • display_errors (ini directive)
  • display_startup_errors (ini directive)
  • error_log (ini directive), with an optional root path prefix
Parameters
$init : array<string|int, mixed>|null = null

Optional associative array of ini settings.

$logRootPath : string|null = null

Optional root directory path to prepend to error_log path if it is relative.

$defaultErrorLevel : int = E_ALL

Default error reporting level to use if none is set in $init.

Tags
throws
ConstantException
see
IniOptions

initMemoryLimit()

Initialize the default memory limit of the PHP application.

initMemoryLimit(string|null $memoryLimit[, string $defaultMemoryLimit = "128M" ]) : bool

This function sets the PHP memory_limit ini directive to the given value if provided, otherwise it uses the provided default memory limit.

Parameters
$memoryLimit : string|null

The memory limit value to set (e.g. '256M', '1G', or '-1' for unlimited), or null to use default.

$defaultMemoryLimit : string = "128M"

The default memory limit to use if $memoryLimit is null. Default is '128M'.

Tags
throws
ConstantException
example
// Sets memory_limit to 256M
initMemoryLimit('256M');

// Uses the default value (128M)
initMemoryLimit(null);
Return values
bool

True if the ini setting was successfully set, false otherwise.

setIniIfExists()

Set a PHP ini directive from a scalar or from a config array if the value exists and is not empty.

setIniIfExists(string $key[, array<string|int, mixed>|string|int|float|bool|null $init = [] ]) : bool

Behavior:

  • If $init is an array, this function looks up $init[$key]; otherwise it treats $init as the value.
  • The value is cast to string and trimmed; empty strings are ignored.
  • ini_set() is invoked only if the function exists and a non-empty value is provided.
  • Returns true when ini_set() is called, false otherwise.

Notes:

  • Most ini values are strings; booleans are commonly expressed as "1"/"0". Passing true/false will be cast to "1"/"" by PHP.
  • Use ini_get($key) to read back the effective value after setting.
Parameters
$key : string

The ini key (e.g. 'display_errors', 'memory_limit').

$init : array<string|int, mixed>|string|int|float|bool|null = []

A config array (looked up by $key) or a direct scalar value.

Tags
throws
ConstantException
see
ini_get()
see
ini_set()
link

PHP manual: ini_set

example
use function oihana\init\setIniIfExists;

// From a configuration array
$config =
[
    'display_errors' => '1',
    'memory_limit'   => '256M',
];
setIniIfExists('display_errors', $config); // calls ini_set('display_errors', '1')
setIniIfExists('upload_max_filesize', $config); // returns false (not present)

// From a direct scalar
setIniIfExists('memory_limit', '512M'); // calls ini_set('memory_limit', '512M')
setIniIfExists('display_errors', "\t\n"); // returns false (empty after trim)
Return values
bool

True if ini_set() was called, false otherwise.


        
On this page

Search results