Created
November 15, 2017 16:56
-
-
Save SamMousa/df84a0c3d0a3db00e93b695ecbeafd15 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
namespace SamIT\Yii2\Behaviors; | |
use yii\base\Behavior; | |
use yii\base\InvalidConfigException; | |
use yii\base\View; | |
use yii\base\ViewEvent; | |
use yii\base\ViewRenderer; | |
class ScopedViewRenderer extends ViewRenderer | |
{ | |
public $params; | |
public $renderer; | |
public function render($view, $file, $params) | |
{ | |
$params = array_merge($this->params, $params); | |
if (!isset($this->renderer)) { | |
return $view->renderPhpFile($file, $params); | |
} | |
if (!$this->renderer instanceof ViewRenderer) { | |
$this->renderer = \Yii::createObject($this->renderer); | |
} | |
return $this->render($view, $file, $params); | |
} | |
} | |
/** | |
* Class ScopedViewParams | |
* @property View $owner | |
* @package SamIT\Yii2\Behaviors | |
*/ | |
class ScopedViewParams extends Behavior | |
{ | |
/** | |
* @var array Map of view file name to array of params. | |
*/ | |
private $scopedParams = []; | |
/** | |
* @param $viewFile Must be the fully resolved view file name. | |
* @param $params | |
*/ | |
public function setScopedParams($viewFile, $params) | |
{ | |
$this->scopedParams[$viewFile] = $params; | |
} | |
public function attach($owner) | |
{ | |
if (!$owner instanceof View) { | |
throw new InvalidConfigException('This behavior must be attached to an instance of \yii\base\View'); | |
} | |
parent::attach($owner); | |
} | |
public function events() | |
{ | |
return [ | |
View::EVENT_BEFORE_RENDER => 'beforeRender' | |
]; | |
} | |
/** | |
* Replace renderer for the view file. | |
* @param ViewEvent $event | |
*/ | |
public function beforeRender(ViewEvent $event) | |
{ | |
if (!isset($this->scopedParams[$event->viewFile])) { | |
return; | |
} | |
// Replace | |
$ext = pathinfo($event->viewFile, PATHINFO_EXTENSION); | |
if (!isset($this->owner->renderers[$ext])) { | |
// Simple case, the file is a PHP file which is rendered directly by the view. | |
$this->owner->renderers[$ext] = new ScopedViewRenderer([ | |
'params' => $this->scopedParams[$event->viewFile], | |
]); | |
} | |
// In case there is a configuration we replace it. | |
if (is_string($this->owner->renderers[$ext]) || is_array($this->owner->renderers[$ext])) { | |
$this->owner->renderers[$ext] = new ScopedViewRenderer([ | |
'params' => $this->scopedParams[$event->viewFile], | |
'renderer' => $this->owner->renderers[$ext] | |
]); | |
} | |
// In case there is already a class. | |
if (!$this->owner->renderers[$ext] instanceof ScopedViewRenderer) { | |
$this->owner->renderers[$ext] = new ScopedViewRenderer([ | |
'params' => $this->scopedParams[$event->viewFile], | |
'renderer' => $this->owner->renderers[$ext] | |
]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment