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