File modules/cms/macros/MacroRunner.class.php
Last commit: Sat Jan 28 19:10:47 2023 +0100 Jan Dankert New: Templates may be rendered as Mustache, Script and Script template.
1 <?php 2 3 4 namespace cms\macros; 5 6 use cms\generator\PageContext; 7 use cms\model\Element; 8 use cms\model\PageContent; 9 use cms\model\Template; 10 use cms\model\Value; 11 use logger\Logger; 12 use util\ArrayUtils; 13 use util\exception\GeneratorException; 14 use util\text\variables\VariableResolver; 15 16 class MacroRunner 17 { 18 public $page; 19 20 /** 21 * @param $name 22 * @param $parameter 23 * @param $page 24 * @param $pageContext PageContext 25 * @return string 26 * @throws GeneratorException 27 */ 28 public function executeMacro($name, $parameter, $page, $pageContext) 29 { 30 $this->page = $page; 31 32 $className = 'cms\macros\macro\\'.$name; 33 $output = ''; 34 35 if (!class_exists($className)) 36 throw new GeneratorException('Class not found:' . $className); 37 38 /** @var \util\Macro $macro */ 39 $macro = new $className; 40 41 if (!method_exists($macro, 'execute')) 42 throw new GeneratorException(' (missing method: execute())'); 43 44 $macro->setPageContext($pageContext); 45 46 $resolver = new VariableResolver(); 47 48 $resolver->namespaceSeparator = ':'; 49 $resolver->defaultSeparator = '?'; 50 $resolver->addResolver('setting',function ($var) { 51 return ArrayUtils::getSubValue($this->page->getSettings(), explode('.', $var)); 52 }); 53 $resolver->addResolver('element',function ($var) use($pageContext) { 54 $template = new Template($this->page->templateid); 55 $elements = $template->getElementNames(); 56 $elementid = array_search($var, $elements); 57 58 $element = new Element($elementid); 59 $element->load(); 60 61 $pageContent = new PageContent(); 62 $pageContent->pageId = $this->page->pageid; 63 $pageContent->languageid = $pageContext->languageId; 64 $pageContent->elementId = $elementid; 65 $pageContent->load(); 66 67 $value = new Value(); 68 $value->contentid = $pageContent->contentId; 69 $value->load(); 70 71 return $value->getRawValue($element->typeid); 72 }); 73 74 $parameters = $resolver->resolveVariablesInArray($parameter); 75 76 foreach ($parameters as $param_name => $param_value) { 77 78 if (!property_exists($macro, $param_name)) { 79 80 Logger::warn( "Unknown parameter $param_name in macro $className" ); 81 continue; 82 } 83 84 if ( DEVELOPMENT ) 85 Logger::trace("Setting parameter for Macro-class $className, " . $param_name . ':' . print_r($param_value, true)); 86 87 // Die Parameter der Makro-Klasse typisiert setzen. 88 if (is_int($macro->$param_name)) 89 $macro->$param_name = intval($param_value); 90 elseif (is_array($macro->$param_name)) 91 $macro->$param_name = (array)$param_value; 92 else 93 $macro->$param_name = $param_value; 94 95 } 96 97 ob_start(); 98 99 $macro->execute(); 100 101 $output .= ob_get_contents(); 102 103 ob_end_clean(); 104 105 return $output; 106 } 107 }
Downloadmodules/cms/macros/MacroRunner.class.php
History Sat, 28 Jan 2023 19:10:47 +0100 Jan Dankert New: Templates may be rendered as Mustache, Script and Script template. Fri, 11 Mar 2022 19:26:18 +0100 dankert Performance: Do "print_r" only in development mode. Sun, 5 Dec 2021 20:33:24 +0100 dankert Cleanup: Removed unusable properties from class 'Value' and 'BaseObject'. Sun, 5 Dec 2021 15:33:29 +0100 dankert Cleanup: Removed unusable properties from class 'Value'. Mon, 21 Sep 2020 23:44:23 +0200 Jan Dankert Fixing editing of values. Mon, 21 Sep 2020 22:48:59 +0200 Jan Dankert Complexe refactoring: Moving all generation logic from the model (Value,Page,File) to generators classes. Sat, 29 Aug 2020 03:23:06 +0200 Jan Dankert Refactoring: Improved Exception-Handling; New: Generating pages using a page context which considers page aliases. Wed, 13 May 2020 23:29:44 +0200 Jan Dankert Refactoring: New Variable Resolver with support for namespaces, default values and nested value expressions. Sun, 23 Feb 2020 04:49:34 +0100 Jan Dankert Refactoring with Namespaces for the cms modules, part 2. Sun, 23 Feb 2020 04:01:30 +0100 Jan Dankert Refactoring with Namespaces for the cms modules, part 1: moving.