Skip to content

Commit 9540313

Browse files
committed
adds model store
1 parent ab034b4 commit 9540313

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Chimera/ModelStore.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace chimera;
4+
5+
class ModelStore
6+
{
7+
protected $_schema_base;
8+
9+
private function chimera() {
10+
static $chimera;
11+
if (!$chimera) $chimera = new \chimera\Chimera();
12+
return $chimera;
13+
}
14+
15+
public function __construct($schema_base, $dsn, $user, $pass) {
16+
$this->_schema_base = $schema_base;
17+
$this->chimera()->connect($dsn, $user, $pass);
18+
}
19+
20+
public function __call($name, $args) {
21+
$schema_file = $this->_schema_base . DIRECTORY_SEPARATOR . $name . '.json';
22+
if (file_exists($schema_file)) {
23+
$json = json_decode(file_get_contents($schema_file));
24+
$schema = array();
25+
foreach($json as $k => $v) {
26+
$schema[$k] = array(
27+
'type' => constant('\\chimera\\' . $v->type),
28+
'default' => $v->default
29+
);
30+
}
31+
return $this->chimera()->model($name, new \Chimera\Schema($schema));
32+
}
33+
}
34+
35+
public function migrate() {
36+
$di = new \DirectoryIterator($this->_schema_base);
37+
foreach ($di as $file) {
38+
if ($file->isFile() && $file->getExtension() == 'json') {
39+
$name = $file->getBasename('.json');
40+
$model = $this->$name();
41+
}
42+
}
43+
$this->chimera()->migrate();
44+
}
45+
}
46+

0 commit comments

Comments
 (0)