Skip to content

Commit 18824fc

Browse files
author
sophpie
committed
Modification de l'api Manager
1 parent 10c182f commit 18824fc

File tree

8 files changed

+109
-108
lines changed

8 files changed

+109
-108
lines changed

Module.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ public function preDispatch (MvcEvent $event)
179179
$defaultApiKey = new ApiKey($targetConfig['zskey'],
180180
$targetConfig['zssecret']);
181181
$serviceManager->setService('defaultApiKey', $defaultApiKey);
182-
$targetServer = new ZendServer($targetConfig, $appConfig['min-zsversion']);
182+
ZendServer::setApiVersionConf($appConfig['min-zsversion']);
183+
$targetServer = ZendServer::factory($targetConfig);
183184
$serviceManager->setService('targetZendServer', $targetServer);
184185
}
185186

composer.json

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
{
2-
"name": "zenddevops/webapi",
3-
"description": "Provides easy access to Zend Server API",
4-
"keywords": [
5-
"zf2", "zend server"
6-
],
7-
"license": "BSD-3-Clause",
8-
"require": {
9-
"php": ">=5.3.3",
10-
"zendframework/zend-serializer" : ">=2.0.0",
11-
"zendframework/zend-text" : ">=2.0.0",
2+
"name" : "zenddevops/webapi",
3+
"description" : "Provides easy access to Zend Server API",
4+
"keywords" : [
5+
"zf2",
6+
"zend server"
7+
],
8+
"license" : [
9+
"BSD-3-Clause"
10+
],
11+
"require" : {
12+
"zendframework/zend-serializer" : ">=2.0.0",
1213
"zendframework/zend-mvc" : ">=2.0.0",
13-
"zendframework/zend-file" : ">=2.0.0",
14-
"zendframework/zend-console" : ">=2.0.0",
15-
"zendframework/zend-servicemanager" : ">=2.0.0",
1614
"zendframework/zend-form" : ">=2.0.0",
15+
"zendframework/zend-file" : ">=2.0.0",
1716
"zendframework/zend-loader" : ">=2.0.0",
17+
"zendframework/zend-servicemanager" : ">=2.0.0",
18+
"zendframework/zend-console" : ">=2.0.0",
19+
"zendframework/zend-text" : ">=2.0.0",
1820
"zendframework/zend-version" : ">=2.0.0",
1921
"zendframework/zend-modulemanager" : ">=2.0.0",
2022
"zendframework/zend-stdlib" : ">=2.0.0",
23+
"php" : ">=5.3.3",
2124
"zendframework/zend-view" : ">=2.0.0",
2225
"zendframework/zend-config" : ">=2.0.0",
23-
"zendframework/zend-http" : ">=2.0.0"
24-
},
25-
"autoload": {
26-
"psr-0": {
27-
"ZendServerWebApi\\": "/src"
28-
},
29-
"classmap": [
30-
"./Module.php"
31-
]
32-
}
26+
"zendframework/zend-http" : ">=2.0.0",
27+
"zendframework/zend-log" : ">=2.0.0"
28+
},
29+
"autoload" : {
30+
"psr-0" : {
31+
"ZendServerWebApi\\" : "/src"
32+
},
33+
"classmap" : [
34+
"./Module.php"
35+
]
36+
}
3337
}

config/api/version-1.4.config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'route' => 'daemonProbe',
1111
'defaults' => array(
1212
'controller' => 'webapi-api-controller',
13-
'action' => 'userAuthentificationSettings'
13+
'action' => 'daemonProbe'
1414
)
1515
),
1616
'info' => array(

config/zendserverwebapi.config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858

5959
'service_manager' => array (
6060
'factories' => array (
61-
'zend_server_api' => 'ZendServerWebApi\Service\ApiManagerFactory'
61+
'zend_server_api' => 'ZendServerWebApi\Service\ApiManagerFactory',
62+
'log' => 'ZendServerWebApi\Service\LogFactory'
6263
),
6364
'invokables' => array (
6465
'zpk' => 'ZendServerWebApi\Service\ZpkInvokable',

deployment.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
<zendframework2>
1414
<min>2.2</min>
1515
</zendframework2>
16+
<library>
17+
<name>Zend Framework 2</name>
18+
<min>2.2.1</min>
19+
</library>
1620
</required>
1721
</dependencies>
1822
</package>

src/ZendServerWebApi/Model/ApiManager.php

Lines changed: 31 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use ZendServerWebApi\Model\Response\ApiResponse;
44
use ZendServerWebApi\Model\Request;
55
use ZendServerWebApi\Model\Exception\ApiException;
6+
use Zend\ServiceManager\ServiceLocatorAwareInterface;
7+
use Zend\ServiceManager\ServiceLocatorInterface;
68

79
/**
810
* API Manager
@@ -11,36 +13,31 @@
1113
* Request can be send as an internal method of this class :
1214
* $this->getNotifications() will call "getNotications" API Method
1315
*/
14-
class ApiManager
16+
class ApiManager implements ServiceLocatorAwareInterface
1517
{
16-
17-
/**
18-
* ApiKey
19-
*
20-
* @var ZendServerWebApi\Model\ApiKey
21-
*/
22-
protected $apiKey;
23-
2418
/**
25-
* Zend server to request
26-
*
27-
* @var ZendServerwebApi\Model\ZendServer
19+
* Service manager
20+
* @var ServiceManager
2821
*/
29-
protected $targetServer;
30-
22+
protected $serviceManager;
23+
3124
/**
32-
* HTTP client to connect with Zend server
33-
*
34-
* @var Zend\Http\Client
25+
*
26+
* @param ServiceLocatorInterface $serviceLocator
3527
*/
36-
protected $zendServerClient;
37-
28+
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
29+
{
30+
$this->serviceManager = $serviceLocator;
31+
}
32+
3833
/**
39-
* API Configuration
40-
*
41-
* @var unknown
34+
* Get Service manager
35+
* @return \ZendServerWebApi\Model\ServiceManager
4236
*/
43-
protected $apiConfig = array();
37+
public function getServiceLocator()
38+
{
39+
return $this->serviceManager;
40+
}
4441

4542
/**
4643
* API Manager constructor
@@ -49,13 +46,9 @@ class ApiManager
4946
* @param unknown $apiKey
5047
* @param unknown $zendServerClient
5148
*/
52-
public function __construct ($targetServer, $apiKey, $zendServerClient,
53-
$apiConfig)
49+
public function __construct ($serviceManager)
5450
{
55-
$this->setTargetServer($targetServer);
56-
$this->setApiKey($apiKey);
57-
$this->setZendServerClient($zendServerClient);
58-
$this->setApiConfig($apiConfig);
51+
$this->setServiceLocator($serviceManager);
5952
}
6053

6154
/**
@@ -71,13 +64,14 @@ public function __call ($action, $args)
7164
if (isset($this->apiConfig[$action]['options']['defaults']['apiMethod'])) {
7265
$methodConf = $this->apiConfig[$action]['options']['defaults']['apiMethod'];
7366
}
74-
$apiRequest = new Request($this->targetServer, $action, $this->apiKey);
67+
$apiRequest = new Request($this->getTargetServer(), $action, $this->getApiKey());
68+
$this->getServiceLocator()->get('log')->info($apiRequest->getUriString());
7569
if (isset($args[0]))
7670
$apiRequest->setParameters($args[0]);
7771
if ($methodConf == 'post')
7872
$apiRequest->setMethod(Request::METHOD_POST);
7973
$apiRequest->prepareRequest();
80-
$httpResponse = $this->zendServerClient->send($apiRequest);
74+
$httpResponse = $this->getZendServerClient()->send($apiRequest);
8175
$response = ApiResponse::factory($httpResponse);
8276
if ($response->isError()) {
8377
throw new ApiException($response);
@@ -91,7 +85,7 @@ public function __call ($action, $args)
9185
*/
9286
public function getApiKey ()
9387
{
94-
return $this->apiKey;
88+
return $this->getServiceLocator()->get('defaultApiKey');
9589
}
9690

9791
/**
@@ -100,7 +94,7 @@ public function getApiKey ()
10094
*/
10195
public function getTargetServer ()
10296
{
103-
return $this->targetServer;
97+
return $this->getServiceLocator()->get('targetZendServer');
10498
}
10599

106100
/**
@@ -109,34 +103,7 @@ public function getTargetServer ()
109103
*/
110104
public function getZendServerClient ()
111105
{
112-
return $this->zendServerClient;
113-
}
114-
115-
/**
116-
*
117-
* @param \ZendServerWebApi\Model\ZendServerWebApi\Model\ApiKey $apiKey
118-
*/
119-
public function setApiKey ($apiKey)
120-
{
121-
$this->apiKey = $apiKey;
122-
}
123-
124-
/**
125-
*
126-
* @param \ZendServerWebApi\Model\ZendServerwebApi\Model\ZendServer $targetServer
127-
*/
128-
public function setTargetServer ($targetServer)
129-
{
130-
$this->targetServer = $targetServer;
131-
}
132-
133-
/**
134-
*
135-
* @param \ZendServerWebApi\Model\Zend\Http\Client $zendServerClient
136-
*/
137-
public function setZendServerClient ($zendServerClient)
138-
{
139-
$this->zendServerClient = $zendServerClient;
106+
return $this->getServiceLocator()->get('zendserverclient');
140107
}
141108

142109
/**
@@ -145,15 +112,8 @@ public function setZendServerClient ($zendServerClient)
145112
*/
146113
public function getApiConfig ()
147114
{
148-
return $this->apiConfig;
149-
}
150-
151-
/**
152-
*
153-
* @param \ZendServerWebApi\Model\unknown $apiConfig
154-
*/
155-
public function setApiConfig ($apiConfig)
156-
{
157-
$this->apiConfig = $apiConfig;
115+
$apiConfig = $this->getServiceLocator()->get('config');
116+
$apiConfig = $apiConfig['console']['router']['routes'];
117+
return $apiConfig;
158118
}
159119
}

src/ZendServerWebApi/Model/ZendServer.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ZendServer
1313
*
1414
* @var string
1515
*/
16-
protected $version = '5.1';
16+
protected $version;
1717

1818
/**
1919
* Zend Server uri
@@ -22,17 +22,24 @@ class ZendServer
2222
*/
2323
protected $uri;
2424

25+
/**
26+
* Api Version
27+
* @var string
28+
*/
29+
protected $apiVersion;
30+
31+
2532
/**
2633
* Api Version / zs version converter
2734
* @var array
2835
*/
29-
protected $apiVersionConfig = array();
36+
protected static $apiVersionConfig = array();
3037

3138
/**
3239
*
3340
* @param string $config
3441
*/
35-
public function __construct ($config,$apiVersionConfig)
42+
protected function __construct ($config)
3643
{
3744
$this->setUri(new \Zend\Uri\Http($config['zsurl']));
3845
$this->setVersion($config['zsversion']);
@@ -79,6 +86,36 @@ public function setUri ($uri)
7986
*/
8087
public function getApiVersion()
8188
{
82-
return current(array_keys($this->apiVersionConfig,$this->version));
89+
return $this->apiVersion;
90+
}
91+
92+
/**
93+
* @param string $apiVersion
94+
*/
95+
public function setApiVersion ($apiVersion)
96+
{
97+
$this->apiVersion = $apiVersion;
98+
}
99+
100+
/**
101+
* Zend Server factory
102+
* @param unknown $config
103+
* @param unknown $apiVersionConfig
104+
*/
105+
public static function factory($config)
106+
{
107+
$zendServer = new self($config);
108+
$apiVersion = current(array_keys(self::$apiVersionConfig,$zendServer->getVersion()));
109+
$zendServer->setApiVersion($apiVersion);
110+
return $zendServer;
111+
}
112+
113+
/**
114+
* Set the Api version configurator
115+
*/
116+
public static function setApiVersionConf($apiVersionConfig)
117+
{
118+
self::$apiVersionConfig = $apiVersionConfig;
83119
}
120+
84121
}

src/ZendServerWebApi/Service/ApiManagerFactory.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ class ApiManagerFactory implements FactoryInterface
1818
*/
1919
public function createService (ServiceLocatorInterface $serviceLocator)
2020
{
21-
$apiKey = $serviceLocator->get('defaultApiKey');
22-
$server = $serviceLocator->get('targetZendServer');
23-
$client = $serviceLocator->get('zendserverclient');
24-
$apiConfig = $serviceLocator->get('config');
25-
$apiConfig = $apiConfig['console']['router']['routes'];
26-
$apiManager = new ApiManager($server, $apiKey, $client, $apiConfig);
27-
21+
$apiManager = new ApiManager($serviceLocator);
2822
return $apiManager;
2923
}
3024
}

0 commit comments

Comments
 (0)