-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLudoDBConfigParser.php
More file actions
811 lines (742 loc) · 22.1 KB
/
LudoDBConfigParser.php
File metadata and controls
811 lines (742 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
<?php
/**
* Parser for LudoDBObject
* User: Alf Magne
* Date: 10.01.13
* @package LudoDB
* @author Alf Magne Kalleland <post@dhtmlgoodies.com>
*/
/**
* Parser of config for LudoDBObject classes. Instances of this class are created automatically by LudoDB.
* @package LudoDB
*/
class LudoDBConfigParser
{
/**
* Config object
* @var array|null
*/
protected $config;
/**
* Internal cache of relations between a column method and a column
* @var array
*/
private static $columnMappingCache = array();
/**
* Mapping of alias names of columns - for fast lookup
* @var array
*/
private $aliasMapping = array();
/**
* Reference to the LudoDBObject the parser is assigned to handle
* @var LudoDBObject
*/
private $obj;
/**
* Instances of class
* @var array
*/
private static $extensionClasses = array();
/**
* Custom constructor params
* @var array
*/
private $customConstructorParams;
/**
* Cache of names of external columns.
* @var array
*/
private $externalCache = array();
/**
* Array of columns of LudoDBObject for this parser.
* @var array
*/
private $myColumns;
/**
* Constructs new parser.
* @param LudoDBObject $obj
* @param array $config
*/
public function __construct(LudoDBObject $obj, $config = array())
{
$this->obj = $obj;
$this->parseConfig($config);
}
/**
* Parse config.
* @param array $config
*/
private function parseConfig($config)
{
$this->config = $this->getValidConfig($config);
$parent = $this->getExtends();
if (isset($parent)) {
$this->config = $this->getMergedConfigs($parent->configParser()->getConfig(), $this->config);
}
$this->mapColumnAliases();
}
/**
* Return a LudoDBObject instance this LudoDBObject object extends. (Only when this class extends another LudoDBObject).
* @return LudoDBObject
*/
private function getExtends()
{
$className = $this->getProperty('extends');
if (!isset($className)) {
$parent = get_parent_class($this->obj);
if ($parent !== 'LudoDBModel' && $parent != 'LudoDBCollection' && $parent != 'LudoDBTreeCollection') {
$className = $parent;
}
}
if (!isset($className)) return null;
if (!isset(self::$extensionClasses[$className])) {
self::$extensionClasses[$className] = new $className;
}
return self::$extensionClasses[$className];
}
/**
* Return config from JSON file as array.
* @return array
* @throws Exception
*/
private function getConfigFromFile()
{
$location = $this->getPathToJSONConfig();
if (file_exists($location)) {
$content = file_get_contents($location);
return JSON_decode($content, true);
} else {
throw new Exception("Could not load config file $location");
}
}
/**
* Return path to JSON config file.
* @return string
*/
private function getPathToJSONConfig()
{
return $this->getFileLocation() . "/JSONConfig/" . get_class($this->obj) . ".json";
}
/**
* Return path to JSON file for default data (name is ClassName.data.json).
* @return string
*/
public function getPathToJsonConfigDefaultData()
{
return $this->getFileLocation() . "/JSONConfig/" . get_class($this->obj) . ".data.json";
}
/**
* Return file location of this class.
* @return string
*/
public function getFileLocation()
{
$obj = new ReflectionClass($this->obj);
return dirname($obj->getFilename());
}
/**
* Save column alias to cache.
*/
private function mapColumnAliases()
{
if (isset($this->config['columns'])) {
foreach ($this->config['columns'] as $colName => $col) {
if (is_array($col) && isset($col['alias'])) {
$this->aliasMapping[$col['alias']] = $colName;
}
}
}
}
/**
* Return config of this LudoDBObject with config of parent LudoDBObject.
* @param $config1
* @param $config2
* @return array
*/
private function getMergedConfigs($config1, $config2)
{
if (!is_array($config1) or !is_array($config2)) {
return $config2;
}
foreach ($config2 as $sKey2 => $sValue2) {
$config1[$sKey2] = $this->getMergedConfigs(@$config1[$sKey2], $sValue2);
}
return $config1;
}
/**
* Return config
* @return array|null
*/
public function getConfig()
{
return $this->config;
}
/**
* Return input config validated.
* @param $config
* @return array
*/
private function getValidConfig($config)
{
if ($this->obj->hasConfigInExternalFile()) {
$config = $this->getConfigFromFile();
}
if (isset($config['sql']) && !LudoDB::hasPDO()) {
$config['sql'] = str_replace("?", "'%s'", $config['sql']);
$config['sql'] = str_replace("''", "'", $config['sql']);
}
if (!isset($config['constructBy']) && isset($config['idField'])) {
$config['constructBy'] = array($config['idField']);
}
if (isset($config['constructBy']) && !is_array($config['constructBy'])) {
$config['constructBy'] = array($config['constructBy']);
}
if (!isset($config['static'])) {
$config['static'] = array();
}
return $config;
}
/**
* Return table name for LudoDB
* @return mixed
*/
public function getTableName()
{
return $this->config['table'];
}
/**
* Return constructor params.
* @return array|null
*/
public function getConstructorParams()
{
if (isset($this->customConstructorParams)) return $this->customConstructorParams;
return isset($this->config['constructBy']) ? $this->config['constructBy'] : null;
}
/**
* Return name of external class for column.
* @param $column
* @return null
*/
public function externalClassNameFor($column)
{
return $this->getColumnProperty($column, 'class');
}
/**
* Return a column property from config, example: "db", "references", "alias"
* @param $name
* @param $property
* @return null
*/
private function getColumnProperty($name, $property)
{
if ($ret = $this->getExternalClassProperty($name, $property)) {
return $ret;
}
$col = $this->getColumn($name);
return isset($col) && is_array($col) && isset($col[$property]) ? $col[$property] : null;
}
/**
* Return config property from external class referenced in config of the LudoDBObject of this class.
* @param $column
* @param $property
* @return null
*/
private function getExternalClassProperty($column, $property)
{
if (isset($this->config['classes']) && isset($this->config['classes'][$column])) {
$cl = $this->config['classes'];
return isset($cl[$column]) && isset($cl[$column][$property]) ? $cl[$column][$property] : null;
}
return null;
}
/**
* Returns true if given column is from external LudoDBObject
* @param $name
* @return mixed
*/
public function isExternalColumn($name)
{
if (!isset($this->externalCache[$name])) {
$col = $this->getColumn($name);
$this->externalCache[$name] = isset($col) && is_array($col) && !isset($col['db']);
}
return $this->externalCache[$name];
}
/**
* Return name of id field.
* @return string
*/
public function getIdField()
{
return isset($this->config['idField']) ? $this->config['idField'] : 'id';
}
/**
* Returns true if auto increment of id is specified in config.
* @return bool
*/
public function isIdAutoIncremented()
{
return strstr($this->getDbDefinition($this->getIdField()), 'auto_increment') ? true : false;
}
/**
* Return db definition of a column, example: "int auto_increment not null primary key"
* @param $column
* @return mixed
*/
private function getDbDefinition($column)
{
$col = $this->config['columns'][$column];
return is_array($col) ? $col['db'] : $col;
}
/**
* Return "set" property of a column in config, i.e. name of set method.
* @param $column
* @return null
*/
public function getSetMethod($column)
{
return $this->getColumnProperty($column, 'set');
}
/**
* Return "get" property of a column in config, i.e. name of get method.
* @param $column
* @return null|string
*/
public function getGetMethod($column)
{
$column = $this->getInternalColName($column);
$method = $this->getColumnProperty($column, 'get');
return isset($method) ? $method : 'getValues';
}
/**
* Return foreign key for a column if exists.
* @param $column
* @return null
*/
public function foreignKeyFor($column)
{
$column = $this->getInternalColName($column);
return $this->getColumnProperty($column, 'fk');
}
/**
* Return indexed columns from config.
* @return null
*/
public function getIndexes()
{
return $this->getProperty('indexes');
}
/**
* Get default data for database table.
* @return array|null
*/
public function getDefaultData()
{
$ret = $this->getProperty('data');
if (is_string($ret)) {
$file = $this->getPathToJsonConfigDefaultData();
if (file_exists($file)) {
return json_decode(file_get_contents($file), true);
} else {
return null;
}
} else {
return $ret;
}
}
/**
* Return array of joined tables
* @return null|array
*/
private function getJoins()
{
return $this->getProperty('join');
}
/**
* Return name of my columns prefixed by tableName + .
* @return array
*/
public function getMyColumnsForSQL()
{
if (!isset($this->myColumns)) {
$columns = $this->getColumns();
$ret = array();
foreach ($columns as $col => $value) {
if (!$this->isExternalColumn($col)) {
$ret[] = $this->getTableName() . "." . $col;
}
}
$this->myColumns = $ret;
}
return $this->myColumns;
}
/**
* Return array of names of joined columns prefixed by their table names.
* @return array
*/
public function getJoinsForSQL()
{
$joins = $this->getJoins();
$ret = array();
if (isset($joins)) {
foreach ($joins as $join) {
$ret[] = $this->getTableName() . "." . $join['fk'] . "=" . $join['table'] . "." . $join['pk'];
}
}
return $ret;
}
/**
* Return columns to select from joined tables prefixed by their table names.
* @return array
*/
public function getColumnsToSelectFromJoins()
{
$ret = array();
$joins = $this->getJoins();
if (isset($joins)) {
foreach ($joins as $join) {
foreach ($join['columns'] as $col) {
$ret[] = $join['table'] . "." . $col;
}
}
}
return $ret;
}
/**
* Return array of columns from config.
* @return array
*/
public function getColumns()
{
return $this->getProperty('columns');
}
/**
* Return public column name. If "alias" is defined in config, that
* @param $name
* @return mixed
*/
public function getPublicColumnName($name)
{
if(!isset($this->config['columns'][$name]))return $name;
$col = $this->config['columns'][$name];
return is_array($col) && isset($col['alias']) ? $col['alias'] : $name;
}
/**
* Return config of a column
* @param $column
* @return array|null
*/
public function getColumn($column)
{
$column = $this->getInternalColName($column);
return isset($this->config['columns'][$column]) ? $this->config['columns'][$column] : null;
}
/**
* Return internal column name, i.e. key in column config. alias name will be translated to internal names.
* @param $column
* @return mixed
*/
public function getInternalColName($column)
{
return (isset($this->aliasMapping[$column])) ? $this->aliasMapping[$column] : $column;
}
/**
* Return orderBy property from config.
* @return null
*/
public function getOrderBy()
{
return $this->getProperty('orderBy');
}
/**
* Return true if any columns are defined in config.
* @return bool
*/
public function hasColumns()
{
$cols = $this->getColumns();
return isset($cols) && is_array($cols) && count($cols) > 0;
}
/**
* Return array of table names for joined columns.
* @return array
*/
public function getTableNamesFromJoins()
{
$ret = array();
$joins = $this->getJoins();
if (isset($joins)) {
foreach ($joins as $join) {
$ret[] = $join['table'];
}
}
return $ret;
}
/**
* Return a config property by key.
* @param $key
* @return null
*/
protected function getProperty($key)
{
return isset($this->config[$key]) ? $this->config[$key] : null;
}
/**
* Return true if config for given column is defined in config.
* @param $columnName
* @return bool
*/
private function hasColumn($columnName)
{
return isset($this->config['columns'][$columnName]);
}
/**
* Return name of column by "set" or "get" method.
* @param $methodName
* @return null|string
*/
public function getColumnByMethod($methodName)
{
if (!$this->hasColumns()) return null;
$col = $this->getFromMappingCache($methodName);
if (!isset($col)) {
$col = substr($methodName, 3);
if ($this->hasColumn($col)) return $this->saveInMappingCache($methodName, $col);
$col = lcfirst($col);
if ($this->hasColumn($col)) return $this->saveInMappingCache($methodName, $col);
$col = strtolower(preg_replace("/([A-Z])/s", "_$1", $col));
if ($this->hasColumn($col)) return $this->saveInMappingCache($methodName, $col);
}
return $col;
}
/**
* Save mapping between set and get methods and column names in internal cache for fast lookup.
* @param $methodName
* @param $col
* @return mixed
*/
private function saveInMappingCache($methodName, $col)
{
$t = $this->getTableName();
if (!isset(self::$columnMappingCache[$t])) {
self::$columnMappingCache[$t] = array();
}
self::$columnMappingCache[$t][$methodName] = $col;
return $col;
}
/**
* Return column name for given set or get method from mapping cache (fast lookup).
* @param $methodName
* @return null
*/
private function getFromMappingCache($methodName)
{
$t = $this->getTableName();
if (!isset(self::$columnMappingCache[$t])) {
self::$columnMappingCache[$t] = array();
}
return isset(self::$columnMappingCache[$t][$methodName]) ? self::$columnMappingCache[$t][$methodName] : null;
}
/**
* Returns true if user can write to given column. This is for the "save" and "setValues" methods of a LudoDBModel. You can
* always call $this->setValue($name, $value) internally in your LudoDBModel classes.
* @param $name
* @return mixed
*/
public function canWriteTo($name)
{
return $this->hasColumnAccess($name, 'w');
}
/**
* Returns true if user can read value of given column. This is for the getValues and read methods of a LudoDBModel. You
* will get access to column values internally using $this->getValue($columnName);
* @param $name
* @return mixed
*/
public function canReadFrom($name)
{
return $this->hasColumnAccess($name, 'r');
}
/**
* Cache of access to columns for fast lookup.
* @var array
*/
private $columnAccessCache = array();
/**
* Returns true if you have given access (read or write) to given column.
* @param $name
* @param $access
* @return mixed
*/
private function hasColumnAccess($name, $access)
{
$key = $name . "__" . $access;
if (!isset($this->columnAccessCache[$key])) {
if ($name === $this->getIdField()) {
$this->columnAccessCache[$key] = true;
} else {
if ($this->isStaticColumn($name)) {
$this->columnAccessCache[$key] = $access === 'r';
} else {
$column = $this->getColumn($name);
if (isset($column) && isset($column['access'])) {
$this->columnAccessCache[$key] = strstr($column['access'], $access) ? true : false;
} else {
$this->columnAccessCache[$key] = false;
}
}
}
}
return $this->columnAccessCache[$key];
}
/**
* Returns true if given column is a static column, i.e. column not defined in database which should have a static value
* defined in config.
* @param $column
* @return bool
*/
public function isStaticColumn($column)
{
return isset($this->config['static'][$column]);
}
/**
* Return value of static column.
* @param $column
* @return mixed
*/
public function getStaticValue($column){
return is_array($this->config['static'][$column]) ? $this->config['static'][$column]['value'] : $this->config['static'][$column];
}
/**
* Return array of values for static columns. Name of column is the key in the returned array.
* @return array
*/
public function getStaticValues(){
$ret = array();
foreach($this->config['static'] as $key=>$value){
$ret[$key] = $this->getStaticValue($key);
}
return $ret;
}
/**
* Return static columns
* @return array|null
*/
public function getStaticColumns(){
return $this->config['static'];
}
/**
* Returns true if config has static columns.
* @return bool
*/
public function hasStaticColumns(){
return !empty($this->config['static']);
}
/**
* Returns true when LudoDBModel can be populated/constructed by this column.
* @param $column
* @return bool
*/
public function canBePopulatedBy($column)
{
if ($column == $this->getIdField()) return true;
$col = $this->getColumn($column);
if (isset($this->config['constructBy']) && in_array($column, $this->config['constructBy'])) {
return true;
}
return is_array($col) && isset($col['canConstructBy']) ? $col['canConstructBy'] : false;
}
/**
* Returns references to other tables as array,
* example array(
* array('table' => 'city', 'column' => 'zip'),
* array('table' => 'country', 'column' => 'id')
* )
* @return array
*/
public function getTableReferences()
{
$ret = array();
$cols = $this->getColumns();
foreach ($cols as $col) {
if (is_array($col) && isset($col['references'])) {
$ret[] = array(
'table' => preg_replace("/^([^\(]+?)\(.*$/", "$1", $col['references']),
'column' => preg_replace("/^[^\(]+?\(([^\)]+)\).*$/", "$1", $col['references'])
);
}
}
return $ret;
}
/**
* Return default value of a column.
* @param $column
* @return null
*/
public function getDefaultValue($column)
{
return $this->getColumnProperty($column, 'default');
}
/**
* Return default values of all columns.
* @return array|null
*/
public function getDefaultValues()
{
$ret = array();
$cols = $this->getColumns();
foreach ($cols as $col) {
if (is_array($col) && isset($col['db']) && isset($col['default'])) {
$ret[] = $col['default'];
}
}
return count($ret) ? $ret : null;
}
/**
* Returns ludoJS config for database columns and static columns.
* @return array
*/
public function getLudoJSConfig(){
$ret = $this->getLudoJSOf($this->getColumns());
$static = $this->getStaticColumns();
if(isset($static))$ret = array_merge($ret, $this->getLudoJSOf($static));
return $ret;
}
/**
* Return LudoJS config of these columns
* @param array $columns
* @return array
*/
private function getLudoJSOf($columns){
$ret = array();
foreach($columns as $name=>$def){
if(isset($def['ludoJS'])){
$ret[$name] = $def['ludoJS'];
$ret[$name]['name'] = $name;
}
}
return $ret;
}
/**
* Cached columns requiring validation
* @var array
*/
private $validationColumns;
/**
* Return columns to validate
* @return array
*/
public function getColumnsToValidate(){
if(!isset($this->validationColumns)){
$this->validationColumns = array();
$columns = $this->getColumns();
foreach($columns as $name=>$def){
if(is_array($def) && isset($def['validation'])){
$this->validationColumns[$name] = $def['validation'];
}
}
}
return $this->validationColumns;
}
}