Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • DatabaseExtension
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Bridges\DatabaseDI;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Nette Framework Database services.
 15:  *
 16:  * @author     David Grudl
 17:  * @author     Jan Skrasek
 18:  */
 19: class DatabaseExtension extends Nette\DI\CompilerExtension
 20: {
 21: 
 22:     public $databaseDefaults = array(
 23:         'dsn' => NULL,
 24:         'user' => NULL,
 25:         'password' => NULL,
 26:         'options' => NULL,
 27:         'debugger' => TRUE,
 28:         'explain' => TRUE,
 29:         'reflection' => 'discovered', // Nette\Database\Reflection\DiscoveredReflection
 30:         'autowired' => NULL,
 31:     );
 32: 
 33: 
 34:     public function loadConfiguration()
 35:     {
 36:         $container = $this->getContainerBuilder();
 37: 
 38:         $config = $this->compiler->getConfig();
 39:         if (isset($config['nette']['database'])) { // back compatibility
 40:             $config = $config['nette']['database'];
 41:             $prefix = 'nette.';
 42:         } else {
 43:             $config = isset($config[$this->name]) ? $config[$this->name] : array();
 44:             $prefix = '';
 45:         }
 46: 
 47:         if (isset($config['dsn'])) {
 48:             $config = array('default' => $config);
 49:         }
 50: 
 51:         $autowired = TRUE;
 52:         foreach ((array) $config as $name => $info) {
 53:             if (!is_array($info)) {
 54:                 continue;
 55:             }
 56:             $this->validate($info, $this->databaseDefaults, 'database');
 57: 
 58:             $info += array('autowired' => $autowired) + $this->databaseDefaults;
 59:             $autowired = FALSE;
 60: 
 61:             foreach ((array) $info['options'] as $key => $value) {
 62:                 if (preg_match('#^PDO::\w+\z#', $key)) {
 63:                     unset($info['options'][$key]);
 64:                     $info['options'][constant($key)] = $value;
 65:                 }
 66:             }
 67: 
 68:             $connection = $container->addDefinition($prefix . $this->prefix($name))
 69:                 ->setClass('Nette\Database\Connection', array($info['dsn'], $info['user'], $info['password'], $info['options']))
 70:                 ->setAutowired($info['autowired'])
 71:                 ->addSetup('Tracy\Debugger::getBlueScreen()->addPanel(?)', array(
 72:                     'Nette\Bridges\DatabaseTracy\ConnectionPanel::renderException'
 73:                 ));
 74: 
 75:             if (!$info['reflection']) {
 76:                 $reflection = NULL;
 77:             } elseif (is_string($info['reflection'])) {
 78:                 $reflection = new Nette\DI\Statement(preg_match('#^[a-z]+\z#', $info['reflection'])
 79:                     ? 'Nette\Database\Reflection\\' . ucfirst($info['reflection']) . 'Reflection'
 80:                     : $info['reflection'], strtolower($info['reflection']) === 'discovered' ? array($connection) : array());
 81:             } else {
 82:                 $tmp = Nette\DI\Compiler::filterArguments(array($info['reflection']));
 83:                 $reflection = reset($tmp);
 84:             }
 85: 
 86:             $container->addDefinition($prefix . $this->prefix("$name.context"))
 87:                 ->setClass('Nette\Database\Context', array($connection, $reflection))
 88:                 ->setAutowired($info['autowired']);
 89: 
 90:             if ($container->parameters['debugMode'] && $info['debugger']) {
 91:                 $connection->addSetup('Nette\Database\Helpers::createDebugPanel', array($connection, !empty($info['explain']), $name));
 92:             }
 93:         }
 94:     }
 95: 
 96: 
 97:     private function validate(array $config, array $expected, $name)
 98:     {
 99:         if ($extra = array_diff_key($config, $expected)) {
100:             $extra = implode(", $name.", array_keys($extra));
101:             throw new Nette\InvalidStateException("Unknown option $name.$extra.");
102:         }
103:     }
104: 
105: }
106: 
Nette 2.2 API documentation generated by ApiGen 2.8.0