1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Annotation\Plugin; |
12
|
|
|
|
13
|
|
|
use Doctrine\Common\Util\Inflector; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Can use this annotation as base to se plugin options. |
17
|
|
|
* Override and define all plugin settings. |
18
|
|
|
*/ |
19
|
|
|
abstract class PluginConfigAnnotation |
20
|
|
|
{ |
21
|
3 |
|
public function __construct(array $config = []) |
22
|
|
|
{ |
23
|
3 |
|
$ref = new \ReflectionClass(get_class($this)); |
24
|
3 |
|
$properties = $ref->getProperties(); |
25
|
|
|
|
26
|
|
|
//set default values |
27
|
3 |
|
foreach ($properties as $property) { |
28
|
3 |
|
$property->setAccessible(true); |
29
|
3 |
|
$value = $property->getValue($this); |
30
|
3 |
|
if (null !== $value) { |
31
|
3 |
|
$this->config[Inflector::tableize($property->getName())] = $property->getValue($this); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
//if only one value is given, the first property is set with the given value |
36
|
3 |
|
if (isset($config['value']) && count($config) === 1) { |
37
|
1 |
|
if ($properties) { |
|
|
|
|
38
|
1 |
|
$propName = $ref->getProperties()[0]->getName(); |
39
|
1 |
|
$this->$propName = $config['value']; |
40
|
1 |
|
$this->config[Inflector::tableize($propName)] = $config['value']; |
41
|
|
|
} |
42
|
|
|
} else { |
43
|
2 |
|
foreach ($config as $key => $value) { |
44
|
|
|
//dynamically added to avoid ide auto-complete for this property |
45
|
2 |
|
$this->config[Inflector::tableize($key)] = $value; |
46
|
|
|
} |
47
|
|
|
} |
48
|
3 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Must return the array with plugin config |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
12 |
|
public function getConfig(): array |
56
|
|
|
{ |
57
|
12 |
|
return $this->config ?? []; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Must return the plugin name to apply this config |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
12 |
|
public function getName(): string |
66
|
|
|
{ |
67
|
12 |
|
preg_match('/\w+$/', get_class($this), $matches); |
68
|
|
|
|
69
|
12 |
|
return Inflector::tableize($matches[0]); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|