|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- <?php
-
-
- namespace yii\base;
-
- use Yii;
- use yii\helpers\FileHelper;
- use yii\widgets\Block;
- use yii\widgets\ContentDecorator;
- use yii\widgets\FragmentCache;
-
-
- class View extends Component
- {
-
-
- const EVENT_BEGIN_PAGE = 'beginPage';
-
-
- const EVENT_END_PAGE = 'endPage';
-
-
- const EVENT_BEFORE_RENDER = 'beforeRender';
-
-
- const EVENT_AFTER_RENDER = 'afterRender';
-
-
-
- public $context;
-
-
- public $params = [];
-
-
- public $renderers;
-
-
- public $defaultExtension = 'php';
-
-
- public $theme;
-
-
- public $blocks;
-
-
- public $cacheStack = [];
-
-
- public $dynamicPlaceholders = [];
-
-
-
- private $_viewFiles = [];
-
-
-
-
- public function init()
- {
- parent::init();
- if (is_array($this->theme)) {
- if (!isset($this->theme['class'])) {
- $this->theme['class'] = 'yii\base\Theme';
- }
- $this->theme = Yii::createObject($this->theme);
- } elseif (is_string($this->theme)) {
- $this->theme = Yii::createObject($this->theme);
- }
- }
-
-
-
- public function render($view, $params = [], $context = null)
- {
- $viewFile = $this->findViewFile($view, $context);
- return $this->renderFile($viewFile, $params, $context);
- }
-
-
-
- protected function findViewFile($view, $context = null)
- {
- if (strncmp($view, '@', 1) === 0) {
-
- $file = Yii::getAlias($view);
- } elseif (strncmp($view, '//', 2) === 0) {
-
- $file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
- } elseif (strncmp($view, '/', 1) === 0) {
-
- if (Yii::$app->controller !== null) {
- $file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
- } else {
- throw new InvalidCallException("Unable to locate view file for view '$view': no active controller.");
- }
- } elseif ($context instanceof ViewContextInterface) {
- $file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;
- } elseif (($currentViewFile = $this->getViewFile()) !== false) {
- $file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view;
- } else {
- throw new InvalidCallException("Unable to resolve view file for view '$view': no active view context.");
- }
-
- if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
- return $file;
- }
- $path = $file . '.' . $this->defaultExtension;
- if ($this->defaultExtension !== 'php' && !is_file($path)) {
- $path = $file . '.php';
- }
-
- return $path;
- }
-
-
-
- public function renderFile($viewFile, $params = [], $context = null)
- {
- $viewFile = Yii::getAlias($viewFile);
-
- if ($this->theme !== null) {
- $viewFile = $this->theme->applyTo($viewFile);
- }
- if (is_file($viewFile)) {
- $viewFile = FileHelper::localize($viewFile);
- } else {
- throw new ViewNotFoundException("The view file does not exist: $viewFile");
- }
-
- $oldContext = $this->context;
- if ($context !== null) {
- $this->context = $context;
- }
- $output = '';
- $this->_viewFiles[] = $viewFile;
-
- if ($this->beforeRender($viewFile, $params)) {
- Yii::trace("Rendering view file: $viewFile", __METHOD__);
- $ext = pathinfo($viewFile, PATHINFO_EXTENSION);
- if (isset($this->renderers[$ext])) {
- if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) {
- $this->renderers[$ext] = Yii::createObject($this->renderers[$ext]);
- }
-
- $renderer = $this->renderers[$ext];
- $output = $renderer->render($this, $viewFile, $params);
- } else {
- $output = $this->renderPhpFile($viewFile, $params);
- }
- $this->afterRender($viewFile, $params, $output);
- }
-
- array_pop($this->_viewFiles);
- $this->context = $oldContext;
-
- return $output;
- }
-
-
-
- public function getViewFile()
- {
- return end($this->_viewFiles);
- }
-
-
-
- public function beforeRender($viewFile, $params)
- {
- $event = new ViewEvent([
- 'viewFile' => $viewFile,
- 'params' => $params,
- ]);
- $this->trigger(self::EVENT_BEFORE_RENDER, $event);
-
- return $event->isValid;
- }
-
-
-
- public function afterRender($viewFile, $params, &$output)
- {
- if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) {
- $event = new ViewEvent([
- 'viewFile' => $viewFile,
- 'params' => $params,
- 'output' => $output,
- ]);
- $this->trigger(self::EVENT_AFTER_RENDER, $event);
- $output = $event->output;
- }
- }
-
-
-
- public function renderPhpFile($_file_, $_params_ = [])
- {
- ob_start();
- ob_implicit_flush(false);
- extract($_params_, EXTR_OVERWRITE);
- require($_file_);
-
- return ob_get_clean();
- }
-
-
-
- public function renderDynamic($statements)
- {
- if (!empty($this->cacheStack)) {
- $n = count($this->dynamicPlaceholders);
- $placeholder = "<![CDATA[YII-DYNAMIC-$n]]>";
- $this->addDynamicPlaceholder($placeholder, $statements);
-
- return $placeholder;
- } else {
- return $this->evaluateDynamicContent($statements);
- }
- }
-
-
-
- public function addDynamicPlaceholder($placeholder, $statements)
- {
- foreach ($this->cacheStack as $cache) {
- $cache->dynamicPlaceholders[$placeholder] = $statements;
- }
- $this->dynamicPlaceholders[$placeholder] = $statements;
- }
-
-
-
- public function evaluateDynamicContent($statements)
- {
- return eval($statements);
- }
-
-
-
- public function beginBlock($id, $renderInPlace = false)
- {
- return Block::begin([
- 'id' => $id,
- 'renderInPlace' => $renderInPlace,
- 'view' => $this,
- ]);
- }
-
-
-
- public function endBlock()
- {
- Block::end();
- }
-
-
-
- public function beginContent($viewFile, $params = [])
- {
- return ContentDecorator::begin([
- 'viewFile' => $viewFile,
- 'params' => $params,
- 'view' => $this,
- ]);
- }
-
-
-
- public function endContent()
- {
- ContentDecorator::end();
- }
-
-
-
- public function beginCache($id, $properties = [])
- {
- $properties['id'] = $id;
- $properties['view'] = $this;
-
- $cache = FragmentCache::begin($properties);
- if ($cache->getCachedContent() !== false) {
- $this->endCache();
-
- return false;
- } else {
- return true;
- }
- }
-
-
-
- public function endCache()
- {
- FragmentCache::end();
- }
-
-
-
- public function beginPage()
- {
- ob_start();
- ob_implicit_flush(false);
-
- $this->trigger(self::EVENT_BEGIN_PAGE);
- }
-
-
-
- public function endPage()
- {
- $this->trigger(self::EVENT_END_PAGE);
- ob_end_flush();
- }
- }
|