From f12076171838489a8a58e7015db1d13fdaefd39d Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Fri, 11 Jan 2019 11:54:58 +0800 Subject: [PATCH] Add constant for path to tasks files --- src/common.php | 23 ++++++++++++----------- src/purjolok.php | 6 +++--- src/unfinished.php | 8 ++++---- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/common.php b/src/common.php index ab0dd8d..d884459 100644 --- a/src/common.php +++ b/src/common.php @@ -334,11 +334,12 @@ function getTasksForTheWeek(int $year, int $monthNum, int $weekNum, array $taskM } const getFilePathForWeek = 'getFilePathForWeek'; -function getFilePathForWeek(int $year, int $monthNum, int $weekNum) { +function getFilePathForWeek(int $year, int $monthNum, int $weekNum, string $base) { // December is part of next year's summer $seasonYear = $year; return sprintf( - 'tasks/%s/%s/%s/week%s.txt', + '%s/tasks/%s/%s/%s/week%s.txt', + $base, $seasonYear, getSeason($monthNum), getMonthName($monthNum), @@ -413,25 +414,25 @@ function getYearWeekBeginsIn(int $year, int $month, int $day) { return $year - (int)($month == 1 && getMonthWeekBeginsIn($year, $month, $day) == 12); } -function getFilePathsForMonth(int $year, int $monthNum) { - return map(function($week) use ($year, $monthNum){ - return getFilePathForWeek($year, $monthNum, $week); +function getFilePathsForMonth(int $year, int $monthNum, string $base) { + return map(function($week) use ($year, $monthNum, $base){ + return getFilePathForWeek($year, $monthNum, $week, $base); })(range(1, count(getMondaysForMonth($year, $monthNum)))); } -function getFilePathsForSeason(int $year, string $season) { - return array_merge(...map(function($monthNum) use ($year) { +function getFilePathsForSeason(int $year, string $season, string $base) { + return array_merge(...map(function($monthNum) use ($year, $base) { // Summer of the current year includes december of the previous year. $seasonYear = $year - ($monthNum == 12 ? 1 : 0); - return getFilePathsForMonth($seasonYear, $monthNum); + return getFilePathsForMonth($seasonYear, $monthNum, $base); })(array_filter(range(1,12), function($month) use ($season) { return getSeason($month) == $season; }))); } -function getFilePathsForYear(int $year) { - return array_merge(...map(function($season) use ($year) { - return getFilePathsForSeason($year, $season); +function getFilePathsForYear(int $year, string $base) { + return array_merge(...map(function($season) use ($year, $base) { + return getFilePathsForSeason($year, $season, $base); })(['summer', 'winter', 'spring', 'autumn'])); } diff --git a/src/purjolok.php b/src/purjolok.php index 94cc011..e66f830 100644 --- a/src/purjolok.php +++ b/src/purjolok.php @@ -17,7 +17,7 @@ $message = getTelegram()->getWebHookUpdates()->get('message') ? getTelegram()->g if ($between = between(reveal($message ?? ''), '[taskid]')) { $monday = turnBackTime(new DateTimeImmutable); - $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum); + $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum, PATH_TO_TASKS_FILES); $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : []; $tasksForTheWeek = getTasksForTheWeek( $monday->year, @@ -233,7 +233,7 @@ getTelegram()->addCommand( require 'taskMatrix.php' ); - $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum); + $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum, PATH_TO_TASKS_FILES); $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : []; if (!array_diff($tasksForTheWeek, $completedTasks)) { @@ -267,7 +267,7 @@ getTelegram()->addCommand( require 'taskMatrix.php' ); - $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum); + $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum, PATH_TO_TASKS_FILES); $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : []; if (!array_diff($tasksForTheWeek, $completedTasks)) { diff --git a/src/unfinished.php b/src/unfinished.php index 890e0bf..27d6c1a 100644 --- a/src/unfinished.php +++ b/src/unfinished.php @@ -15,20 +15,20 @@ $unfinishedForYear = array_diff( return getTasksForTheSeason($season, $taskMatrix); } )(['summer', 'autumn', 'winter', 'spring'])), - array_reduce(getFilePathsForYear($monday->year), $extractTasks, []) + array_reduce(getFilePathsForYear($monday->year, PATH_TO_TASKS_FILES), $extractTasks, []) ); $unfinishedForSeason = array_diff( getTasksForTheSeason($monday->season, $taskMatrix), - array_reduce(getFilePathsForSeason($monday->year, $monday->season), $extractTasks, []) + array_reduce(getFilePathsForSeason($monday->year, $monday->season, PATH_TO_TASKS_FILES), $extractTasks, []) ); $unfinishedForMonth = array_diff( getTasksForTheMonth($monday->month, $taskMatrix), - array_reduce(getFilePathsForMonth($monday->year, $monday->month), $extractTasks, []) + array_reduce(getFilePathsForMonth($monday->year, $monday->month, PATH_TO_TASKS_FILES), $extractTasks, []) ); -$filePathForWeek = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum); +$filePathForWeek = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum, PATH_TO_TASKS_FILES); $unfinishedForWeek = array_diff( getTasksForTheWeek($monday->year, $monday->month, $monday->weekNum, $taskMatrix), file_exists($filePathForWeek) ? lines(trim(file_get_contents($filePathForWeek))) : [] -- 2.11.0