Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / libs / filebackend / FileBackend.php
index a80b6d0..4ad48c7 100644 (file)
@@ -30,6 +30,7 @@
 use Psr\Log\LoggerAwareInterface;
 use Psr\Log\LoggerInterface;
 use Wikimedia\ScopedCallback;
+use Psr\Log\NullLogger;
 
 /**
  * @brief Base class for all file backend classes (including multi-write backends).
@@ -114,7 +115,7 @@ abstract class FileBackend implements LoggerAwareInterface {
        protected $fileJournal;
        /** @var LoggerInterface */
        protected $logger;
-       /** @var object|string Class name or object With profileIn/profileOut methods */
+       /** @var callable|null */
        protected $profiler;
 
        /** @var callable */
@@ -156,14 +157,14 @@ abstract class FileBackend implements LoggerAwareInterface {
         *   - obResetFunc : alternative callback to clear the output buffer
         *   - streamMimeFunc : alternative method to determine the content type from the path
         *   - logger : Optional PSR logger object.
-        *   - profiler : Optional class name or object With profileIn/profileOut methods.
+        *   - profiler : Optional callback that takes a section name argument and returns
+        *      a ScopedCallback instance that ends the profile section in its destructor.
         * @throws InvalidArgumentException
         */
        public function __construct( array $config ) {
                $this->name = $config['name'];
-               $this->domainId = isset( $config['domainId'] )
-                       ? $config['domainId'] // e.g. "my_wiki-en_"
-                       : $config['wikiId']; // b/c alias
+               $this->domainId = $config['domainId'] // e.g. "my_wiki-en_"
+                       ?? $config['wikiId']; // b/c alias
                if ( !preg_match( '!^[a-zA-Z0-9-_]{1,255}$!', $this->name ) ) {
                        throw new InvalidArgumentException( "Backend name '{$this->name}' is invalid." );
                } elseif ( !is_string( $this->domainId ) ) {
@@ -187,7 +188,10 @@ abstract class FileBackend implements LoggerAwareInterface {
                $this->statusWrapper = $config['statusWrapper'] ?? null;
 
                $this->profiler = $config['profiler'] ?? null;
-               $this->logger = $config['logger'] ?? new \Psr\Log\NullLogger();
+               if ( !is_callable( $this->profiler ) ) {
+                       $this->profiler = null;
+               }
+               $this->logger = $config['logger'] ?? new NullLogger();
                $this->statusWrapper = $config['statusWrapper'] ?? null;
                $this->tmpDirectory = $config['tmpDirectory'] ?? null;
        }
@@ -1572,11 +1576,10 @@ abstract class FileBackend implements LoggerAwareInterface {
         *   - StatusValue::newGood() if this method is called without parameters
         *   - StatusValue::newFatal() with all parameters to this method if passed in
         *
-        * @param string $args,...
+        * @param string ...$args
         * @return StatusValue
         */
-       final protected function newStatus() {
-               $args = func_get_args();
+       final protected function newStatus( ...$args ) {
                if ( count( $args ) ) {
                        $sv = StatusValue::newFatal( ...$args );
                } else {
@@ -1599,12 +1602,7 @@ abstract class FileBackend implements LoggerAwareInterface {
         * @return ScopedCallback|null
         */
        protected function scopedProfileSection( $section ) {
-               if ( $this->profiler ) {
-                       call_user_func( [ $this->profiler, 'profileIn' ], $section );
-                       return new ScopedCallback( [ $this->profiler, 'profileOut' ], [ $section ] );
-               }
-
-               return null;
+               return $this->profiler ? ( $this->profiler )( $section ) : null;
        }
 
        protected function resetOutputBuffer() {