|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ConfigurationLoader |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\ProxyExtensionBundle\Definition\Loader; |
|
7
|
|
|
|
|
8
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
|
9
|
|
|
use Graviton\ProxyBundle\Definition\ApiDefinition; |
|
10
|
|
|
use Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\DispersalStrategyInterface; |
|
11
|
|
|
use Graviton\ProxyBundle\Definition\Loader\LoaderInterface; |
|
12
|
|
|
use Psr\Log\LoggerInterface; |
|
13
|
|
|
use Symfony\Component\Finder\Finder; |
|
14
|
|
|
use Symfony\Component\Validator\Constraints\Url; |
|
15
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ConfigurationLoader |
|
19
|
|
|
* |
|
20
|
|
|
* @package Graviton\ProxyExtensionBundle\Definition\Loader |
|
21
|
|
|
* |
|
22
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
23
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
24
|
|
|
* @link http://swisscom.ch |
|
25
|
|
|
*/ |
|
26
|
|
|
class ConfigurationLoader implements LoaderInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var ValidatorInterface */ |
|
29
|
|
|
private $validator; |
|
30
|
|
|
|
|
31
|
|
|
/** @var DispersalStrategyInterface */ |
|
32
|
|
|
private $strategy; |
|
33
|
|
|
|
|
34
|
|
|
/** @var array */ |
|
35
|
|
|
private $options = []; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* constructor |
|
40
|
|
|
* |
|
41
|
|
|
* @param ValidatorInterface $validator validator |
|
42
|
|
|
* @param LoggerInterface $logger Logger |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(ValidatorInterface $validator, LoggerInterface $logger) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->validator = $validator; |
|
47
|
|
|
$this->logger = $logger; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritDoc |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $options Configuration options ['prefix'] |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
View Code Duplication |
public function setOptions($options) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
if (!empty($options['prefix'])) { |
|
60
|
|
|
$options['storeKey'] = $options['prefix']; |
|
61
|
|
|
unset($options['prefix']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->options = array_merge($this->options, $options); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritDoc |
|
69
|
|
|
* |
|
70
|
|
|
* @param DispersalStrategyInterface $strategy Strategy to be used |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setDispersalStrategy(DispersalStrategyInterface $strategy) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->strategy = $strategy; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritDoc |
|
81
|
|
|
* |
|
82
|
|
|
* @param CacheProvider $cache Cache layer to be used |
|
83
|
|
|
* @param string $cacheNamespace Name of the cache to be used |
|
84
|
|
|
* @param int $cacheLifetime Cache time to life |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setCache(CacheProvider $cache, $cacheNamespace, $cacheLifetime) |
|
89
|
|
|
{ |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Determines, if the current loader is capable of handling the request. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $url Current url |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
public function supports($url) |
|
100
|
|
|
{ |
|
101
|
|
|
$error = $this->validator->validate($url, [new Url()]); |
|
102
|
|
|
|
|
103
|
|
|
return 0 === count($error); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @inheritDoc |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $url Current Url |
|
110
|
|
|
* |
|
111
|
|
|
* @return ApiDefinition |
|
112
|
|
|
*/ |
|
113
|
|
|
public function load($url) |
|
114
|
|
|
{ |
|
115
|
|
|
$apiDef = new ApiDefinition(); |
|
116
|
|
|
$apiDef->setHost(parse_url($url, PHP_URL_HOST)); |
|
|
|
|
|
|
117
|
|
|
$apiDef->setBasePath(parse_url($url, PHP_URL_PATH)); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
$this->defineSchema($apiDef); |
|
120
|
|
|
$apiDef->addEndpoint($this->options['endpoint'] . '/'); |
|
121
|
|
|
|
|
122
|
|
|
return $apiDef; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @inheritDoc |
|
127
|
|
|
* |
|
128
|
|
|
* @param ApiDefinition $apiDef Api definition the Schema to be defined in. |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
private function defineSchema(ApiDefinition $apiDef) |
|
133
|
|
|
{ |
|
134
|
|
|
if (array_key_exists('endpoint', $this->options)) { |
|
135
|
|
|
$finder = new Finder(); |
|
136
|
|
|
$finder->files()->in(__DIR__ .'/../../Resources/schema/'. $this->options['storeKey']); |
|
137
|
|
|
|
|
138
|
|
|
foreach ($finder as $file) { |
|
139
|
|
|
$endpoint = $this->options['endpoint']; |
|
140
|
|
|
|
|
141
|
|
|
// MAGIC happens here: |
|
142
|
|
|
// need to streamline endpoint and filename to be able to find the endpoint. |
|
143
|
|
|
$cmp = str_replace('/', '', $endpoint); |
|
144
|
|
|
list($filename, ) = explode('.', $file->getFilename()); |
|
145
|
|
|
|
|
146
|
|
|
if ($cmp == $filename) { |
|
147
|
|
|
$schema = json_decode(file_get_contents($file->getRealPath())); |
|
148
|
|
|
$apiDef->addSchema($endpoint, $schema); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.