1 | <?php |
||
17 | class MongoConfig |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | public $dsn; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | public $host; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | public $database; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | public $user; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | public $pass; |
||
43 | |||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | public $port; |
||
48 | |||
49 | /** |
||
50 | * Constructor. |
||
51 | * |
||
52 | * @param string $database |
||
53 | * @param string $host |
||
54 | * @param string $user |
||
55 | * @param string|null $pass |
||
56 | * @param int|null $port |
||
57 | */ |
||
58 | public function __construct($database, $host = null, $user = null, $pass = null, $port = null) |
||
59 | { |
||
60 | $this->host = $host ?: 'localhost'; |
||
61 | $this->database = $database; |
||
62 | $this->user = $user; |
||
63 | $this->pass = $pass; |
||
64 | $this->port = $port ?: 27017; |
||
65 | $this->dsn = $this->buildDsn(); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Build dsn. |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | private function buildDsn() |
||
74 | { |
||
75 | $str = 'mongodb://'; |
||
76 | $str .= $this->buildAuth(); |
||
77 | |||
78 | if ($this->user) { |
||
79 | $str .= '@'; |
||
80 | } |
||
81 | |||
82 | return $str . $this->host . ':' . $this->port; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Build auth. |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | private function buildAuth() |
||
104 | } |
||
105 |