BryanDavis has uploaded a new change for review.
https://gerrit.wikimedia.org/r/101177
Change subject: Stop referencing Config inside objects
......................................................................
Stop referencing Config inside objects
Consolidate all use of the Config object to the App master object.
Change-Id: I6fd8c41a0cda0ca356cc9b4bfddac1f33b456fca
---
M src/Wikimania/Scholarship/App.php
M src/Wikimania/Scholarship/Config.php
M src/Wikimania/Scholarship/Controllers/ScholarshipApplication.php
M src/Wikimania/Scholarship/Dao/AbstractDao.php
M src/Wikimania/Scholarship/Dao/Apply.php
5 files changed, 80 insertions(+), 83 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/wikimedia/wikimania-scholarships
refs/changes/77/101177/1
diff --git a/src/Wikimania/Scholarship/App.php
b/src/Wikimania/Scholarship/App.php
index cb10c79..ceb1997 100644
--- a/src/Wikimania/Scholarship/App.php
+++ b/src/Wikimania/Scholarship/App.php
@@ -50,13 +50,18 @@
$this->slim = new \Slim\Slim( array(
'mode' => 'production',
'debug' => false,
- 'log.level' => self::config( 'LOG_LEVEL',
\Psr\Log\LogLevel::NOTICE ),
- 'log.file' => self::config( 'LOG_FILE', 'php://stderr'
),
+ 'log.level' => Config::getStr( 'LOG_LEVEL',
\Psr\Log\LogLevel::NOTICE ),
+ 'log.file' => Config::getStr( 'LOG_FILE',
'php://stderr' ),
'view' => new \Slim\Views\Twig(),
'view.cache' => "{$this->deployDir}/data/cache",
- 'smtp.host' => self::config( 'SMTP_HOST', 'localhost' ),
+ 'smtp.host' => Config::getStr( 'SMTP_HOST', 'localhost'
),
'templates.path' => "{$this->deployDir}/data/templates",
'i18n.path' => "{$this->deployDir}/data/i18n",
+ 'db.dsn' => Config::getStr( 'DB_DSN' ),
+ 'db.user' => Config::getStr( 'DB_USER' ),
+ 'db.pass' => Config::getStr( 'DB_PASS' ),
+ 'period.open' => Config::getDate( 'APPLICATION_OPEN' ),
+ 'period.close' => Config::getDate( 'APPLICATION_CLOSE'
),
));
$slim = $this->slim;
@@ -82,7 +87,7 @@
$this->slim->configureMode( 'development', function () use (
$slim ) {
$slim->config( array(
'debug' => true,
- 'log.level' => self::config( 'LOG_LEVEL',
\Psr\Log\LogLevel::DEBUG ),
+ 'log.level' => Config::getStr( 'LOG_LEVEL',
\Psr\Log\LogLevel::DEBUG ),
'view.cache' => false,
) );
});
@@ -116,7 +121,7 @@
session_cache_limiter(false);
session_start();
- $this->slim->mock = Config::get( 'mock' );
+ $this->slim->mock = Config::getBool( 'MOCK' );
// run the app
$this->slim->run();
@@ -130,7 +135,10 @@
$container = $this->slim->container;
$container->singleton( 'userDao', function ( $c ) {
- return new \Wikimania\Scholarship\Dao\User( $c->log );
+ return new \Wikimania\Scholarship\Dao\User(
+ $c->settings['db.dsn'],
+ $c->settings['db.user'],
$c->settings['db.pass'],
+ $c->log );
});
$container->singleton( 'authManager', function ( $c ) {
@@ -139,7 +147,10 @@
$container->singleton( 'applyDao', function ( $c ) {
$uid = $c->authManager->getUserId();
- return new \Wikimania\Scholarship\Dao\Apply( $uid,
$c->log );
+ return new \Wikimania\Scholarship\Dao\Apply(
+ $c->settings['db.dsn'],
+ $c->settings['db.user'],
$c->settings['db.pass'],
+ $uid, $c->log );
});
$container->singleton( 'wgLang', function ( $c ) {
@@ -225,13 +236,19 @@
})->name( 'home' );
$slim->get( 'apply', function () use ( $slim ) {
- $page = new Controllers\ScholarshipApplication(
$slim );
+ $page = new Controllers\ScholarshipApplication(
+ $slim->config( 'period.open' ),
+ $slim->config( 'period.close' ),
+ $slim );
$page->setForm( $slim->applyForm );
$page();
})->name( 'apply' );
$slim->post( 'apply', function () use ( $slim ) {
- $page = new Controllers\ScholarshipApplication(
$slim );
+ $page = new Controllers\ScholarshipApplication(
+ $slim->config( 'period.open' ),
+ $slim->config( 'period.close' ),
+ $slim );
$page->setForm( $slim->applyForm );
$page->setMailer( $slim->mailer );
$page();
@@ -439,25 +456,6 @@
$slim->get( $name, function () use ( $slim, $name ) {
$slim->render( "{$name}.html" );
})->name( $routeName );
- }
-
-
- /**
- * Get the value of a environment variable.
- *
- * @param string $name Variable name
- * @param string $default Default value if variable is not set
- * @return string
- */
- public static function config( $name, $default = '' ) {
- $var = getenv( $name );
- if ( $var === false ) {
- $var = $default;
- }
- return filter_var( $var,
- \FILTER_SANITIZE_STRING,
- \FILTER_FLAG_STRIP_LOW | \FILTER_FLAG_STRIP_HIGH
- );
}
} //end App
diff --git a/src/Wikimania/Scholarship/Config.php
b/src/Wikimania/Scholarship/Config.php
index 3823daf..6fd0021 100644
--- a/src/Wikimania/Scholarship/Config.php
+++ b/src/Wikimania/Scholarship/Config.php
@@ -30,61 +30,40 @@
class Config {
/**
- * @var array $settings
+ * Get a boolean value
+ * @param string $name Setting name
+ * @param bool $default Default value if none found
+ * @return bool Value
*/
- protected $settings;
-
- protected function __construct() {
-
- $this->settings = array(
- 'mock' => self::getBool( 'MOCK' ),
- 'application_open' => self::getDate( 'APPLICATION_OPEN'
),
- 'application_close' => self::getDate(
'APPLICATION_CLOSE' ),
- 'db_dsn' => self::getStr( 'DB_DSN' ),
- 'db_user' => self::getStr( 'DB_USER' ),
- 'db_pass' => self::getStr( 'DB_PASS' ),
- );
+ public static function getBool( $name, $default = false ) {
+ $var = getenv( $name );
+ $val = filter_var( $var, \FILTER_VALIDATE_BOOLEAN,
\FILTER_NULL_ON_FAILURE );
+ return ( $val === null ) ? $default : $val;
}
/**
- * Get a configuration setting.
- * @param string $name Setting
- * @return mixed Configuration value
+ * Get a string value
+ * @param string $name Setting name
+ * @param string $default Default value if none found
+ * @return string Value
*/
- public static function get( $name ) {
- static $conf;
- if ( $conf === null ) {
- $conf = new self();
- }
- return $conf->getSetting( $name );
- }
-
-
- protected function getSetting ( $name ) {
- if ( array_key_exists( $name, $this->settings ) ) {
- return $this->settings[$name];
- }
- return null;
- }
-
-
- protected static function getBool( $name ) {
+ public static function getStr( $name, $default = '' ) {
$var = getenv( $name );
- return filter_var( $var, \FILTER_VALIDATE_BOOLEAN );
- }
-
-
- protected static function getStr( $name ) {
- $var = getenv( $name );
- return filter_var( $var,
+ $val = filter_var( $var,
\FILTER_SANITIZE_STRING,
\FILTER_FLAG_STRIP_LOW | \FILTER_FLAG_STRIP_HIGH
);
+ return ( $val === false ) ? $default : $val;
}
- protected static function getDate( $name ) {
+ /**
+ * Get a date value
+ * @param string $name Setting name
+ * @return int|bool Unix timestamp or false if not found
+ */
+ public static function getDate( $name ) {
return strtotime( self::getStr( $name ) );
}
diff --git a/src/Wikimania/Scholarship/Controllers/ScholarshipApplication.php
b/src/Wikimania/Scholarship/Controllers/ScholarshipApplication.php
index 214c4c4..f77d86d 100644
--- a/src/Wikimania/Scholarship/Controllers/ScholarshipApplication.php
+++ b/src/Wikimania/Scholarship/Controllers/ScholarshipApplication.php
@@ -22,7 +22,6 @@
namespace Wikimania\Scholarship\Controllers;
-use Wikimania\Scholarship\Config;
use Wikimania\Scholarship\Controller;
use Wikimania\Scholarship\Countries;
use Wikimania\Scholarship\Forms\Apply as ApplyForm;
@@ -36,8 +35,26 @@
*/
class ScholarshipApplication extends Controller {
- public function __construct(\Slim\Slim $slim = null ) {
+ /**
+ * @var int $applicationOpen
+ */
+ protected $periodOpen;
+
+ /**
+ * @var int $applicationClose
+ */
+ protected $periodClose;
+
+
+ /**
+ * @param int $open Unix timestanp that applications will first be
accepted
+ * @param int $close Unix timestamp that applications will last be
accepted
+ * @param \Slim\Slim $slim Slim application
+ */
+ public function __construct( $open, $close, \Slim\Slim $slim = null ) {
parent::__construct( $slim );
+ $this->periodOpen = $open;
+ $this->periodClose = $close;
}
protected function handle() {
@@ -67,13 +84,13 @@
}
}
- $openTime = Config::get( 'application_open' );
- $closeTime = Config::get( 'application_close' );
$now = time();
- $this->slim->view->setData( 'registration_open', $now >
$openTime || $this->slim->mock );
+ $this->slim->view->setData( 'registration_open',
+ $now > $this->periodOpen || $this->slim->mock );
// FIXME: legacy app allowed '?special' to override
- $this->slim->view->setData( 'registration_closed', $now
> $closeTime && !$this->slim->mock );
+ $this->slim->view->setData( 'registration_closed',
+ $now > $this->periodClose && !$this->slim->mock
);
$this->slim->view->setData( 'form', $this->form );
$this->slim->view->setData( 'submitted', $submitted );
diff --git a/src/Wikimania/Scholarship/Dao/AbstractDao.php
b/src/Wikimania/Scholarship/Dao/AbstractDao.php
index 5ac5fe5..7ede753 100644
--- a/src/Wikimania/Scholarship/Dao/AbstractDao.php
+++ b/src/Wikimania/Scholarship/Dao/AbstractDao.php
@@ -22,8 +22,6 @@
namespace Wikimania\Scholarship\Dao;
-use Wikimania\Scholarship\Config;
-
use \PDO;
use \PDOException;
use Psr\Log\LoggerInterface;
@@ -48,13 +46,15 @@
/**
+ * @param string $dsn PDO data source name
+ * @param string $user Database user
+ * @param string $pass Database password
* @param LoggerInterface $logger Log channel
*/
- public function __construct( $logger = null ) {
+ public function __construct( $dsn, $user, $pass, $logger = null ) {
$this->logger = $logger ?: new \Psr\Log\NullLogger();
- $this->dbh = new PDO( Config::get( 'db_dsn' ),
- Config::get( 'db_user' ), Config::get( 'db_pass' ),
+ $this->dbh = new PDO( $dsn, $user, $pass,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => true, //FIXME: good
idea?
diff --git a/src/Wikimania/Scholarship/Dao/Apply.php
b/src/Wikimania/Scholarship/Dao/Apply.php
index d0b2212..34147fd 100644
--- a/src/Wikimania/Scholarship/Dao/Apply.php
+++ b/src/Wikimania/Scholarship/Dao/Apply.php
@@ -40,11 +40,14 @@
/**
+ * @param string $dsn PDO data source name
+ * @param string $user Database user
+ * @param string $pass Database password
* @param int|bool $uid Authenticated user
* @param LoggerInterface $logger Log channel
*/
- public function __construct( $uid = false, $logger = null ) {
- parent::__construct( $logger );
+ public function __construct( $dsn, $user, $pass, $uid = false, $logger
= null ) {
+ parent::__construct( $dsn, $user, $pass, $logger );
$this->userid = $uid;
}
--
To view, visit https://gerrit.wikimedia.org/r/101177
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6fd8c41a0cda0ca356cc9b4bfddac1f33b456fca
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/wikimania-scholarships
Gerrit-Branch: master
Gerrit-Owner: BryanDavis <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits