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

  • ConfigCompiler
  • ConfigCompilerExtension
  • ConfigHelpers
  • ConfigLoader
  • Configurator

Interfaces

  • IConfigAdapter
  • 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
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Initial system DI container generator.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property   bool $debugMode
 21:  * @property-write $tempDirectory
 22:  * @package Nette\Config
 23:  */
 24: class Configurator extends Object
 25: {
 26:     /** config file sections */
 27:     const DEVELOPMENT = 'development',
 28:         PRODUCTION = 'production',
 29:         AUTO = NULL,
 30:         NONE = FALSE;
 31: 
 32:     /** @var array of function(Configurator $sender, Compiler $compiler); Occurs after the compiler is created */
 33:     public $onCompile;
 34: 
 35:     /** @var array */
 36:     protected $parameters;
 37: 
 38:     /** @var array */
 39:     protected $files = array();
 40: 
 41: 
 42: 
 43:     public function __construct()
 44:     {
 45:         $this->parameters = $this->getDefaultParameters();
 46:     }
 47: 
 48: 
 49: 
 50:     /**
 51:      * Set parameter %debugMode%.
 52:      * @param  bool|string|array
 53:      * @return Configurator  provides a fluent interface
 54:      */
 55:     public function setDebugMode($value = TRUE)
 56:     {
 57:         $this->parameters['debugMode'] = is_bool($value) ? $value : self::detectDebugMode($value);
 58:         $this->parameters['productionMode'] = !$this->parameters['debugMode']; // compatibility
 59:         return $this;
 60:     }
 61: 
 62: 
 63: 
 64:     /**
 65:      * @return bool
 66:      */
 67:     public function isDebugMode()
 68:     {
 69:         return !$this->parameters['productionMode'];
 70:     }
 71: 
 72: 
 73: 
 74:     /**
 75:      * Sets path to temporary directory.
 76:      * @return Configurator  provides a fluent interface
 77:      */
 78:     public function setTempDirectory($path)
 79:     {
 80:         $this->parameters['tempDir'] = $path;
 81:         if (($cacheDir = $this->getCacheDirectory()) && !is_dir($cacheDir)) {
 82:             mkdir($cacheDir, 0777);
 83:         }
 84:         return $this;
 85:     }
 86: 
 87: 
 88: 
 89:     /**
 90:      * Adds new parameters. The %params% will be expanded.
 91:      * @return Configurator  provides a fluent interface
 92:      */
 93:     public function addParameters(array $params)
 94:     {
 95:         $this->parameters = ConfigHelpers::merge($params, $this->parameters);
 96:         return $this;
 97:     }
 98: 
 99: 
100: 
101:     /**
102:      * @return array
103:      */
104:     protected function getDefaultParameters()
105:     {
106:         $trace = PHP_VERSION_ID < 50205 ? debug_backtrace() :debug_backtrace(FALSE);
107:         $debugMode = self::detectDebugMode();
108:         return array(
109:             'appDir' => isset($trace[1]['file']) ? dirname($trace[1]['file']) : NULL,
110:             'wwwDir' => isset($_SERVER['SCRIPT_FILENAME']) ? dirname($_SERVER['SCRIPT_FILENAME']) : NULL,
111:             'debugMode' => $debugMode,
112:             'productionMode' => !$debugMode,
113:             'environment' => $debugMode ? self::DEVELOPMENT : self::PRODUCTION,
114:             'consoleMode' => PHP_SAPI === 'cli',
115:             'container' => array(
116:                 'class' => 'SystemContainer',
117:                 'parent' => 'DIContainer',
118:             )
119:         );
120:     }
121: 
122: 
123: 
124:     /**
125:      * @param  string        error log directory
126:      * @param  string        administrator email
127:      * @return void
128:      */
129:     public function enableDebugger($logDirectory = NULL, $email = NULL)
130:     {
131:         Debugger::$strictMode = TRUE;
132:         Debugger::enable($this->parameters['productionMode'], $logDirectory, $email);
133:     }
134: 
135: 
136: 
137:     /**
138:      * @return RobotLoader
139:      */
140:     public function createRobotLoader()
141:     {
142:         if (!($cacheDir = $this->getCacheDirectory())) {
143:             throw new InvalidStateException("Set path to temporary directory using setTempDirectory().");
144:         }
145:         $loader = new RobotLoader;
146:         $loader->setCacheStorage(new FileStorage($cacheDir));
147:         $loader->autoRebuild = !$this->parameters['productionMode'];
148:         return $loader;
149:     }
150: 
151: 
152: 
153:     /**
154:      * Adds configuration file.
155:      * @return Configurator  provides a fluent interface
156:      */
157:     public function addConfig($file, $section = self::AUTO)
158:     {
159:         $this->files[] = array($file, $section === self::AUTO ? $this->parameters['environment'] : $section);
160:         return $this;
161:     }
162: 
163: 
164: 
165:     /** @deprecated */
166:     public function loadConfig($file, $section = NULL)
167:     {
168:         trigger_error(__METHOD__ . '() is deprecated; use addConfig(file, [section])->createContainer() instead.', E_USER_WARNING);
169:         return $this->addConfig($file, $section)->createContainer();
170:     }
171: 
172: 
173: 
174:     /**
175:      * Returns system DI container.
176:      * @return SystemContainer
177:      */
178:     public function createContainer()
179:     {
180:         if ($cacheDir = $this->getCacheDirectory()) {
181:             $cache = new Cache(new PhpFileStorage($cacheDir), 'Nette.Configurator');
182:             $cacheKey = array($this->parameters, $this->files);
183:             $cached = $cache->load($cacheKey);
184:             if (!$cached) {
185:                 $code = $this->buildContainer($dependencies);
186:                 $cache->save($cacheKey, $code, array(
187:                     Cache::FILES => $this->parameters['productionMode'] ? NULL : $dependencies,
188:                 ));
189:                 $cached = $cache->load($cacheKey);
190:             }
191:             LimitedScope::load($cached['file'], TRUE);
192: 
193:         } elseif ($this->files) {
194:             throw new InvalidStateException("Set path to temporary directory using setTempDirectory().");
195: 
196:         } else {
197:             LimitedScope::evaluate($this->buildContainer()); // back compatibility with Environment
198:         }
199: 
200:         $container = new $this->parameters['container']['class'];
201:         $container->initialize();
202:         Environment::setContext($container); // back compatibility
203:         return $container;
204:     }
205: 
206: 
207: 
208:     /**
209:      * Build system container class.
210:      * @return string
211:      */
212:     protected function buildContainer(& $dependencies = NULL)
213:     {
214:         $loader = $this->createLoader();
215:         $config = array();
216:         $code = "<?php\n";
217:         foreach ($this->files as $tmp) {
218:             list($file, $section) = $tmp;
219:             $config = ConfigHelpers::merge($loader->load($file, $section), $config);
220:             $code .= "// source: $file $section\n";
221:         }
222:         $code .= "\n";
223: 
224:         $this->checkCompatibility($config);
225: 
226:         if (!isset($config['parameters'])) {
227:             $config['parameters'] = array();
228:         }
229:         $config['parameters'] = ConfigHelpers::merge($config['parameters'], $this->parameters);
230: 
231:         $compiler = $this->createCompiler();
232:         $this->onCompile($this, $compiler);
233: 
234:         $code .= $compiler->compile(
235:             $config,
236:             $this->parameters['container']['class'],
237:             $config['parameters']['container']['parent']
238:         );
239:         $dependencies = array_merge($loader->getDependencies(), $compiler->getContainerBuilder()->getDependencies());
240:         return $code;
241:     }
242: 
243: 
244: 
245:     protected function checkCompatibility(array $config)
246:     {
247:         foreach (array('service' => 'services', 'variable' => 'parameters', 'variables' => 'parameters', 'mode' => 'parameters', 'const' => 'constants') as $old => $new) {
248:             if (isset($config[$old])) {
249:                 throw new DeprecatedException("Section '$old' in configuration file is deprecated; use '$new' instead.");
250:             }
251:         }
252:         if (isset($config['services'])) {
253:             foreach ($config['services'] as $key => $def) {
254:                 foreach (array('option' => 'arguments', 'methods' => 'setup') as $old => $new) {
255:                     if (is_array($def) && isset($def[$old])) {
256:                         throw new DeprecatedException("Section '$old' in service definition is deprecated; refactor it into '$new'.");
257:                     }
258:                 }
259:             }
260:         }
261:     }
262: 
263: 
264: 
265:     /**
266:      * @return ConfigCompiler
267:      */
268:     protected function createCompiler()
269:     {
270:         $compiler = new ConfigCompiler;
271:         $compiler->addExtension('php', new PhpExtension)
272:             ->addExtension('constants', new ConstantsExtension)
273:             ->addExtension('nette', new NetteExtension);
274:         return $compiler;
275:     }
276: 
277: 
278: 
279:     /**
280:      * @return ConfigLoader
281:      */
282:     protected function createLoader()
283:     {
284:         return new ConfigLoader;
285:     }
286: 
287: 
288: 
289:     protected function getCacheDirectory()
290:     {
291:         return empty($this->parameters['tempDir']) ? NULL : $this->parameters['tempDir'] . '/cache';
292:     }
293: 
294: 
295: 
296:     /********************* tools ****************d*g**/
297: 
298: 
299: 
300:     /**
301:      * Detects debug mode by IP address.
302:      * @param  string|array  IP addresses or computer names whitelist detection
303:      * @return bool
304:      */
305:     public static function detectDebugMode($list = NULL)
306:     {
307:         $list = is_string($list) ? preg_split('#[,\s]+#', $list) : $list;
308:         $list[] = '127.0.0.1';
309:         $list[] = '::1';
310:         return in_array(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : php_uname('n'), $list, TRUE);
311:     }
312: 
313: 
314: 
315:     /** @deprecated */
316:     public function setProductionMode($value = TRUE)
317:     {
318:         return $this->setDebugMode(is_bool($value) ? !$value : $value);
319:     }
320: 
321: 
322: 
323:     /** @deprecated */
324:     public function isProductionMode()
325:     {
326:         return !$this->isDebugMode();
327:     }
328: 
329: 
330: 
331:     /** @deprecated */
332:     public static function detectProductionMode($list = NULL)
333:     {
334:         return !self::detectDebugMode($list);
335:     }
336: 
337: }
338: 
Nette Framework 2.0.3 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0