1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tidal/WampWatch package. |
5
|
|
|
* (c) 2016 Timo Michna <timomichna/yahoo.de> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tidal\WampWatch\Model; |
13
|
|
|
|
14
|
|
|
use Guzzle\Common\Exception\RuntimeException; |
15
|
|
|
use Tidal\WampWatch\Model\Property\Collection\HasRealmsTrait; |
16
|
|
|
use Tidal\WampWatch\Model\Property\Scalar\HasUriTrait; |
17
|
|
|
use Tidal\WampWatch\Model\Behavior\Property\HasCollectionsTrait; |
18
|
|
|
use Tidal\WampWatch\Model\Property\ObjectCollection; |
19
|
|
|
use Exception; |
20
|
|
|
|
21
|
|
|
class Router implements Contract\RouterInterface |
22
|
|
|
{ |
23
|
|
|
use HasCollectionsTrait; |
24
|
|
|
use HasRealmsTrait; |
25
|
|
|
use HasUriTrait; |
26
|
|
|
|
27
|
|
|
const DEFAULT_CONNECTION_CLS = '\Tidal\WampWatch\Model\Connection'; |
28
|
|
|
|
29
|
|
|
private static $connectionFactory; |
30
|
|
|
|
31
|
|
|
public function __construct($uri) |
32
|
|
|
{ |
33
|
|
|
$this->setUri((string) $uri); |
34
|
|
|
$this->setRealms(new ObjectCollection()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param callable $factory |
39
|
|
|
*/ |
40
|
|
|
public static function setConnectionFactory(callable $factory) |
41
|
|
|
{ |
42
|
|
|
self::$connectionFactory = $factory; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param \Tidal\WampWatch\Model\Contract\RealmInterface $realm |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
* |
50
|
|
|
* @throws \Exception |
51
|
|
|
*/ |
52
|
|
|
public function connect(Contract\RealmInterface $realm) |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
$factory = self::$connectionFactory; |
56
|
|
|
/** @var Contract\ConnectionInterface $connection */ |
57
|
|
|
$connection = $factory($this, $realm); |
58
|
|
|
if (!is_a($connection, Contract\ConnectionInterface::class)) { |
59
|
|
|
throw new RuntimeException( |
60
|
|
|
'Callable registered as factory for Connection did not return Connection instance' |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} catch (Exception $e) { |
64
|
|
|
throw $e; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $connection; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
Router::setConnectionFactory(function (Contract\RouterInterface $router, Contract\RealmInterface $realm) { |
72
|
|
|
return new Connection($router, $realm); |
73
|
|
|
}); |
74
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.