2020-05-27 18:46:16 +02:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-04-21 12:53:47 +00:00
|
|
|
use Config\Services;
|
2022-10-14 15:29:01 +00:00
|
|
|
use Config\View;
|
2021-09-02 16:34:25 +00:00
|
|
|
use ViewThemes\Theme;
|
|
|
|
|
2020-05-27 18:46:16 +02:00
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* The goal of this file is to allow developers a location where they can overwrite core procedural functions and
|
2022-10-14 14:56:30 +00:00
|
|
|
* replace them with their own. This file is loaded during the bootstrap process and is called during the framework's
|
2020-05-27 18:46:16 +02:00
|
|
|
* execution.
|
|
|
|
*
|
2021-05-19 16:35:13 +00:00
|
|
|
* This can be looked at as a `master helper` file that is loaded early on, and may also contain additional functions
|
2020-05-27 18:46:16 +02:00
|
|
|
* that you'd like to use throughout your entire application
|
|
|
|
*
|
|
|
|
* @link: https://codeigniter4.github.io/CodeIgniter4/
|
|
|
|
*/
|
2021-09-02 16:34:25 +00:00
|
|
|
|
|
|
|
if (! function_exists('view')) {
|
|
|
|
/**
|
|
|
|
* Grabs the current RendererInterface-compatible class and tells it to render the specified view. Simply provides a
|
|
|
|
* convenience method that can be used in Controllers, libraries, and routed closures.
|
|
|
|
*
|
|
|
|
* NOTE: Does not provide any escaping of the data, so that must all be handled manually by the developer.
|
|
|
|
*
|
|
|
|
* @param array<string, mixed> $data
|
|
|
|
* @param array<string, mixed> $options Unused - reserved for third-party extensions.
|
|
|
|
*/
|
|
|
|
function view(string $name, array $data = [], array $options = []): string
|
|
|
|
{
|
2022-10-15 11:22:08 +00:00
|
|
|
if (array_key_exists('theme', $options)) {
|
|
|
|
Theme::setTheme($options['theme']);
|
|
|
|
}
|
|
|
|
|
2021-09-02 16:34:25 +00:00
|
|
|
$path = Theme::path();
|
|
|
|
|
|
|
|
/** @var CodeIgniter\View\View $renderer */
|
|
|
|
$renderer = single_service('renderer', $path);
|
|
|
|
|
|
|
|
$saveData = config(View::class)->saveData;
|
|
|
|
|
|
|
|
if (array_key_exists('saveData', $options)) {
|
|
|
|
$saveData = (bool) $options['saveData'];
|
|
|
|
unset($options['saveData']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $renderer->setData($data, 'raw')
|
|
|
|
->render($name, $options, $saveData);
|
|
|
|
}
|
|
|
|
}
|
2022-04-21 12:53:47 +00:00
|
|
|
|
|
|
|
if (! function_exists('lang')) {
|
|
|
|
/**
|
|
|
|
* A convenience method to translate a string or array of them and format the result with the intl extension's
|
|
|
|
* MessageFormatter.
|
|
|
|
*
|
|
|
|
* Overwritten to include an escape parameter (escaped by default).
|
|
|
|
*
|
|
|
|
* @param array<int|string, string> $args
|
|
|
|
*
|
2022-10-15 11:22:08 +00:00
|
|
|
* TODO: remove, and escape args when necessary
|
|
|
|
*
|
2022-04-21 12:53:47 +00:00
|
|
|
* @return string|string[]
|
|
|
|
*/
|
|
|
|
function lang(string $line, array $args = [], ?string $locale = null, bool $escape = true): string | array
|
|
|
|
{
|
|
|
|
$language = Services::language();
|
|
|
|
|
|
|
|
// Get active locale
|
|
|
|
$activeLocale = $language->getLocale();
|
|
|
|
|
|
|
|
if ($locale && $locale !== $activeLocale) {
|
|
|
|
$language->setLocale($locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
$line = $language->getLine($line, $args);
|
|
|
|
if (! $locale) {
|
|
|
|
return $escape ? esc($line) : $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($locale === $activeLocale) {
|
|
|
|
return $escape ? esc($line) : $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset to active locale
|
|
|
|
$language->setLocale($activeLocale);
|
|
|
|
return $escape ? esc($line) : $line;
|
|
|
|
}
|
|
|
|
}
|