Skip to content

Commit

Permalink
add callback hook for building mailable data
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 18, 2018
1 parent f6e8f65 commit 7dc3d8d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ class Mailable implements MailableContract, Renderable
*/
public $callbacks = [];

/**
* The callback that should be invoked while building the view data.
*
* @var callable
*/
public static $viewDataCallback;

/**
* Send the message using the given mailer.
*
Expand Down Expand Up @@ -263,6 +270,10 @@ public function buildViewData()
{
$data = $this->viewData;

if (static::$viewDataCallback) {
$data = array_merge($data, call_user_func(static::$viewDataCallback, $this));
}

foreach ((new ReflectionClass($this))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
if ($property->getDeclaringClass()->getName() !== self::class) {
$data[$property->getName()] = $property->getValue($this);
Expand Down Expand Up @@ -793,6 +804,17 @@ public function withSwiftMessage($callback)
return $this;
}

/**
* Register a callback to be called while building the view data.
*
* @param callable $callback
* @return void
*/
public static function buildViewDataUsing(callable $callback)
{
static::$viewDataCallback = $callback;
}

/**
* Dynamically bind parameters to the message.
*
Expand Down

0 comments on commit 7dc3d8d

Please sign in to comment.