This repository was archived by the owner on Jan 31, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed
src/ZendDiagnostics/Check Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ It currently ships with the following Diagnostic Checks:
2222 * [ GuzzleHttpService] ( #guzzlehttpservice ) - check if given http host is responding using Guzzle,
2323 * [ HttpService] ( #httpservice ) - check if given http host is responding,
2424 * [ Memcache] ( #memcache ) - check if memcache extension is loaded and given server is reachable,
25+ * [ Mongo] ( #mongodb ) - check if connection to MongoDb is possible,
2526 * [ OpCacheMemory] ( #opcachememory ) - check if the OpCache memory usage is below warning/critical thresholds,
2627 * [ PDOCheck] ( #pdocheck ) - check if connection is possible,
2728 * [ PhpVersion] ( #phpversion ) - make sure that PHP version matches constraint,
@@ -487,6 +488,18 @@ $checkLocal = new Memcache('127.0.0.1'); // default port
487488$checkBackup = new Memcache('10.0.30.40', 11212);
488489````
489490
491+ ### MongoDb
492+ Check if connection to MongoDb is possible
493+
494+ ```` php
495+ <?php
496+ use ZendDiagnostics\Check\Mongo;
497+
498+ $mongoCheck = new Mongo('mongodb://127.0.0.1:27017');
499+ // and with user/password
500+ $mongoCheck = new Mongo('mongodb://user:
[email protected] :27017');
501+ ````
502+
490503### PhpVersion
491504
492505Check if current PHP version matches the given requirement. The test accepts 2 parameters - baseline version and
Original file line number Diff line number Diff line change 1+ <?php
2+ namespace ZendDiagnostics \Check ;
3+
4+ use ZendDiagnostics \Result \Success ;
5+ use ZendDiagnostics \Result \Failure ;
6+
7+ class Mongo extends AbstractCheck
8+ {
9+ /**
10+ * @var string
11+ */
12+ private $ connectionUri ;
13+
14+ /**
15+ * @param string $connectionUri
16+ */
17+ public function __construct ($ connectionUri )
18+ {
19+ $ this ->connectionUri = $ connectionUri ;
20+ }
21+
22+ /**
23+ * {@inheritdoc}
24+ */
25+ public function check ()
26+ {
27+ try {
28+ $ this ->getListDBs ();
29+ } catch (\Exception $ e ) {
30+ return new Failure (sprintf ('Failed to connect to MongoDB server. Reason: `%s` ' , $ e ->getMessage ()));
31+ }
32+
33+ return new Success ();
34+ }
35+
36+ /**
37+ * @return array|\Iterator
38+ *
39+ * @throws \RuntimeException
40+ * @throws \MongoDB\Driver\Exception
41+ * @throws \MongoConnectionException
42+ */
43+ private function getListDBs ()
44+ {
45+ if (class_exists ('\MongoDB\Client ' )) {
46+ return (new \MongoDB \Client ($ this ->connectionUri ))->listDatabases ();
47+ } elseif (class_exists ('\MongoClient ' )) {
48+ return (new \MongoClient ($ this ->server ))->listDBs ();
49+ }
50+
51+ throw new \RuntimeException ('Neither the mongo extension or mongodb are installed ' );
52+ }
53+ }
You can’t perform that action at this time.
0 commit comments