1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file ConnectCommand.php |
5
|
|
|
* @brief This file contains the ConnectCommand class. |
6
|
|
|
* @details |
7
|
|
|
* @author Filippo F. Fadda |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace EoC\CLI\Command; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Symfony\Component\Console\Question\Question; |
19
|
|
|
|
20
|
|
|
use EoC\Couch; |
21
|
|
|
use EoC\Adapter; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @brief Connects to CouchDB server.. |
26
|
|
|
* @nosubgrouping |
27
|
|
|
* @todo Fix a bug when read map and reduce from file. |
28
|
|
|
*/ |
29
|
|
|
class ConnectCommand extends AbstractCommand { |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @brief Configures the command. |
34
|
|
|
*/ |
35
|
|
|
protected function configure() { |
36
|
|
|
$this->setName("connect"); |
37
|
|
|
$this->setDescription("Connects to CouchDB server"); |
38
|
|
|
|
39
|
|
|
$this->addArgument("user", |
40
|
|
|
InputArgument::REQUIRED, |
41
|
|
|
"The CouchDB user name to use when connecting to the server"); |
42
|
|
|
|
43
|
|
|
$this->addOption("server", |
44
|
|
|
"s", |
45
|
|
|
InputOption::VALUE_OPTIONAL, |
46
|
|
|
<<<'DESC' |
47
|
|
|
Connects to the CouchDB server on the given host |
48
|
|
|
Server must be expressed as host:port according to RFC 3986. |
49
|
|
|
DESC |
50
|
|
|
, |
51
|
|
|
Adapter\AbstractAdapter::DEFAULT_SERVER); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @brief Executes the command. |
57
|
|
|
*/ |
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
|
|
|
59
|
|
|
$user = (string)$input->getArgument('user'); |
60
|
|
|
|
61
|
|
|
$question = new Question('Enter password:'); |
62
|
|
|
$question->setHidden(true); |
63
|
|
|
$question->setHiddenFallback(false); |
64
|
|
|
|
65
|
|
|
$helper = $this->getHelper('question'); |
66
|
|
|
$password = $helper->ask($input, $output, $question); |
67
|
|
|
|
68
|
|
|
$server = (string)$input->getOption('server'); |
69
|
|
|
|
70
|
|
|
$couch = new Couch(new Adapter\CurlAdapter($server, $user, $password)); |
71
|
|
|
$couch->getServerInfo(); // Checks the connection. |
72
|
|
|
|
73
|
|
|
$connection = []; |
74
|
|
|
$connection['server'] = $server; |
75
|
|
|
$connection['user'] = $user; |
76
|
|
|
$connection['password'] = $password; |
77
|
|
|
|
78
|
|
|
$serialized = serialize($connection); |
79
|
|
|
|
80
|
|
|
// We reset the database in use. |
81
|
|
|
if ($shmKey = ftok($_SERVER['PHP_SELF'], 'd')) { |
82
|
|
|
if (@$shmId = shmop_open($shmKey, "a", 0644, 0)) |
83
|
|
|
shmop_delete($shmId); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
if ($shmKey = ftok($_SERVER['PHP_SELF'], 'c')) { |
|
|
|
|
87
|
|
|
|
88
|
|
|
if (@$shmId = shmop_open($shmKey, 'a', 0644, 0)) |
89
|
|
|
shmop_delete($shmId); |
90
|
|
|
|
91
|
|
|
$shmId = shmop_open($shmKey, 'n', 0644, mb_strlen($serialized)); |
92
|
|
|
|
93
|
|
|
if ($shmId) { |
94
|
|
|
$shmBytesWritten = shmop_write($shmId, $serialized, 0); |
|
|
|
|
95
|
|
|
shmop_close($shmId); |
96
|
|
|
} |
97
|
|
|
else |
98
|
|
|
throw new \RuntimeException("Couldn't create shared memory segment."); |
99
|
|
|
} |
100
|
|
|
else |
101
|
|
|
throw new \RuntimeException("Cannot get a System V IPC key."); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: