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\Database\Connection; |
14
|
|
|
use Directus\Util\ArrayUtils; |
15
|
|
|
use Directus\Database\TableSchema; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Client |
19
|
|
|
* |
20
|
|
|
* @author Welling Guzmán <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Client |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Client |
26
|
|
|
*/ |
27
|
|
|
protected static $instance = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $defaultConfig = [ |
33
|
|
|
'environment' => 'development', |
34
|
|
|
'database' => [ |
35
|
|
|
'driver' => 'pdo_mysql' |
36
|
|
|
], |
37
|
|
|
'status' => [ |
38
|
|
|
'column_name' => 'active', |
39
|
|
|
'deleted_value' => 0, |
40
|
|
|
'active_value' => 1, |
41
|
|
|
'draft_value' => 2, |
42
|
|
|
'mapping' => [ |
43
|
|
|
0 => [ |
44
|
|
|
'name' => 'Delete', |
45
|
|
|
'color' => '#C1272D', |
46
|
|
|
'sort' => 3 |
47
|
|
|
], |
48
|
|
|
1 => [ |
49
|
|
|
'name' => 'Active', |
50
|
|
|
'color' => '#5B5B5B', |
51
|
|
|
'sort' => 1 |
52
|
|
|
], |
53
|
|
|
2 => [ |
54
|
|
|
'name' => 'Draft', |
55
|
|
|
'color' => '#BBBBBB', |
56
|
|
|
'sort' => 2 |
57
|
|
|
] |
58
|
|
|
] |
59
|
|
|
] |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $userToken |
64
|
|
|
* @param array $options |
65
|
|
|
* |
66
|
|
|
* @return ClientLocal|ClientRemote |
67
|
|
|
*/ |
68
|
38 |
|
public static function create($userToken, $options = []) |
69
|
|
|
{ |
70
|
38 |
|
if (static::$instance == null) { |
71
|
2 |
|
static::$instance = new static; |
72
|
2 |
|
} |
73
|
|
|
|
74
|
38 |
|
if (!is_array($userToken)) { |
75
|
38 |
|
$newClient = static::$instance->createRemoteClient($userToken, $options); |
76
|
38 |
|
} else { |
77
|
|
|
$options = $userToken; |
78
|
|
|
$newClient = static::$instance->createLocalClient($options); |
79
|
|
|
} |
80
|
|
|
|
81
|
38 |
|
return $newClient; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Creates a new local client instance |
86
|
|
|
* |
87
|
|
|
* @param array $options |
88
|
|
|
* |
89
|
|
|
* @return ClientLocal |
90
|
|
|
*/ |
91
|
|
|
public function createLocalClient(array $options) |
92
|
|
|
{ |
93
|
|
|
$options = ArrayUtils::defaults($this->defaultConfig, $options); |
94
|
|
|
$dbConfig = ArrayUtils::get($options, 'database', []); |
95
|
|
|
|
96
|
|
|
$config = ArrayUtils::omit($options, 'database'); |
97
|
|
|
// match the actual directus status mapping config key |
98
|
|
|
$config['statusMapping'] = $config['status']['mapping']; |
99
|
|
|
unset($config['status']['mapping']); |
100
|
|
|
|
101
|
|
|
if (!defined('STATUS_DELETED_NUM')) { |
102
|
|
|
define('STATUS_DELETED_NUM', ArrayUtils::get($config, 'status.deleted_value', 0)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!defined('STATUS_ACTIVE_NUM')) { |
106
|
|
|
define('STATUS_ACTIVE_NUM', ArrayUtils::get($config, 'status.active_value', 1)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!defined('STATUS_DRAFT_NUM')) { |
110
|
|
|
define('STATUS_DRAFT_NUM', ArrayUtils::get($config, 'status.draft_value', 2)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (!defined('STATUS_COLUMN_NAME')) { |
114
|
|
|
define('STATUS_COLUMN_NAME', ArrayUtils::get($config, 'status.column_name', 'active')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (!defined('DIRECTUS_ENV')) { |
118
|
|
|
define('DIRECTUS_ENV', ArrayUtils::get($config, 'environment', 'development')); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$connection = new Connection($dbConfig); |
122
|
|
|
$connection->connect(); |
123
|
|
|
|
124
|
|
|
$schema = new \Directus\Database\Schemas\Sources\MySQLSchema($connection); |
125
|
|
|
$schema = new \Directus\Database\SchemaManager($schema); |
126
|
|
|
TableSchema::setSchemaManagerInstance($schema); |
127
|
|
|
TableSchema::setAclInstance(false); |
128
|
|
|
TableSchema::setConnectionInstance($connection); |
129
|
|
|
TableSchema::setConfig($config); |
130
|
|
|
|
131
|
|
|
return new ClientLocal($connection); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Create a new remote client instance |
136
|
|
|
* |
137
|
|
|
* @param $userToken |
138
|
|
|
* @param array $options |
139
|
|
|
* |
140
|
|
|
* @return ClientRemote |
141
|
|
|
*/ |
142
|
38 |
|
public function createRemoteClient($userToken, array $options = []) |
143
|
|
|
{ |
144
|
38 |
|
return new ClientRemote($userToken, $options); |
145
|
|
|
} |
146
|
|
|
} |