Merge pull request #16390 from artfulrobot/tame-propbag-deprecation-logs
authorMatthew Wire <mjw@mjwconsult.co.uk>
Tue, 28 Jan 2020 18:17:00 +0000 (18:17 +0000)
committerGitHub <noreply@github.com>
Tue, 28 Jan 2020 18:17:00 +0000 (18:17 +0000)
Prevent PropertBag from being so noisy about deprecation warnings

1  2 
Civi/Payment/PropertyBag.php

index c8db41457194200f3ed81adb5e1ef1c6004bc741,8a364a258fa48ea98606274eac8860ba6ebd36b6..afdee0abb6908da4660426ea60a30a8cbfd84ce5
@@@ -21,6 -21,10 +21,10 @@@ use CRM_Core_PseudoConstant
   *
   */
  class PropertyBag implements \ArrayAccess {
+   /**
+    * @var array - see legacyWarning */
+   public static $legacyWarnings = [];
    protected $props = ['default' => []];
  
    protected static $propMap = [
@@@ -87,9 -91,7 +91,9 @@@
    }
  
    /**
 -   * @var string Just for unit testing.
 +   * Just for unit testing.
 +   *
 +   * @var string
     */
    public $lastWarning;
  
    }
  
    /**
+    * Log legacy warnings info.
+    *
     * @param string $message
     */
    protected function legacyWarning($message) {
-     $message = "Deprecated code: $message";
+     if (empty(static::$legacyWarnings)) {
+       // First time we have been called.
+       register_shutdown_function([PropertyBag::class, 'writeLegacyWarnings']);
+     }
+     // Store warnings instead of logging immediately, as calls to Civi::log()
+     // can take over half a second to work in some hosting environments.
+     static::$legacyWarnings[$message] = TRUE;
+     // For unit tests:
      $this->lastWarning = $message;
-     Civi::log()->warning($message);
+   }
+   /**
+    * Save any legacy warnings to log.
+    *
+    * Called as a shutdown function.
+    */
+   public static function writeLegacyWarnings() {
+     if (!empty(static::$legacyWarnings)) {
+       $message = "Civi\\Payment\\PropertyBag related deprecation warnings:\n"
+         . implode("\n", array_keys(static::$legacyWarnings));
+       Civi::log()->warning($message, ['civi.tag' => 'deprecated']);
+     }
    }
  
    /**