Namespaces

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

Classes

  • DatabaseExtension
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Bridges\DatabaseDI;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Nette Framework Database services.
 15:  */
 16: class DatabaseExtension extends Nette\DI\CompilerExtension
 17: {
 18:     public $databaseDefaults = [
 19:         'dsn' => NULL,
 20:         'user' => NULL,
 21:         'password' => NULL,
 22:         'options' => NULL,
 23:         'debugger' => TRUE,
 24:         'explain' => TRUE,
 25:         'reflection' => NULL, // BC
 26:         'conventions' => 'discovered', // Nette\Database\Conventions\DiscoveredConventions
 27:         'autowired' => NULL,
 28:     ];
 29: 
 30:     /** @var bool */
 31:     private $debugMode;
 32: 
 33: 
 34:     public function __construct($debugMode = FALSE)
 35:     {
 36:         $this->debugMode = $debugMode;
 37:     }
 38: 
 39: 
 40:     public function loadConfiguration()
 41:     {
 42:         $configs = $this->getConfig();
 43:         foreach ($configs as $k => $v) {
 44:             if (is_scalar($v)) {
 45:                 $configs = ['default' => $configs];
 46:                 break;
 47:             }
 48:         }
 49: 
 50:         $defaults = $this->databaseDefaults;
 51:         $defaults['autowired'] = TRUE;
 52:         foreach ((array) $configs as $name => $config) {
 53:             if (!is_array($config)) {
 54:                 continue;
 55:             }
 56:             $config = $this->validateConfig($defaults, $config, $this->prefix($name));
 57:             $defaults['autowired'] = FALSE;
 58:             $this->setupDatabase($config, $name);
 59:         }
 60:     }
 61: 
 62: 
 63:     private function setupDatabase($config, $name)
 64:     {
 65:         $builder = $this->getContainerBuilder();
 66: 
 67:         foreach ((array) $config['options'] as $key => $value) {
 68:             if (preg_match('#^PDO::\w+\z#', $key)) {
 69:                 unset($config['options'][$key]);
 70:                 $config['options'][constant($key)] = $value;
 71:             }
 72:         }
 73: 
 74:         $connection = $builder->addDefinition($this->prefix("$name.connection"))
 75:             ->setClass(Nette\Database\Connection::class, [$config['dsn'], $config['user'], $config['password'], $config['options']])
 76:             ->setAutowired($config['autowired']);
 77: 
 78:         $structure = $builder->addDefinition($this->prefix("$name.structure"))
 79:             ->setClass(Nette\Database\Structure::class)
 80:             ->setArguments([$connection])
 81:             ->setAutowired($config['autowired']);
 82: 
 83:         if (!empty($config['reflection'])) {
 84:             $conventionsServiceName = 'reflection';
 85:             $config['conventions'] = $config['reflection'];
 86:             if (is_string($config['conventions']) && strtolower($config['conventions']) === 'conventional') {
 87:                 $config['conventions'] = 'Static';
 88:             }
 89:         } else {
 90:             $conventionsServiceName = 'conventions';
 91:         }
 92: 
 93:         if (!$config['conventions']) {
 94:             $conventions = NULL;
 95: 
 96:         } elseif (is_string($config['conventions'])) {
 97:             $conventions = $builder->addDefinition($this->prefix("$name.$conventionsServiceName"))
 98:                 ->setClass(preg_match('#^[a-z]+\z#i', $config['conventions'])
 99:                     ? 'Nette\Database\Conventions\\' . ucfirst($config['conventions']) . 'Conventions'
100:                     : $config['conventions'])
101:                 ->setArguments(strtolower($config['conventions']) === 'discovered' ? [$structure] : [])
102:                 ->setAutowired($config['autowired']);
103: 
104:         } else {
105:             $tmp = Nette\DI\Compiler::filterArguments([$config['conventions']]);
106:             $conventions = reset($tmp);
107:         }
108: 
109:         $builder->addDefinition($this->prefix("$name.context"))
110:             ->setClass(Nette\Database\Context::class, [$connection, $structure, $conventions])
111:             ->setAutowired($config['autowired']);
112: 
113:         if ($config['debugger']) {
114:             $connection->addSetup('@Tracy\BlueScreen::addPanel', [
115:                 'Nette\Bridges\DatabaseTracy\ConnectionPanel::renderException'
116:             ]);
117:             if ($this->debugMode) {
118:                 $connection->addSetup('Nette\Database\Helpers::createDebugPanel', [$connection, !empty($config['explain']), $name]);
119:             }
120:         }
121: 
122:         if ($this->name === 'database') {
123:             $builder->addAlias($this->prefix($name), $this->prefix("$name.connection"));
124:             $builder->addAlias("nette.database.$name", $this->prefix($name));
125:             $builder->addAlias("nette.database.$name.context", $this->prefix("$name.context"));
126:         }
127:     }
128: 
129: }
130: 
Nette 2.4-20161109 API API documentation generated by ApiGen 2.8.0