1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BearerService.php |
4
|
|
|
* |
5
|
|
|
* PHP version 5.6+ |
6
|
|
|
* |
7
|
|
|
* @author Philippe Gaultier <[email protected]> |
8
|
|
|
* @copyright 2010-2016 Philippe Gaultier |
9
|
|
|
* @license http://www.sweelix.net/license license |
10
|
|
|
* @version XXX |
11
|
|
|
* @link http://www.sweelix.net |
12
|
|
|
* @package sweelix\oauth2\server\services\redis |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace sweelix\oauth2\server\services\redis; |
16
|
|
|
|
17
|
|
|
use sweelix\oauth2\server\interfaces\BaseModelInterface; |
18
|
|
|
use sweelix\oauth2\server\models\BaseModel; |
19
|
|
|
use sweelix\oauth2\server\Module; |
20
|
|
|
use sweelix\oauth2\server\traits\redis\TypeConverter; |
21
|
|
|
use yii\base\Object; |
22
|
|
|
use yii\di\Instance; |
23
|
|
|
use yii\helpers\Json; |
24
|
|
|
use yii\redis\Connection; |
25
|
|
|
use Yii; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This is the base service for redis |
29
|
|
|
* |
30
|
|
|
* @author Philippe Gaultier <[email protected]> |
31
|
|
|
* @copyright 2010-2016 Philippe Gaultier |
32
|
|
|
* @license http://www.sweelix.net/license license |
33
|
|
|
* @version XXX |
34
|
|
|
* @link http://www.sweelix.net |
35
|
|
|
* @package modules\v1\services\redis |
36
|
|
|
* @since XXX |
37
|
|
|
*/ |
38
|
|
|
class BaseService extends Object |
39
|
|
|
{ |
40
|
|
|
use TypeConverter; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string namespace used for key generation |
44
|
|
|
*/ |
45
|
|
|
public $namespace = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Connection|array|string the Redis DB connection object or the application component ID of the DB connection. |
49
|
|
|
*/ |
50
|
|
|
protected $db; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
6 |
|
public function init() |
56
|
|
|
{ |
57
|
6 |
|
parent::init(); |
58
|
6 |
|
$this->db = Instance::ensure(Module::getInstance()->db, Connection::className()); |
59
|
6 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Compute etag based on model attributes |
63
|
|
|
* @param BaseModelInterface $model |
64
|
|
|
* @return string |
65
|
|
|
* @since XXX |
66
|
|
|
*/ |
67
|
|
|
protected function computeEtag(BaseModelInterface $model) |
68
|
|
|
{ |
69
|
|
|
return $this->encodeAttributes($model->attributes); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Encode attributes array |
74
|
|
|
* |
75
|
|
|
* @param array $attributes |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
* @since XXX |
79
|
|
|
*/ |
80
|
|
|
protected function encodeAttributes(Array $attributes) |
81
|
|
|
{ |
82
|
|
|
$data = Json::encode($attributes); |
83
|
|
|
$etag = '"' . rtrim(base64_encode(sha1($data, true)), '=') . '"'; |
84
|
|
|
return $etag; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: