Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • NConstantsExtension
  • NNetteExtension
  • NPhpExtension
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  * @package Nette\Config\Extensions
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Core Nette Framework services.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Config\Extensions
 20:  */
 21: class NNetteExtension extends NConfigCompilerExtension
 22: {
 23:     public $defaults = array(
 24:         'xhtml' => TRUE,
 25:         'session' => array(
 26:             'iAmUsingBadHost' => NULL,
 27:             'autoStart' => NULL,  // true|false|smart
 28:             'expiration' => NULL,
 29:         ),
 30:         'application' => array(
 31:             'debugger' => TRUE,
 32:             'errorPresenter' => NULL,
 33:             'catchExceptions' => '%productionMode%',
 34:         ),
 35:         'routing' => array(
 36:             'debugger' => TRUE,
 37:             'routes' => array(), // of [mask => action]
 38:         ),
 39:         'security' => array(
 40:             'debugger' => TRUE,
 41:             'frames' => 'DENY', // X-Frame-Options
 42:             'users' => array(), // of [user => password]
 43:             'roles' => array(), // of [role => parents]
 44:             'resources' => array(), // of [resource => parents]
 45:         ),
 46:         'mailer' => array(
 47:             'smtp' => FALSE,
 48:         ),
 49:         'database' => array(), // of [name => dsn, user, password, debugger, explain, autowired, reflection]
 50:         'forms' => array(
 51:             'messages' => array(),
 52:         ),
 53:         'container' => array(
 54:             'debugger' => FALSE,
 55:         ),
 56:         'debugger' => array(
 57:             'email' => NULL,
 58:             'editor' => NULL,
 59:             'browser' => NULL,
 60:             'strictMode' => NULL,
 61:             'bar' => array(), // of class name
 62:             'blueScreen' => array(), // of callback
 63:         ),
 64:     );
 65: 
 66:     public $databaseDefaults = array(
 67:         'dsn' => NULL,
 68:         'user' => NULL,
 69:         'password' => NULL,
 70:         'options' => NULL,
 71:         'debugger' => TRUE,
 72:         'explain' => TRUE,
 73:         'reflection' => 'NDiscoveredReflection',
 74:     );
 75: 
 76: 
 77: 
 78:     public function loadConfiguration()
 79:     {
 80:         $container = $this->getContainerBuilder();
 81:         $config = $this->getConfig($this->defaults);
 82: 
 83: 
 84:         // cache
 85:         $container->addDefinition($this->prefix('cacheJournal'))
 86:             ->setClass('NFileJournal', array('%tempDir%'));
 87: 
 88:         $container->addDefinition('cacheStorage') // no namespace for back compatibility
 89:             ->setClass('NFileStorage', array('%tempDir%/cache'));
 90: 
 91:         $container->addDefinition($this->prefix('templateCacheStorage'))
 92:             ->setClass('NPhpFileStorage', array('%tempDir%/cache'))
 93:             ->setAutowired(FALSE);
 94: 
 95:         $container->addDefinition($this->prefix('cache'))
 96:             ->setClass('NCache', array(1 => '%namespace%'))
 97:             ->setParameters(array('namespace' => NULL));
 98: 
 99: 
100:         // http
101:         $container->addDefinition($this->prefix('httpRequestFactory'))
102:             ->setClass('NHttpRequestFactory')
103:             ->addSetup('setEncoding', array('UTF-8'))
104:             ->setInternal(TRUE);
105: 
106:         $container->addDefinition('httpRequest') // no namespace for back compatibility
107:             ->setClass('NHttpRequest')
108:             ->setFactory('@\NHttpRequestFactory::createHttpRequest');
109: 
110:         $container->addDefinition('httpResponse') // no namespace for back compatibility
111:             ->setClass('NHttpResponse');
112: 
113:         $container->addDefinition($this->prefix('httpContext'))
114:             ->setClass('NHttpContext');
115: 
116: 
117:         // session
118:         $session = $container->addDefinition('session') // no namespace for back compatibility
119:             ->setClass('NSession');
120: 
121:         if (isset($config['session']['expiration'])) {
122:             $session->addSetup('setExpiration', array($config['session']['expiration']));
123:         }
124:         if (isset($config['session']['iAmUsingBadHost'])) {
125:             $session->addSetup('NFramework::$iAmUsingBadHost = ?;', array((bool) $config['session']['iAmUsingBadHost']));
126:         }
127:         unset($config['session']['expiration'], $config['session']['autoStart'], $config['session']['iAmUsingBadHost']);
128:         if (!empty($config['session'])) {
129:             $session->addSetup('setOptions', array($config['session']));
130:         }
131: 
132: 
133:         // security
134:         $container->addDefinition($this->prefix('userStorage'))
135:             ->setClass('NUserStorage');
136: 
137:         $user = $container->addDefinition('user') // no namespace for back compatibility
138:             ->setClass('NUser');
139: 
140:         if (!$container->parameters['productionMode'] && $config['security']['debugger']) {
141:             $user->addSetup('NDebugger::$bar->addPanel(?)', array(
142:                 new NDIStatement('NUserPanel')
143:             ));
144:         }
145: 
146:         if ($config['security']['users']) {
147:             $container->addDefinition($this->prefix('authenticator'))
148:                 ->setClass('NSimpleAuthenticator', array($config['security']['users']));
149:         }
150: 
151:         if ($config['security']['roles'] || $config['security']['resources']) {
152:             $authorizator = $container->addDefinition($this->prefix('authorizator'))
153:                 ->setClass('NPermission');
154:             foreach ($config['security']['roles'] as $role => $parents) {
155:                 $authorizator->addSetup('addRole', array($role, $parents));
156:             }
157:             foreach ($config['security']['resources'] as $resource => $parents) {
158:                 $authorizator->addSetup('addResource', array($resource, $parents));
159:             }
160:         }
161: 
162: 
163:         // application
164:         $application = $container->addDefinition('application') // no namespace for back compatibility
165:             ->setClass('NApplication')
166:             ->addSetup('$catchExceptions', $config['application']['catchExceptions'])
167:             ->addSetup('$errorPresenter', $config['application']['errorPresenter']);
168: 
169:         if ($config['application']['debugger']) {
170:             $application->addSetup('NRoutingDebugger::initializePanel');
171:         }
172: 
173:         $container->addDefinition($this->prefix('presenterFactory'))
174:             ->setClass('NPresenterFactory', array(
175:                 isset($container->parameters['appDir']) ? $container->parameters['appDir'] : NULL
176:             ));
177: 
178: 
179:         // routing
180:         $router = $container->addDefinition('router') // no namespace for back compatibility
181:             ->setClass('NRouteList');
182: 
183:         foreach ($config['routing']['routes'] as $mask => $action) {
184:             $router->addSetup('$service[] = new NRoute(?, ?);', array($mask, $action));
185:         }
186: 
187:         if (!$container->parameters['productionMode'] && $config['routing']['debugger']) {
188:             $application->addSetup('NDebugger::$bar->addPanel(?)', array(
189:                 new NDIStatement('NRoutingDebugger')
190:             ));
191:         }
192: 
193: 
194:         // mailer
195:         if (empty($config['mailer']['smtp'])) {
196:             $container->addDefinition($this->prefix('mailer'))
197:                 ->setClass('NSendmailMailer');
198:         } else {
199:             NValidators::assertField($config, 'mailer', 'array');
200:             $container->addDefinition($this->prefix('mailer'))
201:                 ->setClass('NSmtpMailer', array($config['mailer']));
202:         }
203: 
204:         $container->addDefinition($this->prefix('mail'))
205:             ->setClass('NMail')
206:             ->addSetup('setMailer')
207:             ->setShared(FALSE);
208: 
209: 
210:         // forms
211:         $container->addDefinition($this->prefix('basicForm'))
212:             ->setClass('NForm')
213:             ->setShared(FALSE);
214: 
215: 
216:         // templating
217:         $latte = $container->addDefinition($this->prefix('latte'))
218:             ->setClass('NLatteFilter')
219:             ->setShared(FALSE);
220: 
221:         if (empty($config['xhtml'])) {
222:             $latte->addSetup('$service->getCompiler()->defaultContentType = ?', NLatteCompiler::CONTENT_HTML);
223:         }
224: 
225:         $container->addDefinition($this->prefix('template'))
226:             ->setClass('NFileTemplate')
227:             ->addSetup('registerFilter', array($latte))
228:             ->addSetup('registerHelperLoader', array('NTemplateHelpers::loader'))
229:             ->setShared(FALSE);
230: 
231: 
232:         // database
233:         $container->addDefinition($this->prefix('database'))
234:                 ->setClass('NDINestedAccessor', array('@container', $this->prefix('database')));
235: 
236:         $autowired = TRUE;
237:         foreach ((array) $config['database'] as $name => $info) {
238:             if (!is_array($info)) {
239:                 continue;
240:             }
241:             $info += $this->databaseDefaults + array('autowired' => $autowired);
242:             $autowired = FALSE;
243: 
244:             foreach ((array) $info['options'] as $key => $value) {
245:                 unset($info['options'][$key]);
246:                 $info['options'][constant($key)] = $value;
247:             }
248: 
249:             $connection = $container->addDefinition($this->prefix("database.$name"))
250:                 ->setClass('NConnection', array($info['dsn'], $info['user'], $info['password'], $info['options']))
251:                 ->setAutowired($info['autowired'])
252:                 ->addSetup('setCacheStorage')
253:                 ->addSetup('NDebugger::$blueScreen->addPanel(?)', array(
254:                     'NDatabasePanel::renderException'
255:                 ));
256: 
257:             if ($info['reflection']) {
258:                 $connection->addSetup('setDatabaseReflection', is_string($info['reflection'])
259:                     ? array(new NDIStatement(preg_match('#^[a-z]+$#', $info['reflection']) ? 'Nette\Database\Reflection\\' . ucfirst($info['reflection']) . 'Reflection' : $info['reflection']))
260:                     : NConfigCompiler::filterArguments(array($info['reflection']))
261:                 );
262:             }
263: 
264:             if (!$container->parameters['productionMode'] && $info['debugger']) {
265:                 $panel = $container->addDefinition($this->prefix("database.{$name}ConnectionPanel"))
266:                     ->setClass('NDatabasePanel')
267:                     ->setAutowired(FALSE)
268:                     ->addSetup('$explain', !empty($info['explain']))
269:                     ->addSetup('NDebugger::$bar->addPanel(?)', array('@self'));
270: 
271:                 $connection->addSetup('$service->onQuery[] = ?', array(array($panel, 'logQuery')));
272:             }
273:         }
274:     }
275: 
276: 
277: 
278:     public function afterCompile(NPhpClassType $class)
279:     {
280:         $initialize = $class->methods['initialize'];
281:         $container = $this->getContainerBuilder();
282:         $config = $this->getConfig($this->defaults);
283: 
284:         // debugger
285:         foreach (array('email', 'editor', 'browser', 'strictMode') as $key) {
286:             if (isset($config['debugger'][$key])) {
287:                 $initialize->addBody('NDebugger::$? = ?;', array($key, $config['debugger'][$key]));
288:             }
289:         }
290: 
291:         if (!$container->parameters['productionMode']) {
292:             if ($config['container']['debugger']) {
293:                 $config['debugger']['bar'][] = 'NContainerPanel';
294:             }
295: 
296:             foreach ((array) $config['debugger']['bar'] as $item) {
297:                 $initialize->addBody($container->formatPhp(
298:                     'NDebugger::$bar->addPanel(?);',
299:                     NConfigCompiler::filterArguments(array(is_string($item) ? new NDIStatement($item) : $item))
300:                 ));
301:             }
302: 
303:             foreach ((array) $config['debugger']['blueScreen'] as $item) {
304:                 $initialize->addBody($container->formatPhp(
305:                     'NDebugger::$blueScreen->addPanel(?);',
306:                     NConfigCompiler::filterArguments(array($item))
307:                 ));
308:             }
309:         }
310: 
311:         if (!empty($container->parameters['tempDir'])) {
312:             $initialize->addBody($this->checkTempDir($container->expand('%tempDir%/cache')));
313:         }
314: 
315:         foreach ((array) $config['forms']['messages'] as $name => $text) {
316:             $initialize->addBody('NRules::$defaultMessages[NForm::?] = ?;', array($name, $text));
317:         }
318: 
319:         if ($config['session']['autoStart'] === 'smart') {
320:             $initialize->addBody('$this->session->exists() && $this->session->start();');
321:         } elseif ($config['session']['autoStart']) {
322:             $initialize->addBody('$this->session->start();');
323:         }
324: 
325:         if (empty($config['xhtml'])) {
326:             $initialize->addBody('NHtml::$xhtml = ?;', array((bool) $config['xhtml']));
327:         }
328: 
329:         if (isset($config['security']['frames'])) {
330:             $initialize->addBody('header(?);', array('X-Frame-Options: ' . $config['security']['frames']));
331:         }
332: 
333:         foreach ($container->findByTag('run') as $name => $on) {
334:             if ($on) {
335:                 $initialize->addBody('$this->getService(?);', array($name));
336:             }
337:         }
338:     }
339: 
340: 
341: 
342:     private function checkTempDir($dir)
343:     {
344:         // checks whether directory is writable
345:         $uniq = uniqid('_', TRUE);
346:         if (!@mkdir("$dir/$uniq", 0777)) { // @ - is escalated to exception
347:             throw new InvalidStateException("Unable to write to directory '$dir'. Make this directory writable.");
348:         }
349: 
350:         // tests subdirectory mode
351:         $useDirs = @file_put_contents("$dir/$uniq/_", '') !== FALSE; // @ - error is expected
352:         @unlink("$dir/$uniq/_");
353:         @rmdir("$dir/$uniq"); // @ - directory may not already exist
354: 
355:         return 'NFileStorage::$useDirectories = ' . ($useDirs ? 'TRUE' : 'FALSE') . ";\n";
356:     }
357: 
358: }
359: 
Nette Framework 2.0.1 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.7.0