Issues (2882)

src/includes/functions.php (1 issue)

Severity
1
<?php
2
3
use SilverStripe\Core\Config\Config;
4
use SilverStripe\Core\Injector\Injector;
5
use SilverStripe\i18n\i18n;
6
use SilverStripe\Core\Manifest\ModuleManifest;
7
8
///////////////////////////////////////////////////////////////////////////////
9
// HELPER FUNCTIONS
10
11
/**
12
 * Creates a class instance by the "singleton" design pattern.
13
 * It will always return the same instance for this class,
14
 * which can be used for performance reasons and as a simple
15
 * way to access instance methods which don't rely on instance
16
 * data (e.g. the custom SilverStripe static handling).
17
 *
18
 * @param string $className
19
 * @return mixed
20
 */
21
function singleton($className)
22
{
23
    if ($className === Config::class) {
24
        throw new InvalidArgumentException("Don't pass Config to singleton()");
25
    }
26
    if (!isset($className)) {
27
        throw new InvalidArgumentException("singleton() Called without a class");
28
    }
29
    if (!is_string($className)) {
0 ignored issues
show
The condition is_string($className) is always true.
Loading history...
30
        throw new InvalidArgumentException(
31
            "singleton() passed bad class_name: " . var_export($className, true)
32
        );
33
    }
34
    return Injector::inst()->get($className);
35
}
36
37
function project()
38
{
39
    return ModuleManifest::config()->get('project');
40
}
41
42
/**
43
 * This is the main translator function. Returns the string defined by $entity according to the
44
 * currently set locale.
45
 *
46
 * Also supports pluralisation of strings. Pass in a `count` argument, as well as a
47
 * default value with `|` pipe-delimited options for each plural form.
48
 *
49
 * @param string $entity Entity that identifies the string. It must be in the form
50
 * "Namespace.Entity" where Namespace will be usually the class name where this
51
 * string is used and Entity identifies the string inside the namespace.
52
 * @param mixed $arg,... Additional arguments are parsed as such:
53
 *  - Next string argument is a default. Pass in a `|` pipe-delimeted value with `{count}`
54
 *    to do pluralisation.
55
 *  - Any other string argument after default is context for i18nTextCollector
56
 *  - Any array argument in any order is an injection parameter list. Pass in a `count`
57
 *    injection parameter to pluralise.
58
 * @return string
59
 */
60
function _t($entity, $arg = null)
61
{
62
    // Pass args directly to handle deprecation
63
    return call_user_func_array([i18n::class, '_t'], func_get_args());
64
}
65