commit b1b95b1d1dda75735df503f64aed56258235c694
parent 651be9b411b4aa62deba5b1b483d32d2f8bf1a3e
Author: Jan Dankert <develop@jandankert.de>
Date: Tue, 31 May 2022 00:32:27 +0200
New: More context objects for the DSL
Diffstat:
9 files changed, 246 insertions(+), 5 deletions(-)
diff --git a/modules/cms/generator/ValueGenerator.class.php b/modules/cms/generator/ValueGenerator.class.php
@@ -11,6 +11,8 @@ use cms\base\Startup;
use cms\generator\dsl\DslAlert;
use cms\generator\dsl\DslConsole;
use cms\generator\dsl\DslDocument;
+use cms\generator\dsl\DslHttp;
+use cms\generator\dsl\DslJson;
use cms\generator\dsl\DslPage;
use cms\generator\dsl\DslWrite;
use cms\macros\MacroRunner;
@@ -799,6 +801,8 @@ class ValueGenerator extends BaseGenerator
$executor->addContext( [
'console' => new DslConsole(),
'document' => new DslDocument(),
+ 'http' => new DslHttp(),
+ 'json' => new DslJson(),
'write' => new DslWrite(),
'alert' => new DslAlert(),
'page' => new DslPage( $page ),
diff --git a/modules/cms/generator/dsl/DslConsole.class.php b/modules/cms/generator/dsl/DslConsole.class.php
@@ -16,6 +16,10 @@ class DslConsole implements DslObject
{
Logger::debug( $text );
}
+ public function trace( $text )
+ {
+ Logger::trace( $text );
+ }
public function info( $text )
{
Logger::info( $text );
diff --git a/modules/cms/generator/dsl/DslDate.class.php b/modules/cms/generator/dsl/DslDate.class.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace cms\generator\dsl;
+
+use cms\model\Page;
+use cms\model\Project;
+use dsl\context\DslObject;
+
+class DslDate implements DslObject
+{
+ private $time;
+
+ /**
+ * DslPage constructor.
+ */
+ public function __construct($time=null)
+ {
+ if ( $time = null )
+ $time = time();
+
+ $this->time = $time;
+ }
+
+
+ public function getDate() { return date('d',$this->time); }
+ public function getDay() { return date('w',$this->time); }
+ public function getFullYear() { return date('Y',$this->time); }
+ public function getHours() { return date('H',$this->time); }
+ public function getMilliseconds() { return 0; }
+ public function getMinutes() { return date('i',$this->time); }
+ public function getMonth() { return date('m',$this->time); }
+ public function getSeconds() { return date('s',$this->time); }
+ public function getTime() { return $this->time * 1000; }
+ public function getTimezoneOffset() {return date('y',$this->time)/60;}
+ public function getUTCDate() {return date('d',$this->time);}
+ public function getUTCDay() {return date('w',$this->time);}
+ public function getUTCFullYear() {return date('Y',$this->time);}
+ public function getUTCHours() {return date('H',$this->time);}
+ public function getUTCMilliseconds() {return 0;}
+ public function getUTCMinutes() {return date('i',$this->time);}
+ public function getUTCMonth() {return date('m',$this->time);}
+ public function getUTCSeconds() {return date('s',$this->time);}
+ public function getYear() { return date('y',$this->time); }
+}
+\ No newline at end of file
diff --git a/modules/cms/generator/dsl/DslHttp.class.php b/modules/cms/generator/dsl/DslHttp.class.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace cms\generator\dsl;
+
+use dsl\context\DslObject;
+use util\Http;
+use util\json\JSON;
+
+class DslHttp implements DslObject
+{
+ public function get( $url )
+ {
+ return $this->request('GET', $url );
+ }
+
+ public function post($url )
+ {
+ return $this->request('POST', $url );
+ }
+
+ private function request($method, $url ) {
+ $http = new Http( $url );
+ $http->method = $method;
+ $http->request();
+ return $http->body;
+ }
+}
+\ No newline at end of file
diff --git a/modules/cms/generator/dsl/DslJson.class.php b/modules/cms/generator/dsl/DslJson.class.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace cms\generator\dsl;
+
+use dsl\context\DslObject;
+use util\json\JSON;
+
+class DslJson implements DslObject
+{
+
+ public function parse( $text )
+ {
+ return JSON::decode( $text );
+ }
+
+ public function stringify( $text )
+ {
+ return JSON::encode( $text );
+ }
+}
+\ No newline at end of file
diff --git a/modules/cms/generator/dsl/DslProject.class.php b/modules/cms/generator/dsl/DslProject.class.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace cms\generator\dsl;
+
+use cms\model\Folder;
+use cms\model\Page;
+use cms\model\Project;
+use dsl\context\DslObject;
+
+class DslProject implements DslObject
+{
+ private $project;
+
+ public $id;
+
+ /**
+ * DslPage constructor.
+ * @param Project $project
+ */
+ public function __construct($project)
+ {
+ $this->project = $project;
+
+ $this->id = $project->getId();
+ }
+
+ /**
+ * @return array
+ * @throws \util\exception\ObjectNotFoundException
+ */
+ public function languages() {
+ return $this->project->getLanguages();
+ }
+ /**
+ * @return array
+ * @throws \util\exception\ObjectNotFoundException
+ */
+ public function models() {
+ return $this->project->getLanguages();
+
+ }
+ /**
+ * @return DslObject
+ * @throws \util\exception\ObjectNotFoundException
+ */
+ public function root() {
+ $oid = $this->project->getRootObjectId();
+ $folder = new Folder( $oid );
+ $folder->load();
+ return new \cms\generator\dsl\DslObject( $folder );
+ }
+
+}
+\ No newline at end of file
diff --git a/modules/cms/generator/dsl/DslSystem.class.php b/modules/cms/generator/dsl/DslSystem.class.php
@@ -0,0 +1,54 @@
+<?php
+
+namespace cms\generator\dsl;
+
+use cms\base\Startup;
+use dsl\context\DslObject;
+
+class DslSystem implements DslObject
+{
+ /**
+ * application version
+ * @var string
+ */
+ public $version;
+
+ /**
+ * application name
+ * @var string
+ */
+ public $name;
+ /**
+ * build date
+ * @var null
+ */
+ private $build;
+ /**
+ * api version
+ * @var string
+ */
+ private $api;
+ /**
+ * runtime
+ * @var string
+ */
+ private $runtime;
+ /**
+ * Operating system
+ * @var string
+ */
+ private $os;
+
+ public function __construct()
+ {
+ $this->version = Startup::VERSION;
+ $this->build = Startup::DATE;
+ $this->name = Startup::TITLE;
+ $this->api = Startup::API_LEVEL;
+ $this->runtime = PHP_VERSION;
+ $this->os = PHP_OS;
+ }
+
+ public function now() { return new DslDate(); }
+ public function env( $name ) { if ( substr($name,0,8) == 'CMS_DSL_') return getenv($name); else return null; }
+}
+\ No newline at end of file
diff --git a/modules/dsl/DslAstParser.class.php b/modules/dsl/DslAstParser.class.php
@@ -22,7 +22,7 @@ class DslAstParser
$this->rootStatement = new DslStatementList( $tokens );
- echo "<h1>AST:</h1><pre>"; var_export( $this->rootStatement ); echo "</pre>";
+ //echo "<h1>AST:</h1><pre>"; var_export( $this->rootStatement ); echo "</pre>";
}
diff --git a/modules/dsl/standard/StandardMath.class.php b/modules/dsl/standard/StandardMath.class.php
@@ -3,9 +3,39 @@ namespace dsl\standard;
class StandardMath
{
- public function sqrt( $number ) {
-
- return sqrt( $number );
- }
+ public $E = M_EULER;
+ public $PI = M_PI;
+ public $LN2 = M_LN2;
+ public $LN10 = M_LN10;
+ public $SQRT1_2 = M_SQRT1_2;
+ public $SQRT2 = M_SQRT2;
+ public function sqrt($x) { return sqrt( $x ); }
+ public function abs($x) { return abs($x);}
+ public function acos($x) { return ($x); }
+ public function acosh($x) { return ($x); }
+ public function asin($x) { return ($x); }
+ public function asinh($x) { return ($x); }
+ public function atan($x) { return atan($x); }
+ public function atanh($x) { return atanh($x); }
+ public function atan2($y, $x) { return atan2($y,$x); }
+ public function cbrt($x) { return ($x); }
+ public function ceil($x) { return ceil($x); }
+ public function cos($x) { return cos($x); }
+ public function cosh($x) { return cosh($x); }
+ public function exp($x) { return exp($x); }
+ public function expm1($x) { return expm1($x); }
+ public function floor($x) { return floor($x); }
+ public function log($x) { return ($x); }
+ public function log1p($x) { return log($x); }
+ public function log10($x) { return log10($x); }
+ public function max($x,$y) { return max($x,$y); }
+ public function min($x,$y) { return min($x,$y); }
+ public function pow($x,$y) { return pow($x,$y); }
+ public function random() { return rand(); }
+ public function round($x) { return round($x); }
+ public function sin($x) { return sin($x); }
+ public function sinh($x) { return sinh($x); }
+ public function tan($x) { return tan($x); }
+ public function tanh($x) { return tanh($x); }
}
\ No newline at end of file