|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ArangoDB PHP client |
| 4 | + * File: bootstrap_connection_keep_alive.php |
| 5 | + * |
| 6 | + * @package ArangoDBClient |
| 7 | + * @author Frank Mayer |
| 8 | + */ |
| 9 | + |
| 10 | +namespace ArangoDBClient; |
| 11 | + |
| 12 | +require __DIR__ . '/../autoload.php'; |
| 13 | + |
| 14 | +if (class_exists('\PHPUnit\Framework\TestCase')) { |
| 15 | + class_alias(\PHPUnit\Framework\TestCase::class, 'PHPUnit_Framework_TestCase'); |
| 16 | +} |
| 17 | + |
| 18 | +/* set up a trace function that will be called for each communication with the server */ |
| 19 | + |
| 20 | +function isCluster(Connection $connection) |
| 21 | +{ |
| 22 | + static $isCluster = null; |
| 23 | + |
| 24 | + if ($isCluster === null) { |
| 25 | + $adminHandler = new AdminHandler($connection); |
| 26 | + try { |
| 27 | + $role = $adminHandler->getServerRole(); |
| 28 | + $isCluster = ($role === 'COORDINATOR' || $role === 'DBSERVER'); |
| 29 | + } catch (\Exception $e) { |
| 30 | + // maybe server version is too "old" |
| 31 | + $isCluster = false; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return $isCluster; |
| 36 | +} |
| 37 | + |
| 38 | +function getConnectionOptions() |
| 39 | +{ |
| 40 | + $traceFunc = function ($type, $data) { |
| 41 | + print 'TRACE FOR ' . $type . PHP_EOL; |
| 42 | + }; |
| 43 | + |
| 44 | + return [ |
| 45 | + ConnectionOptions::OPTION_ENDPOINT => 'tcp://localhost:8529', |
| 46 | + // endpoint to connect to |
| 47 | + ConnectionOptions::OPTION_CONNECTION => 'Keep-Alive', |
| 48 | + // can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections) |
| 49 | + ConnectionOptions::OPTION_AUTH_TYPE => 'Basic', |
| 50 | + // use basic authorization |
| 51 | + ConnectionOptions::OPTION_AUTH_USER => 'root', |
| 52 | + // user for basic authorization |
| 53 | + ConnectionOptions::OPTION_AUTH_PASSWD => '', |
| 54 | + // password for basic authorization |
| 55 | + ConnectionOptions::OPTION_TIMEOUT => 12, |
| 56 | + // timeout in seconds |
| 57 | + //ConnectionOptions::OPTION_TRACE => $traceFunc, // tracer function, can be used for debugging |
| 58 | + ConnectionOptions::OPTION_CREATE => false, |
| 59 | + // do not create unknown collections automatically |
| 60 | + ConnectionOptions::OPTION_UPDATE_POLICY => UpdatePolicy::LAST, |
| 61 | + // last update wins |
| 62 | + ConnectionOptions::OPTION_CHECK_UTF8_CONFORM => true |
| 63 | + // force UTF-8 checks for data |
| 64 | + ]; |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +function getConnection() |
| 69 | +{ |
| 70 | + return new Connection(getConnectionOptions()); |
| 71 | +} |
0 commit comments