Skip to content

Commit 30be717

Browse files
committed
replacing some cases of the antipattern "Service Locator" with proper DI
Adding Logging
1 parent d398367 commit 30be717

File tree

8 files changed

+90
-75
lines changed

8 files changed

+90
-75
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"require": {
2626
"php": ">=5.6.0",
2727
"ext-memcached": "*",
28-
"rdlowrey/auryn": "*"
28+
"rdlowrey/auryn": "*",
29+
"psr/log": "*"
2930
}
3031
}

src/Controller/Session.php

+31-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,35 @@
44

55
class Session implements YetAnotherWebStack\PhpMemcachedSession\Interfaces\Controller {
66

7+
/**
8+
*
9+
* @var \Psr\Log\LoggerInterface
10+
*/
11+
protected $logger;
12+
13+
/**
14+
*
15+
* @var \YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration
16+
*/
17+
protected $configuration;
18+
19+
/**
20+
*
21+
* @param \Psr\Log\LoggerInterface $logger
22+
* @param YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration $configuration
23+
*/
24+
public function __construct(\Psr\Log\LoggerInterface $logger,
25+
\YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration $configuration) {
26+
$this->logger = $logger;
27+
$this->configuration = $configuration;
28+
}
29+
730
/**
831
*
932
* @return boolean
1033
*/
1134
public function close() {
35+
$this->logger->debug("Trying to close the session...");
1236
return true;
1337
}
1438

@@ -17,11 +41,10 @@ public function close() {
1741
* @return string
1842
*/
1943
public function create_sid() {
44+
$this->logger->debug("Creating new session id");
2045
return sha1(
2146
mt_rand() . microtime() . getmypid() .
22-
\YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
23-
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration'
24-
)->getSpecific('sid_pepper')
47+
$this->configuration->getSpecific('sid_pepper')
2548
);
2649
}
2750

@@ -30,6 +53,7 @@ public function create_sid() {
3053
* @param string $session_id
3154
*/
3255
public function destroy($session_id) {
56+
$this->logger->debug("Destroying session");
3357
return \YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
3458
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Model',
3559
[':sessionId' => $session_id])->delete();
@@ -41,6 +65,7 @@ public function destroy($session_id) {
4165
* @return boolean
4266
*/
4367
public function gc($maxlifetime) {
68+
$this->logger->debug("Trying to clean sessions with $maxlifetime as lifetime");
4469
return true;
4570
}
4671

@@ -51,6 +76,7 @@ public function gc($maxlifetime) {
5176
* @return boolean
5277
*/
5378
public function open($save_path, $name) {
79+
$this->logger->debug("Trying open a session with the following parameters: $save_path,$name");
5480
return true;
5581
}
5682

@@ -60,6 +86,7 @@ public function open($save_path, $name) {
6086
* @return string
6187
*/
6288
public function read($session_id) {
89+
$this->logger->debug("Trying read session $session_id");
6390
return \YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
6491
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Model',
6592
['sessionId' => $session_id])->load();
@@ -72,6 +99,7 @@ public function read($session_id) {
7299
* @return boolean
73100
*/
74101
public function write($session_id, $session_data) {
102+
$this->logger->debug("Trying write to the session $session_id");
75103
return \YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
76104
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Model',
77105
['sessionId' => $session_id])->save($session_data);

src/Interfaces/Configuration.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@ interface Configuration {
99
* @param string $key
1010
* @return mixed
1111
*/
12-
public function getGeneral($key);
12+
public function getGeneral(string $key);
1313

1414
/**
1515
*
1616
* @param string $key
1717
* @return mixed
1818
*/
19-
public function getSpecific($key);
19+
public function getSpecific(string $key);
2020

2121
/**
2222
*
2323
* @param string $key
24+
* @param mixed $value
2425
* @return mixed
2526
*/
26-
public function setGeneral($key, $value);
27+
public function setGeneral(string $key, $value);
2728

2829
/**
2930
*
3031
* @param string $key
32+
* @param mixed $value
3133
* @return mixed
3234
*/
33-
public function setSpecific($key, $value);
35+
public function setSpecific(string $key, $value);
3436
}

src/Interfaces/Model.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ interface Model {
99
* @param string $sessionId
1010
* @param \YetAnotherWebStack\PhpMemcachedSession\Interfaces\Repository $repository
1111
*/
12-
public function __construct($sessionId, $repository);
12+
public function __construct(string $sessionId,
13+
\YetAnotherWebStack\PhpMemcachedSession\Interfaces\Repository $repository);
1314

1415
/**
1516
*
@@ -22,17 +23,10 @@ public function load();
2223
* @param string $data
2324
* @return boolean was it saved?
2425
*/
25-
public function save($data);
26+
public function save(string $data);
2627

2728
/**
2829
* deletes the current data and instance
2930
*/
3031
public function delete();
31-
32-
/**
33-
*
34-
* @param string $sessionId
35-
* @return \YetAnotherWebStack\PhpMemcachedSession\Model\Session
36-
*/
37-
public static function get($sessionId);
3832
}

src/Interfaces/Repository.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function getByKey(array $params);
1717
* @param string $value
1818
* @return boolean
1919
*/
20-
public function setByKey(array $params, $value);
20+
public function setByKey(array $params, string $value);
2121

2222
/**
2323
*

src/Model/Session.php

+15-29
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,23 @@ class Session implements YetAnotherWebStack\PhpMemcachedSession\Interfaces\Model
3434
*/
3535
protected $ipPart;
3636

37+
/**
38+
*
39+
* @var \Psr\Log\LoggerInterface
40+
*/
41+
protected $logger;
42+
3743
/**
3844
*
3945
* @param string $sessionId
4046
* @param \YetAnotherWebStack\PhpMemcachedSession\Interfaces\Repository $repository
4147
*/
4248
protected function __construct($sessionId,
43-
\YetAnotherWebStack\PhpMemcachedSession\Interfaces\Repository $repository) {
49+
\YetAnotherWebStack\PhpMemcachedSession\Interfaces\Repository $repository,
50+
\Psr\Log\LoggerInterface $logger) {
51+
$this->logger = $logger;
4452
$this->sessionId = $sessionId;
45-
$this->agent = $this->getUserAgent();
53+
$this->agent = md5($_SERVER['HTTP_USER_AGENT']);
4654
$this->ipPart = explode(strpos($_SERVER['REMOTE_ADDR'], '.') ? '.' : ':',
4755
$_SERVER['REMOTE_ADDR'])[0];
4856
$this->repository = $repository;
@@ -61,6 +69,7 @@ protected function getKeys() {
6169
*/
6270
protected function __destruct() {
6371
$this->save(serialize($_SESSION));
72+
$this->logger->debug("Saved session");
6473
}
6574

6675
/**
@@ -69,6 +78,7 @@ protected function __destruct() {
6978
*/
7079
public function load() {
7180
$this->original = $this->getByKey($this->getKeys()) . '';
81+
$this->logger->debug("Loading session");
7282
return $this->original;
7383
}
7484

@@ -79,44 +89,20 @@ public function load() {
7989
*/
8090
public function save($data) {
8191
if ($data === $this->original) {
92+
$this->logger->debug("Session unchanged, nothing to store");
8293
return true; //nothing to change
8394
}
95+
$this->logger->debug("Session changed, storing");
8496
return $this->repository->updateByKey($this->getKeys(), $data);
8597
}
8698

8799
/**
88100
* deletes the current data and instance
89101
*/
90102
public function delete() {
103+
$this->logger->debug("Deleting session");
91104
$this->repository->removeByKey($this->getKeys());
92105
self::$instance = null;
93106
}
94107

95-
/**
96-
*
97-
* @return string
98-
*/
99-
private function getUserAgent() {
100-
$agent = $_SERVER['HTTP_USER_AGENT'];
101-
if (strpos($agent, 'Opera') || strpos($agent, 'OPR/')) {
102-
return 'opera';
103-
}
104-
if (strpos($agent, 'Edge')) {
105-
return 'edge';
106-
}
107-
if (strpos($agent, 'Chrome')) {
108-
return 'chrome';
109-
}
110-
if (strpos($agent, 'Safari')) {
111-
return 'safari';
112-
}
113-
if (strpos($agent, 'Firefox')) {
114-
return 'firefox';
115-
}
116-
if (strpos($agent, 'MSIE') || strpos($agent, 'Trident/7')) {
117-
return 'internet explorer';
118-
}
119-
return trim(preg_replace('/(\/|\(|[0-9]|V[0-9]).*/i', '', $agent));
120-
}
121-
122108
}

src/Repository/MemCacheRead.php

+29-25
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,31 @@ class MemCacheRead implements \YetAnotherWebStack\PhpMemcachedSession\Interfaces
2222
*/
2323
protected $memcache;
2424

25+
/**
26+
*
27+
* @var \Psr\Log\LoggerInterface
28+
*/
29+
protected $logger;
30+
31+
/**
32+
*
33+
* @var \YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration
34+
*/
35+
protected $configuration;
36+
2537
/**
2638
* parma \Memcached $memcache
2739
*/
28-
public function __construct(\Memcached $memcache) {
40+
public function __construct(\Memcached $memcache,
41+
\Psr\Log\LoggerInterface $logger,
42+
\YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration $configuration) {
2943
$this->memcache = $memcache;
44+
$this->logger = $logger;
45+
$this->configuration = $configuration;
3046
$this->memcache->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
3147
$this->setMemcacheLogin();
3248
$this->initializeServer();
33-
$this->duration = DependencyInjector::get(
34-
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration'
35-
)->getGeneral("gc_maxlifetime");
49+
$this->duration = $configuration->getGeneral("gc_maxlifetime");
3650
if (!$this->duration) {
3751
$this->duration = 3600;
3852
}
@@ -43,19 +57,10 @@ public function __construct(\Memcached $memcache) {
4357
* add login data if provided
4458
*/
4559
protected function setMemcacheLogin() {
46-
if (\YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
47-
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration'
48-
)->getSpecific('memcache_user') &&
49-
\YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
50-
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration'
51-
)->getSpecific('memcache_password')) {
60+
if ($this->configuration->getSpecific('memcache_user') && $this->configuration->getSpecific('memcache_password')) {
5261
$this->memcache->setSaslAuthData(
53-
\YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
54-
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration'
55-
)->getSpecific('memcache_user'),
56-
\YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
57-
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration'
58-
)->getSpecific('memcache_password')
62+
$this->configuration->getSpecific('memcache_user'),
63+
$this->configuration->getSpecific('memcache_password')
5964
);
6065
}
6166
}
@@ -66,12 +71,8 @@ protected function setMemcacheLogin() {
6671
protected function initializeServer() {
6772
if (count($this->memcache->getServerList()) == 0) {
6873
$this->memcache->addServer(
69-
\YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
70-
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration'
71-
)->getSpecific('memcache_server'),
72-
\YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
73-
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration'
74-
)->getSpecific('memcache_port')
74+
$this->configuration->getSpecific('memcache_server'),
75+
$this->configuration->getSpecific('memcache_port')
7576
);
7677
}
7778
}
@@ -98,10 +99,10 @@ public function getByKey(array $params) {
9899
$this->memcache->touch($this->getKey($params),
99100
time() + $this->duration);
100101
}
101-
$unserializer = \YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
102-
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration'
103-
)->getSpecific('unserializer');
102+
$unserializer = $this->configuration->getSpecific('unserializer');
104103
if ($unserializer && is_callable($unserializer)) {
104+
$this->logger->debug("re-serializing value of " . implode('.',
105+
$params));
105106
$value = serialize(call_user_func($unserializer, $value));
106107
}
107108
return $value;
@@ -114,6 +115,8 @@ public function getByKey(array $params) {
114115
* @return boolean
115116
*/
116117
public function setByKey(array $params, $value) {
118+
$this->logger->debug("will not set value $value at " . implode('.',
119+
$params));
117120
return false;
118121
}
119122

@@ -123,6 +126,7 @@ public function setByKey(array $params, $value) {
123126
* @return boolean
124127
*/
125128
public function removeByKey(array $params) {
129+
$this->logger->debug("will not delete " . implode('.', $params));
126130
return false;
127131
}
128132

src/Repository/MemCacheReadWrite.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ public function getByKey(array $params) {
2424
* @return boolean
2525
*/
2626
public function setByKey(array $params, $value) {
27-
$serializer = \YetAnotherWebStack\PhpMemcachedSession\Service\DependencyInjector::get(
28-
'YetAnotherWebStack\PhpMemcachedSession\Interfaces\Configuration'
29-
)->getSpecific('serializer');
27+
$this->logger->debug("setting $value at " . implode('.', $params));
28+
$serializer = $this->configuration->getSpecific('serializer');
3029
$status = $this->memcache->set($this->getKey($params),
3130
$serializer ?
3231
call_user_func($serializer, unserialize($value)) : $value,
@@ -41,6 +40,7 @@ public function setByKey(array $params, $value) {
4140
* @return boolean
4241
*/
4342
public function removeByKey(array $params) {
43+
$this->logger->debug("deleting " . implode('.', $params));
4444
$this->memcache->delete($this->getKey($params) . '.locked');
4545
return $this->memcache->delete($this->getKey($params));
4646
}

0 commit comments

Comments
 (0)