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
|
|
|
// TODO: Use operation name in path. |
42
|
|
|
$operation = $info->operation; |
|
|
|
|
43
|
|
|
$path = $info->path; |
44
|
|
|
|
45
|
|
|
do { |
46
|
|
|
$key = implode('.', $path); |
47
|
|
|
if (isset($this->contexts[$key][$name])) { |
48
|
|
|
return $this->contexts[$key][$name]; |
49
|
|
|
} |
50
|
|
|
} while (array_pop($path) && !empty($path)); |
51
|
|
|
|
52
|
|
|
return $default; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $name |
57
|
|
|
* @param $value |
58
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function setContext($name, $value, ResolveInfo $info) { |
63
|
|
|
// TODO: Use operation name in path. |
64
|
|
|
$operation = $info->operation; |
|
|
|
|
65
|
|
|
$key = implode('.', $info->path); |
66
|
|
|
$this->contexts[$key][$name] = $value; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $name |
73
|
|
|
* @param null $default |
74
|
|
|
* |
75
|
|
|
* @return mixed|null |
76
|
|
|
*/ |
77
|
|
|
public function getGlobal($name, $default = NULL) { |
78
|
|
|
if (isset($this->globals[$name])) { |
79
|
|
|
return $this->globals[$name]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $default; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.