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' => array(__CLASS__, 'createApplication'),
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\\Caching\\ICacheJournal' => array(__CLASS__, 'createCacheJournal'),
38: 'Nette\\Web\\Session' => 'Nette\Web\Session',
39: 'Nette\\Loaders\\RobotLoader' => array(__CLASS__, 'createRobotLoader'),
40: );
41:
42:
43:
44: 45: 46: 47: 48:
49: public function detect($name)
50: {
51: switch ($name) {
52: case 'environment':
53: 54: if ($this->detect('console')) {
55: return Environment::CONSOLE;
56:
57: } else {
58: return Environment::getMode('production') ? Environment::PRODUCTION : Environment::DEVELOPMENT;
59: }
60:
61: case 'production':
62: 63: if (PHP_SAPI === 'cli') {
64: return FALSE;
65:
66: } elseif (isset($_SERVER['SERVER_ADDR']) || isset($_SERVER['LOCAL_ADDR'])) {
67: $addr = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
68: $oct = explode('.', $addr);
69: 70: 71: 72: 73: 74: return $addr !== '::1' && (count($oct) !== 4 || ($oct[0] !== '10' && $oct[0] !== '127' && ($oct[0] !== '172' || $oct[1] < 16 || $oct[1] > 31)
75: && ($oct[0] !== '169' || $oct[1] !== '254') && ($oct[0] !== '192' || $oct[1] !== '168')));
76:
77: } else {
78: return TRUE;
79: }
80:
81: case 'console':
82: return PHP_SAPI === 'cli';
83:
84: default:
85: 86: return NULL;
87: }
88: }
89:
90:
91:
92: 93: 94: 95: 96:
97: public function loadConfig($file)
98: {
99: $name = Environment::getName();
100:
101: if ($file instanceof Config) {
102: $config = $file;
103: $file = NULL;
104:
105: } else {
106: if ($file === NULL) {
107: $file = $this->defaultConfigFile;
108: }
109: $file = Environment::expand($file);
110: $config = Config::fromFile($file, $name);
111: }
112:
113: 114: if ($config->variable instanceof Config) {
115: foreach ($config->variable as $key => $value) {
116: Environment::setVariable($key, $value);
117: }
118: }
119:
120: 121: $iterator = new \RecursiveIteratorIterator($config);
122: foreach ($iterator as $key => $value) {
123: $tmp = $iterator->getDepth() ? $iterator->getSubIterator($iterator->getDepth() - 1)->current() : $config;
124: $tmp[$key] = Environment::expand($value);
125: }
126:
127: 128: $runServices = array();
129: $context = Environment::getContext();
130: if ($config->service instanceof Config) {
131: foreach ($config->service as $key => $value) {
132: $key = strtr($key, '-', '\\'); 133: if (is_string($value)) {
134: $context->removeService($key);
135: $context->addService($key, $value);
136: } else {
137: if ($value->factory) {
138: $context->removeService($key);
139: $context->addService($key, $value->factory, isset($value->singleton) ? $value->singleton : TRUE, (array) $value->option);
140: } elseif (isset($this->defaultServices[$key])) {
141: $context->removeService($key);
142: $context->addService($key, $this->defaultServices[$key], isset($value->singleton) ? $value->singleton : TRUE, (array) $value->option);
143: }
144: if ($value->run) {
145: $runServices[] = $key;
146: }
147: }
148: }
149: }
150:
151: 152: if (!$config->php) { 153: $config->php = $config->set;
154: unset($config->set);
155: }
156:
157: if ($config->php instanceof Config) {
158: if (PATH_SEPARATOR !== ';' && isset($config->php->include_path)) {
159: $config->php->include_path = str_replace(';', PATH_SEPARATOR, $config->php->include_path);
160: }
161:
162: foreach (clone $config->php as $key => $value) { 163: if ($value instanceof Config) {
164: unset($config->php->$key);
165: foreach ($value as $k => $v) {
166: $config->php->{"$key.$k"} = $v;
167: }
168: }
169: }
170:
171: foreach ($config->php as $key => $value) {
172: $key = strtr($key, '-', '.'); 173:
174: if (!is_scalar($value)) {
175: throw new \InvalidStateException("Configuration value for directive '$key' is not scalar.");
176: }
177:
178: if ($key === 'date.timezone') { 179: date_default_timezone_set($value);
180: }
181:
182: if (function_exists('ini_set')) {
183: ini_set($key, $value);
184: } else {
185: switch ($key) {
186: case 'include_path':
187: set_include_path($value);
188: break;
189: case 'iconv.internal_encoding':
190: iconv_set_encoding('internal_encoding', $value);
191: break;
192: case 'mbstring.internal_encoding':
193: mb_internal_encoding($value);
194: break;
195: case 'date.timezone':
196: date_default_timezone_set($value);
197: break;
198: case 'error_reporting':
199: error_reporting($value);
200: break;
201: case 'ignore_user_abort':
202: ignore_user_abort($value);
203: break;
204: case 'max_execution_time':
205: set_time_limit($value);
206: break;
207: default:
208: if (ini_get($key) != $value) { 209: throw new \NotSupportedException('Required function ini_set() is disabled.');
210: }
211: }
212: }
213: }
214: }
215:
216: 217: if ($config->const instanceof Config) {
218: foreach ($config->const as $key => $value) {
219: define($key, $value);
220: }
221: }
222:
223: 224: if (isset($config->mode)) {
225: foreach($config->mode as $mode => $state) {
226: Environment::setMode($mode, $state);
227: }
228: }
229:
230: 231: foreach ($runServices as $name) {
232: $context->getService($name);
233: }
234:
235: return $config;
236: }
237:
238:
239:
240:
241:
242:
243:
244: 245: 246: 247:
248: public function createContext()
249: {
250: $context = new Context;
251: foreach ($this->defaultServices as $name => $service) {
252: $context->addService($name, $service);
253: }
254: return $context;
255: }
256:
257:
258:
259: 260: 261:
262: public static function createApplication()
263: {
264: if (Environment::getVariable('baseUri', NULL) === NULL) {
265: Environment::setVariable('baseUri', Environment::getHttpRequest()->getUri()->getBasePath());
266: }
267:
268: $context = clone Environment::getContext();
269: $context->addService('Nette\\Application\\IRouter', 'Nette\Application\MultiRouter');
270: $context->addService('Nette\\Application\\IPresenterLoader', function() {
271: return new Nette\Application\PresenterLoader(Environment::getVariable('appDir'));
272: });
273:
274: $application = new Nette\Application\Application;
275: $application->setContext($context);
276: $application->catchExceptions = Environment::isProduction();
277: return $application;
278: }
279:
280:
281:
282: 283: 284:
285: public static function createCacheStorage()
286: {
287: $context = new Context;
288: $context->addService('Nette\\Caching\\ICacheJournal', array(__CLASS__, 'createCacheJournal'));
289: $dir = Environment::getVariable('tempDir') . '/cache';
290: umask(0000);
291: @mkdir($dir, 0755); 292: return new Nette\Caching\FileStorage($dir, $context);
293: }
294:
295:
296:
297: 298: 299:
300: public static function createCacheJournal()
301: {
302: 303: 304: {
305: return new Nette\Caching\FileJournal(Environment::getVariable('tempDir') . '/cache');
306: }
307: }
308:
309:
310:
311: 312: 313:
314: public static function createRobotLoader($options)
315: {
316: $loader = new Nette\Loaders\RobotLoader;
317: $loader->autoRebuild = isset($options['autoRebuild']) ? $options['autoRebuild'] : !Environment::isProduction();
318: $loader->setCacheStorage(Environment::getService('Nette\\Caching\\ICacheStorage'));
319: if (isset($options['directory'])) {
320: $loader->addDirectory($options['directory']);
321: } else {
322: foreach (array('appDir', 'libsDir') as $var) {
323: if ($dir = Environment::getVariable($var, NULL)) {
324: $loader->addDirectory($dir);
325: }
326: }
327: }
328: $loader->register();
329: return $loader;
330: }
331:
332: }
333: