public function loadConfig() { $cfg = PathResolver::getInstance()->getPath("{APP}/config/application.sdl"); if (file_exists($cfg)) { $this->debug("WebApplication: Reading %s", $cfg); $config = new SdlTag("root"); $config->loadString(file_get_contents($cfg)); $this->config = $config; } else { $this->debug("WebApplication: Could not find %s", $cfg); } }
public function getSdlTag() { $table = $this; // Print out the table nodes $sdl = new SdlTag("table", $table->name); $sdl->setComment("Table {$table->name} from database {$table->database->name}"); // Convert the columns to SDL foreach ($table->getColumns() as $co) { $col = new SdlTag("column", $co->name, ['type' => $co->type]); if (!empty($co->default)) { $col->setAttribute('default', $co->default); } if ($co->auto) { $col->setAttribute('auto', 1); } $col->setAttribute('null', $co->null); if (!empty($co->comment)) { $col->setAttribute('comment', $co->comment); } $sdl->addChild($col); } // Convert the indexes to SDL $idx = new SdlTag("indexes"); foreach ($table->getIndexes() as $ix) { $cols = $ix->columns; $ii = new SdlTag($ix->type, $ix->name); $ii->setComment("Index {$ix->name}"); $ii->addChild(new SdlTag(NULL, $cols)); $idx->addChild($ii); } $sdl->addChild($idx); return $sdl; }
public function generateModel(SdlTag $tag, $namespace = "Models") { $model = $tag->modelname ? $tag->modelname : ucwords($tag[0]) . 'Model'; $cols = $tag->query("column"); $php = "<?php\n\n"; $php .= "namespace {$namespace};\n\n"; $php .= "use \\Cherry\\Data\\Model;\n\n"; $php .= "/** \n"; $php .= " * @brief Database model.\n"; $php .= " * \n"; $php .= " * @generator app-setup 1.0\n"; $php .= " */\n"; $php .= "class {$model} extends Model {\n"; $php .= "\n"; $php .= " /// @var Table name\n"; $php .= " public \$table = \"{$tag[0]}\";\n"; $php .= "\n"; $php .= " /// @var Columns and types\n"; $php .= " public \$columns = [\n"; foreach ($cols as $column) { $def = $column->type; $php .= " \"{$column[0]}\" => \"{$def}\"" . (end($cols) != $column ? ",\n" : "\n"); } $php .= " ];\n"; $primary = $tag->auto; $php .= "\n"; $php .= " /// @var Primary index column\n"; if ($primary) { $php .= " public \$primary = \"{$primary}\";\n"; } else { $php .= " public \$primary = null;\n"; } $php .= "\n"; $php .= " /** \n"; $php .= " * Static invoker.\n"; $php .= " * \n"; $php .= " * @param Mixed \$m Method name\n"; $php .= " * @param Array \$a Method arguments\n"; $php .= " * \n"; $php .= " */\n"; $php .= " public static function __callStatic(\$m,\$a) {\n"; $php .= " return (new self())->call(\$m,\$a);\n"; $php .= " }\n"; $php .= "\n"; $php .= " /** \n"; $php .= " * Definitions for the model. Is called when the model is set up.\n"; $php .= " */\n"; $php .= " public function define() {\n"; $php .= " // \$this->setValidator(\"column\", new ValidatorClass());\n"; $php .= " }\n"; $php .= "}\n"; return $php; }
public function __construct() { $cfg = PathResolver::getInstance()->getPath("{APP}/config/routes.sdl"); if (!file_exists($cfg) || !is_readable($cfg)) { \debug("Could not read routelist from {$cfg}."); } else { $root = new SdlTag(); $root->loadFile($cfg); $this->routes = $root->query("/routes/route"); $num = count($this->routes); \debug("Parsed {$num} routes from {$cfg}"); } }
public static function getPool($identifier) { \utils::deprecated(__CLASS__ . "::getPool", "\\Cherry\\Core\\ConfigManager::get"); if (array_key_exists($identifier, self::$pools)) { $pool = self::$pools[$identifier]; if (!array_key_exists($pool->cfghash, self::$configfiles)) { $path = $pool->cfgpath; $cpath = dirname($path) . '/.' . basename($path) . '.cache'; if (file_exists($path)) { // Check for a serialized cache of the config if (file_exists($cpath) && filemtime($cpath) > filemtime($path)) { // Load from cache self::debug("Reading config '{$identifier}' from cache..."); $tag = unserialize(file_get_contents($cpath)); } else { // Update cache self::debug("Updating cache for config '{$identifier}'..."); $tag = SdlTag::createFromFile($path); file_put_contents($cpath, serialize($tag)); } self::$configfiles[$pool->cfghash] = $tag; } else { self::debug("File not found: {$path}"); self::$configfiles[$pool->cfghash] = null; } } return self::$configfiles[$pool->cfghash]; } else { self::debug("Error: Pool has not been bound: {$identifier}"); return null; } }
private function buildNodeTree(SdlTag $node) { $tag = strtolower($node->getName()); $attr = ""; foreach ($node->getAttributes() as $name => $value) { $attr .= " {$name}=\"{$value}\""; } $out = ""; if ($node->hasChildren()) { if ($tag) { $out .= "<{$tag}{$attr}>"; } foreach ($node->getChildren() as $child) { $out .= $this->buildNodeTree($child); } if ($tag) { $out .= "</{$tag}>"; } } else { if (count($node) > 0) { if (!$tag) { $out .= $node[0]; } else { $out .= "<{$tag}{$attr}>" . $node[0] . "</{$tag}>"; } } else { $out .= "<{$tag}{$attr}>"; } } return $out; }
public function dbtosdl() { $args = func_get_args(); $opts = $this->parseOpts($args, array('database' => 'database:', 'table' => 'table:', 'with-data' => '+withdata')); var_dump($opts); // Get the connection instance if (empty($opts['database'])) { $conn = DatabaseConnection::getInstance("default"); } else { $conn = DatabaseConnection::getInstance($opts['database']); } // Enumerate all the tables foreach ($conn->tables->getAll() as $table) { if (empty($opts['table']) || $table->name == $opts['table']) { // Print out the table nodes $sdl = new SdlTag("table", $table->name); $sdl->setComment("Table {$table->name} from database {$table->database->name}"); // Convert the columns to SDL foreach ($table->getColumns() as $co) { $col = new SdlTag("column", $co->name, ['type' => $co->type]); if ($co->default !== NULL) { $col->setAttribute('default', $co->default); } if ($co->auto) { $col->setAttribute('auto', 1); } $col->setAttribute('null', $co->null); $sdl->addChild($col); } // Convert the indexes to SDL $idx = new SdlTag("indexes"); foreach ($table->getIndexes() as $ix) { $cols = $ix->columns; $ii = new SdlTag($ix->type, $ix->name); $ii->setComment("Index {$ix->name}"); $ii->addChild(new SdlTag(NULL, $cols)); $idx->addChild($ii); } $sdl->addChild($idx); $rs = $conn->query("SELECT * FROM {$table->name}"); $data = new SdlTag("data"); foreach ($rs as $row) { $rdata = new SdlTag("row"); foreach ($row as $k => $v) { if (!is_integer($k)) { $rdata->addChild(new SdlTag($k, $v)); } } $data->addChild($rdata); } $sdl->addChild($data); echo $sdl->encode() . "\n"; } } }
use Cherry\Util\Timer; // Create a new root node, we need this. $root = new SdlTag("cyanogenmod"); $root->setComment("These are the latest three CyanogenMod versions with some info"); // You can create nodes and set attributes manually $cm9 = new SdlTag("version", "9"); $cm9->multiuser = false; $cm9->android = "4.0.x"; $cm9->setComment("Based on AOSP 4.0.x"); $root->addChild($cm9); // Or provide them to the constructor $cm10 = new SdlTag("version", "10", ['multiuser' => false, 'android' => "4.1.x"]); $cm10->setComment("Based on AOSP 4.1.x"); $root->addChild($cm10); // Comments can also go as the 5th argument $cm101 = new SdlTag("version", "10.1", ['multiuser' => true, 'android' => "4.2.x", 'note' => "New and hot!"], null, "Based on AOSP 4.2.x"); $root->addChild($cm101); // And the data can be serialized to SDL $sdl = $root->encode(); echo $sdl; // Read it back out again $test = new SdlTag("root"); $test->loadString($sdl); // Print some info about the various versions. foreach ($test->getChild("cyanogenmod")->getChildren() as $child) { echo "CM{$child[0]}: Based on Android AOSP {$child->android}."; if ($child->multiuser) { echo " Multiple user-profiles supported."; } echo "\n"; }
protected function getSqlVartype(SdlTag $tag) { $type = $tag->type; if (strpos($type, ":") !== false) { list($type, $len) = explode(":", $type); } else { $len = null; } switch (strtolower($type)) { case 'text': if (!$len) { $len = 65535; } case 'char': if ($len == null) { $len = 250; } if ($len < 1024) { $type = "TEXT({$len})"; } elseif ($len < 65535) { $type = "TEXT"; } else { $type = "TEXT"; } break; case 'blob': case 'binary': $type = "BLOB"; break; case 'bool': $type = "INTEGER"; break; case 'int': case 'integer': if (!$len) { $len = 11; } $type = "INTEGER"; $type .= "({$len})"; break; case 'float': $type = "REAL"; if ($len) { $type .= "({$len})"; } break; case 'double': $type = "REAL"; if ($len) { $type .= "({$len})"; } break; case 'date': $type = "TEXT"; break; case 'set': $values = []; foreach ($tag->query("value") as $value) { $values[] = $value[0]; } $type = "TEXT"; case 'enum': $values = []; foreach ($tag->query("value") as $value) { $values[] = $value[0]; } $type = "TEXT"; } return $type; }
public function testNamespacesWithName() { $test = new SdlTag("foo:test"); $this->assertEquals("test", $test->getName()); $this->assertEquals("foo", $test->getNamespace()); $this->assertEquals("foo:test", $test->getNameNs()); $test->setNamespace("bar"); $this->assertEquals("bar", $test->getNamespace()); $this->assertEquals("bar:test", $test->getNameNs()); $test->setName("arf"); $this->assertEquals("bar", $test->getNamespace()); $this->assertEquals("bar:arf", $test->getNameNs()); $test->setNamespace(null); $this->assertEquals(null, $test->getNamespace()); $this->assertEquals(":arf", $test->getNameNs()); }
<?php require_once "../../share/include/cherryphp"; use Cherry\Data\Ddl\SdlTag; use Cherry\Util\Timer; // This is a node with base64-encoded data. $sdl = <<<EOT testnode [SGVsbG8gV29ybGQh]; EOT; // Read it back out again $test = new SdlTag("root"); $test->loadString($sdl); $node = new SdlTag("testnode2"); $node->setValue("Putting binary data in the SDL node", 0, SdlTag::LT_BINARY); $test->addChild($node); echo "Current state of SDL tree:\n"; echo $test->encode() . "\n"; echo "Node values:\n"; echo " testnode=" . $test->getChild("testnode")[0] . "\n"; echo " testnode2=" . $test->getChild("testnode2")[0] . "\n";
protected function getSqlVartype(SdlTag $tag) { $type = $tag->type; if (strpos($type, ":") !== false) { list($type, $len) = explode(":", $type); } else { $len = null; } switch (strtolower($type)) { case 'text': if (!$len) { $len = 65535; } case 'char': if ($len == null) { $len = 250; } if ($len < 1024) { $type = "VARCHAR({$len})"; } elseif ($len < 65535) { $type = "TEXT"; } else { $type = "MEDIUMTEXT"; } break; case 'blob': if (!$len) { $len = 65535; } case 'binary': if ($len == null) { $len = 250; } if ($len < 1024) { $type = "VARBINARY({$len})"; } elseif ($len < 65535) { $type = "BLOB"; } else { $type = "MEDIUMBLOB"; } break; case 'bool': $type = "TINYINT(1)"; break; case 'int': case 'integer': if (!$len) { $len = 11; } $type = "INT"; $type .= "({$len})"; break; case 'float': $type = "FLOAT"; if ($len) { $type .= "({$len})"; } break; case 'double': $type = "FLOAT"; if ($len) { $type .= "({$len})"; } break; case 'date': $type = "DATETIME"; break; case 'set': $values = []; foreach ($tag->query("value") as $value) { $values[] = $value[0]; } $type = "SET('" . join("','", $values) . "')"; case 'enum': $values = []; foreach ($tag->query("value") as $value) { $values[] = $value[0]; } $type = "ENUM('" . join("','", $values) . "')"; } return $type; }
static function getInstance($pool = null) { if (!$pool) { $pool = self::POOL_DEFAULT; } if (!self::$connections) { $cp = PathResolver::path("{APP}/config/database.sdl"); if (file_exists($cp)) { $cfg = SdlTag::createFromFile($cp); foreach ($cfg->query("database/connection") as $connection) { $cpool = $connection[0]; $curi = $connection[1]; if (empty(self::$connections[$cpool])) { self::$connections[$cpool] = []; } self::sdebug("Database: Registered connection '{$curi}' to pool {$cpool}"); self::$connections[$cpool][] = $connection; } } } if (!self::$connections) { self::$connections = []; } if (!array_key_exists($pool, self::$dbpool)) { if (array_key_exists($pool, self::$connections)) { $pc = self::$connections[$pool]; if (is_array($pc)) { $dp = $pc[0][1]; } else { $dp = $pc[1]; } //var_dump($pool); //die(); //self::$dbpool[$pool] = new self(self::$connections[$pool]); $opts = ["pool" => $pool]; self::$dbpool[$pool] = new self($dp, $opts); } else { if (strpos($pool, "://") !== false) { self::$dbpool[$pool] = new self($pool); } else { throw new \UnexpectedValueException("Unable to connect to pool {$pool}"); } } } return self::$dbpool[$pool]; }
$root = new SdlTag("cyanogenmod"); $root->setComment("These are the latest three CyanogenMod versions with some info"); // You can create nodes and set attributes manually $cm9 = new SdlTag("version", "9"); $cm9->multiuser = false; $cm9->android = "4.0.x"; $cm9->setComment("Based on AOSP 4.0.x"); $root->addChild($cm9); // Or provide them to the constructor $cm10 = new SdlTag("version", "10", ['multiuser' => false, 'android' => "4.1.x"]); $cm10->setComment("Based on AOSP 4.1.x"); $root->addChild($cm10); // Comments can also go as the 5th argument $cm101 = new SdlTag("version", "10.1", ['multiuser' => true, 'android' => "4.2.x", 'note' => "New and hot!"], null, "Based on AOSP 4.2.x"); $root->addChild($cm101); $temp = new SdlTag("root"); $temp->addChild($root); testspath($temp, "cyanogenmod/version[@multiuser=true]"); testspath($temp, "cyanogenmod/version[10]"); testspath($temp, "cyanogenmod/*"); function testspath($root, $expr) { echo "Expression: '{$expr}'\n"; foreach ($root->query($expr) as $child) { echo " CM{$child[0]}: Based on Android AOSP {$child->android}."; if ($child->multiuser) { echo " Multiple user-profiles supported."; } echo "\n"; } }