Logger and Configuration have test suites
This commit is contained in:
@@ -51,12 +51,17 @@ class Configuration {
|
||||
* @param string $filePath
|
||||
* @return void
|
||||
*/
|
||||
public static function setConfigFilePath($filePath)
|
||||
public static function setConfigFilePath($filePath, $clearPrevious = false)
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
throw new \RuntimeException("The file path $filePath, does not exist");
|
||||
}
|
||||
self::$customConfigFilePath = $filePath;
|
||||
$instance = self::getInstance();
|
||||
if ($clearPrevious) {
|
||||
$instance->clear();
|
||||
}
|
||||
self::getInstance()->loadConfigFromFile();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +93,7 @@ class Configuration {
|
||||
* @return void
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function loadConfigFromFile()
|
||||
private function loadConfigFromFile()
|
||||
{
|
||||
$this->setCustomConfig($this->getConfigFromFile());
|
||||
}
|
||||
@@ -162,6 +167,14 @@ class Configuration {
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow clearing previously loaded config
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$this->values = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
@@ -178,6 +191,7 @@ class Configuration {
|
||||
{
|
||||
return self::existsKeys(self::get(), self::parseKeys($keys));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param mixed $default the default value for your configuration option
|
||||
|
||||
@@ -8,6 +8,8 @@ class Logger {
|
||||
const QUIET = 0;
|
||||
const NORMAL = 1;
|
||||
const VERBOSE = 2;
|
||||
const NO_FILE_LOGGED = '';
|
||||
const NO_LOG_YET_MSG = 'No log yet !';
|
||||
|
||||
private static $start;
|
||||
private static $data = array();
|
||||
@@ -56,6 +58,25 @@ class Logger {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the static properties, needed for testging
|
||||
*/
|
||||
public static function clear()
|
||||
{
|
||||
self::$start = null;
|
||||
self::$data = array();
|
||||
self::$log = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush previous static properties and start again
|
||||
*/
|
||||
public static function reset($data = array())
|
||||
{
|
||||
self::clear();
|
||||
self::start($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* If $key is passed, creates/overwrites existing self::$data[$key] with $info
|
||||
* Otherwise, uses array_merge:
|
||||
@@ -98,6 +119,7 @@ class Logger {
|
||||
* - store the time lapse between start() and stop() calls in self::$data
|
||||
*
|
||||
* @param $data allow storing some info on stop
|
||||
* @return string saved log message filepath
|
||||
*/
|
||||
public static function stop($data = null)
|
||||
{
|
||||
@@ -108,9 +130,11 @@ class Logger {
|
||||
$time = (microtime(true) - self::$start) * 1000;
|
||||
self::$data['time'] = round($time, 2).'ms';
|
||||
|
||||
$savedLogMessageFilePath = self::NO_FILE_LOGGED;
|
||||
if (Configuration::get('log.verbosity') > Logger::QUIET) {
|
||||
self::saveLogMessageToFile(self::generateLogMessage());
|
||||
$savedLogMessageFilePath = self::saveLogMessageToFile(self::generateLogMessage());
|
||||
}
|
||||
return $savedLogMessageFilePath;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,25 +146,32 @@ class Logger {
|
||||
|
||||
$patterns = array_map(function($p) { return "%$p%"; }, array_keys(self::$data));
|
||||
$msg = str_replace($patterns, array_values(self::$data), $format)."\n";
|
||||
return $msg . (strlen(self::$log) > 0 ? self::$log : '');
|
||||
return $msg . self::$log;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $msg
|
||||
* @return string saved log message file path
|
||||
*/
|
||||
private static function saveLogMessageToFile($msg)
|
||||
{
|
||||
$mostRecentLogFileName = Configuration::get('log.file');
|
||||
|
||||
if(self::isMostRecentLogFileFromYesterday()) {
|
||||
self::makeRoomForNewLogFile($mostRecentLogFileName);
|
||||
} else {
|
||||
mkdir(dirname($mostRecentLogFileName), 0777, true);
|
||||
if (!file_exists($mostRecentLogFileName)) {
|
||||
if (!is_dir(dirname($mostRecentLogFileName))) {
|
||||
mkdir(dirname($mostRecentLogFileName), 0777, true);
|
||||
}
|
||||
touch($mostRecentLogFileName);
|
||||
}
|
||||
|
||||
if(self::isMostRecentLogFileFromYesterday()) {
|
||||
self::makeRoomForNewLogFile($mostRecentLogFileName);
|
||||
touch($mostRecentLogFileName);
|
||||
}
|
||||
|
||||
touch($mostRecentLogFileName);
|
||||
file_put_contents($mostRecentLogFileName, $msg, FILE_APPEND | LOCK_EX);
|
||||
return $mostRecentLogFileName;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,9 +180,6 @@ class Logger {
|
||||
private static function isMostRecentLogFileFromYesterday()
|
||||
{
|
||||
$mostRecentLogFileName = Configuration::get('log.file');
|
||||
if (!file_exists($mostRecentLogFileName)) {
|
||||
return false;
|
||||
}
|
||||
return filemtime($mostRecentLogFileName) < strtotime("midnight");
|
||||
}
|
||||
|
||||
@@ -223,7 +251,7 @@ class Logger {
|
||||
return "$logFileBaseName.$next";
|
||||
}
|
||||
|
||||
public static function data()
|
||||
public static function getData()
|
||||
{
|
||||
return self::$data;
|
||||
}
|
||||
@@ -232,7 +260,7 @@ class Logger {
|
||||
{
|
||||
$file = Configuration::get('log.file');
|
||||
if(! file_exists($file)) {
|
||||
return 'No log yet !';
|
||||
return self::NO_LOG_YET_MSG;
|
||||
}
|
||||
|
||||
$f = fopen($file, 'r');
|
||||
|
||||
Reference in New Issue
Block a user