diff --git a/src/DeprecatedHelper/Deprecated84.php b/src/DeprecatedHelper/Deprecated84.php new file mode 100644 index 0000000..c9de92c --- /dev/null +++ b/src/DeprecatedHelper/Deprecated84.php @@ -0,0 +1,95 @@ + $fields + * @param string $separator + * @param string $enclosure + * @param string $escape + * @param string $eol + * @return int|false + * @throws InvalidArgumentException + */ + public static function fputcsv( + mixed $stream, + array $fields, + string $separator = ",", + string $enclosure = '"', + string $escape = '', // set to empty for future compatible + string $eol = PHP_EOL + ): int | false { + if (!is_resource($stream)) { + throw new \InvalidArgumentException("fputcsv stream parameter must be a resrouce"); + } + return fputcsv($stream, $fields, $separator, $enclosure, $escape, $eol); + } + + /** + * This is a wrapper for fgetcsv to fix deprecated warning for $escape parameter + * See: https://www.php.net/manual/en/function.fgetcsv.php + * escape parameter deprecation and recommend to set to "" for compatible with PHP 9.0 + * + * @param mixed $stream + * @param null|int<0,max> $length + * @param string $separator + * @param string $enclosure + * @param string $escape + * @return array|false + * @throws InvalidArgumentException + */ + public static function fgetcsv( + mixed $stream, + ?int $length = null, + string $separator = ',', + string $enclosure = '"', + string $escape = '' // set to empty for future compatible + ): array | false { + if (!is_resource($stream)) { + throw new \InvalidArgumentException("fgetcsv stream parameter must be a resrouce"); + } + return fgetcsv($stream, $length, $separator, $enclosure, $escape); + } + + /** + * This is a wrapper for str_getcsv to fix deprecated warning for $escape parameter + * See: https://www.php.net/manual/en/function.str-getcsv.php + * escape parameter deprecation and recommend to set to "" for compatible with PHP 9.0 + * + * @param string $string + * @param string $separator + * @param string $enclosure + * @param string $escape + * @return array + */ + // phpcs:disable PSR1.Methods.CamelCapsMethodName + public static function str_getcsv( + string $string, + string $separator = ",", + string $enclosure = '"', + string $escape = '' // set to empty for future compatible + ): array { + return str_getcsv($string, $separator, $enclosure, $escape); + } + // phpcs:enable PSR1.Methods.CamelCapsMethodName +} + +// __END__ diff --git a/src/Logging/Logging.php b/src/Logging/Logging.php index d672d9a..7476ef5 100644 --- a/src/Logging/Logging.php +++ b/src/Logging/Logging.php @@ -30,6 +30,10 @@ class Logging { /** @var int minimum size for a max file size, so we don't set 1 byte, 10kb */ public const MIN_LOG_MAX_FILESIZE = 10 * 1024; + /** @var string log file extension, not changeable */ + private const LOG_FILE_NAME_EXT = "log"; + /** @var string log file block separator, not changeable */ + private const LOG_FILE_BLOCK_SEPARATOR = '.'; // NOTE: the second party array{} hs some errors /** @var array>|array{string:array{type:string,type_info?:string,mandatory:true,alias?:string,default:string|bool|Level,deprecated:bool,use?:string}} */ @@ -104,8 +108,6 @@ class Logging private string $log_folder = ''; /** @var string a alphanumeric name that has to be set as global definition */ private string $log_file_id = ''; - /** @var string log file name extension */ - private string $log_file_name_ext = 'log'; /** @var string log file name with folder, for actual writing */ private string $log_file_name = ''; /** @var int set in bytes */ @@ -431,7 +433,7 @@ class Logging private function buildLogFileName(Level $level, string $group_id = ''): string { // init base file path - $fn = $this->log_print_file . '.' . $this->log_file_name_ext; + $fn = $this->log_print_file . '.' . self::LOG_FILE_NAME_EXT; // log ID prefix settings, if not valid, replace with empty if (!empty($this->log_file_id)) { $rpl_string = $this->log_file_id; @@ -440,14 +442,15 @@ class Logging } $fn = str_replace('{LOGID}', $rpl_string, $fn); // log id (like a log file prefix) - $rpl_string = !$this->getLogFlag(Flag::per_level) ? '' : - '_' . $level->getName(); + $rpl_string = $this->getLogFlag(Flag::per_level) ? + self::LOG_FILE_BLOCK_SEPARATOR . $level->getName() : + ''; $fn = str_replace('{LEVEL}', $rpl_string, $fn); // create output filename // write per level - $rpl_string = !$this->getLogFlag(Flag::per_group) ? '' : + $rpl_string = $this->getLogFlag(Flag::per_group) ? // normalize level, replace all non alphanumeric characters with - - '_' . ( + self::LOG_FILE_BLOCK_SEPARATOR . ( // if return is only - then set error string preg_match( "/^-+$/", @@ -455,25 +458,29 @@ class Logging ) ? 'INVALID-LEVEL-STRING' : $level_string - ); + ) : + ''; $fn = str_replace('{GROUP}', $rpl_string, $fn); // create output filename // set per class, but don't use get_class as we will only get self - $rpl_string = !$this->getLogFlag(Flag::per_class) ? '' : '_' - // set sub class settings - . str_replace('\\', '-', Support::getCallerTopLevelClass()); + $rpl_string = $this->getLogFlag(Flag::per_class) ? + // set sub class settings + self::LOG_FILE_BLOCK_SEPARATOR . str_replace('\\', '-', Support::getCallerTopLevelClass()) : + ''; $fn = str_replace('{CLASS}', $rpl_string, $fn); // create output filename // if request to write to one file - $rpl_string = !$this->getLogFlag(Flag::per_page) ? - '' : - '_' . System::getPageName(System::NO_EXTENSION); + $rpl_string = $this->getLogFlag(Flag::per_page) ? + self::LOG_FILE_BLOCK_SEPARATOR . System::getPageName(System::NO_EXTENSION) : + ''; $fn = str_replace('{PAGENAME}', $rpl_string, $fn); // create output filename // if run id, we auto add ymd, so we ignore the log file date if ($this->getLogFlag(Flag::per_run)) { - $rpl_string = '_' . $this->getLogUniqueId(); // add 8 char unique string + // add 8 char unique string and date block with time + $rpl_string = self::LOG_FILE_BLOCK_SEPARATOR . $this->getLogUniqueId(); } elseif ($this->getLogFlag(Flag::per_date)) { - $rpl_string = '_' . $this->getLogDate(); // add date to file + // add date to file + $rpl_string = self::LOG_FILE_BLOCK_SEPARATOR . $this->getLogDate(); } else { $rpl_string = ''; } @@ -739,7 +746,10 @@ class Logging { if (empty($this->log_file_unique_id) || $override == true) { $this->log_file_unique_id = - date('Y-m-d_His') . '_U_' + date('Y-m-d_His') + . self::LOG_FILE_BLOCK_SEPARATOR + . 'U_' + // this doesn't have to be unique for everything, just for this logging purpose . substr(hash( 'sha1', random_bytes(63) diff --git a/test/phpunit/Logging/CoreLibsLoggingLoggingTest.php b/test/phpunit/Logging/CoreLibsLoggingLoggingTest.php index da99916..3d2b389 100644 --- a/test/phpunit/Logging/CoreLibsLoggingLoggingTest.php +++ b/test/phpunit/Logging/CoreLibsLoggingLoggingTest.php @@ -395,7 +395,7 @@ final class CoreLibsLoggingLoggingTest extends TestCase } $per_run_id = $log->getLogUniqueId(); $this->assertMatchesRegularExpression( - "/^\d{4}-\d{2}-\d{2}_\d{6}_U_[a-z0-9]{8}$/", + "/^\d{4}-\d{2}-\d{2}_\d{6}\.U_[a-z0-9]{8}$/", $per_run_id, 'assert per log run id 1st' ); @@ -403,7 +403,7 @@ final class CoreLibsLoggingLoggingTest extends TestCase $log->setLogUniqueId(true); $per_run_id_2nd = $log->getLogUniqueId(); $this->assertMatchesRegularExpression( - "/^\d{4}-\d{2}-\d{2}_\d{6}_U_[a-z0-9]{8}$/", + "/^\d{4}-\d{2}-\d{2}_\d{6}\.U_[a-z0-9]{8}$/", $per_run_id_2nd, 'assert per log run id 2nd' ); @@ -824,13 +824,13 @@ final class CoreLibsLoggingLoggingTest extends TestCase $this->assertTrue($log_ok, 'assert ::log (debug) OK'); $this->assertEquals( $log->getLogFile(), - $log->getLogFileId() . '_DEBUG.log' + $log->getLogFileId() . '.DEBUG.log' ); $log_ok = $log->log(Level::Info, 'INFO', group_id: 'GROUP_ID', prefix: 'PREFIX:'); $this->assertTrue($log_ok, 'assert ::log (info) OK'); $this->assertEquals( $log->getLogFile(), - $log->getLogFileId() . '_INFO.log' + $log->getLogFileId() . '.INFO.log' ); }