1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19:
20: class Configurator extends Object
21: {
22:
23: public $defaultConfigFile = '%appDir%/config.neon';
24:
25:
26: public $defaultServices = array(
27: 'Nette\\Application\\Application' => array(__CLASS__, 'createApplication'),
28: 'Nette\\Web\\HttpContext' => 'HttpContext',
29: 'Nette\\Web\\IHttpRequest' => array(__CLASS__, 'createHttpRequest'),
30: 'Nette\\Web\\IHttpResponse' => 'HttpResponse',
31: 'Nette\\Web\\IUser' => 'User',
32: 'Nette\\Caching\\ICacheStorage' => array(__CLASS__, 'createCacheStorage'),
33: 'Nette\\Caching\\ICacheJournal' => array(__CLASS__, 'createCacheJournal'),
34: 'Nette\\Mail\\IMailer' => array(__CLASS__, 'createMailer'),
35: 'Nette\\Web\\Session' => 'Session',
36: 'Nette\\Loaders\\RobotLoader' => array(__CLASS__, 'createRobotLoader'),
37: );
38:
39:
40:
41: 42: 43: 44: 45:
46: public function detect($name)
47: {
48: switch ($name) {
49: case 'environment':
50: 51: if ($this->detect('console')) {
52: return Environment::CONSOLE;
53:
54: } else {
55: return Environment::getMode('production') ? Environment::PRODUCTION : Environment::DEVELOPMENT;
56: }
57:
58: case 'production':
59: 60: if (PHP_SAPI === 'cli') {
61: return FALSE;
62:
63: } elseif (isset($_SERVER['SERVER_ADDR']) || isset($_SERVER['LOCAL_ADDR'])) {
64: $addrs = array();
65: if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 66: $addrs = preg_split('#,\s*#', $_SERVER['HTTP_X_FORWARDED_FOR']);
67: }
68: if (isset($_SERVER['REMOTE_ADDR'])) {
69: $addrs[] = $_SERVER['REMOTE_ADDR'];
70: }
71: $addrs[] = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
72: foreach ($addrs as $addr) {
73: $oct = explode('.', $addr);
74: 75: 76: 77: 78: 79: if ($addr !== '::1' && (count($oct) !== 4 || ($oct[0] !== '10' && $oct[0] !== '127' && ($oct[0] !== '172' || $oct[1] < 16 || $oct[1] > 31)
80: && ($oct[0] !== '169' || $oct[1] !== '254') && ($oct[0] !== '192' || $oct[1] !== '168')))
81: ) {
82: return TRUE;
83: }
84: }
85: return FALSE;
86:
87: } else {
88: return TRUE;
89: }
90:
91: case 'console':
92: return PHP_SAPI === 'cli';
93:
94: default:
95: 96: return NULL;
97: }
98: }
99:
100:
101:
102: 103: 104: 105: 106:
107: public function loadConfig($file)
108: {
109: $name = Environment::getName();
110:
111: if ($file instanceof Config) {
112: $config = $file;
113: $file = NULL;
114:
115: } else {
116: if ($file === NULL) {
117: $file = $this->defaultConfigFile;
118: }
119: $file = Environment::expand($file);
120: if (!is_file($file)) {
121: $file = preg_replace('#\.neon$#', '.ini', $file); 122: }
123: $config = Config::fromFile($file, $name);
124: }
125:
126: 127: if ($config->variable instanceof Config) {
128: foreach ($config->variable as $key => $value) {
129: Environment::setVariable($key, $value);
130: }
131: }
132:
133: 134: $iterator = new RecursiveIteratorIterator($config);
135: foreach ($iterator as $key => $value) {
136: $tmp = $iterator->getDepth() ? $iterator->getSubIterator($iterator->getDepth() - 1)->current() : $config;
137: $tmp[$key] = Environment::expand($value);
138: }
139:
140: 141: $runServices = array();
142: $context = Environment::getContext();
143: if ($config->service instanceof Config) {
144: foreach ($config->service as $key => $value) {
145: $key = strtr($key, '-', '\\'); 146: if (is_string($value)) {
147: $context->removeService($key);
148: $context->addService($key, $value);
149: } else {
150: if ($value->factory || isset($this->defaultServices[$key])) {
151: $context->removeService($key);
152: $context->addService(
153: $key,
154: $value->factory ? $value->factory : $this->defaultServices[$key],
155: isset($value->singleton) ? $value->singleton : TRUE,
156: (array) $value->option
157: );
158: } else {
159: throw new InvalidStateException("Factory method is not specified for service $key.");
160: }
161: if ($value->run) {
162: $runServices[] = $key;
163: }
164: }
165: }
166: }
167:
168: 169: if (!$config->php) { 170: $config->php = $config->set;
171: unset($config->set);
172: }
173:
174: if ($config->php instanceof Config) {
175: if (PATH_SEPARATOR !== ';' && isset($config->php->include_path)) {
176: $config->php->include_path = str_replace(';', PATH_SEPARATOR, $config->php->include_path);
177: }
178:
179: foreach (clone $config->php as $key => $value) { 180: if ($value instanceof Config) {
181: unset($config->php->$key);
182: foreach ($value as $k => $v) {
183: $config->php->{"$key.$k"} = $v;
184: }
185: }
186: }
187:
188: foreach ($config->php as $key => $value) {
189: $key = strtr($key, '-', '.'); 190:
191: if (!is_scalar($value)) {
192: throw new InvalidStateException("Configuration value for directive '$key' is not scalar.");
193: }
194:
195: if ($key === 'date.timezone') { 196: date_default_timezone_set($value);
197: }
198:
199: if (function_exists('ini_set')) {
200: ini_set($key, $value);
201: } else {
202: switch ($key) {
203: case 'include_path':
204: set_include_path($value);
205: break;
206: case 'iconv.internal_encoding':
207: iconv_set_encoding('internal_encoding', $value);
208: break;
209: case 'mbstring.internal_encoding':
210: mb_internal_encoding($value);
211: break;
212: case 'date.timezone':
213: date_default_timezone_set($value);
214: break;
215: case 'error_reporting':
216: error_reporting($value);
217: break;
218: case 'ignore_user_abort':
219: ignore_user_abort($value);
220: break;
221: case 'max_execution_time':
222: set_time_limit($value);
223: break;
224: default:
225: if (ini_get($key) != $value) { 226: throw new NotSupportedException('Required function ini_set() is disabled.');
227: }
228: }
229: }
230: }
231: }
232:
233: 234: if ($config->const instanceof Config) {
235: foreach ($config->const as $key => $value) {
236: define($key, $value);
237: }
238: }
239:
240: 241: if (isset($config->mode)) {
242: foreach ($config->mode as $mode => $state) {
243: Environment::setMode($mode, $state);
244: }
245: }
246:
247: 248: foreach ($runServices as $name) {
249: $context->getService($name);
250: }
251:
252: return $config;
253: }
254:
255:
256:
257:
258:
259:
260:
261: 262: 263: 264:
265: public function createContext()
266: {
267: $context = new Context;
268: foreach ($this->defaultServices as $name => $service) {
269: $context->addService($name, $service);
270: }
271: return $context;
272: }
273:
274:
275:
276: 277: 278:
279: public static function createApplication(array $options = NULL)
280: {
281: if (Environment::getVariable('baseUri', NULL) === NULL) {
282: Environment::setVariable('baseUri', Environment::getHttpRequest()->getUri()->getBaseUri());
283: }
284:
285: $context = clone Environment::getContext();
286: $context->addService('Nette\\Application\\IRouter', 'MultiRouter');
287:
288: if (!$context->hasService('Nette\\Application\\IPresenterFactory')) {
289: $context->addService('Nette\\Application\\IPresenterFactory', callback(create_function('', 'extract(NClosureFix::$vars['.NClosureFix::uses(array('context'=>$context)).'], EXTR_REFS);
290: return new PresenterFactory(Environment::getVariable(\'appDir\'), $context);
291: ')));
292: }
293:
294: $class = isset($options['class']) ? $options['class'] : 'Application';
295: $application = new $class;
296: $application->setContext($context);
297: $application->catchExceptions = Environment::isProduction();
298: return $application;
299: }
300:
301:
302:
303: 304: 305:
306: public static function createHttpRequest()
307: {
308: $factory = new HttpRequestFactory;
309: $factory->setEncoding('UTF-8');
310: return $factory->createHttpRequest();
311: }
312:
313:
314:
315: 316: 317:
318: public static function createCacheStorage()
319: {
320: $dir = Environment::getVariable('tempDir') . '/cache';
321: umask(0000);
322: @mkdir($dir, 0777); 323: return new FileStorage($dir, Environment::getService('Nette\\Caching\\ICacheJournal'));
324: }
325:
326:
327:
328: 329: 330:
331: public static function createCacheJournal()
332: {
333: return new FileJournal(Environment::getVariable('tempDir'));
334: }
335:
336:
337:
338: 339: 340:
341: public static function createMailer(array $options = NULL)
342: {
343: if (empty($options['smtp'])) {
344: return new SendmailMailer;
345: } else {
346: return new SmtpMailer($options);
347: }
348: }
349:
350:
351:
352: 353: 354:
355: public static function createRobotLoader(array $options = NULL)
356: {
357: $loader = new RobotLoader;
358: $loader->autoRebuild = isset($options['autoRebuild']) ? $options['autoRebuild'] : !Environment::isProduction();
359: $loader->setCacheStorage(Environment::getService('Nette\\Caching\\ICacheStorage'));
360: if (isset($options['directory'])) {
361: $loader->addDirectory($options['directory']);
362: } else {
363: foreach (array('appDir', 'libsDir') as $var) {
364: if ($dir = Environment::getVariable($var, NULL)) {
365: $loader->addDirectory($dir);
366: }
367: }
368: }
369: $loader->register();
370: return $loader;
371: }
372:
373: }
374: