Passed
Pull Request — 4 (#10359)
by
unknown
18:52
created

srv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
use SilverStripe\Core\Config\Config;
4
use SilverStripe\Core\Environment;
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\i18n\i18n;
7
use SilverStripe\Core\Manifest\ModuleManifest;
8
use SilverStripe\SiteConfig\SiteConfig;
0 ignored issues
show
Bug introduced by
The type SilverStripe\SiteConfig\SiteConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
///////////////////////////////////////////////////////////////////////////////
11
// HELPER FUNCTIONS
12
13
/**
14
 * Creates a class instance by the "singleton" design pattern.
15
 * It will always return the same instance for this class,
16
 * which can be used for performance reasons and as a simple
17
 * way to access instance methods which don't rely on instance
18
 * data (e.g. the custom SilverStripe static handling).
19
 *
20
 * @param string $className
21
 * @return mixed
22
 */
23
function singleton($className)
24
{
25
    if ($className === Config::class) {
26
        throw new InvalidArgumentException("Don't pass Config to singleton()");
27
    }
28
    if (!isset($className)) {
29
        throw new InvalidArgumentException("singleton() Called without a class");
30
    }
31
    if (!is_string($className)) {
0 ignored issues
show
introduced by
The condition is_string($className) is always true.
Loading history...
32
        throw new InvalidArgumentException(
33
            "singleton() passed bad class_name: " . var_export($className, true)
34
        );
35
    }
36
    return Injector::inst()->get($className);
37
}
38
39
function project()
40
{
41
    return ModuleManifest::config()->get('project');
42
}
43
44
/**
45
 * This is the main translator function. Returns the string defined by $entity according to the
46
 * currently set locale.
47
 *
48
 * Also supports pluralisation of strings. Pass in a `count` argument, as well as a
49
 * default value with `|` pipe-delimited options for each plural form.
50
 *
51
 * @param string $entity Entity that identifies the string. It must be in the form
52
 * "Namespace.Entity" where Namespace will be usually the class name where this
53
 * string is used and Entity identifies the string inside the namespace.
54
 * @param mixed $arg Additional arguments are parsed as such:
55
 *  - Next string argument is a default. Pass in a `|` pipe-delimeted value with `{count}`
56
 *    to do pluralisation.
57
 *  - Any other string argument after default is context for i18nTextCollector
58
 *  - Any array argument in any order is an injection parameter list. Pass in a `count`
59
 *    injection parameter to pluralise.
60
 * @return string
61
 */
62
function _t($entity, $arg = null)
63
{
64
    // Pass args directly to handle deprecation
65
    return call_user_func_array([i18n::class, '_t'], func_get_args());
66
}
67
68
/**
69
 * ExpressionLanguage service getter function. Substitute for '%$' in spec expression 
70
 * based syntax.
71
 *
72
 * @param string $service
73
 * @return object
74
 */
75
function srv($service)
76
{
77
    return Injector::inst()->get($service);
78
}
79
80
/**
81
 * ExpressionLanguage environment variable getter function.
82
 *
83
 * @param string $name
84
 * @return string|false
85
 */
86
function env($name)
87
{
88
    return Environment::getEnv($name);
89
}
90
91
/**
92
 * ExpressionLanguage current site config getter function.
93
 *
94
 * @param string $key
95
 * @return string
96
 */
97
function site($key)
98
{
99
    return SiteConfig::current_site_config()->{$key};
100
}
101