|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Graviton\ProxyBundle\Definition\Loader; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
|
7
|
|
|
use Graviton\ProxyBundle\Definition\ApiDefinition; |
|
8
|
|
|
use Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\DispersalStrategyInterface; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Symfony\Component\Finder\Finder; |
|
11
|
|
|
use Symfony\Component\Validator\Constraints\Url; |
|
12
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ConfigurationLoader |
|
17
|
|
|
* |
|
18
|
|
|
* @package Graviton\ProxyBundle\Definition\Loader |
|
19
|
|
|
*/ |
|
20
|
|
|
class ConfigurationLoader implements LoaderInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var ValidatorInterface */ |
|
23
|
|
|
private $validator; |
|
24
|
|
|
|
|
25
|
|
|
/** @var DispersalStrategyInterface */ |
|
26
|
|
|
private $strategy; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array */ |
|
29
|
|
|
private $options = []; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* constructor |
|
34
|
|
|
* |
|
35
|
|
|
* @param ValidatorInterface $validator validator |
|
36
|
|
|
* @param LoggerInterface $logger Logger |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(ValidatorInterface $validator, LoggerInterface $logger) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->validator = $validator; |
|
41
|
|
|
$this->logger = $logger; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritDoc |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
public function setOptions($options) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
if (!empty($options['prefix'])) { |
|
50
|
|
|
$options['storeKey'] = $options['prefix']; |
|
51
|
|
|
unset($options['prefix']); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->options = array_merge($this->options, $options); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritDoc |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setDispersalStrategy(DispersalStrategyInterface $strategy) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->strategy = $strategy; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritDoc |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setCache(CacheProvider $cache, $cacheNamespace, $cacheLifetime) |
|
69
|
|
|
{ |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Determines, if the current loader is capable of handling the request. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $url |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
public function supports($url) |
|
80
|
|
|
{ |
|
81
|
|
|
$error = $this->validator->validate($url, [new Url()]); |
|
82
|
|
|
|
|
83
|
|
|
return 0 === count($error); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritDoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public function load($url) |
|
90
|
|
|
{ |
|
91
|
|
|
$apiDef = new ApiDefinition(); |
|
92
|
|
|
$apiDef->setHost(parse_url($url, PHP_URL_HOST)); |
|
|
|
|
|
|
93
|
|
|
$apiDef->setBasePath(parse_url($url, PHP_URL_PATH)); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
$this->defineSchema($apiDef); |
|
96
|
|
|
$apiDef->addEndpoint($this->options['endpoint'] . '/'); |
|
97
|
|
|
|
|
98
|
|
|
return $apiDef; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function defineSchema(ApiDefinition $apiDef) |
|
102
|
|
|
{ |
|
103
|
|
|
if (array_key_exists('endpoint', $this->options)) { |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
$finder = new Finder(); |
|
106
|
|
|
$finder->files()->in(__DIR__ .'/../../Resources/schema/'. $this->options['storeKey']); |
|
107
|
|
|
|
|
108
|
|
|
foreach ($finder as $file) { |
|
109
|
|
|
$endpoint = $this->options['endpoint']; |
|
110
|
|
|
|
|
111
|
|
|
// MAGIC happens here: |
|
112
|
|
|
// need to streamline endpoint and filename to be able to find the endpoint. |
|
113
|
|
|
$cmp = str_replace('/', '', $endpoint); |
|
114
|
|
|
list($filename, ) = explode('.', $file->getFilename()); |
|
115
|
|
|
|
|
116
|
|
|
if ($cmp == $filename) { |
|
117
|
|
|
$schema = json_decode(file_get_contents($file->getRealPath())); |
|
118
|
|
|
$apiDef->addSchema($endpoint, $schema); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
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.