File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments