|
1
|
|
|
<?php |
|
2
|
|
|
namespace NOSQL\Models\base; |
|
3
|
|
|
|
|
4
|
|
|
use MongoDB\Database; |
|
5
|
|
|
use NOSQL\Dto\CollectionDto; |
|
6
|
|
|
use NOSQL\Dto\IndexDto; |
|
7
|
|
|
use NOSQL\Dto\PropertyDto; |
|
8
|
|
|
use NOSQL\Exceptions\NOSQLParserException; |
|
9
|
|
|
use NOSQL\Models\NOSQLActiveRecord; |
|
10
|
|
|
use NOSQL\Services\ParserService; |
|
11
|
|
|
use PSFS\base\Cache; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Trait NOSQLParserTrait |
|
15
|
|
|
* @package NOSQL\Models\base |
|
16
|
|
|
*/ |
|
17
|
|
|
trait NOSQLParserTrait { |
|
18
|
|
|
protected $domain; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var CollectionDto |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $schema; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return mixed |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getDomain() |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->domain; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param mixed $domain |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setDomain($domain) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->domain = $domain; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return CollectionDto |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getSchema() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->schema; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param CollectionDto $schema |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setSchema(CollectionDto $schema) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->schema = $schema; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param PropertyDto $property |
|
58
|
|
|
*/ |
|
59
|
|
|
public function addProperty(PropertyDto $property) { |
|
60
|
|
|
$this->schema->properties[] = $property; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param IndexDto $index |
|
65
|
|
|
*/ |
|
66
|
|
|
public function addIndex(IndexDto $index) { |
|
67
|
|
|
$this->schema->indexes[] = $index; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @throws NOSQLParserException |
|
72
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function hydrate() { |
|
75
|
|
|
if(empty($this->domain)) { |
|
76
|
|
|
throw new NOSQLParserException(t('Domain not defined'), NOSQLParserException::NOSQL_PARSER_DOMAIN_NOT_DEFINED); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
$schemaFilename = CORE_DIR . DIRECTORY_SEPARATOR . $this->domain . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'schema.json'; |
|
79
|
|
|
if(file_exists($schemaFilename)) { |
|
80
|
|
|
$schema = Cache::getInstance()->getDataFromFile($schemaFilename, Cache::JSON, true); |
|
81
|
|
|
$class = get_called_class(); |
|
82
|
|
|
$this->schema = new CollectionDto(false); |
|
83
|
|
|
foreach($schema as $collection) { |
|
84
|
|
|
$collectionName = $collection['name']; |
|
85
|
|
|
if(false !== strpos($class, $collectionName)) { |
|
86
|
|
|
$this->schema->fromArray($collection); |
|
87
|
|
|
break; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} else { |
|
91
|
|
|
throw new NOSQLParserException(t('Schema file not exists'), NOSQLParserException::NOSQL_PARSER_SCHEMA_NOT_DEFINED); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param Database $con |
|
97
|
|
|
* @param NOSQLActiveRecord $model |
|
98
|
|
|
* @return Database |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function initConnection(Database $con = null, NOSQLActiveRecord $model) |
|
101
|
|
|
{ |
|
102
|
|
|
if (null === $con) { |
|
103
|
|
|
$con = ParserService::getInstance()->createConnection($model->getDomain()); |
|
104
|
|
|
} |
|
105
|
|
|
return $con; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |