-
Notifications
You must be signed in to change notification settings - Fork 5
/
code.php
168 lines (146 loc) · 3.51 KB
/
code.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace Craft;
class Craft extends \Yii
{
// Edition constants
const Personal = 0;
const Client = 1;
const Pro = 2;
/**
* Takes a path alias and will import any files/folders that it contains.
*/
public static function import($alias, $forceInclude = false)
{
$segs = explode('.', $alias);
if ($segs)
{
$firstSeg = array_shift($segs);
switch ($firstSeg)
{
case 'app':
{
$rootPath = CRAFT_APP_PATH;
break;
}
case 'plugins':
{
$rootPath = CRAFT_PLUGINS_PATH;
break;
}
default:
{
throw new \Exception('Unknown alias “'.$alias.'”');
}
}
}
else
{
$rootPath = CRAFT_APP_PATH;
}
$path = $rootPath.implode('/', $segs);
$folder = (mb_substr($path, -2) == '/*');
if ($folder)
{
$path = mb_substr($path, 0, -1);
$files = glob($path."*.php");
if (is_array($files) && count($files) > 0)
{
foreach ($files as $file)
{
static::_importFile(realpath($file));
}
}
}
else
{
$file = $path.'.php';
static::_importFile($file);
if ($forceInclude)
{
require_once $file;
}
}
}
/**
* Translates a given message into the specified language. If the config setting 'translationDebugOutput'
* is set, the the output will be wrapped in a pair of '@' to help diagnose any missing translations.
*/
public static function t($message, $variables = array(), $source = null, $language = null, $category = 'craft')
{
// Normalize the param keys
$normalizedVariables = array();
if (is_array($variables))
{
foreach ($variables as $key => $value)
{
$key = '{'.trim($key, '{}').'}';
$normalizedVariables[$key] = $value;
}
}
// If this isn't set, presumably we can't connect to the database.
if (!craft()->getIsDbConnectionValid())
{
$source = 'en_us';
$language = craft()->getTranslatedBrowserLanguage();
}
$translation = parent::t($category, (string)$message, $normalizedVariables, $source, $language);
if (craft()->config->get('translationDebugOutput'))
{
$translation = '@'.$translation.'@';
}
return $translation;
}
/**
* Logs a message. Messages logged by this method may be retrieved via {@link Logger::getLogs} and may be recorded in different media, such as file, email, database, using {@link LogRouter}.
*/
public static function log($msg, $level = LogLevel::Info, $force = false, $category = 'application', $plugin = null)
{
if ((YII_DEBUG && YII_TRACE_LEVEL > 0 && $level !== LogLevel::Profile) || $force)
{
$traces = debug_backtrace();
$count = 0;
foreach ($traces as $trace)
{
if (isset($trace['file'], $trace['line']) && mb_strpos($trace['file'], YII_PATH) !== 0)
{
$msg .= "\nin ".$trace['file'].' ('.$trace['line'].')';
if (++$count >= YII_TRACE_LEVEL)
{
break;
}
}
}
}
if (craft()->isConsole())
{
echo $msg."\n";
}
if (!$plugin)
{
$plugin = 'craft';
}
static::getLogger()->log($msg, $level, $force, $category, $plugin);
}
/**
* Imports a file into Craft's classMap.
*/
private static function _importFile($file)
{
$file = str_replace('\\', '/', $file);
// Don't add any Composer vendor files to the class map.
if (strpos($file, '/app/vendor/') === false)
{
$class = __NAMESPACE__.'\\'.pathinfo($file, PATHINFO_FILENAME);
\Yii::$classMap[$class] = $file;
}
}
}
/**
* Returns the current craft() instance. This is a wrapper function for the Craft::app() instance.
*
* @return WebApp|ConsoleApp
*/
function craft()
{
return Craft::app();
}