openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit a58fa93a643b9904db1361f0563eea442209dcf1
parent ac3ba1741026afdcccd70e3b44a2363599ad45f9
Author: Jan Dankert <develop@jandankert.de>
Date:   Thu,  2 Jun 2022 01:50:05 +0200

Refactoring: DSL Interpreter is now using a write buffer

Diffstat:
Mmodules/cms/generator/ValueGenerator.class.php | 16+++++-----------
Dmodules/cms/generator/dsl/DslDocument.class.php | 14--------------
Mmodules/cms/generator/dsl/DslFolder.class.php | 2+-
Dmodules/cms/generator/dsl/DslWrite.class.php | 15---------------
Mmodules/cms/ui/themes/default/script/openrat/workbench.min.js | 23++++++++++++++---------
Mmodules/dsl/executor/DslInterpreter.class.php | 31++++++++++++++++++++++++++++++-
Amodules/dsl/standard/Write.class.php | 16++++++++++++++++
7 files changed, 66 insertions(+), 51 deletions(-)

diff --git a/modules/cms/generator/ValueGenerator.class.php b/modules/cms/generator/ValueGenerator.class.php @@ -873,15 +873,11 @@ class ValueGenerator extends BaseGenerator break; case self::CODE_SCRIPT: - ob_start(); $executor = new DslInterpreter(); $executor->addContext( [ 'console' => new DslConsole(), - 'document' => new DslDocument(), 'http' => new DslHttp(), 'json' => new DslJson(), - 'write' => new DslWrite(), - 'alert' => new DslAlert(), 'page' => new DslPage( $page ), ]); @@ -889,15 +885,15 @@ class ValueGenerator extends BaseGenerator $executor->runCode( $element->code ); } catch( DslException $e ) { - if ( $pageContext->scheme == Producer::SCHEME_PREVIEW ) - echo $e->getMessage(); Logger::warn( $e ); + if ( $pageContext->scheme == Producer::SCHEME_PREVIEW ) + $inhalt = $e->getMessage(); + break; } - $output = ob_get_contents(); - ob_end_clean(); // Ausgabe ermitteln. - $inhalt = $output; + $inhalt = $executor->getOutput(); + break; case self::CODE_MUSTACHE: // TODO @@ -1256,11 +1252,9 @@ class ValueGenerator extends BaseGenerator $executor->addContext( [ 'console' => new DslConsole(), - 'document' => new DslDocument(), 'value' => $inhalt, 'http' => new DslHttp(), 'json' => new DslJson(), - 'write' => new DslWrite(), ]); try { diff --git a/modules/cms/generator/dsl/DslDocument.class.php b/modules/cms/generator/dsl/DslDocument.class.php @@ -1,13 +0,0 @@ -<?php - -namespace cms\generator\dsl; - -use dsl\context\DslObject; - -class DslDocument implements DslObject -{ - public $write; - public function write( $text ) { - echo $text; - } -} -\ No newline at end of file diff --git a/modules/cms/generator/dsl/DslFolder.class.php b/modules/cms/generator/dsl/DslFolder.class.php @@ -21,7 +21,7 @@ class DslFolder implements DslContextObject */ public function __construct($folder) { - $this->folder = folder; + $this->folder = $folder; $this->id = $folder->getId(); } diff --git a/modules/cms/generator/dsl/DslWrite.class.php b/modules/cms/generator/dsl/DslWrite.class.php @@ -1,14 +0,0 @@ -<?php - -namespace cms\generator\dsl; - -use dsl\context\DslFunction; - -class DslWrite implements DslFunction -{ - - public function execute( $text ) - { - echo $text; - } -} -\ No newline at end of file diff --git a/modules/cms/ui/themes/default/script/openrat/workbench.min.js b/modules/cms/ui/themes/default/script/openrat/workbench.min.js @@ -279,7 +279,8 @@ let link = e.currentTarget; e.dataTransfer.effectAllowed = 'link'; e.dataTransfer.setData('id' , link.dataset.id ); e.dataTransfer.setData('action', link.dataset.action); -console.debug('drag started',link,e.dataTransfer); +e.dataTransfer.setData('name' , link.dataset.name || link.textContent.trim() ); +console.debug('drag started',e.dataTransfer); }) .on('drag',(e)=>{ }) @@ -291,14 +292,18 @@ registerDroppable(viewEl) { $(viewEl).find('.or-droppable-selector').on('dragover', (e) => { e.preventDefault(); }).on('drop', (event) => { -let data = event.dataTransfer.getData('text'); -console.debug('dropped:', dropped); -let id = $(data).find('.or-link').data('id'); -let name = $(data).find('.or-navtree-text').text(); +let id = event.dataTransfer.getData('id'); +if ( !id) { +console.debug("dropped object has no object id, ignoring"); +return; +} +let name = event.dataTransfer.getData('name'); if (!name) name = id; -$(this).find('.or-selector-link-value').val(id); -$(this).find('.or-selector-link-name').val(name).attr('placeholder', name); +console.debug("dropped",id,name,event.dataTransfer ); +$(event.currentTarget).find('.or-selector-link-value').val(id); +$(event.currentTarget).find('.or-selector-link-name').val(name).attr('placeholder', name); +event.preventDefault(); }); $(viewEl).find('.or-droppable') } @@ -526,7 +531,7 @@ load .then( response => response.text() ) .then( html => { let $ul = $.create('ul' ).addClass('navtree-list'); -$ul.appendTo( $targetElement ).append( html ); +$ul.appendTo( $targetElement ).html( html ); $ul.find('li').orTree( { 'openAction' : function(name,action,id) { @@ -567,7 +572,7 @@ onSearchInactive: function() { $(this).parent('or-selector').removeClass('selector-search--is-active' ); }, dropdown: '.or-act-selector-search-results', -resultEntryClass: 'or-search-result-entry', +resultEntryClass: 'search-result-entry', select: function(obj) { $($element).find('.or-selector-link-value').val(obj.id ); $($element).find('.or-selector-link-name' ).val(obj.name).attr('placeholder',obj.name); diff --git a/modules/dsl/executor/DslInterpreter.class.php b/modules/dsl/executor/DslInterpreter.class.php @@ -5,15 +5,26 @@ namespace dsl\executor; use dsl\DslAstParser; use dsl\DslException; use dsl\DslLexer; -use dsl\DslParserException; use dsl\standard\StandardArray; use dsl\standard\StandardDate; use dsl\standard\StandardMath; +use dsl\standard\Write; class DslInterpreter { + /** + * Execution context. + * + * @var array + */ private $context = []; + /** + * Holds a reference to the write()-Function for getting the output buffer after execution. + * @var Write + */ + private $writer; + public function __construct() { // Standard-Globals @@ -21,6 +32,7 @@ class DslInterpreter 'Math' => new StandardMath(), 'Array' => new StandardArray(), 'Date' => new StandardDate(), + 'write' => $this->writer = new Write(), ] ); } @@ -37,15 +49,32 @@ class DslInterpreter /** * Parses and runs the DSL code. * + * @param $code String Script-Code * @throws DslException + * @return mixed value of last return statement (if any) */ public function runCode( $code ) { + + // Step 1: Splitting the source code into tokens (the "Lexer") $lexer = new DslLexer(); $token = $lexer->tokenize( $code ); + // Step 2: Creating a syntax tree (abstract syntax tree, AST). $parser = new DslAstParser(); $parser->parse( $token ); + // Step 3: Executing the syntax tree. return $parser->execute( $this->context ); } + + + /** + * Gets the output which was written by the code. + * + * @return mixed + */ + public function getOutput() { + + return $this->writer->buffer; + } } \ No newline at end of file diff --git a/modules/dsl/standard/Write.class.php b/modules/dsl/standard/Write.class.php @@ -0,0 +1,15 @@ +<?php + +namespace dsl\standard; + +use dsl\context\DslFunction; + +class Write implements DslFunction +{ + public $buffer; + + public function execute( $text ) + { + $this->buffer .= $text; + } +} +\ No newline at end of file