Skip to content

Commit dcb1ce3

Browse files
TybazethePanz
authored andcommitted
PHP 8.1 > strftime deprecated.
2 possible workaround, use partial implementation with php date method or use IntlDateFormatter. As symfony1 is not intended to evolve, it should not rely on currently unused PHP-Extension (nor update composer.json). I propose a fix to replace "strftime" by "date", with a translation of format for PHP 8.1+ It will lose the ability of translating date of logs (does anybody use it ?) and it will deprecated a few strftime format (probably not used) Performances should remains OK I only tested it with a few formats (the default one and a few others) If you need some missing strftime formats, please add them.
1 parent 029b0e7 commit dcb1ce3

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

lib/log/sfFileLogger.class.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ public function initialize(sfEventDispatcher $dispatcher, $options = array())
9292
* Logs a message.
9393
*
9494
* @param string $message Message
95-
* @param int $priority Message priority
95+
* @param int $priority Message priority
9696
*/
9797
protected function doLog($message, $priority)
9898
{
9999
flock($this->fp, LOCK_EX);
100100
fwrite($this->fp, strtr($this->format, array(
101101
'%type%' => $this->type,
102102
'%message%' => $message,
103-
'%time%' => strftime($this->timeFormat),
103+
'%time%' => self::strftime($this->timeFormat),
104104
'%priority%' => $this->getPriority($priority),
105105
'%EOL%' => PHP_EOL,
106106
)));
@@ -129,4 +129,60 @@ public function shutdown()
129129
fclose($this->fp);
130130
}
131131
}
132+
133+
/**
134+
* @param $format
135+
* @return false|string
136+
*/
137+
public static function strftime($format)
138+
{
139+
if (version_compare(PHP_VERSION, '8.1.0') < 0) {
140+
return strftime($format);
141+
}
142+
return date(self::_strftimeFormatToDateFormat($format));
143+
}
144+
145+
/**
146+
* Try to Convert a strftime to date format
147+
*
148+
* Unable to find a perfect implementation, based on those one (Each contains some errors)
149+
* https://github.com/Fabrik/fabrik/blob/master/plugins/fabrik_element/date/date.php
150+
* https://gist.github.com/mcaskill/02636e5970be1bb22270
151+
* https://stackoverflow.com/questions/22665959/using-php-strftime-using-date-format-string
152+
*
153+
* Limitation:
154+
* - Do not apply translation
155+
* - Some few strftime format could be broken (low probability to be used on logs)
156+
*
157+
* Private: because it should not be used outside of this scope
158+
*
159+
* A better solution is to use : IntlDateFormatter, but it will require to load a new php extension, which could break some setup.
160+
*
161+
* @return array|string|string[]
162+
*/
163+
private static function _strftimeFormatToDateFormat($strftimeFormat) {
164+
165+
// Missing %V %C %g %G
166+
$search = array(
167+
'%a', '%A', '%d', '%e', '%u',
168+
'%w', '%W', '%b', '%h', '%B',
169+
'%m', '%y', '%Y', '%D', '%F',
170+
'%x', '%n', '%t', '%H', '%k',
171+
'%I', '%l', '%M', '%p', '%P',
172+
'%r' /* %I:%M:%S %p */, '%R' /* %H:%M */, '%S', '%T' /* %H:%M:%S */, '%X', '%z', '%Z',
173+
'%c', '%s', '%j',
174+
'%%');
175+
176+
$replace = array(
177+
'D', 'l', 'd', 'j', 'N',
178+
'w', 'W', 'M', 'M', 'F',
179+
'm', 'y', 'Y', 'm/d/y', 'Y-m-d',
180+
'm/d/y', "\n", "\t", 'H', 'G',
181+
'h', 'g', 'i', 'A', 'a',
182+
'h:i:s A', 'H:i', 's', 'H:i:s', 'H:i:s', 'O', 'T',
183+
'D M j H:i:s Y' /*Tue Feb 5 00:45:10 2009*/, 'U', 'z',
184+
'%');
185+
186+
return str_replace($search, $replace, $strftimeFormat);
187+
}
132188
}

test/unit/log/sfFileLoggerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of the symfony package.
55
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6-
*
6+
*
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
@@ -61,7 +61,7 @@ protected function getPriority($priority)
6161
unlink($file);
6262
$logger = new TestLogger($dispatcher, array('file' => $file));
6363
$logger->log('foo');
64-
$t->is(file_get_contents($file), strftime($logger->getTimeFormat()).' symfony [*6*] foo'.PHP_EOL, '->initialize() can take a format option');
64+
$t->is(file_get_contents($file), TestLogger::strftime($logger->getTimeFormat()).' symfony [*6*] foo'.PHP_EOL, '->initialize() can take a format option');
6565

6666
unlink($file);
6767
$logger = new TestLogger($dispatcher, array('file' => $file, 'format' => '%message%'));
@@ -73,14 +73,14 @@ protected function getPriority($priority)
7373
unlink($file);
7474
$logger = new TestLogger($dispatcher, array('file' => $file, 'time_format' => '%Y %m %d'));
7575
$logger->log('foo');
76-
$t->is(file_get_contents($file), strftime($logger->getTimeFormat()).' symfony [*6*] foo'.PHP_EOL, '->initialize() can take a format option');
76+
$t->is(file_get_contents($file), TestLogger::strftime($logger->getTimeFormat()).' symfony [*6*] foo'.PHP_EOL, '->initialize() can take a format option');
7777

7878
// option: type
7979
$t->diag('option: type');
8080
unlink($file);
8181
$logger = new TestLogger($dispatcher, array('file' => $file, 'type' => 'foo'));
8282
$logger->log('foo');
83-
$t->is(file_get_contents($file), strftime($logger->getTimeFormat()).' foo [*6*] foo'.PHP_EOL, '->initialize() can take a format option');
83+
$t->is(file_get_contents($file), TestLogger::strftime($logger->getTimeFormat()).' foo [*6*] foo'.PHP_EOL, '->initialize() can take a format option');
8484

8585
// ->shutdown()
8686
$t->diag('->shutdown()');

0 commit comments

Comments
 (0)