1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class NConfigurator extends NObject
22: {
23:
24: public $defaultConfigFile = '%appDir%/config.ini';
25:
26:
27: public $defaultServices = array(
28: 'Nette\\Application\\Application' => 'NApplication',
29: 'Nette\\Web\\HttpContext' => 'NHttpContext',
30: 'Nette\\Web\\IHttpRequest' => 'NHttpRequest',
31: 'Nette\\Web\\IHttpResponse' => 'NHttpResponse',
32: 'Nette\\Web\\IUser' => 'NUser',
33: 'Nette\\Caching\\ICacheStorage' => array(__CLASS__, 'createCacheStorage'),
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, 0);
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: $config->expand();
117:
118:
119: $runServices = array();
120: $locator = NEnvironment::getServiceLocator();
121: if ($config->service instanceof NConfig) {
122: foreach ($config->service as $key => $value) {
123: $key = strtr($key, '-', '\\');
124: if (is_string($value)) {
125: $locator->removeService($key);
126: $locator->addService($key, $value);
127: } else {
128: if ($value->factory) {
129: $locator->removeService($key);
130: $locator->addService($key, $value->factory, isset($value->singleton) ? $value->singleton : TRUE, (array) $value->option);
131: }
132: if ($value->run) {
133: $runServices[] = $key;
134: }
135: }
136: }
137: }
138:
139:
140: if (!$config->php) {
141: $config->php = $config->set;
142: unset($config->set);
143: }
144:
145: if ($config->php instanceof NConfig) {
146: if (PATH_SEPARATOR !== ';' && isset($config->php->include_path)) {
147: $config->php->include_path = str_replace(';', PATH_SEPARATOR, $config->php->include_path);
148: }
149:
150: foreach ($config->php as $key => $value) {
151: if ($value instanceof NConfig) {
152: unset($config->php->$key);
153: foreach ($value as $k => $v) {
154: $config->php->{"$key.$k"} = $v;
155: }
156: }
157: }
158:
159: foreach ($config->php as $key => $value) {
160: $key = strtr($key, '-', '.');
161:
162: if (!is_scalar($value)) {
163: throw new InvalidStateException("Configuration value for directive '$key' is not scalar.");
164: }
165:
166: if ($key === 'date.timezone') {
167: date_default_timezone_set($value);
168: }
169:
170: if (function_exists('ini_set')) {
171: ini_set($key, $value);
172: } else {
173: switch ($key) {
174: case 'include_path':
175: set_include_path($value);
176: break;
177: case 'iconv.internal_encoding':
178: iconv_set_encoding('internal_encoding', $value);
179: break;
180: case 'mbstring.internal_encoding':
181: mb_internal_encoding($value);
182: break;
183: case 'date.timezone':
184: date_default_timezone_set($value);
185: break;
186: case 'error_reporting':
187: error_reporting($value);
188: break;
189: case 'ignore_user_abort':
190: ignore_user_abort($value);
191: break;
192: case 'max_execution_time':
193: set_time_limit($value);
194: break;
195: default:
196: if (ini_get($key) != $value) {
197: throw new NotSupportedException('Required function ini_set() is disabled.');
198: }
199: }
200: }
201: }
202: }
203:
204:
205: if ($config->const instanceof NConfig) {
206: foreach ($config->const as $key => $value) {
207: define($key, $value);
208: }
209: }
210:
211:
212: if (isset($config->mode)) {
213: foreach($config->mode as $mode => $state) {
214: NEnvironment::setMode($mode, $state);
215: }
216: }
217:
218:
219: foreach ($runServices as $name) {
220: $locator->getService($name);
221: }
222:
223: $config->freeze();
224: return $config;
225: }
226:
227:
228:
229:
230:
231:
232:
233: 234: 235: 236:
237: public function createServiceLocator()
238: {
239: $locator = new NServiceLocator;
240: foreach ($this->defaultServices as $name => $service) {
241: $locator->addService($name, $service);
242: }
243: return $locator;
244: }
245:
246:
247:
248: 249: 250:
251: public static function createCacheStorage()
252: {
253: return new NFileStorage(NEnvironment::getVariable('tempDir'));
254: }
255:
256:
257:
258: 259: 260:
261: public static function createRobotLoader($options)
262: {
263: $loader = new NRobotLoader;
264: $loader->autoRebuild = !NEnvironment::isProduction();
265:
266: $dirs = isset($options['directory']) ? $options['directory'] : array(NEnvironment::getVariable('appDir'), NEnvironment::getVariable('libsDir'));
267: $loader->addDirectory($dirs);
268: $loader->register();
269: return $loader;
270: }
271:
272: }
273: