Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Component / Utility / ToStringTrait.php
1 <?php
2
3 namespace Drupal\Component\Utility;
4
5 /**
6  * Wraps __toString in a trait to avoid some fatals.
7  */
8 trait ToStringTrait {
9
10   /**
11    * Implements the magic __toString() method.
12    */
13   public function __toString() {
14     try {
15       return (string) $this->render();
16     }
17     catch (\Exception $e) {
18       // User errors in __toString() methods are considered fatal in the Drupal
19       // error handler.
20       trigger_error(get_class($e) . ' thrown while calling __toString on a ' . get_class($this) . ' object in ' . $e->getFile() . ' on line ' . $e->getLine() . ': ' . $e->getMessage(), E_USER_ERROR);
21       // In case we are using another error handler that did not fatal on the
22       // E_USER_ERROR, we terminate execution. However, for test purposes allow
23       // a return value.
24       return $this->_die();
25     }
26   }
27
28   /**
29    * For test purposes, wrap die() in an overridable method.
30    */
31   protected function _die() {
32     die();
33   }
34
35   /**
36    * Renders the object as a string.
37    *
38    * @return string|object
39    *   The rendered string or an object implementing __toString().
40    */
41   abstract public function render();
42
43 }