Files
kupshop/class/smarty_plugins/compiler.extends_parent.php
2025-08-02 16:30:27 +02:00

71 lines
2.0 KiB
PHP

<?php
/**
* Smarty Internal Plugin Compile extend
* Compiles the {extends} tag.
*
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile extend Class.
*/
class Smarty_Compiler_Extends_Parent extends Smarty_Internal_Compile_Extends
{
public function getAttributes($compiler, $attributes)
{
return [
'file' => $this->getParent($compiler, $attributes),
];
}
protected function getParent(object $compiler, array $attributes)
{
$_template = $compiler->template;
$source = $compiler->template->source;
$bundle = '';
if (!$bundle) {
if ($source->smarty->_current_file === null && $_template) {
$filePath = $_template->source->filepath;
} else {
$filePath = $source->smarty->_current_file;
}
$directories = $source->smarty->getTemplateDir();
foreach ($directories as $dir_bundle => $_directory) {
if (\KupShop\KupShopBundle\Util\StringUtil::startsWith($filePath, $_directory)) {
$bundle = $dir_bundle;
break;
}
}
if (!$bundle) {
throw new \Exception('Cannot detect current bundle');
}
}
$current_file = preg_replace('/\[(.*)\]/i', '', $_template->source->name);
$directories = $source->smarty->getTemplateDir();
$found = false;
foreach ($directories as $dir_bundle => $_directory) {
if (!$found) {
if ($bundle == $dir_bundle) {
$found = true;
}
} else {
$_filepath = $_directory.$current_file;
// TODO: chybejici metoda fileExists
// if ($this->fileExists($source, $_filepath))
if (file_exists($_filepath)) {
return "'[{$dir_bundle}]{$current_file}'";
}
}
}
throw new \Exception('Extended template doesn\'t found!');
}
}