Skip to content

Commit

Permalink
Environment: variables, modes and config are passed to system context…
Browse files Browse the repository at this point in the history
… (as params, config as service)
  • Loading branch information
dg committed May 10, 2011
1 parent 05411c5 commit b8b94c0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
36 changes: 21 additions & 15 deletions Nette/DI/Configurator.php
Expand Up @@ -111,7 +111,7 @@ public function detect($name)
* @param string file name
* @return Nette\ArrayHash
*/
public function loadConfig($file)
public function loadConfig($container, $file)

This comment has been minimized.

Copy link
@JanTvrdik

JanTvrdik May 10, 2011

Contributor

zapomenutá úprava phpDoc

{
$name = Environment::getName();

Expand All @@ -129,6 +129,7 @@ public function loadConfig($file)
}
$config = Nette\Config\Config::fromFile($file, $name);
}
$container->config = $config;

// process environment variables
if (isset($config->variable) && $config->variable instanceof \Traversable) {
Expand All @@ -146,7 +147,6 @@ public function loadConfig($file)

// process services
$runServices = array();
$container = Environment::getContext();
if (isset($config->service) && $config->service instanceof \Traversable) {
foreach ($config->service as $key => $value) {
$key = strtr($key, '-', '\\'); // limited INI chars
Expand Down Expand Up @@ -248,7 +248,7 @@ public function loadConfig($file)
// set modes
if (isset($config->mode) && isset($config->mode)) {
foreach ($config->mode as $mode => $state) {
Environment::setMode($mode, $state);
$container->setParam($mode . 'Mode', (bool) $state);
}
}

Expand Down Expand Up @@ -276,6 +276,12 @@ public function createContainer()
foreach ($this->defaultServices as $name => $service) {
$container->addService($name, $service);
}

defined('APP_DIR') && $container->setParam('appDir', APP_DIR);
defined('LIBS_DIR') && $container->setParam('libsDir', LIBS_DIR);
defined('TEMP_DIR') && $container->setParam('tempDir', TEMP_DIR);
$container->setParam('productionMode', $this->detect('production'));

return $container;
}

Expand All @@ -286,20 +292,20 @@ public function createContainer()
*/
public static function createApplication(IContainer $container, array $options = NULL)
{
$context = new Nette\DI\Container;
$context = new Container;
$context->addService('httpRequest', $container->getService('Nette\\Web\\IHttpRequest'));
$context->addService('httpResponse', $container->getService('Nette\\Web\\IHttpResponse'));
$context->addService('session', $container->getService('Nette\\Web\\Session'));
$context->addService('presenterFactory', $container->getService('Nette\\Application\\IPresenterFactory'));
$context->addService('router', 'Nette\Application\Routers\RouteList');

Nette\Application\UI\Presenter::$invalidLinkMode = Environment::isProduction()
Nette\Application\UI\Presenter::$invalidLinkMode = $container->getParam('productionMode')
? Nette\Application\UI\Presenter::INVALID_LINK_SILENT
: Nette\Application\UI\Presenter::INVALID_LINK_WARNING;

$class = isset($options['class']) ? $options['class'] : 'Nette\Application\Application';
$application = new $class($context);
$application->catchExceptions = Environment::isProduction();
$application->catchExceptions = $container->getParam('productionMode');
return $application;
}

Expand All @@ -310,7 +316,7 @@ public static function createApplication(IContainer $container, array $options =
*/
public static function createPresenterFactory(IContainer $container)
{
return new Nette\Application\PresenterFactory(Environment::getVariable('appDir'), $container);
return new Nette\Application\PresenterFactory($container->getParam('appDir'), $container);
}


Expand Down Expand Up @@ -358,7 +364,7 @@ public static function createHttpSession(IContainer $container)
*/
public static function createHttpUser(IContainer $container)
{
$context = new Nette\DI\Container;
$context = new Container;
// copies services from $container and preserves lazy loading
$context->addService('authenticator', function() use ($container) {
return $container->getService('Nette\\Security\\IAuthenticator');
Expand All @@ -377,7 +383,7 @@ public static function createHttpUser(IContainer $container)
*/
public static function createCacheStorage(IContainer $container)
{
$dir = Environment::getVariable('tempDir') . '/cache';
$dir = $container->expand('%tempDir%/cache');

This comment has been minimized.

Copy link
@JanTvrdik

JanTvrdik May 10, 2011

Contributor

Rozhraní IContainer negarantuje existenci metody expand. Řešení ale v kapse nemám.

This comment has been minimized.

Copy link
@dg

dg May 10, 2011

Author Member

Negarantuje toho mnohem víc, takže zvažuju uvnitř Nette přejít na Container.

This comment has been minimized.

Copy link
@PetrP

PetrP May 11, 2011

Contributor

A nepřidat Application\DI\IContainer ktery tyhle veci garantovat bude?

This comment has been minimized.

Copy link
@dg

dg May 12, 2011

Author Member

Koketuju spíš s myšlenkou vrazit tam public $params a expand udělat statickou metodou. Zapouzdřené $params přináší řadu komplikací, když jde o vícerozměrné pole.

umask(0000);
@mkdir($dir, 0777); // @ - directory may exists
return new Nette\Caching\Storages\FileStorage($dir, $container->getService('Nette\\Caching\\ICacheJournal'));
Expand All @@ -388,9 +394,9 @@ public static function createCacheStorage(IContainer $container)
/**
* @return Nette\Caching\IStorage
*/
public static function createTemplateCacheStorage()
public static function createTemplateCacheStorage(IContainer $container)
{
$dir = Environment::getVariable('tempDir') . '/cache';
$dir = $container->expand('%tempDir%/cache');
umask(0000);
@mkdir($dir, 0777); // @ - directory may exists
return new Nette\Templating\PhpFileStorage($dir);
Expand All @@ -401,9 +407,9 @@ public static function createTemplateCacheStorage()
/**
* @return Nette\Caching\Storages\IJournal
*/
public static function createCacheJournal()
public static function createCacheJournal(IContainer $container)
{
return new Nette\Caching\Storages\FileJournal(Environment::getVariable('tempDir'));
return new Nette\Caching\Storages\FileJournal($container->getParam('tempDir'));
}


Expand All @@ -428,13 +434,13 @@ public static function createMailer(IContainer $container, array $options = NULL
public static function createRobotLoader(IContainer $container, array $options = NULL)
{
$loader = new Nette\Loaders\RobotLoader;
$loader->autoRebuild = isset($options['autoRebuild']) ? $options['autoRebuild'] : !Environment::isProduction();
$loader->autoRebuild = isset($options['autoRebuild']) ? $options['autoRebuild'] : !$container->getParam('productionMode');
$loader->setCacheStorage($container->getService('Nette\\Caching\\ICacheStorage'));
if (isset($options['directory'])) {
$loader->addDirectory($options['directory']);
} else {
foreach (array('appDir', 'libsDir') as $var) {
if ($dir = Environment::getVariable($var, NULL)) {
if ($dir = $container->getParam($var, NULL)) {
$loader->addDirectory($dir);
}
}
Expand Down
28 changes: 12 additions & 16 deletions Nette/common/Environment.php
Expand Up @@ -30,12 +30,6 @@ final class Environment
/** @var Nette\DI\Configurator */
private static $configurator;

/** @var string the mode of current application */
private static $modes = array();

/** @var \ArrayObject */
private static $config;

/** @var Nette\DI\IContainer */
private static $context;

Expand Down Expand Up @@ -137,7 +131,7 @@ public static function getName()
*/
public static function setMode($mode, $value = TRUE)
{
self::$modes[$mode] = (bool) $value;
self::getContext()->setParam($mode . 'Mode', (bool) $value);
}


Expand All @@ -149,11 +143,11 @@ public static function setMode($mode, $value = TRUE)
*/
public static function getMode($mode)
{
if (isset(self::$modes[$mode])) {
return self::$modes[$mode];
if (isset(self::getContext()->params[$mode . 'Mode'])) {
return self::getContext()->params[$mode . 'Mode'];

} else {
return self::$modes[$mode] = self::getConfigurator()->detect($mode);
return self::getContext()->params[$mode . 'Mode'] = self::getConfigurator()->detect($mode);
}
}

Expand Down Expand Up @@ -194,10 +188,11 @@ public static function isProduction()
*/
public static function setVariable($name, $value, $expand = TRUE)
{
if (!is_string($value)) {
$expand = FALSE;
$expand = $expand && is_string($value) && strpos($value, '%') !== FALSE;
if (!$expand) {
self::getContext()->setParam($name, $value);
}
self::$vars[$name] = array($value, (bool) $expand);
self::$vars[$name] = array($value, $expand);
}


Expand Down Expand Up @@ -461,7 +456,7 @@ public static function getSession($namespace = NULL)
*/
public static function loadConfig($file = NULL)
{
return self::$config = self::getConfigurator()->loadConfig($file);
return self::getConfigurator()->loadConfig(self::getContext(), $file);
}


Expand All @@ -474,11 +469,12 @@ public static function loadConfig($file = NULL)
*/
public static function getConfig($key = NULL, $default = NULL)
{
$config = self::getContext()->config;
if (func_num_args()) {
return isset(self::$config[$key]) ? self::$config[$key] : $default;
return isset($config[$key]) ? $config[$key] : $default;

} else {
return self::$config;
return $config;
}
}

Expand Down

0 comments on commit b8b94c0

Please sign in to comment.