MilkCore

Get
in package

Facade Class for Core System Functionality

This class serves as a facade for the core system, providing static methods to facilitate access and management of key functionalities such as database connections, email sending, module and controller loading, and theme page and module management.

Tags
example
// Database connection
$db = Get::db();
$results = $db->query("SELECT * FROM users");

// Send an email
Get::mail()
    ->to('recipient@example.com')
    ->from('sender@example.com')
    ->subject('Hello')
    ->message('This is a test email')
    ->send();

// Load modules and theme functions
Get::load_modules();

// Load a theme page
Get::theme_page('home', 'Main content', ['title' => 'Home Page']);

// Load a theme module
Get::theme_plugin('sidebar', ['active' => 'home']);

Table of Contents

Properties

$db  : null|MySql|SQLite
Reference to the primary database connection instance
$db2  : null|MySql|SQLite
Reference to the secondary database connection instance
$mail_class  : null|Mail
Reference to the mail handling class instance
$math_parser_loaded  : bool
Indicates whether the MathParser class has been loaded

Methods

bind()  : mixed
Bind a service to the dependency container
client_ip()  : string
Gets the client IP address
date_time_zone()  : DateTime
Returns the current date and time based on configured timezone
db()  : MySql|SQLite
Creates or returns an instance of the primary database connection
db2()  : MySql|SQLite
Creates or returns an instance of the secondary database connection
dir_path()  : string
Returns the secure path of a file
format_date()  : string
Format a date based on the system settings
has()  : bool
Checks if a service is registered in the container
load_modules()  : void
Loads all module controllers and theme functions
mail()  : Mail
Creates or returns an instance of the Mail class for sending emails
make()  : mixed
Creates an instance of a registered service
parser()  : MathParser|null
Returns an instance of the advanced mathematical parser
response_json()  : void
Responds with JSON data and terminates the application
schema()  : mixed
temp_dir()  : string
Returns the path of the temporary directory
theme_page()  : void
Loads a theme page with the specified content and variables
theme_plugin()  : string
Loads a theme plugin with the necessary variables
uri_path()  : string
Returns the URI path for resources like JS or CSS files

Properties

$db

Reference to the primary database connection instance

public static null|MySql|SQLite $db = null

Stores the singleton instance of the database class (MySql or SQLite) for database operations.

$db2

Reference to the secondary database connection instance

public static null|MySql|SQLite $db2 = null

Stores the singleton instance of the secondary database connection (MySql or SQLite) for data operations.

$mail_class

Reference to the mail handling class instance

public static null|Mail $mail_class = null

Stores the singleton instance of the Mail class for sending emails.

$math_parser_loaded

Indicates whether the MathParser class has been loaded

public static bool $math_parser_loaded = false

Flag to track if the mathematical parser library has been initialized.

Methods

bind()

Bind a service to the dependency container

public static bind(string $service_name, mixed $implementation[, bool $singleton = false ][, array<string|int, mixed> $arguments = [] ]) : mixed

Example:

// Bind a regular class
Get::bind('database', DatabaseConnection::class);

// Bind a singleton service
Get::bind('config', AppConfig::class, true);

// Bind a factory function with initialization arguments
Get::bind('logger', function($filename) {
    return new FileLogger($filename);
}, false, ['app.log']);
Parameters
$service_name : string

Nome del servizio

$implementation : mixed

Classe o funzione da bindare

$singleton : bool = false

Se true, il servizio sarà istanziato una sola volta

$arguments : array<string|int, mixed> = []

Argomenti per l'inizializzazione se singleton

client_ip()

Gets the client IP address

public static client_ip([bool $trust_proxy_headers = false ]) : string

This method detects and returns the client's IP address, handling various scenarios including proxy headers and IPv6 to IPv4 conversion for localhost.

Parameters
$trust_proxy_headers : bool = false

Whether to trust proxy headers like X-Forwarded-For

Return values
string

The client's IP address or 'CLI' if running in command line

date_time_zone()

Returns the current date and time based on configured timezone

public static date_time_zone() : DateTime

Creates a DateTime object using the timezone settings from the configuration file.

Tags
example
$now = Get::date_time_zone();
echo $now->format('Y-m-d H:i:s');
Return values
DateTime

DateTime object with the configured timezone

db()

Creates or returns an instance of the primary database connection

public static db() : MySql|SQLite

This method implements the singleton pattern for database connections. It creates a new connection if one doesn't exist, or returns the existing one. The database type (MySQL or SQLite) is determined by the 'db_type' configuration.

Tags
example
// Get database connection and run a query
$db = Get::db();
$results = $db->query("SELECT * FROM users WHERE active = 1");
Return values
MySql|SQLite

Instance of the database connection

db2()

Creates or returns an instance of the secondary database connection

public static db2() : MySql|SQLite

The system can use two databases: one for configuration and one for data. This method handles the connection to the secondary database (for data). Currently, the secondary database is always MySQL regardless of the primary db_type.

Tags
example
// Get secondary database connection and run a query
$db2 = Get::db2();
$results = $db2->query("SELECT * FROM data_table WHERE status = 'active'");
Return values
MySql|SQLite

Instance of the secondary database connection

dir_path()

Returns the secure path of a file

public static dir_path(string $file) : string

This method protects against path traversal attacks by ensuring paths remain within the site directory. It also checks for customizations files with the same name in the customizations directory.

Parameters
$file : string

Absolute path including MILK_DIR or THEME_DIR of the file to check

Tags
example
// Get secure path to a theme file
$require Get::dir_path(THEME_DIR.'/template_parts/sidebar.php');
Return values
string

Path of the file to load

format_date()

Format a date based on the system settings

public static format_date(string|null $date[, string $format = 'date' ]) : string

Converts a date string to the specified format according to system configuration.

Parameters
$date : string|null

The date to format (in MySQL format)

$format : string = 'date'

The format to use: 'date' (only date), 'time' (only time), or 'datetime' (both)

Tags
example
$formatted_date = Get::format_date('2021-01-01', 'date');
Return values
string

The formatted date

has()

Checks if a service is registered in the container

public static has(string $name) : bool

Example:

if (Get::has('database')) {
    $db = Get::make('database', ['localhost', 'user', 'pass']);
} else {
    // Handle missing service
}
Parameters
$name : string

Name of the service to check

Return values
bool

True if the service is registered, false otherwise

load_modules()

Loads all module controllers and theme functions

public static load_modules() : void

This method initializes all the module controllers and theme functions required for the application to work properly.

Tags
example
Get::load_modules();

mail()

Creates or returns an instance of the Mail class for sending emails

public static mail() : Mail

This method implements the singleton pattern for the mail service. It creates a new instance if one doesn't exist, or returns the existing one.

Tags
example
// Get mail instance and send an email
Get::mail()
    ->to('recipient@example.com')
    ->from('sender@example.com')
    ->subject('Hello')
    ->message('This is a test email')
    ->send();
Return values
Mail

Instance of the mail service

make()

Creates an instance of a registered service

public static make(string $name[, array<string|int, mixed> $arguments = [] ]) : mixed

This method instantiates and returns a service from the dependency container. Services must be registered first using the bind() method before they can be instantiated.

Example:

// Register services
Get::bind('database', DatabaseConnection::class);
Get::bind('config', AppConfig::class, true);
Get::bind('logger', LoggerFactory::class);

// Create service instances
$db = Get::make('database', ['localhost', 'user', 'pass']);
$config = Get::make('config', []);
$logger = Get::make('logger', ['app.log']);
Parameters
$name : string

Name of the service to instantiate

$arguments : array<string|int, mixed> = []

Arguments to pass to the service constructor/factory

Return values
mixed

The requested service instance or null if not found

parser()

Returns an instance of the advanced mathematical parser

public static parser([mixed $data = null ]) : MathParser|null

Creates and returns a configured instance of the mathematical expression parser. This function is experimental and has only been unit tested.

Parameters
$data : mixed = null

Optional data to be used by the parser

Tags
example
$parser = Get::parser($data);
$result = $parser->evaluate('x + y', ['x' => 5, 'y' => 3]);
experimental

This was not used in production due to performance issues with large datasets

Return values
MathParser|null

The configured parser instance

response_json()

Responds with JSON data and terminates the application

public static response_json(array<string|int, mixed> $data) : void

This method sends a JSON response to the client and ends the application execution.

Parameters
$data : array<string|int, mixed>

Data to be converted to JSON and sent as response

Tags
example
Get::response_json(['status' => 'success', 'data' => $result]);
Return values
void

This function terminates execution

schema()

public static schema(mixed $table[, mixed $db = null ]) : mixed
Parameters
$table : mixed
$db : mixed = null

temp_dir()

Returns the path of the temporary directory

public static temp_dir() : string

Provides the path to the system's temporary directory with a trailing slash.

Tags
example
$path = Get::temp_dir().$file;
Return values
string

The path of the temporary directory with trailing slash

theme_page()

Loads a theme page with the specified content and variables

public static theme_page(string $page[, string|null $content = null ][, array<string|int, mixed> $variables = [] ]) : void

This method loads the requested theme page and passes the required variables to it.

Parameters
$page : string

Name of the page to load

$content : string|null = null

Path to the content file or string content

$variables : array<string|int, mixed> = []

Variables to pass to the page

Tags
example
Get::theme_page('home', 'Main content', ['title' => 'Home Page']);

theme_plugin()

Loads a theme plugin with the necessary variables

public static theme_plugin(string $module[, array<string|int, mixed> $variables = [] ]) : string

This method loads the requested theme plugin and passes the required variables to it.

Parameters
$module : string

Name of the module to load

$variables : array<string|int, mixed> = []

Variables to pass to the module

Tags
example
Get::theme_plugin('sidebar', ['active' => 'home']);
Return values
string

Content of the loaded module

uri_path()

Returns the URI path for resources like JS or CSS files

public static uri_path(string $file) : string

This method generates the correct URI path for web resources, handling theme customizationss and proper URL formatting.

Parameters
$file : string

The file to load

Tags
example
<img src="<?php echo Get::uri_path($path); ?>" alt="Logo">
Return values
string

The absolute URI path of the file


        
On this page

Search results