Namespaces

  • 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

  • ConstantsExtension
  • NetteExtension
  • PhpExtension
  • Overview
  • Namespace
  • 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:  */
 11: 
 12: namespace Nette\Config\Extensions;
 13: 
 14: use Nette,
 15:     Nette\DI\ContainerBuilder,
 16:     Nette\Utils\Validators;
 17: 
 18: 
 19: 
 20: /**
 21:  * Core Nette Framework services.
 22:  *
 23:  * @author     David Grudl
 24:  */
 25: class NetteExtension extends Nette\Config\CompilerExtension
 26: {
 27:     public $defaults = array(
 28:         'xhtml' => TRUE,
 29:         'session' => array(
 30:             'iAmUsingBadHost' => NULL,
 31:             'autoStart' => 'smart',  // true|false|smart
 32:             'expiration' => NULL,
 33:         ),
 34:         'application' => array(
 35:             'debugger' => TRUE,
 36:             'errorPresenter' => NULL,
 37:             'catchExceptions' => '%productionMode%',
 38:         ),
 39:         'routing' => array(
 40:             'debugger' => TRUE,
 41:             'routes' => array(), // of [mask => action]
 42:         ),
 43:         'security' => array(
 44:             'debugger' => TRUE,
 45:             'frames' => 'SAMEORIGIN', // X-Frame-Options
 46:             'users' => array(), // of [user => password]
 47:             'roles' => array(), // of [role => parents]
 48:             'resources' => array(), // of [resource => parents]
 49:         ),
 50:         'mailer' => array(
 51:             'smtp' => FALSE,
 52:         ),
 53:         'database' => array(), // of [name => dsn, user, password, debugger, explain, autowired, reflection]
 54:         'forms' => array(
 55:             'messages' => array(),
 56:         ),
 57:         'container' => array(
 58:             'debugger' => FALSE,
 59:         ),
 60:         'debugger' => array(
 61:             'email' => NULL,
 62:             'editor' => NULL,
 63:             'browser' => NULL,
 64:             'strictMode' => NULL,
 65:             'bar' => array(), // of class name
 66:             'blueScreen' => array(), // of callback
 67:         ),
 68:     );
 69: 
 70:     public $databaseDefaults = array(
 71:         'dsn' => NULL,
 72:         'user' => NULL,
 73:         'password' => NULL,
 74:         'options' => NULL,
 75:         'debugger' => TRUE,
 76:         'explain' => TRUE,
 77:         'reflection' => 'Nette\Database\Reflection\DiscoveredReflection',
 78:     );
 79: 
 80: 
 81: 
 82:     public function loadConfiguration()
 83:     {
 84:         $container = $this->getContainerBuilder();
 85:         $config = $this->getConfig($this->defaults);
 86: 
 87: 
 88:         // cache
 89:         $container->addDefinition($this->prefix('cacheJournal'))
 90:             ->setClass('Nette\Caching\Storages\FileJournal', array('%tempDir%'));
 91: 
 92:         $container->addDefinition('cacheStorage') // no namespace for back compatibility
 93:             ->setClass('Nette\Caching\Storages\FileStorage', array('%tempDir%/cache'));
 94: 
 95:         $container->addDefinition($this->prefix('templateCacheStorage'))
 96:             ->setClass('Nette\Caching\Storages\PhpFileStorage', array('%tempDir%/cache'))
 97:             ->setAutowired(FALSE);
 98: 
 99:         $container->addDefinition($this->prefix('cache'))
100:             ->setClass('Nette\Caching\Cache', array(1 => '%namespace%'))
101:             ->setParameters(array('namespace' => NULL));
102: 
103: 
104:         // http
105:         $container->addDefinition($this->prefix('httpRequestFactory'))
106:             ->setClass('Nette\Http\RequestFactory')
107:             ->addSetup('setEncoding', array('UTF-8'))
108:             ->setInternal(TRUE);
109: 
110:         $container->addDefinition('httpRequest') // no namespace for back compatibility
111:             ->setClass('Nette\Http\Request')
112:             ->setFactory('@Nette\Http\RequestFactory::createHttpRequest');
113: 
114:         $container->addDefinition('httpResponse') // no namespace for back compatibility
115:             ->setClass('Nette\Http\Response');
116: 
117:         $container->addDefinition($this->prefix('httpContext'))
118:             ->setClass('Nette\Http\Context');
119: 
120: 
121:         // session
122:         $session = $container->addDefinition('session') // no namespace for back compatibility
123:             ->setClass('Nette\Http\Session');
124: 
125:         if (isset($config['session']['expiration'])) {
126:             $session->addSetup('setExpiration', array($config['session']['expiration']));
127:         }
128:         if (isset($config['session']['iAmUsingBadHost'])) {
129:             $session->addSetup('Nette\Framework::$iAmUsingBadHost = ?;', array((bool) $config['session']['iAmUsingBadHost']));
130:         }
131:         unset($config['session']['expiration'], $config['session']['autoStart'], $config['session']['iAmUsingBadHost']);
132:         if (!empty($config['session'])) {
133:             $session->addSetup('setOptions', array($config['session']));
134:         }
135: 
136: 
137:         // security
138:         $container->addDefinition($this->prefix('userStorage'))
139:             ->setClass('Nette\Http\UserStorage');
140: 
141:         $user = $container->addDefinition('user') // no namespace for back compatibility
142:             ->setClass('Nette\Security\User');
143: 
144:         if (!$container->parameters['productionMode'] && $config['security']['debugger']) {
145:             $user->addSetup('Nette\Diagnostics\Debugger::$bar->addPanel(?)', array(
146:                 new Nette\DI\Statement('Nette\Security\Diagnostics\UserPanel')
147:             ));
148:         }
149: 
150:         if ($config['security']['users']) {
151:             $container->addDefinition($this->prefix('authenticator'))
152:                 ->setClass('Nette\Security\SimpleAuthenticator', array($config['security']['users']));
153:         }
154: 
155:         if ($config['security']['roles'] || $config['security']['resources']) {
156:             $authorizator = $container->addDefinition($this->prefix('authorizator'))
157:                 ->setClass('Nette\Security\Permission');
158:             foreach ($config['security']['roles'] as $role => $parents) {
159:                 $authorizator->addSetup('addRole', array($role, $parents));
160:             }
161:             foreach ($config['security']['resources'] as $resource => $parents) {
162:                 $authorizator->addSetup('addResource', array($resource, $parents));
163:             }
164:         }
165: 
166: 
167:         // application
168:         $application = $container->addDefinition('application') // no namespace for back compatibility
169:             ->setClass('Nette\Application\Application')
170:             ->addSetup('$catchExceptions', $config['application']['catchExceptions'])
171:             ->addSetup('$errorPresenter', $config['application']['errorPresenter']);
172: 
173:         if ($config['application']['debugger']) {
174:             $application->addSetup('Nette\Application\Diagnostics\RoutingPanel::initializePanel');
175:         }
176: 
177:         $container->addDefinition($this->prefix('presenterFactory'))
178:             ->setClass('Nette\Application\PresenterFactory', array(
179:                 isset($container->parameters['appDir']) ? $container->parameters['appDir'] : NULL
180:             ));
181: 
182: 
183:         // routing
184:         $router = $container->addDefinition('router') // no namespace for back compatibility
185:             ->setClass('Nette\Application\Routers\RouteList');
186: 
187:         foreach ($config['routing']['routes'] as $mask => $action) {
188:             $router->addSetup('$service[] = new Nette\Application\Routers\Route(?, ?);', array($mask, $action));
189:         }
190: 
191:         if (!$container->parameters['productionMode'] && $config['routing']['debugger']) {
192:             $application->addSetup('Nette\Diagnostics\Debugger::$bar->addPanel(?)', array(
193:                 new Nette\DI\Statement('Nette\Application\Diagnostics\RoutingPanel')
194:             ));
195:         }
196: 
197: 
198:         // mailer
199:         if (empty($config['mailer']['smtp'])) {
200:             $container->addDefinition($this->prefix('mailer'))
201:                 ->setClass('Nette\Mail\SendmailMailer');
202:         } else {
203:             $container->addDefinition($this->prefix('mailer'))
204:                 ->setClass('Nette\Mail\SmtpMailer', array($config['mailer']));
205:         }
206: 
207:         $container->addDefinition($this->prefix('mail'))
208:             ->setClass('Nette\Mail\Message')
209:             ->addSetup('setMailer')
210:             ->setShared(FALSE);
211: 
212: 
213:         // forms
214:         $container->addDefinition($this->prefix('basicForm'))
215:             ->setClass('Nette\Forms\Form')
216:             ->setShared(FALSE);
217: 
218: 
219:         // templating
220:         $latte = $container->addDefinition($this->prefix('latte'))
221:             ->setClass('Nette\Latte\Engine')
222:             ->setShared(FALSE);
223: 
224:         if (empty($config['xhtml'])) {
225:             $latte->addSetup('$service->getCompiler()->defaultContentType = ?', Nette\Latte\Compiler::CONTENT_HTML);
226:         }
227: 
228:         $container->addDefinition($this->prefix('template'))
229:             ->setClass('Nette\Templating\FileTemplate')
230:             ->addSetup('registerFilter', array($latte))
231:             ->addSetup('registerHelperLoader', array('Nette\Templating\Helpers::loader'))
232:             ->setShared(FALSE);
233: 
234: 
235:         // database
236:         $container->addDefinition($this->prefix('database'))
237:                 ->setClass('Nette\DI\NestedAccessor', array('@container', $this->prefix('database')));
238: 
239:         if (isset($config['database']['dsn'])) {
240:             $config['database'] = array('default' => $config['database']);
241:         }
242: 
243:         $autowired = TRUE;
244:         foreach ((array) $config['database'] as $name => $info) {
245:             if (!is_array($info)) {
246:                 continue;
247:             }
248:             $info += $this->databaseDefaults + array('autowired' => $autowired);
249:             $autowired = FALSE;
250: 
251:             foreach ((array) $info['options'] as $key => $value) {
252:                 unset($info['options'][$key]);
253:                 $info['options'][constant($key)] = $value;
254:             }
255: 
256:             $connection = $container->addDefinition($this->prefix("database.$name"))
257:                 ->setClass('Nette\Database\Connection', array($info['dsn'], $info['user'], $info['password'], $info['options']))
258:                 ->setAutowired($info['autowired'])
259:                 ->addSetup('setCacheStorage')
260:                 ->addSetup('Nette\Diagnostics\Debugger::$blueScreen->addPanel(?)', array(
261:                     'Nette\Database\Diagnostics\ConnectionPanel::renderException'
262:                 ));
263: 
264:             if ($info['reflection']) {
265:                 $connection->addSetup('setDatabaseReflection', is_string($info['reflection'])
266:                     ? array(new Nette\DI\Statement(preg_match('#^[a-z]+$#', $info['reflection']) ? 'Nette\Database\Reflection\\' . ucfirst($info['reflection']) . 'Reflection' : $info['reflection']))
267:                     : Nette\Config\Compiler::filterArguments(array($info['reflection']))
268:                 );
269:             }
270: 
271:             if (!$container->parameters['productionMode'] && $info['debugger']) {
272:                 $panel = $container->addDefinition($this->prefix("database.{$name}ConnectionPanel"))
273:                     ->setClass('Nette\Database\Diagnostics\ConnectionPanel')
274:                     ->setAutowired(FALSE)
275:                     ->addSetup('$explain', !empty($info['explain']))
276:                     ->addSetup('Nette\Diagnostics\Debugger::$bar->addPanel(?)', array('@self'));
277: 
278:                 $connection->addSetup('$service->onQuery[] = ?', array(array($panel, 'logQuery')));
279:             }
280:         }
281:     }
282: 
283: 
284: 
285:     public function afterCompile(Nette\Utils\PhpGenerator\ClassType $class)
286:     {
287:         $initialize = $class->methods['initialize'];
288:         $container = $this->getContainerBuilder();
289:         $config = $this->getConfig($this->defaults);
290: 
291:         // debugger
292:         foreach (array('email', 'editor', 'browser', 'strictMode', 'maxLen', 'maxDepth') as $key) {
293:             if (isset($config['debugger'][$key])) {
294:                 $initialize->addBody('Nette\Diagnostics\Debugger::$? = ?;', array($key, $config['debugger'][$key]));
295:             }
296:         }
297: 
298:         if (!$container->parameters['productionMode']) {
299:             if ($config['container']['debugger']) {
300:                 $config['debugger']['bar'][] = 'Nette\DI\Diagnostics\ContainerPanel';
301:             }
302: 
303:             foreach ((array) $config['debugger']['bar'] as $item) {
304:                 $initialize->addBody($container->formatPhp(
305:                     'Nette\Diagnostics\Debugger::$bar->addPanel(?);',
306:                     Nette\Config\Compiler::filterArguments(array(is_string($item) ? new Nette\DI\Statement($item) : $item))
307:                 ));
308:             }
309: 
310:             foreach ((array) $config['debugger']['blueScreen'] as $item) {
311:                 $initialize->addBody($container->formatPhp(
312:                     'Nette\Diagnostics\Debugger::$blueScreen->addPanel(?);',
313:                     Nette\Config\Compiler::filterArguments(array($item))
314:                 ));
315:             }
316:         }
317: 
318:         if (!empty($container->parameters['tempDir'])) {
319:             $initialize->addBody($this->checkTempDir($container->expand('%tempDir%/cache')));
320:         }
321: 
322:         foreach ((array) $config['forms']['messages'] as $name => $text) {
323:             $initialize->addBody('Nette\Forms\Rules::$defaultMessages[Nette\Forms\Form::?] = ?;', array($name, $text));
324:         }
325: 
326:         if ($config['session']['autoStart'] === 'smart') {
327:             $initialize->addBody('$this->session->exists() && $this->session->start();');
328:         } elseif ($config['session']['autoStart']) {
329:             $initialize->addBody('$this->session->start();');
330:         }
331: 
332:         if (empty($config['xhtml'])) {
333:             $initialize->addBody('Nette\Utils\Html::$xhtml = ?;', array((bool) $config['xhtml']));
334:         }
335: 
336:         if (isset($config['security']['frames']) && $config['security']['frames'] !== TRUE) {
337:             $frames = $config['security']['frames'];
338:             if ($frames === FALSE) {
339:                 $frames = 'DENY';
340:             } elseif (preg_match('#^https?:#', $frames)) {
341:                 $frames = "ALLOW-FROM $frames";
342:             }
343:             $initialize->addBody('header(?);', array("X-Frame-Options: $frames"));
344:         }
345: 
346:         foreach ($container->findByTag('run') as $name => $on) {
347:             if ($on) {
348:                 $initialize->addBody('$this->getService(?);', array($name));
349:             }
350:         }
351:     }
352: 
353: 
354: 
355:     private function checkTempDir($dir)
356:     {
357:         // checks whether directory is writable
358:         $uniq = uniqid('_', TRUE);
359:         if (!@mkdir("$dir/$uniq", 0777)) { // @ - is escalated to exception
360:             throw new Nette\InvalidStateException("Unable to write to directory '$dir'. Make this directory writable.");
361:         }
362: 
363:         // tests subdirectory mode
364:         $useDirs = @file_put_contents("$dir/$uniq/_", '') !== FALSE; // @ - error is expected
365:         @unlink("$dir/$uniq/_");
366:         @rmdir("$dir/$uniq"); // @ - directory may not already exist
367: 
368:         return 'Nette\Caching\Storages\FileStorage::$useDirectories = ' . ($useDirs ? 'TRUE' : 'FALSE') . ";\n";
369:     }
370: 
371: }
372: 
Nette Framework 2.0.6 API API documentation generated by ApiGen 2.7.0