<pre> <?php require_once '../../Nette/loader.php'; /*use Nette\Debug;*/ /*use Nette\Environment;*/ Debug::$time = 1201042800; $_SERVER = array_intersect_key($_SERVER, array('PHP_SELF' => 1, 'SCRIPT_NAME' => 1, 'SERVER_ADDR' => 1, 'SERVER_SOFTWARE' => 1, 'HTTP_HOST' => 1, 'DOCUMENT_ROOT' => 1)); echo "Debug::\$productionMode\n"; Debug::dump(Debug::$productionMode); echo "setting production environment...\n"; Environment::setMode('production', TRUE); Debug::enable(); echo "Debug::\$productionMode\n"; echo Debug::dump(Debug::$productionMode, TRUE);
/** * Loads global configuration from file and process it. * @param string|Nette\Config\Config file name or Config object * @param bool * @return Nette\Config\Config */ public function loadConfig($file, $useCache) { if ($useCache === NULL) { $useCache = Environment::isLive(); } $cache = $useCache && $this->cacheKey ? Environment::getCache('Nette.Environment') : NULL; $name = Environment::getName(); $cacheKey = Environment::expand($this->cacheKey); if (isset($cache[$cacheKey])) { Environment::swapState($cache[$cacheKey]); $config = Environment::getConfig(); } else { if ($file instanceof Config) { $config = $file; $file = NULL; } else { if ($file === NULL) { $file = $this->defaultConfigFile; } $file = Environment::expand($file); $config = Config::fromFile($file, $name, 0); } // process environment variables if ($config->variable instanceof Config) { foreach ($config->variable as $key => $value) { Environment::setVariable($key, $value); } } if (PATH_SEPARATOR !== ';' && isset($config->set->include_path)) { $config->set->include_path = str_replace(';', PATH_SEPARATOR, $config->set->include_path); } $config->expand(); $config->setReadOnly(); // process services $locator = Environment::getServiceLocator(); if ($config->service instanceof Config) { foreach ($config->service as $key => $value) { $locator->addService($value, strtr($key, '-', '\\')); } } // save cache if ($cache) { $state = Environment::swapState(NULL); $state[0] = $config; // TODO: better! $cache->save($cacheKey, $state, array(Cache::FILES => $file)); } } // check temporary directory - TODO: discuss /* $dir = Environment::getVariable('tempDir'); if ($dir && !(is_dir($dir) && is_writable($dir))) { trigger_error("Temporary directory '$dir' is not writable", E_USER_NOTICE); } */ // process ini settings if ($config->set instanceof Config) { foreach ($config->set as $key => $value) { $key = strtr($key, '-', '.'); if (function_exists('ini_set')) { ini_set($key, $value); } else { switch ($key) { case 'include_path': set_include_path($value); break; case 'iconv.internal_encoding': iconv_set_encoding('internal_encoding', $value); break; case 'mbstring.internal_encoding': mb_internal_encoding($value); break; case 'date.timezone': date_default_timezone_set($value); break; case 'error_reporting': error_reporting($value); break; case 'ignore_user_abort': ignore_user_abort($value); break; case 'max_execution_time': set_time_limit($value); break; default: throw new NotSupportedException('Required function ini_set() is disabled.'); } } } } // define constants if ($config->const instanceof Config) { foreach ($config->const as $key => $value) { define($key, $value); } } // set modes if (isset($config->mode)) { foreach ($config->mode as $mode => $state) { Environment::setMode($mode, $state); } } return $config; }
/** * Loads global configuration from file and process it. * @param string|Config file name or Config object * @return Config */ public function loadConfig($file) { $name = Environment::getName(); if ($file instanceof Config) { $config = $file; $file = NULL; } else { if ($file === NULL) { $file = $this->defaultConfigFile; } $file = Environment::expand($file); $config = Config::fromFile($file, $name, 0); } // process environment variables if ($config->variable instanceof Config) { foreach ($config->variable as $key => $value) { Environment::setVariable($key, $value); } } $config->expand(); // process services $runServices = array(); $locator = Environment::getServiceLocator(); if ($config->service instanceof Config) { foreach ($config->service as $key => $value) { $key = strtr($key, '-', '\\'); // limited INI chars if (is_string($value)) { $locator->removeService($key); $locator->addService($key, $value); } else { if ($value->factory) { $locator->removeService($key); $locator->addService($key, $value->factory, isset($value->singleton) ? $value->singleton : TRUE, (array) $value->option); } if ($value->run) { $runServices[] = $key; } } } } // process ini settings if (!$config->php) { // backcompatibility $config->php = $config->set; unset($config->set); } if ($config->php instanceof Config) { if (PATH_SEPARATOR !== ';' && isset($config->php->include_path)) { $config->php->include_path = str_replace(';', PATH_SEPARATOR, $config->php->include_path); } foreach ($config->php as $key => $value) { // flatten INI dots if ($value instanceof Config) { unset($config->php->{$key}); foreach ($value as $k => $v) { $config->php->{"{$key}.{$k}"} = $v; } } } foreach ($config->php as $key => $value) { $key = strtr($key, '-', '.'); // backcompatibility if (!is_scalar($value)) { throw new InvalidStateException("Configuration value for directive '{$key}' is not scalar."); } if ($key === 'date.timezone') { // PHP bug #47466 date_default_timezone_set($value); } if (function_exists('ini_set')) { ini_set($key, $value); } else { switch ($key) { case 'include_path': set_include_path($value); break; case 'iconv.internal_encoding': iconv_set_encoding('internal_encoding', $value); break; case 'mbstring.internal_encoding': mb_internal_encoding($value); break; case 'date.timezone': date_default_timezone_set($value); break; case 'error_reporting': error_reporting($value); break; case 'ignore_user_abort': ignore_user_abort($value); break; case 'max_execution_time': set_time_limit($value); break; default: if (ini_get($key) != $value) { // intentionally == throw new NotSupportedException('Required function ini_set() is disabled.'); } } } } } // define constants if ($config->const instanceof Config) { foreach ($config->const as $key => $value) { define($key, $value); } } // set modes if (isset($config->mode)) { foreach ($config->mode as $mode => $state) { Environment::setMode($mode, $state); } } // auto-start services foreach ($runServices as $name) { $locator->getService($name); } $config->freeze(); return $config; }
<?php /** * My NApplication bootstrap file. * * @copyright Copyright (c) 2010 John Doe * @package MyApplication */ // Step 1: Load Nette Framework // this allows load Nette Framework classes automatically so that // you don't have to litter your code with 'require' statements require LIBS_DIR . '/Nette/loader.php'; Environment::setMode('production', FALSE); // Step 2: Configure environment // 2a) enable NDebug for better exception and error visualisation Debug::enable(); // 2b) load configuration from config.ini file Environment::loadConfig(); // Step 3: Configure application // 3a) get and setup a front controller $application = Environment::getApplication(); $application->errorPresenter = 'Error'; //$application->catchExceptions = TRUE; require_once 'db.php'; dibi::connect(array('driver' => 'mysql', 'host' => 'porthos.wsolution.cz', 'username' => $user_mysql, 'password' => $pass_mysql, 'database' => $db_mysql, 'charset' => 'utf8')); // Step 4: Setup application router $router = $application->getRouter(); // mod_rewrite detection if (function_exists('apache_get_modules') && in_array('mod_rewrite', apache_get_modules())) { $router[] = new Route('index.php', array('module' => 'Front', 'presenter' => 'Default'), Route::ONE_WAY); $router[] = new Route('<module>/<presenter>/<action>/<id>', array('module' => 'Front', 'presenter' => 'Default', 'action' => 'default', 'id' => NULL));
<h1>Nette\Environment mode test</h1> <pre> <?php require_once '../../Nette/loader.php'; /*use Nette\Debug;*/ /*use Nette\Environment;*/ echo "Is console?\n"; Debug::dump(Environment::isConsole()); echo "Is production mode?\n"; Debug::dump(Environment::isProduction()); define('DEBUG_MODE', FALSE); echo "Is debugging?\n"; Debug::dump(Environment::isDebugging()); echo "Setting mode...\n"; Environment::setMode('debug', 123); echo "Is debugging?\n"; Debug::dump(Environment::isDebugging());
function loadConfig($file) { $name = Environment::getName(); if ($file instanceof Config) { $config = $file; $file = NULL; } else { if ($file === NULL) { $file = $this->defaultConfigFile; } $file = Environment::expand($file); $config = Config::fromFile($file, $name); } if ($config->variable instanceof Config) { foreach ($config->variable as $key => $value) { Environment::setVariable($key, $value); } } $iterator = new RecursiveIteratorIterator($config); foreach ($iterator as $key => $value) { $tmp = $iterator->getDepth() ? $iterator->getSubIterator($iterator->getDepth() - 1)->current() : $config; $tmp[$key] = Environment::expand($value); } $runServices = array(); $locator = Environment::getServiceLocator(); if ($config->service instanceof Config) { foreach ($config->service as $key => $value) { $key = strtr($key, '-', '\\'); if (is_string($value)) { $locator->removeService($key); $locator->addService($key, $value); } else { if ($value->factory) { $locator->removeService($key); $locator->addService($key, $value->factory, isset($value->singleton) ? $value->singleton : TRUE, (array) $value->option); } if ($value->run) { $runServices[] = $key; } } } } if (!$config->php) { $config->php = $config->set; unset($config->set); } if ($config->php instanceof Config) { if (PATH_SEPARATOR !== ';' && isset($config->php->include_path)) { $config->php->include_path = str_replace(';', PATH_SEPARATOR, $config->php->include_path); } foreach (clone $config->php as $key => $value) { if ($value instanceof Config) { unset($config->php->{$key}); foreach ($value as $k => $v) { $config->php->{"{$key}.{$k}"} = $v; } } } foreach ($config->php as $key => $value) { $key = strtr($key, '-', '.'); if (!is_scalar($value)) { throw new InvalidStateException("Configuration value for directive '{$key}' is not scalar."); } if ($key === 'date.timezone') { date_default_timezone_set($value); } if (function_exists('ini_set')) { ini_set($key, $value); } else { switch ($key) { case 'include_path': set_include_path($value); break; case 'iconv.internal_encoding': iconv_set_encoding('internal_encoding', $value); break; case 'mbstring.internal_encoding': mb_internal_encoding($value); break; case 'date.timezone': date_default_timezone_set($value); break; case 'error_reporting': error_reporting($value); break; case 'ignore_user_abort': ignore_user_abort($value); break; case 'max_execution_time': set_time_limit($value); break; default: if (ini_get($key) != $value) { throw new NotSupportedException('Required function ini_set() is disabled.'); } } } } } if ($config->const instanceof Config) { foreach ($config->const as $key => $value) { define($key, $value); } } if (isset($config->mode)) { foreach ($config->mode as $mode => $state) { Environment::setMode($mode, $state); } } foreach ($runServices as $name) { $locator->getService($name); } return $config; }
/** * Loads global configuration from file and process it. * @param string|Nette\Config\Config file name or Config object * @return Config */ public function loadConfig($file) { $name = Environment::getName(); if ($file instanceof Config) { $config = $file; $file = NULL; } else { if ($file === NULL) { $file = $this->defaultConfigFile; } $file = Environment::expand($file); $config = Config::fromFile($file, $name, 0); } // process environment variables if ($config->variable instanceof Config) { foreach ($config->variable as $key => $value) { Environment::setVariable($key, $value); } } $config->expand(); // process services $locator = Environment::getServiceLocator(); if ($config->service instanceof Config) { foreach ($config->service as $key => $value) { $locator->addService($value, strtr($key, '-', '\\')); } } // check temporary directory - TODO: discuss /* $dir = Environment::getVariable('tempDir'); if ($dir && !(is_dir($dir) && is_writable($dir))) { trigger_error("Temporary directory '$dir' is not writable", E_USER_NOTICE); } */ // process ini settings if ($config->set instanceof Config) { if (PATH_SEPARATOR !== ';' && isset($config->set->include_path)) { $config->set->include_path = str_replace(';', PATH_SEPARATOR, $config->set->include_path); } foreach ($config->set as $key => $value) { $key = strtr($key, '-', '.'); // old INI compatibility if (!is_scalar($value)) { throw new InvalidStateException("Configuration value for directive '{$key}' is not scalar."); } if (function_exists('ini_set')) { ini_set($key, $value); } else { switch ($key) { case 'include_path': set_include_path($value); break; case 'iconv.internal_encoding': iconv_set_encoding('internal_encoding', $value); break; case 'mbstring.internal_encoding': mb_internal_encoding($value); break; case 'date.timezone': date_default_timezone_set($value); break; case 'error_reporting': error_reporting($value); break; case 'ignore_user_abort': ignore_user_abort($value); break; case 'max_execution_time': set_time_limit($value); break; default: if (ini_get($key) != $value) { // intentionally == throw new NotSupportedException('Required function ini_set() is disabled.'); } } } } } // define constants if ($config->const instanceof Config) { foreach ($config->const as $key => $value) { define($key, $value); } } // set modes if (isset($config->mode)) { foreach ($config->mode as $mode => $state) { Environment::setMode($mode, $state); } } $config->setReadOnly(); return $config; }