1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Psr\Container\ContainerExceptionInterface; |
10
|
|
|
use Spiral\Core\Container\Autowire; |
11
|
|
|
use Spiral\Core\ContainerScope; |
12
|
|
|
use Spiral\Core\Exceptions\ScopeException; |
13
|
|
|
use Spiral\Framework\DirectoriesInterface; |
14
|
|
|
use Spiral\Framework\EnvironmentInterface; |
15
|
|
|
use Spiral\Framework\Exceptions\DirectoryException; |
16
|
|
|
|
17
|
|
|
if (!function_exists('bind')) { |
18
|
|
|
/** |
19
|
|
|
* Shortcut for new Autowire(). |
20
|
|
|
* |
21
|
|
|
* @param string $alias Class name or alias. |
22
|
|
|
* @param array $parameters |
23
|
|
|
* |
24
|
|
|
* @return Autowire |
25
|
|
|
*/ |
26
|
|
|
function bind(string $alias, array $parameters = []): Autowire |
27
|
|
|
{ |
28
|
|
|
return new Autowire($alias, $parameters); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (!function_exists('spiral')) { |
33
|
|
|
/** |
34
|
|
|
* Resolve given alias in current IoC scope. |
35
|
|
|
* |
36
|
|
|
* @param string $alias Class name or alias. |
37
|
|
|
* @return object|null |
38
|
|
|
* |
39
|
|
|
* @throws ScopeException |
40
|
|
|
*/ |
41
|
|
|
function spiral(string $alias) |
42
|
|
|
{ |
43
|
|
|
if (ContainerScope::getContainer() === null) { |
44
|
|
|
throw new ScopeException('Container scope was not set.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
return ContainerScope::getContainer()->get($alias); |
49
|
|
|
} catch (ContainerExceptionInterface $e) { |
50
|
|
|
throw new ScopeException($e->getMessage(), $e->getCode(), $e); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!function_exists('directory')) { |
56
|
|
|
/** |
57
|
|
|
* Get directory alias value. |
58
|
|
|
* |
59
|
|
|
* @param string $alias Directory alias, ie. "framework". |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
* |
63
|
|
|
* @throws ScopeException |
64
|
|
|
* @throws DirectoryException |
65
|
|
|
*/ |
66
|
|
|
function directory(string $alias): string |
67
|
|
|
{ |
68
|
|
|
return spiral(DirectoriesInterface::class)->directory($alias); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (!function_exists('env')) { |
73
|
|
|
/** |
74
|
|
|
* Gets the value of an environment variable. |
75
|
|
|
* |
76
|
|
|
* @param string $key |
77
|
|
|
* @param mixed $default |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
function env(string $key, $default = null) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
return spiral(EnvironmentInterface::class)->get($key, $default); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (!function_exists('e')) { |
88
|
|
|
/** |
89
|
|
|
* Short alias for htmlentities(). This function is identical to htmlspecialchars() in all ways, |
90
|
|
|
* except with htmlentities(), all characters which have HTML character entity equivalents are |
91
|
|
|
* translated into these entities. |
92
|
|
|
* |
93
|
|
|
* @param string|null $string |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
function e(string $string = null): string |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
if (is_null($string)) { |
100
|
|
|
return ''; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return htmlentities($string, ENT_QUOTES, 'UTF-8', false); |
104
|
|
|
} |
105
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.