1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\GraphQL\Execution; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Cache\RefinableCacheableDependencyInterface; |
6
|
|
|
use Drupal\Core\Cache\RefinableCacheableDependencyTrait; |
7
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
8
|
|
|
|
9
|
|
|
class ResolveContext implements RefinableCacheableDependencyInterface { |
10
|
|
|
use RefinableCacheableDependencyTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Read-only list of global values. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $globals; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $contexts = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* ResolveContext constructor. |
26
|
|
|
* |
27
|
|
|
* @param array $globals |
28
|
|
|
*/ |
29
|
|
|
public function __construct(array $globals = []) { |
30
|
|
|
$this->globals = $globals; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $name |
35
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
36
|
|
|
* @param null $default |
37
|
|
|
* |
38
|
|
|
* @return mixed|null |
39
|
|
|
*/ |
40
|
|
|
public function getContext($name, ResolveInfo $info, $default = NULL) { |
41
|
|
|
$operation = isset($info->operation->name->value) ? $info->operation->name->value : $info->operation->operation; |
42
|
|
|
$path = $info->path; |
43
|
|
|
|
44
|
|
|
do { |
45
|
|
|
$key = implode('.', $path); |
46
|
|
|
if (isset($this->contexts[$operation][$key][$name])) { |
47
|
|
|
return $this->contexts[$operation][$key][$name]; |
48
|
|
|
} |
49
|
|
|
} while (array_pop($path) && !empty($path)); |
50
|
|
|
|
51
|
|
|
return $default; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $name |
56
|
|
|
* @param $value |
57
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
58
|
|
|
* |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function setContext($name, $value, ResolveInfo $info) { |
62
|
|
|
$operation = isset($info->operation->name->value) ? $info->operation->name->value : $info->operation->operation; |
63
|
|
|
$key = implode('.', $info->path); |
64
|
|
|
$this->contexts[$operation][$key][$name] = $value; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $name |
71
|
|
|
* @param null $default |
72
|
|
|
* |
73
|
|
|
* @return mixed|null |
74
|
|
|
*/ |
75
|
|
|
public function getGlobal($name, $default = NULL) { |
76
|
|
|
if (isset($this->globals[$name])) { |
77
|
|
|
return $this->globals[$name]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $default; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} |