Permalink
Browse files

Added simple object support.

  • Loading branch information...
theothernic committed Nov 19, 2011
1 parent 0109ee8 commit e62dae0e290c1371c28972eea656c42b34f42e16
Showing with 55 additions and 2 deletions.
  1. +55 −2 class.jsbuilder.php
View
@@ -14,18 +14,28 @@
a project.
*/
class SimpleJSBuilder {
+ private $namespace = null;
+ private $minify = false;
private $vars = array();
private $functions = array();
+ private $objects = array();
private $comments = array();
private $header = '';
protected $output;
- function __construct($displayType = 'file') {
+ function __construct($minify = false, $ns = null, $displayType = 'file') {
+ if (!is_null($ns)) {
+ $this->namespace = $ns;
+ }
+
+ $this->minify = $minify;
+
$this->displayType = $displayType;
}
function __compileJS() {
$this->output .= $this->__compileHeader();
+ $this->output .= $this->__compileObjects();
$this->output .= $this->__compileVars();
$this->output .= $this->__compileFunctions();
$this->output .= $this->__compileFooter();
@@ -70,7 +80,32 @@ function __compileVars() {
} else {
$varList .= $this->genStringVar($name, $value);
}
- $varList .= chr(13) . chr(10);
+ $varList .= $this->__crlf();
+ }
+ return $varList;
+ }
+
+ function __compileObjects() {
+ $varList = '';
+ if (!is_null($this->namespace)) {
+ $varList .= 'var ' . $this->namespace . ' = new Object();' . $this->__crlf();
+ }
+
+ foreach($this->object as $objName => $obj) {
+ if (!is_null($this->namespace)) {
+ $varList .= $this->namespace . '.' . $objName . ' = new Object();';
+ } else {
+ $varList .= 'var ' . $objName . ' = new Object();';
+ }
+ $varList .= $this->__crlf();
+ foreach($obj as $objProp => $objValue) {
+ if (!is_null($this->namespace)) {
+ $varList .= $this->namespace . '.' . $objName . '.' . $objProp . ' = ' . $objValue . ';';
+ } else {
+ $varList .= $objName . '.' . $objProp . ' = ' . $objValue . ';';
+ }
+ $varList .= $this->__crlf();
+ }
}
return $varList;
}
@@ -165,6 +200,18 @@ function getVar($name) {
return $this->vars[$name];
}
+ function createObject($objName) {
+ $this->object[$objName] = new stdClass();
+ }
+
+ function setObjectProperty($objName, $propName, $propVal) {
+ $this->object[$objName]->$propName = $propVal;
+ }
+
+ function removeObject($objName) {
+ unset($this->object[$objName]);
+ }
+
function defineFunction($name, $arguments, $code) {
$this->functions[$name]['name'] = $name;
$this->functions[$name]['args'] = $arguments;
@@ -199,5 +246,11 @@ function getOutput() {
$this->__compileJS();
return $this->output;
}
+
+ private function __crlf() {
+ if ($this->minify === false) {
+ return chr(13) . chr(10);
+ }
+ }
}
?>

0 comments on commit e62dae0

Please sign in to comment.