-
Notifications
You must be signed in to change notification settings - Fork 1
/
AbstractService.php
123 lines (91 loc) · 3.33 KB
/
AbstractService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php namespace Comodojo\Dispatcher\Service;
use \Comodojo\Dispatcher\Components\AbstractModel;
use \Comodojo\Dispatcher\Request\Model as Request;
use \Comodojo\Dispatcher\Router\Model as Router;
use \Comodojo\Dispatcher\Response\Model as Response;
use \Comodojo\Dispatcher\Extra\Model as Extra;
use \Comodojo\Dispatcher\Traits\CacheTrait;
use \Comodojo\Dispatcher\Traits\RequestTrait;
use \Comodojo\Dispatcher\Traits\ResponseTrait;
use \Comodojo\Dispatcher\Traits\RouterTrait;
use \Comodojo\Dispatcher\Traits\ExtraTrait;
use \Comodojo\Foundation\Events\EventsTrait;
use \Comodojo\SimpleCache\Manager as CacheManager;
use \Comodojo\Foundation\Base\Configuration;
use \Comodojo\Foundation\Events\Manager as EventsManager;
use \Psr\Log\LoggerInterface;
use \Exception;
/**
* @package Comodojo Dispatcher
* @author Marco Giovinazzi <marco.giovinazzi@comodojo.org>
* @author Marco Castiello <marco.castiello@gmail.com>
* @license MIT
*
* LICENSE:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
abstract class AbstractService extends AbstractModel {
use CacheTrait;
use EventsTrait;
use RequestTrait;
use RouterTrait;
use ResponseTrait;
use ExtraTrait;
protected static $supported_methods = ['GET','PUT','POST','DELETE','OPTIONS','HEAD','TRACE','CONNECT','PURGE'];
public function __construct(
Configuration $configuration,
LoggerInterface $logger,
CacheManager $cache,
EventsManager $events,
Request $request,
Router $router,
Response $response,
Extra $extra
) {
parent::__construct($configuration, $logger);
$this->setCache($cache);
$this->setEvents($events);
$this->setRequest($request);
$this->setRouter($router);
$this->setResponse($response);
$this->setExtra($extra);
}
/**
* Get service-implemented HTTP methods
*
* @return array Service implemented methods, in uppercase
*/
public function getImplementedMethods() {
$supported_methods = $this->getConfiguration()->get('supported-http-methods');
if ( is_null($supported_methods) ) $supported_methods = self::$supported_methods;
if ( method_exists($this, 'any') ) {
return $supported_methods;
}
$implemented_methods = [];
foreach ( $supported_methods as $method ) {
if ( method_exists($this, strtolower($method)) ) array_push($implemented_methods, $method);
}
return $implemented_methods;
}
/**
* Return the callable class method that reflect the requested one
*
*/
public function getMethod($method) {
$method = strtolower($method);
if ( method_exists($this, $method) ) {
return $method;
} else if ( method_exists($this, 'any') ) {
return 'any';
} else {
return null;
}
}
}