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

  • Callback
  • Configurator
  • Environment
  • Framework
  • FreezableObject
  • Object

Interfaces

  • IFreezable

Exceptions

  • ArgumentOutOfRangeException
  • DeprecatedException
  • DirectoryNotFoundException
  • FileNotFoundException
  • InvalidArgumentException
  • InvalidStateException
  • IOException
  • MemberAccessException
  • NotImplementedException
  • NotSupportedException
  • OutOfRangeException
  • StaticClassException
  • UnexpectedValueException
  • 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;
  9: 
 10: use Nette;
 11: use Nette\DI;
 12: use Tracy;
 13: 
 14: 
 15: /**
 16:  * Initial system DI container generator.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property   bool $debugMode
 21:  * @property-write $tempDirectory
 22:  */
 23: class Configurator extends Object
 24: {
 25:     const AUTO = TRUE;
 26: 
 27:     /** @deprecated */
 28:     const DEVELOPMENT = 'development',
 29:         PRODUCTION = 'production',
 30:         NONE = FALSE;
 31: 
 32:     const COOKIE_SECRET = 'nette-debug';
 33: 
 34:     /** @var callable[]  function (Configurator $sender, DI\Compiler $compiler); Occurs after the compiler is created */
 35:     public $onCompile;
 36: 
 37:     /** @var array */
 38:     public $defaultExtensions = array(
 39:         'php' => 'Nette\DI\Extensions\PhpExtension',
 40:         'constants' => 'Nette\DI\Extensions\ConstantsExtension',
 41:         'nette' => 'Nette\Bridges\Framework\NetteExtension',
 42:         'database' => 'Nette\Bridges\DatabaseDI\DatabaseExtension',
 43:         'extensions' => 'Nette\DI\Extensions\ExtensionsExtension',
 44:     );
 45: 
 46:     /** @var array */
 47:     protected $parameters;
 48: 
 49:     /** @var array */
 50:     protected $files = array();
 51: 
 52: 
 53:     public function __construct()
 54:     {
 55:         $this->parameters = $this->getDefaultParameters();
 56:     }
 57: 
 58: 
 59:     /**
 60:      * Set parameter %debugMode%.
 61:      * @param  bool|string|array
 62:      * @return self
 63:      */
 64:     public function setDebugMode($value)
 65:     {
 66:         if (is_string($value) || is_array($value)) {
 67:             $value = static::detectDebugMode($value);
 68:         } elseif (!is_bool($value)) {
 69:             throw new Nette\InvalidArgumentException(sprintf('Value must be either a string, array, or boolean, %s given.', gettype($value)));
 70:         }
 71:         $this->parameters['debugMode'] = $value;
 72:         $this->parameters['productionMode'] = !$this->parameters['debugMode']; // compatibility
 73:         return $this;
 74:     }
 75: 
 76: 
 77:     /**
 78:      * @return bool
 79:      */
 80:     public function isDebugMode()
 81:     {
 82:         return $this->parameters['debugMode'];
 83:     }
 84: 
 85: 
 86:     /**
 87:      * Sets path to temporary directory.
 88:      * @return self
 89:      */
 90:     public function setTempDirectory($path)
 91:     {
 92:         $this->parameters['tempDir'] = $path;
 93:         return $this;
 94:     }
 95: 
 96: 
 97:     /**
 98:      * Adds new parameters. The %params% will be expanded.
 99:      * @return self
100:      */
101:     public function addParameters(array $params)
102:     {
103:         $this->parameters = DI\Config\Helpers::merge($params, $this->parameters);
104:         return $this;
105:     }
106: 
107: 
108:     /**
109:      * @return array
110:      */
111:     protected function getDefaultParameters()
112:     {
113:         $trace = debug_backtrace(PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_IGNORE_ARGS : FALSE);
114:         $debugMode = static::detectDebugMode();
115:         return array(
116:             'appDir' => isset($trace[1]['file']) ? dirname($trace[1]['file']) : NULL,
117:             'wwwDir' => isset($_SERVER['SCRIPT_FILENAME'])
118:                 ? dirname(realpath($_SERVER['SCRIPT_FILENAME']))
119:                 : NULL,
120:             'debugMode' => $debugMode,
121:             'productionMode' => !$debugMode,
122:             'environment' => $debugMode ? 'development' : 'production',
123:             'consoleMode' => PHP_SAPI === 'cli',
124:             'container' => array(
125:                 'class' => 'SystemContainer',
126:                 'parent' => 'Nette\DI\Container',
127:             ),
128:         );
129:     }
130: 
131: 
132:     /**
133:      * @param  string        error log directory
134:      * @param  string        administrator email
135:      * @return void
136:      */
137:     public function enableDebugger($logDirectory = NULL, $email = NULL)
138:     {
139:         Tracy\Debugger::$strictMode = TRUE;
140:         Tracy\Debugger::enable(!$this->parameters['debugMode'], $logDirectory, $email);
141:         Nette\Bridges\Framework\TracyBridge::initialize();
142:     }
143: 
144: 
145:     /**
146:      * @return Nette\Loaders\RobotLoader
147:      * @throws Nette\NotSupportedException if RobotLoader is not available
148:      */
149:     public function createRobotLoader()
150:     {
151:         if (!class_exists('Nette\Loaders\RobotLoader')) {
152:             throw new Nette\NotSupportedException('RobotLoader not found, do you have `nette/robot-loader` package installed?');
153:         }
154: 
155:         $loader = new Nette\Loaders\RobotLoader;
156:         $loader->setCacheStorage(new Nette\Caching\Storages\FileStorage($this->getCacheDirectory()));
157:         $loader->autoRebuild = $this->parameters['debugMode'];
158:         return $loader;
159:     }
160: 
161: 
162:     /**
163:      * Adds configuration file.
164:      * @return self
165:      */
166:     public function addConfig($file, $section = NULL)
167:     {
168:         if ($section === NULL && is_string($file) && $this->parameters['debugMode']) { // back compatibility
169:             try {
170:                 $loader = new DI\Config\Loader;
171:                 $loader->load($file, $this->parameters['environment']);
172:                 trigger_error("Config file '$file' has sections, call addConfig() with second parameter Configurator::AUTO.", E_USER_WARNING);
173:                 $section = $this->parameters['environment'];
174:             } catch (\Exception $e) {
175:             }
176:         }
177:         $this->files[] = array($file, $section === self::AUTO ? $this->parameters['environment'] : $section);
178:         return $this;
179:     }
180: 
181: 
182:     /**
183:      * Returns system DI container.
184:      * @return \SystemContainer|DI\Container
185:      */
186:     public function createContainer()
187:     {
188:         $container = $this->createContainerFactory()->create();
189:         $container->initialize();
190:         if (class_exists('Nette\Environment')) {
191:             Nette\Environment::setContext($container); // back compatibility
192:         }
193:         return $container;
194:     }
195: 
196: 
197:     /**
198:      * @return DI\ContainerFactory
199:      */
200:     protected function createContainerFactory()
201:     {
202:         $factory = new DI\ContainerFactory(NULL);
203:         $factory->autoRebuild = $this->parameters['debugMode'] ? TRUE : 'compat';
204:         $factory->class = $this->parameters['container']['class'];
205:         $factory->config = array('parameters' => $this->parameters);
206:         $factory->configFiles = $this->files;
207:         $factory->tempDirectory = $this->getCacheDirectory() . '/Nette.Configurator';
208:         if (!is_dir($factory->tempDirectory)) {
209:             @mkdir($factory->tempDirectory); // @ - directory may already exist
210:         }
211: 
212:         $me = $this;
213:         $factory->onCompile[] = function (DI\ContainerFactory $factory, DI\Compiler $compiler, $config) use ($me) {
214:             foreach ($me->defaultExtensions as $name => $class) {
215:                 if (class_exists($class)) {
216:                     $compiler->addExtension($name, new $class);
217:                 }
218:             }
219:             $factory->parentClass = $config['parameters']['container']['parent'];
220:             $me->onCompile($me, $compiler);
221:         };
222:         return $factory;
223:     }
224: 
225: 
226:     protected function getCacheDirectory()
227:     {
228:         if (empty($this->parameters['tempDir'])) {
229:             throw new Nette\InvalidStateException('Set path to temporary directory using setTempDirectory().');
230:         }
231:         $dir = $this->parameters['tempDir'] . '/cache';
232:         if (!is_dir($dir)) {
233:             @mkdir($dir); // @ - directory may already exist
234:         }
235:         return $dir;
236:     }
237: 
238: 
239:     /********************* tools ****************d*g**/
240: 
241: 
242:     /**
243:      * Detects debug mode by IP address.
244:      * @param  string|array  IP addresses or computer names whitelist detection
245:      * @return bool
246:      */
247:     public static function detectDebugMode($list = NULL)
248:     {
249:         $addr = isset($_SERVER['REMOTE_ADDR'])
250:             ? $_SERVER['REMOTE_ADDR']
251:             : php_uname('n');
252:         $secret = isset($_COOKIE[self::COOKIE_SECRET]) && is_string($_COOKIE[self::COOKIE_SECRET])
253:             ? $_COOKIE[self::COOKIE_SECRET]
254:             : NULL;
255:         $list = is_string($list)
256:             ? preg_split('#[,\s]+#', $list)
257:             : (array) $list;
258:         if (!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
259:             $list[] = '127.0.0.1';
260:             $list[] = '::1';
261:         }
262:         return in_array($addr, $list, TRUE) || in_array("$secret@$addr", $list, TRUE);
263:     }
264: 
265: }
266: 
Nette 2.2 API documentation generated by ApiGen 2.8.0