1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Directus – <http://getdirectus.com> |
5
|
|
|
* |
6
|
|
|
* @link The canonical repository – <https://github.com/directus/directus> |
7
|
|
|
* @copyright Copyright 2006-2016 RANGER Studio, LLC – <http://rangerstudio.com> |
8
|
|
|
* @license GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Directus\SDK; |
12
|
|
|
|
13
|
|
|
use Directus\Acl\Acl; |
14
|
|
|
use Directus\Database\Connection; |
15
|
|
|
use Directus\Util\ArrayUtils; |
16
|
|
|
use Directus\Database\TableSchema; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Client |
20
|
|
|
* |
21
|
|
|
* @author Welling Guzmán <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ClientFactory |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ClientFactory |
27
|
|
|
*/ |
28
|
|
|
protected static $instance = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $defaultConfig = [ |
34
|
|
|
'environment' => 'development', |
35
|
|
|
'database' => [ |
36
|
|
|
'driver' => 'pdo_mysql' |
37
|
|
|
], |
38
|
|
|
'status' => [ |
39
|
|
|
'column_name' => 'active', |
40
|
|
|
'deleted_value' => 0, |
41
|
|
|
'active_value' => 1, |
42
|
|
|
'draft_value' => 2, |
43
|
|
|
'mapping' => [ |
44
|
|
|
0 => [ |
45
|
|
|
'name' => 'Delete', |
46
|
|
|
'color' => '#C1272D', |
47
|
|
|
'sort' => 3 |
48
|
|
|
], |
49
|
|
|
1 => [ |
50
|
|
|
'name' => 'Active', |
51
|
|
|
'color' => '#5B5B5B', |
52
|
|
|
'sort' => 1 |
53
|
|
|
], |
54
|
|
|
2 => [ |
55
|
|
|
'name' => 'Draft', |
56
|
|
|
'color' => '#BBBBBB', |
57
|
|
|
'sort' => 2 |
58
|
|
|
] |
59
|
|
|
] |
60
|
|
|
] |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $userToken |
65
|
|
|
* @param array $options |
66
|
|
|
* |
67
|
|
|
* @return ClientLocal|ClientRemote |
68
|
|
|
*/ |
69
|
|
|
public static function create($userToken, $options = []) |
70
|
|
|
{ |
71
|
|
|
if (static::$instance == null) { |
72
|
|
|
static::$instance = new static; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (!is_array($userToken)) { |
76
|
|
|
$newClient = static::$instance->createRemoteClient($userToken, $options); |
77
|
|
|
} else { |
78
|
|
|
$options = $userToken; |
79
|
|
|
$newClient = static::$instance->createLocalClient($options); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $newClient; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Creates a new local client instance |
87
|
|
|
* |
88
|
|
|
* @param array $options |
89
|
|
|
* |
90
|
|
|
* @return ClientLocal |
91
|
|
|
*/ |
92
|
|
|
public function createLocalClient(array $options) |
93
|
|
|
{ |
94
|
|
|
$options = ArrayUtils::defaults($this->defaultConfig, $options); |
95
|
|
|
$dbConfig = ArrayUtils::get($options, 'database', []); |
96
|
|
|
|
97
|
|
|
$config = ArrayUtils::omit($options, 'database'); |
98
|
|
|
// match the actual directus status mapping config key |
99
|
|
|
$config['statusMapping'] = $config['status']['mapping']; |
100
|
|
|
unset($config['status']['mapping']); |
101
|
|
|
|
102
|
|
|
if (!defined('STATUS_DELETED_NUM')) { |
103
|
|
|
define('STATUS_DELETED_NUM', ArrayUtils::get($config, 'status.deleted_value', 0)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!defined('STATUS_ACTIVE_NUM')) { |
107
|
|
|
define('STATUS_ACTIVE_NUM', ArrayUtils::get($config, 'status.active_value', 1)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (!defined('STATUS_DRAFT_NUM')) { |
111
|
|
|
define('STATUS_DRAFT_NUM', ArrayUtils::get($config, 'status.draft_value', 2)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!defined('STATUS_COLUMN_NAME')) { |
115
|
|
|
define('STATUS_COLUMN_NAME', ArrayUtils::get($config, 'status.column_name', 'active')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!defined('DIRECTUS_ENV')) { |
119
|
|
|
define('DIRECTUS_ENV', ArrayUtils::get($config, 'environment', 'development')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$connection = new Connection($dbConfig); |
123
|
|
|
$connection->connect(); |
124
|
|
|
|
125
|
|
|
$acl = new Acl(); |
126
|
|
|
$acl->setUserId(1); |
127
|
|
|
$acl->setGroupId(1); |
128
|
|
|
$acl->setGroupPrivileges([ |
129
|
|
|
'*' => [ |
130
|
|
|
'id' => 1, |
131
|
|
|
'table_name' => '*', |
132
|
|
|
'group_id' => 1, |
133
|
|
|
'read_field_blacklist' => [], |
134
|
|
|
'write_field_blacklist' => [], |
135
|
|
|
'nav_listed' => 1, |
136
|
|
|
'status_id' => 0, |
137
|
|
|
'allow_view' => 2, |
138
|
|
|
'allow_add' => 1, |
139
|
|
|
'allow_edit' => 2, |
140
|
|
|
'allow_delete' => 2, |
141
|
|
|
'allow_alter' => 1 |
142
|
|
|
] |
143
|
|
|
]); |
144
|
|
|
|
145
|
|
|
$schema = new \Directus\Database\Schemas\Sources\MySQLSchema($connection); |
146
|
|
|
$schema = new \Directus\Database\SchemaManager($schema); |
147
|
|
|
TableSchema::setSchemaManagerInstance($schema); |
148
|
|
|
TableSchema::setAclInstance($acl); |
149
|
|
|
TableSchema::setConnectionInstance($connection); |
150
|
|
|
TableSchema::setConfig($config); |
151
|
|
|
|
152
|
|
|
return new ClientLocal($connection); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Create a new remote client instance |
157
|
|
|
* |
158
|
|
|
* @param $userToken |
159
|
|
|
* @param array $options |
160
|
|
|
* |
161
|
|
|
* @return ClientRemote |
162
|
|
|
*/ |
163
|
|
|
public function createRemoteClient($userToken, array $options = []) |
164
|
|
|
{ |
165
|
|
|
return new ClientRemote($userToken, $options); |
166
|
|
|
} |
167
|
|
|
} |