public function fetchTplImpl($ident, $smParams = null, $returnType = self::FETCH_RETURN_CONTENT, $addResources = false, $cacheId = null) { $this->assertHasAccess($ident); $logMsg = null; if ($this->LOGGER->isEnabled()) { $rqNum = ++self::$FETCH_REQUEST_CNT; $logMsg = "#{$rqNum} Smarty params count: " . count(to_array($smParams)) . ", type: {$returnType}, resources: " . var_export($addResources, true) . ", " . ($cacheId ? "cache id: [{$cacheId}]" : 'nocache'); $this->LOGGER->info("Tpl fetching requested for entity [{$ident}]. {$logMsg}"); FoldedResourcesManager::onEntityAction(FoldedResourcesManager::ACTION_ENTITY_FETCH_REQUESTD, $this, $ident, $logMsg); } $entity = $this->getFoldedEntity($ident); //Сразу установим зависимость от текущей сущности FoldedContextWatcher::getInstance()->setDependsOnEntity($entity); $CTXT = $this->getFoldedContext(); $PCLASS = $CTXT->tplFetchParamsClass(); $PCLASS_BASE = FoldedTplFetchPrams::getClassName(); check_condition(PsUtil::isInstanceOf($PCLASS, $PCLASS_BASE), "Класс [{$PCLASS}] для хранения данных контекста {$CTXT} должен быть подклассом {$PCLASS_BASE}"); //Если мы не возвращаем содержимое, то в любом случае ресурсы добавлять не к чему $addResources = $addResources && !in_array($returnType, array(self::FETCH_RETURN_PARAMS, self::FETCH_RETURN_PARAMS_OB)); $keysRequired = PsUtil::getClassConsts($PCLASS, 'PARAM_'); $keysRequiredParams = array_diff($keysRequired, array(FoldedTplFetchPrams::PARAM_CONTENT)); $PARAMS = null; $PARAMS_KEY = null; $CONTENT = null; $CONTENT_KEY = null; $RETURN_KEY = null; if ($cacheId) { $cacheId = ensure_wrapped_with($cacheId, '[', ']') . '[' . PsDefines::getReplaceFormulesType() . ']'; $RETURN_KEY = $cacheId . '-' . $returnType; if (array_key_exists($ident, $this->FETCH_RETURNS)) { if (array_key_exists($RETURN_KEY, $this->FETCH_RETURNS[$ident])) { return $this->FETCH_RETURNS[$ident][$RETURN_KEY]; } } else { $this->FETCH_RETURNS[$ident] = array(); } $PARAMS_KEY = empty($keysRequiredParams) ? null : $cacheId . '-params'; $CONTENT_KEY = $cacheId . '-content'; switch ($returnType) { case self::FETCH_RETURN_FULL: case self::FETCH_RETURN_FULL_OB: $CONTENT = $this->getFromFoldedCache($ident, $CONTENT_KEY); $PARAMS = $PARAMS_KEY ? $this->getFromFoldedCache($ident, $PARAMS_KEY, $keysRequiredParams) : array(); if ($CONTENT && is_array($PARAMS)) { $CONTENT = $addResources ? $this->getResourcesLinks($ident, $CONTENT) : $CONTENT; $PARAMS[FoldedTplFetchPrams::PARAM_CONTENT] = $CONTENT; switch ($returnType) { case self::FETCH_RETURN_FULL: return $this->FETCH_RETURNS[$ident][$RETURN_KEY] = $PARAMS; case self::FETCH_RETURN_FULL_OB: return $this->FETCH_RETURNS[$ident][$RETURN_KEY] = new $PCLASS($PARAMS); default: raise_error("Unprocessed fetch return type [{$returnType}]."); } } break; case self::FETCH_RETURN_CONTENT: $CONTENT = $this->getFromFoldedCache($ident, $CONTENT_KEY); if ($CONTENT) { $CONTENT = $addResources ? $this->getResourcesLinks($ident, $CONTENT) : $CONTENT; return $this->FETCH_RETURNS[$ident][$RETURN_KEY] = $CONTENT; } break; case self::FETCH_RETURN_PARAMS: case self::FETCH_RETURN_PARAMS_OB: $PARAMS = $PARAMS_KEY ? $this->getFromFoldedCache($ident, $PARAMS_KEY, $keysRequiredParams) : array(); if (is_array($PARAMS)) { switch ($returnType) { case self::FETCH_RETURN_PARAMS: return $this->FETCH_RETURNS[$ident][$RETURN_KEY] = $PARAMS; case self::FETCH_RETURN_PARAMS_OB: return $this->FETCH_RETURNS[$ident][$RETURN_KEY] = new $PCLASS($PARAMS); default: raise_error("Unprocessed fetch return type [{$returnType}]."); } } break; } } $settedNow = false; if (!$entity->equalTo(FoldedContextWatcher::getInstance()->getFoldedEntity())) { $CTXT->setContextWithFoldedEntity($entity); $settedNow = true; } try { $CONTENT = $this->getTpl($ident, $smParams)->fetch(); $entityNow = FoldedContextWatcher::getInstance()->getFoldedEntity(); check_condition($entity->equalTo($entityNow), "After tpl fetching folded entity [{$entity}] chenged to [{$entityNow}]"); $PARAMS_FULL = $CTXT->finalizeTplContent($CONTENT); check_condition(is_array($PARAMS_FULL), "After [{$entity}] tpl finalisation not array is returned"); $keysReturned = array_keys($PARAMS_FULL); if (count(array_diff($keysReturned, $keysRequired)) || count(array_diff($keysRequired, $keysReturned))) { raise_error("After [{$entity}] tpl finalisation required keys: " . array_to_string($keysRequired) . '], returned keys: [' . array_to_string($keysReturned) . ']'); } if ($this->LOGGER->isEnabled()) { $this->LOGGER->info("Tpl fetching actually done for entity [{$ident}]. {$logMsg}"); FoldedResourcesManager::onEntityAction(FoldedResourcesManager::ACTION_ENTITY_FETCH_DONE, $this, $ident, $logMsg); } } catch (Exception $e) { /* * Произошла ошибка! * * Если мы устанавливали контенст и он не поменялся после завершения фетчинга (если поменялся, это ошибка), то нужно его обязательно завершить. * Если контекст был установлен во внешнем блоке, то этот блок должен позаботиться о сбросе контекста. * * Далее от нас требуется только пробросить ошибку наверх. */ if ($settedNow && $entity->equalTo(FoldedContextWatcher::getInstance()->getFoldedEntity())) { $CTXT->dropContext(); } throw $e; } $CONTENT = $PARAMS_FULL[FoldedTplFetchPrams::PARAM_CONTENT]; $PARAMS = $PARAMS_FULL; unset($PARAMS[FoldedTplFetchPrams::PARAM_CONTENT]); if ($PARAMS_KEY) { $this->saveToFoldedCache($PARAMS, $ident, $PARAMS_KEY); } if ($CONTENT_KEY) { $this->saveToFoldedCache($CONTENT, $ident, $CONTENT_KEY); } if ($settedNow) { $CTXT->dropContext(); } if ($addResources) { $CONTENT = $this->getResourcesLinks($ident, $CONTENT); $PARAMS_FULL[FoldedTplFetchPrams::PARAM_CONTENT] = $CONTENT; } switch ($returnType) { case self::FETCH_RETURN_FULL: return $RETURN_KEY ? $this->FETCH_RETURNS[$ident][$RETURN_KEY] = $PARAMS_FULL : $PARAMS_FULL; case self::FETCH_RETURN_FULL_OB: return $RETURN_KEY ? $this->FETCH_RETURNS[$ident][$RETURN_KEY] = new $PCLASS($PARAMS_FULL) : new $PCLASS($PARAMS_FULL); case self::FETCH_RETURN_CONTENT: return $RETURN_KEY ? $this->FETCH_RETURNS[$ident][$RETURN_KEY] = $CONTENT : $CONTENT; case self::FETCH_RETURN_PARAMS: return $RETURN_KEY ? $this->FETCH_RETURNS[$ident][$RETURN_KEY] = $PARAMS : $PARAMS; case self::FETCH_RETURN_PARAMS_OB: return $RETURN_KEY ? $this->FETCH_RETURNS[$ident][$RETURN_KEY] = new $PCLASS($PARAMS) : new $PCLASS($PARAMS); } raise_error("Unknown fetch return type [{$returnType}]."); }
public function getUsedVerses() { return to_array(parent::__get(self::PARAM_VERSES)); }
public function tplFetchParamsClass() { return FoldedTplFetchPrams::getClassName(); }
public function getSmartyParams4Resources() { return to_array(parent::__get(self::PARAM_RESOURCES)); }