|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ConfigurationLoaderAbstract |
|
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 ConfigurationLoaderAbstract |
|
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
|
|
|
abstract class ConfigurationLoaderAbstract 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 = $this->findSchemaFile($this->options['storeKey']); |
|
136
|
|
|
|
|
137
|
|
|
foreach ($finder as $file) { |
|
138
|
|
|
$endpoint = $this->options['endpoint']; |
|
139
|
|
|
|
|
140
|
|
|
// MAGIC happens here: |
|
141
|
|
|
// need to streamline endpoint and filename to be able to find the endpoint. |
|
142
|
|
|
$cmp = str_replace('/', '', $endpoint); |
|
143
|
|
|
list($filename, ) = explode('.', $file->getFilename()); |
|
144
|
|
|
|
|
145
|
|
|
if ($cmp == $filename) { |
|
146
|
|
|
$schema = json_decode(file_get_contents($file->getRealPath())); |
|
147
|
|
|
$apiDef->addSchema($endpoint, $schema); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Provides information about a filesystem directory and its' containing files. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $prefix Name of the directory to search in. |
|
157
|
|
|
* |
|
158
|
|
|
* @return Finder |
|
159
|
|
|
*/ |
|
160
|
|
|
abstract protected function findSchemaFile($prefix); |
|
161
|
|
|
} |
|
162
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.