1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file UseCommand.php |
5
|
|
|
* @brief This file contains the UseCommand 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\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @brief Selects the specified database. |
21
|
|
|
* @nosubgrouping |
22
|
|
|
*/ |
23
|
|
|
class UseCommand extends AbstractCommand { |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @brief Configures the command. |
28
|
|
|
*/ |
29
|
|
|
protected function configure() { |
30
|
|
|
$this->setName("use"); |
31
|
|
|
$this->setDescription("Uses the specified database"); |
32
|
|
|
$this->setAliases(['select']); |
33
|
|
|
|
34
|
|
|
$this->addArgument("database", |
35
|
|
|
InputArgument::REQUIRED, |
36
|
|
|
"The CouchDB database name to use"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @brief Executes the command. |
42
|
|
|
*/ |
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
|
|
|
44
|
|
|
$database = (string)$input->getArgument('database'); |
45
|
|
|
|
46
|
|
View Code Duplication |
if ($shmKey = ftok($_SERVER['PHP_SELF'], 'd')) { |
|
|
|
|
47
|
|
|
|
48
|
|
|
if (@$shmId = shmop_open($shmKey, 'a', 0644, 0)) |
49
|
|
|
shmop_delete($shmId); |
50
|
|
|
|
51
|
|
|
$shmId = shmop_open($shmKey, 'n', 0644, mb_strlen($database)); |
52
|
|
|
|
53
|
|
|
if ($shmId) { |
54
|
|
|
$shmBytesWritten = shmop_write($shmId, $database, 0); |
|
|
|
|
55
|
|
|
shmop_close($shmId); |
56
|
|
|
} |
57
|
|
|
else |
58
|
|
|
throw new \RuntimeException("Couldn't create shared memory segment."); |
59
|
|
|
} |
60
|
|
|
else |
61
|
|
|
throw new \RuntimeException("Cannot get a System V IPC key."); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
} |
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: