Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Refactor system configuration handling
Browse files Browse the repository at this point in the history
- Add /config/bootstrapper.php file to hold system configuration.
- Remove legacy JFactory::getConfig() method
- Remove legacy ApplicationDispatcherHttp::getCfg() method
  • Loading branch information
johanjanssens committed Aug 17, 2014
1 parent 3215a23 commit e6398c8
Show file tree
Hide file tree
Showing 24 changed files with 165 additions and 408 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.vagrant/
config/config.php
config/bootstrapper.php

/scripts/translations/vendor/
110 changes: 24 additions & 86 deletions application/admin/component/application/dispatcher/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public function __construct(Library\ObjectConfig $config)
$this->_site = $config->site;
}

$this->loadConfig();

$this->addCommandCallback('before.run', 'loadLanguage');
$this->addCommandCallback('before.run', 'setLanguage');
}

/**
Expand All @@ -60,10 +58,7 @@ protected function _initialize(Library\ObjectConfig $config)
$config->append(array(
'base_url' => '/administrator',
'site' => null,
'options' => array(
'config_file' => JPATH_ROOT . '/config/config.php',
'language' => null,
),
'language' => 'en-GB',
));

parent::_initialize($config);
Expand All @@ -76,14 +71,10 @@ protected function _initialize(Library\ObjectConfig $config)
*/
protected function _actionRun(Library\DispatcherContextInterface $context)
{
//Set the site error reporting
$this->getObject('exception.handler')->setErrorLevel($this->getCfg('debug_mode'));

define('JPATH_FILES', JPATH_SITES . '/' . $this->getSite() . '/files');
define('JPATH_CACHE', $this->getCfg('cache_path', JPATH_ROOT . '/cache'));

// Set timezone to user's setting, falling back to global configuration.
$timezone = new \DateTimeZone($context->user->get('timezone', $this->getCfg('timezone')));
$timezone = new \DateTimeZone($context->user->get('timezone', $this->getConfig()->timezone));
date_default_timezone_set($timezone->getName());

//Route the request
Expand Down Expand Up @@ -111,26 +102,31 @@ protected function _actionRoute(Library\DispatcherContextInterface $context)
}

/**
* Load the configuration
* Get the application languages.
*
* @return void
* @return ApplicationDatabaseRowsetLanguages
*/
public function loadConfig()
public function setLanguage(Library\DispatcherContextInterface $context)
{
// Check if the site exists
if ($this->getObject('com:sites.model.sites')->fetch()->find($this->getSite())) {
//Load the application config settings
JFactory::getConfig()->loadArray($this->getConfig()->options->toArray());
$languages = $this->getObject('application.languages');
$language = null;

//Load the global config settings
require_once($this->getConfig()->options->config_file);
JFactory::getConfig()->loadObject(new JConfig());
// Otherwise use user language setting.
if(!$language && $iso_code = $context->user->get('language')) {
$language = $languages->find(array('iso_code' => $iso_code));
}

// If no user language specified, use application
if($iso_code = $this->getConfig()->language) {
$language = $languages->find(array('iso_code' => $iso_code));
}

//Load the site config settings
require_once(JPATH_SITES . '/' . $this->getSite() . '/config/config.php');
JFactory::getConfig()->loadObject(new JSiteConfig());
// If language still not set, use the primary.
if(!$language) {
$language = $languages->getPrimary();
}

} else throw new Library\ControllerExceptionResourceNotFound('Site :' . $this->getSite() . ' not found');
$languages->setActive($language);
}

/**
Expand All @@ -140,7 +136,8 @@ public function loadConfig()
*/
public function getUser()
{
if (!$this->_user instanceof Library\UserInterface) {
if (!$this->_user instanceof Library\UserInterface)
{
$user = parent::getUser();
$session = $user->getSession();

Expand All @@ -157,52 +154,6 @@ public function getUser()
return parent::getUser();
}

/**
* Get the application languages.
*
* @return ApplicationDatabaseRowsetLanguages
*/
public function loadLanguage(Library\DispatcherContextInterface $context)
{
$languages = $this->getObject('application.languages');
$language = null;

// If a language was specified it has priority.
if ($iso_code = $this->getConfig()->options->language)
{
$result = $languages->find(array('iso_code' => $iso_code));
if (count($result) == 1) {
$language = $result->top();
}
}

// Otherwise use user language setting.
if (!$language && $iso_code = $context->user->get('language'))
{
$result = $languages->find(array('iso_code' => $iso_code));
if (count($result) == 1) {
$language = $result->top();
}
}

// If language still not set, use the primary.
if (!$language) {
$language = $languages->getPrimary();
}

$languages->setActive($language);

$translator = $this->getObject('translator', array('locale' => $language->iso_code));

// Load Framework translations.
if (($file = $translator->find(JPATH_ROOT . '/library/resources/language/')) && !$translator->load($file, true)) {
throw new \RuntimeException('Unable to load framework translations');
}

// Load application translations.
$translator->import('application');
}

/**
* Get the application router.
*
Expand All @@ -217,19 +168,6 @@ public function getRouter(array $options = array())
return $router;
}

/**
* Gets a configuration value.
*
* @param string $name The name of the value to get.
* @param mixed $default The default value
*
* @return mixed The user state.
*/
public function getCfg($name, $default = null)
{
return JFactory::getConfig()->getValue('config.' . $name, $default);
}

/**
* Gets the name of site
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function setActive($active)
$this->_active = $active;
}

//Set the translator locale
$this->getObject('translator')->setLocale($this->_active->iso_code);

return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion application/admin/component/application/view/page/html.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ApplicationViewPageHtml extends Application\ViewPageHtml
*/
public function getTitle()
{
$title = $this->getObject('application')->getCfg('sitename' );
$title = $this->getObject('application')->getConfig()->sitename;
return $title;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function _afterAdd(Library\ControllerContextInterface $context)
$url = $context->request->getUrl()
->toString(Library\HttpUrl::SCHEME | Library\HttpUrl::HOST | Library\HttpUrl::PORT) . $url;

$site = $this->getObject('application')->getCfg('sitename');
$site = $this->getObject('application')->getConfig->sitename;

$subject = $translator->translate('User Account Activation');
$message = $translator->translate('User account activation E-mail',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function _afterToken(Library\ControllerContextInterface $context)
$url = $context->request->getUrl()
->toString(Library\HttpUrl::SCHEME | Library\HttpUrl::HOST | Library\HttpUrl::PORT) . $url;

$site = $this->getObject('application')->getCfg('sitename');
$site = $this->getObject('application')->getConfig()->sitename;

$subject = $translator->translate('Reset your password');
$message = $translator->translate('Password reset instructions E-mail',
Expand Down
18 changes: 5 additions & 13 deletions application/admin/public/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,26 @@

use Nooku\Library;

//Installation check
if (!file_exists(JPATH_ROOT . '/config/config.php') || (filesize(JPATH_ROOT . '/config/config.php') < 10)) {
echo 'No configuration file found. Exciting...';
exit();
}

//Don't run in STRICT mode (Joomla is not E_STRICT compat)
error_reporting(error_reporting() | ~ E_STRICT);

// Koowa : setup
require_once JPATH_ROOT . '/config/config.php';
$config = new JConfig();
// Bootstrap the framework
$config = require JPATH_ROOT . '/config/bootstrapper.php';

require_once(JPATH_ROOT . '/library/nooku.php');
$nooku = Nooku::getInstance(array(
'debug' => $config->debug,
'debug' => isset($config['debug']) ? (bool) $config['debug'] : false,
'cache_namespace' => 'admin',
'cache_enabled' => $config->caching,
'cache_enabled' => isset($config['caching']) ? (bool) $config['debug'] : false,
'base_path' => JPATH_ROOT.'/application/admin'
));

unset($config);

//Bootstrap the application
Library\ObjectManager::getInstance()->getObject('object.bootstrapper')
->registerApplication('site' , $nooku->getRootPath().'/application/site/component')
->registerApplication('admin', $nooku->getRootPath().'/application/admin/component', true)
->registerComponents($nooku->getRootPath().'/component', 'nooku')
->registerFile($nooku->getRootPath(). '/config/bootstrapper.php')
->bootstrap();

// Joomla : setup
Expand Down
Loading

0 comments on commit e6398c8

Please sign in to comment.