wiki.techinc.nl/includes/parser/DateFormatterFactory.php
Tim Starling 76ca6c9b18 Rehabilitate DateFormatter
This code is surprisingly little changed since I added the class in
November 2003, and needs some modernisation.

* Remove the "linked" option, unused since 1.21. Similarly, make the
  "match-whole" option implied. This allows the regexes to be
  simplified. Nothing will be broken, according to CodeSearch.
* Instead of ucfirst(), use the canonical month name from the language.
  This will work with e.g. French which does not capitalise month names.
* Stop caching DateFormatter instances in APC. Caching was added
  in 2005 when initialisation was being done on every request, but now
  it is only needed when parsing a page with {{#formatdate}}, which is
  rarely, and the constructor overhead is only 200µs after Language
  object data initialisation. Instead, use an in-process cache via a
  factory service.
* Add docs and extra tests.
* Remove todo note obsolete since 38 minutes after the original commit.
* Rename many variables.
* Use double-slash comments
* Don't store the Language object, just get arrays.
* Use mb_strtolower() instead of Language::lc() -- any customisation of
  Language::lc() would break PCRE case-insensitive matching.
* Use named subpatterns instead of "keys"
* Remove the ISO1/ISO2 distinction, the only difference was linking.
* Use closure variables instead of temporary object members

Change-Id: I25fb1203dba2930724d7bc28ad0d51f59f88e1ea
2019-04-10 21:00:04 +10:00

18 lines
363 B
PHP

<?php
class DateFormatterFactory {
/** @var DateFormatter[] */
private $instances;
/**
* @param Language $lang
* @return DateFormatter
*/
public function get( Language $lang ) {
$code = $lang->getCode();
if ( !isset( $this->instances[$code] ) ) {
$this->instances[$code] = new DateFormatter( $lang );
}
return $this->instances[$code];
}
}