1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette;
13:
14: use Nette,
15: Nette\Config\Config;
16:
17:
18:
19: 20: 21: 22: 23:
24: class Configurator extends Object
25: {
26:
27: public $defaultConfigFile = '%appDir%/config.ini';
28:
29:
30: public $defaultServices = array(
31: 'Nette\\Application\\Application' => 'Nette\Application\Application',
32: 'Nette\\Web\\HttpContext' => 'Nette\Web\HttpContext',
33: 'Nette\\Web\\IHttpRequest' => 'Nette\Web\HttpRequest',
34: 'Nette\\Web\\IHttpResponse' => 'Nette\Web\HttpResponse',
35: 'Nette\\Web\\IUser' => 'Nette\Web\User',
36: 'Nette\\Caching\\ICacheStorage' => array(__CLASS__, 'createCacheStorage'),
37: 'Nette\\Web\\Session' => 'Nette\Web\Session',
38: 'Nette\\Loaders\\RobotLoader' => array(__CLASS__, 'createRobotLoader'),
39: );
40:
41:
42:
43: 44: 45: 46: 47:
48: public function detect($name)
49: {
50: switch ($name) {
51: case 'environment':
52:
53: if ($this->detect('console')) {
54: return Environment::CONSOLE;
55:
56: } else {
57: return Environment::getMode('production') ? Environment::PRODUCTION : Environment::DEVELOPMENT;
58: }
59:
60: case 'production':
61:
62: if (PHP_SAPI === 'cli') {
63: return FALSE;
64:
65: } elseif (isset($_SERVER['SERVER_ADDR']) || isset($_SERVER['LOCAL_ADDR'])) {
66: $addr = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
67: $oct = explode('.', $addr);
68:
69:
70:
71:
72:
73: return $addr !== '::1' && (count($oct) !== 4 || ($oct[0] !== '10' && $oct[0] !== '127' && ($oct[0] !== '172' || $oct[1] < 16 || $oct[1] > 31)
74: && ($oct[0] !== '169' || $oct[1] !== '254') && ($oct[0] !== '192' || $oct[1] !== '168')));
75:
76: } else {
77: return TRUE;
78: }
79:
80: case 'console':
81: return PHP_SAPI === 'cli';
82:
83: default:
84:
85: return NULL;
86: }
87: }
88:
89:
90:
91: 92: 93: 94: 95:
96: public function loadConfig($file)
97: {
98: $name = Environment::getName();
99:
100: if ($file instanceof Config) {
101: $config = $file;
102: $file = NULL;
103:
104: } else {
105: if ($file === NULL) {
106: $file = $this->defaultConfigFile;
107: }
108: $file = Environment::expand($file);
109: $config = Config::fromFile($file, $name, 0);
110: }
111:
112:
113: if ($config->variable instanceof Config) {
114: foreach ($config->variable as $key => $value) {
115: Environment::setVariable($key, $value);
116: }
117: }
118:
119: $config->expand();
120:
121:
122: $runServices = array();
123: $locator = Environment::getServiceLocator();
124: if ($config->service instanceof Config) {
125: foreach ($config->service as $key => $value) {
126: $key = strtr($key, '-', '\\');
127: if (is_string($value)) {
128: $locator->removeService($key);
129: $locator->addService($key, $value);
130: } else {
131: if ($value->factory) {
132: $locator->removeService($key);
133: $locator->addService($key, $value->factory, isset($value->singleton) ? $value->singleton : TRUE, (array) $value->option);
134: }
135: if ($value->run) {
136: $runServices[] = $key;
137: }
138: }
139: }
140: }
141:
142:
143: if (!$config->php) {
144: $config->php = $config->set;
145: unset($config->set);
146: }
147:
148: if ($config->php instanceof Config) {
149: if (PATH_SEPARATOR !== ';' && isset($config->php->include_path)) {
150: $config->php->include_path = str_replace(';', PATH_SEPARATOR, $config->php->include_path);
151: }
152:
153: foreach ($config->php as $key => $value) {
154: if ($value instanceof Config) {
155: unset($config->php->$key);
156: foreach ($value as $k => $v) {
157: $config->php->{"$key.$k"} = $v;
158: }
159: }
160: }
161:
162: foreach ($config->php as $key => $value) {
163: $key = strtr($key, '-', '.');
164:
165: if (!is_scalar($value)) {
166: throw new \InvalidStateException("Configuration value for directive '$key' is not scalar.");
167: }
168:
169: if ($key === 'date.timezone') {
170: date_default_timezone_set($value);
171: }
172:
173: if (function_exists('ini_set')) {
174: ini_set($key, $value);
175: } else {
176: switch ($key) {
177: case 'include_path':
178: set_include_path($value);
179: break;
180: case 'iconv.internal_encoding':
181: iconv_set_encoding('internal_encoding', $value);
182: break;
183: case 'mbstring.internal_encoding':
184: mb_internal_encoding($value);
185: break;
186: case 'date.timezone':
187: date_default_timezone_set($value);
188: break;
189: case 'error_reporting':
190: error_reporting($value);
191: break;
192: case 'ignore_user_abort':
193: ignore_user_abort($value);
194: break;
195: case 'max_execution_time':
196: set_time_limit($value);
197: break;
198: default:
199: if (ini_get($key) != $value) {
200: throw new \NotSupportedException('Required function ini_set() is disabled.');
201: }
202: }
203: }
204: }
205: }
206:
207:
208: if ($config->const instanceof Config) {
209: foreach ($config->const as $key => $value) {
210: define($key, $value);
211: }
212: }
213:
214:
215: if (isset($config->mode)) {
216: foreach($config->mode as $mode => $state) {
217: Environment::setMode($mode, $state);
218: }
219: }
220:
221:
222: foreach ($runServices as $name) {
223: $locator->getService($name);
224: }
225:
226: $config->freeze();
227: return $config;
228: }
229:
230:
231:
232:
233:
234:
235:
236: 237: 238: 239:
240: public function createServiceLocator()
241: {
242: $locator = new ServiceLocator;
243: foreach ($this->defaultServices as $name => $service) {
244: $locator->addService($name, $service);
245: }
246: return $locator;
247: }
248:
249:
250:
251: 252: 253:
254: public static function createCacheStorage()
255: {
256: return new Nette\Caching\FileStorage(Environment::getVariable('tempDir'));
257: }
258:
259:
260:
261: 262: 263:
264: public static function createRobotLoader($options)
265: {
266: $loader = new Nette\Loaders\RobotLoader;
267: $loader->autoRebuild = !Environment::isProduction();
268:
269: $dirs = isset($options['directory']) ? $options['directory'] : array(Environment::getVariable('appDir'), Environment::getVariable('libsDir'));
270: $loader->addDirectory($dirs);
271: $loader->register();
272: return $loader;
273: }
274:
275: }
276: