1
|
|
|
<?php |
2
|
|
|
namespace samson\activerecord; |
3
|
|
|
use samson\core\CompressableExternalModule; |
4
|
|
|
|
5
|
|
|
class Module extends CompressableExternalModule |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Идентификатор модуля |
9
|
|
|
* @var string |
10
|
|
|
*/ |
11
|
|
|
protected $id = 'activerecord'; |
12
|
|
|
|
13
|
|
|
/** Database table prefix */ |
14
|
|
|
public $prefix; |
15
|
|
|
|
16
|
|
|
/** Database name */ |
17
|
|
|
public $name; |
18
|
|
|
|
19
|
|
|
/** Login */ |
20
|
|
|
public $login = 'root'; |
21
|
|
|
|
22
|
|
|
/** Password */ |
23
|
|
|
public $pwd; |
24
|
|
|
|
25
|
|
|
/** Host */ |
26
|
|
|
public $host = '127.0.0.1'; |
27
|
|
|
|
28
|
|
|
/** @var string Port number */ |
29
|
|
|
public $port = ''; |
30
|
|
|
|
31
|
|
|
/* Array of additional relations to set */ |
32
|
|
|
public $relations = array(); |
33
|
|
|
|
34
|
|
|
/** @see \samson\core\CompressableExternalModule::beforeCompress() */ |
35
|
|
|
public function beforeCompress(& $obj = null, array & $code = null) |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** @see \samson\core\CompressableExternalModule::afterCompress() */ |
41
|
|
|
public function afterCompress(& $obj = null, array & $code = null) |
42
|
|
|
{ |
43
|
|
|
// Iterate through generated php code |
44
|
|
|
$files = array(); |
45
|
|
|
foreach (\samson\core\File::dir($this->cache_path . 'metadata', 'php', '', $files, 1) as $file) { |
46
|
|
|
|
47
|
|
|
// No namespace for global function file |
48
|
|
|
$ns = strpos($file, 'func') === false ? __NAMESPACE__ : ''; |
49
|
|
|
|
50
|
|
|
// Compress generated php code |
51
|
|
|
$obj->compress_php($file, $this, $code, $ns); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Iterate through generated php code |
55
|
|
|
$files = array(); |
56
|
|
|
foreach (\samson\core\File::dir($this->cache_path . 'relations', 'php', '', $files, 1) as $file) { |
57
|
|
|
// No namespace for global function file |
58
|
|
|
$ns = strpos($file, 'func') === false ? __NAMESPACE__ : ''; |
59
|
|
|
|
60
|
|
|
// Compress generated php code |
61
|
|
|
$obj->compress_php($file, $this, $code, $ns); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @see \samson\core\ExternalModule::prepare() */ |
66
|
|
|
public function prepare() |
67
|
|
|
{ |
68
|
|
|
// Set table prefix |
69
|
|
|
dbMySQLConnector::$prefix = $this->prefix; |
70
|
|
|
|
71
|
|
|
// Connect to database |
72
|
|
|
db()->connect($this->name, $this->login, $this->pwd, $this->host, $this->port); |
73
|
|
|
|
74
|
|
|
// Create specific relations |
75
|
|
|
foreach ($this->relations as $args) { |
76
|
|
|
switch (sizeof($args)) { |
77
|
|
|
case 2: |
78
|
|
|
new TableRelation($args[0], $args[1]); |
79
|
|
|
break; |
80
|
|
|
case 3: |
81
|
|
|
new TableRelation($args[0], $args[1], $args[2]); |
82
|
|
|
break; |
83
|
|
|
case 4: |
84
|
|
|
new TableRelation($args[0], $args[1], $args[2], $args[3]); |
85
|
|
|
break; |
86
|
|
View Code Duplication |
case 5: |
|
|
|
|
87
|
|
|
new TableRelation($args[0], $args[1], $args[2], $args[3], $args[4]); |
88
|
|
|
break; |
89
|
|
View Code Duplication |
case 6: |
|
|
|
|
90
|
|
|
new TableRelation($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]); |
91
|
|
|
break; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Generate db table classes |
96
|
|
|
db()->generate(false, $this->cache_path); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
100
|
|
|
public function relations() |
101
|
|
|
{ |
102
|
|
|
db()->relations($this->cache_path); |
103
|
|
|
} |
104
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
105
|
|
|
|
106
|
|
|
/** @see \samson\core\ExternalModule::init() */ |
107
|
|
|
public function init(array $params = array()) |
108
|
|
|
{ |
109
|
|
|
parent::init($params); |
110
|
|
|
|
111
|
|
|
// Set table prefix |
112
|
|
|
dbMySQLConnector::$prefix = $this->prefix; |
113
|
|
|
|
114
|
|
|
db()->connect($this->name, $this->login, $this->pwd, $this->host, $this->port); |
115
|
|
|
|
116
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
117
|
|
|
// Generate table relations |
118
|
|
|
$this->relations(); |
119
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.