Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
      • Config\Extensions
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
      • DI\Diagnostics
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Security\Diagnostics
    • Templating
    • Utils
      • Utils\PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • ConfigCompiler
  • ConfigCompilerExtension
  • 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 $productionMode
 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 %productionMode%.
 52:      * @param  bool
 53:      * @return Configurator  provides a fluent interface
 54:      */
 55:     public function setProductionMode($on = TRUE)
 56:     {
 57:         $this->parameters['productionMode'] = (bool) $on;
 58:         return $this;
 59:     }
 60: 
 61: 
 62: 
 63:     /**
 64:      * @return bool
 65:      */
 66:     public function isProductionMode()
 67:     {
 68:         return $this->parameters['productionMode'];
 69:     }
 70: 
 71: 
 72: 
 73:     /**
 74:      * Sets path to temporary directory.
 75:      * @return Configurator  provides a fluent interface
 76:      */
 77:     public function setTempDirectory($path)
 78:     {
 79:         $this->parameters['tempDir'] = $path;
 80:         if (($cacheDir = $this->getCacheDirectory()) && !is_dir($cacheDir)) {
 81:             mkdir($cacheDir, 0777);
 82:         }
 83:         return $this;
 84:     }
 85: 
 86: 
 87: 
 88:     /**
 89:      * Adds new parameters. The %params% will be expanded.
 90:      * @return Configurator  provides a fluent interface
 91:      */
 92:     public function addParameters(array $params)
 93:     {
 94:         $this->parameters = ConfigHelpers::merge($params, $this->parameters);
 95:         return $this;
 96:     }
 97: 
 98: 
 99: 
100:     /**
101:      * @return array
102:      */
103:     protected function getDefaultParameters()
104:     {
105:         $trace = debug_backtrace(FALSE);
106:         return array(
107:             'appDir' => isset($trace[1]['file']) ? dirname($trace[1]['file']) : NULL,
108:             'wwwDir' => isset($_SERVER['SCRIPT_FILENAME']) ? dirname($_SERVER['SCRIPT_FILENAME']) : NULL,
109:             'productionMode' => self::detectProductionMode(),
110:             'consoleMode' => PHP_SAPI === 'cli',
111:             'container' => array(
112:                 'class' => 'SystemContainer',
113:                 'parent' => 'DIContainer',
114:             )
115:         );
116:     }
117: 
118: 
119: 
120:     /**
121:      * @return RobotLoader
122:      */
123:     public function createRobotLoader()
124:     {
125:         if (!($cacheDir = $this->getCacheDirectory())) {
126:             throw new InvalidStateException("Set path to temporary directory using setTempDirectory().");
127:         }
128:         $loader = new RobotLoader;
129:         $loader->setCacheStorage(new FileStorage($cacheDir));
130:         $loader->autoRebuild = !$this->parameters['productionMode'];
131:         return $loader;
132:     }
133: 
134: 
135: 
136:     /**
137:      * Adds configuration file.
138:      * @return Configurator  provides a fluent interface
139:      */
140:     public function addConfig($file, $section = self::AUTO)
141:     {
142:         if ($section === self::AUTO) {
143:             $section = $this->parameters['productionMode'] ? self::PRODUCTION : self::DEVELOPMENT;
144:         }
145:         $this->files[] = array($file, $section);
146:         return $this;
147:     }
148: 
149: 
150: 
151:     /** @deprecated */
152:     public function loadConfig($file, $section = NULL)
153:     {
154:         trigger_error(__METHOD__ . '() is deprecated; use addConfig(file, [section])->createContainer() instead.', E_USER_WARNING);
155:         return $this->addConfig($file, $section)->createContainer();
156:     }
157: 
158: 
159: 
160:     /**
161:      * Returns system DI container.
162:      * @return SystemContainer
163:      */
164:     public function createContainer()
165:     {
166:         if ($cacheDir = $this->getCacheDirectory()) {
167:             $cache = new Cache(new PhpFileStorage($cacheDir), 'Nette.Configurator');
168:             $cacheKey = array($this->parameters, $this->files);
169:             $cached = $cache->load($cacheKey);
170:             if (!$cached) {
171:                 $code = $this->buildContainer($dependencies);
172:                 $cache->save($cacheKey, $code, array(
173:                     Cache::FILES => $this->parameters['productionMode'] ? NULL : $dependencies,
174:                 ));
175:                 $cached = $cache->load($cacheKey);
176:             }
177:             LimitedScope::load($cached['file'], TRUE);
178: 
179:         } elseif ($this->files) {
180:             throw new InvalidStateException("Set path to temporary directory using setTempDirectory().");
181: 
182:         } else {
183:             LimitedScope::evaluate($this->buildContainer()); // back compatibility with Environment
184:         }
185: 
186:         $container = new $this->parameters['container']['class'];
187:         $container->initialize();
188:         Environment::setContext($container); // back compatibility
189:         return $container;
190:     }
191: 
192: 
193: 
194:     /**
195:      * Build system container class.
196:      * @return string
197:      */
198:     protected function buildContainer(& $dependencies = NULL)
199:     {
200:         $loader = $this->createLoader();
201:         $config = array();
202:         $code = "<?php\n";
203:         foreach ($this->files as $tmp) {
204:             list($file, $section) = $tmp;
205:             $config = ConfigHelpers::merge($loader->load($file, $section), $config);
206:             $code .= "// source: $file $section\n";
207:         }
208:         $code .= "\n";
209: 
210:         $this->checkCompatibility($config);
211: 
212:         if (!isset($config['parameters'])) {
213:             $config['parameters'] = array();
214:         }
215:         $config['parameters'] = ConfigHelpers::merge($config['parameters'], $this->parameters);
216: 
217:         $compiler = $this->createCompiler();
218:         $this->onCompile($this, $compiler);
219: 
220:         $code .= $compiler->compile(
221:             $config,
222:             $this->parameters['container']['class'],
223:             $config['parameters']['container']['parent']
224:         );
225:         $dependencies = array_merge($loader->getDependencies(), $compiler->getContainerBuilder()->getDependencies());
226:         return $code;
227:     }
228: 
229: 
230: 
231:     protected function checkCompatibility(array $config)
232:     {
233:         foreach (array('service' => 'services', 'variable' => 'parameters', 'variables' => 'parameters', 'mode' => 'parameters', 'const' => 'constants') as $old => $new) {
234:             if (isset($config[$old])) {
235:                 throw new DeprecatedException("Section '$old' in configuration file is deprecated; use '$new' instead.");
236:             }
237:         }
238:         if (isset($config['services'])) {
239:             foreach ($config['services'] as $key => $def) {
240:                 foreach (array('option' => 'arguments', 'methods' => 'setup') as $old => $new) {
241:                     if (is_array($def) && isset($def[$old])) {
242:                         throw new DeprecatedException("Section '$old' in service definition is deprecated; refactor it into '$new'.");
243:                     }
244:                 }
245:             }
246:         }
247:     }
248: 
249: 
250: 
251:     /**
252:      * @return ConfigCompiler
253:      */
254:     protected function createCompiler()
255:     {
256:         $compiler = new ConfigCompiler;
257:         $compiler->addExtension('php', new PhpExtension)
258:             ->addExtension('constants', new ConstantsExtension)
259:             ->addExtension('nette', new NetteExtension);
260:         return $compiler;
261:     }
262: 
263: 
264: 
265:     /**
266:      * @return ConfigLoader
267:      */
268:     protected function createLoader()
269:     {
270:         return new ConfigLoader;
271:     }
272: 
273: 
274: 
275:     protected function getCacheDirectory()
276:     {
277:         return empty($this->parameters['tempDir']) ? NULL : $this->parameters['tempDir'] . '/cache';
278:     }
279: 
280: 
281: 
282:     /********************* tools ****************d*g**/
283: 
284: 
285: 
286:     /**
287:      * Detects production mode by IP address.
288:      * @return bool
289:      */
290:     public static function detectProductionMode()
291:     {
292:         return !isset($_SERVER['REMOTE_ADDR']) || !in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'), TRUE);
293:     }
294: 
295: }
296: 
Nette Framework 2.0beta2 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0