Last active
August 5, 2018 18:46
-
-
Save lagbox/919e8383a151f6315a695c723eae2c46 to your computer and use it in GitHub Desktop.
view factory share later
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// View/Factory.php | |
... | |
public function shareLater($key, callable $value) | |
{ | |
$keys = is_array($key) ? $key : [$key => $value]; | |
foreach ($keys as $key => $value) { | |
$this->share($key, new Shareable($this->container, $value)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function boot() | |
{ | |
View::shareLater('currentUser', function () { | |
return auth()->user(); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// View/View.php | |
protected function gatherData() | |
{ | |
$data = array_merge($this->factory->getShared(), $this->data); | |
foreach ($data as $key => $value) { | |
if ($value instanceof Renderable) { | |
$data[$key] = $value->render(); | |
} elseif ($value instanceof Shareable) { | |
$data[$key] = $value->resolve(); | |
} | |
} | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment