1
|
|
|
<?php |
2
|
|
|
namespace samsoncms\api; |
3
|
|
|
|
4
|
|
|
use samson\activerecord\dbRelation; |
5
|
|
|
use samson\activerecord\field; |
|
|
|
|
6
|
|
|
use samson\activerecord\materialfield; |
|
|
|
|
7
|
|
|
use samson\activerecord\structure; |
8
|
|
|
use samson\activerecord\structurefield; |
9
|
|
|
use samson\activerecord\structurematerial; |
10
|
|
|
use samson\activerecord\TableRelation; |
11
|
|
|
use samson\activerecord\material; |
|
|
|
|
12
|
|
|
use samson\core\CompressableService; |
13
|
|
|
use samson\activerecord\dbRecord; |
14
|
|
|
use samson\activerecord\dbMySQLConnector; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* SamsonCMS API |
18
|
|
|
* @package samsoncms\api |
19
|
|
|
*/ |
20
|
|
|
class CMS extends CompressableService |
21
|
|
|
{ |
22
|
|
|
/** Identifier */ |
23
|
|
|
protected $id = 'cmsapi'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Read SQL file with variables placeholders pasting |
27
|
|
|
* @param string $filePath SQL file for reading |
28
|
|
|
* @param string $prefix Prefix for addition |
29
|
|
|
* @return string SQL command text |
30
|
|
|
*/ |
31
|
|
|
public function readSQL($filePath, $prefix = '') |
32
|
|
|
{ |
33
|
|
|
$sql = ''; |
34
|
|
|
|
35
|
|
|
// Build path to SQL folder |
36
|
|
|
if (file_exists($filePath)) { |
37
|
|
|
// Replace prefix |
38
|
|
|
$sql = str_replace('@prefix', $prefix, file_get_contents($filePath)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $sql; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @see ModuleConnector::prepare() |
46
|
|
|
*/ |
47
|
|
|
public function prepare() |
48
|
|
|
{ |
49
|
|
|
// Perform SQL table creation |
50
|
|
|
$path = __DIR__.'/../sql/'; |
51
|
|
|
foreach (array_slice(scandir($path), 2) as $file) { |
52
|
|
|
db()->query($this->readSQL($path.$file, $this->tablePrefix)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Initiate migration mechanism |
56
|
|
|
db()->migration(get_class($this), array($this, 'migrator')); |
57
|
|
|
|
58
|
|
|
// Define permanent table relations |
59
|
|
|
new TableRelation('material', 'user', 'UserID', 0, 'user_id'); |
60
|
|
|
new TableRelation('material', 'gallery', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
61
|
|
|
new TableRelation('material', 'materialfield', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
62
|
|
|
new TableRelation('material', 'field', 'materialfield.FieldID', TableRelation::T_ONE_TO_MANY); |
63
|
|
|
new TableRelation('material', 'structurematerial', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
64
|
|
|
new TableRelation('material', 'structure', 'structurematerial.StructureID', TableRelation::T_ONE_TO_MANY); |
65
|
|
|
new TableRelation('materialfield', 'field', 'FieldID'); |
66
|
|
|
new TableRelation('materialfield', 'material', 'MaterialID'); |
67
|
|
|
new TableRelation('structurematerial', 'structure', 'StructureID'); |
68
|
|
|
new TableRelation('structurematerial', 'materialfield', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
69
|
|
|
new TableRelation('structurematerial', 'material', 'MaterialID', TableRelation::T_ONE_TO_MANY); |
70
|
|
|
new TableRelation('structure', 'material', 'structurematerial.MaterialID', TableRelation::T_ONE_TO_MANY, null, 'manymaterials'); |
71
|
|
|
new TableRelation('structure', 'gallery', 'structurematerial.MaterialID', TableRelation::T_ONE_TO_MANY, null, 'manymaterials'); |
72
|
|
|
/*new TableRelation( 'structure', 'material', 'MaterialID' );*/ |
73
|
|
|
new TableRelation('structure', 'user', 'UserID', 0, 'user_id'); |
74
|
|
|
new TableRelation('structure', 'materialfield', 'material.MaterialID', TableRelation::T_ONE_TO_MANY, 'MaterialID', '_mf'); |
75
|
|
|
new TableRelation('structure', 'structurematerial', 'StructureID', TableRelation::T_ONE_TO_MANY); |
76
|
|
|
new TableRelation('related_materials', 'material', 'first_material', TableRelation::T_ONE_TO_MANY, 'MaterialID'); |
77
|
|
|
new TableRelation('related_materials', 'materialfield', 'first_material', TableRelation::T_ONE_TO_MANY, 'MaterialID'); |
78
|
|
|
new TableRelation('field', 'structurefield', 'FieldID'); |
79
|
|
|
new TableRelation('field', 'structure', 'structurefield.StructureID'); |
80
|
|
|
new TableRelation('structurefield', 'field', 'FieldID'); |
81
|
|
|
new TableRelation('structurefield', 'materialfield', 'FieldID'); |
82
|
|
|
new TableRelation('structurefield', 'material', 'materialfield.MaterialID'); |
83
|
|
|
new TableRelation('structure', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'parent_id', 'children_relations'); |
84
|
|
|
new TableRelation('structure', 'structure', 'children_relations.child_id', TableRelation::T_ONE_TO_MANY, 'StructureID', 'children'); |
85
|
|
|
new TableRelation('structure', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'child_id', 'parents_relations'); |
86
|
|
|
new TableRelation('structure', 'structure', 'parents_relations.parent_id', TableRelation::T_ONE_TO_MANY, 'StructureID', 'parents'); |
87
|
|
|
new TableRelation('structurematerial', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'parent_id'); |
88
|
|
|
new TableRelation('groupright', 'right', 'RightID', TableRelation::T_ONE_TO_MANY); |
89
|
|
|
//elapsed('CMS:prepare'); |
90
|
|
|
|
91
|
|
|
// Все прошло успешно |
92
|
|
|
return true && parent::prepare(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Handler for CMSAPI database version manipulating |
97
|
|
|
* @param string $to_version Version to switch to |
98
|
|
|
* @return string Current database version |
99
|
|
|
*/ |
100
|
|
|
public function migrator($to_version = null) |
101
|
|
|
{ |
102
|
|
|
// If something passed - change database version to it |
103
|
|
|
if (func_num_args()) { |
104
|
|
|
// Save current version to special db table |
105
|
|
|
db()->query("ALTER TABLE `" . dbMySQLConnector::$prefix . "cms_version` CHANGE `version` `version` VARCHAR( 15 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '" . $to_version . "';"); |
106
|
|
|
die('Database successfully migrated to [' . $to_version . ']'); |
107
|
|
|
} else { // Return current database version |
108
|
|
|
$version_row = db()->fetchOne('SHOW COLUMNS FROM `' . dbMySQLConnector::$prefix . 'cms_version`'); |
109
|
|
|
if (isset($version_row[0]['Default'])) { |
110
|
|
|
return $version_row[0]['Default']; |
111
|
|
|
} else { |
112
|
|
|
return false; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: