|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* @file MapDocCmd.php |
|
6
|
|
|
* @brief This file contains the MapDocCmd class. |
|
7
|
|
|
* @details |
|
8
|
|
|
* @author Filippo F. Fadda |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace EoC\Command; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @brief Maps a document against every single map function stored into the server. |
|
17
|
|
|
* @details When the view function is stored in the server, CouchDB starts sending in all the documents in the |
|
18
|
|
|
* database, one at a time. The server calls the previously stored functions one after another with the document |
|
19
|
|
|
* and stores its result. When all functions have been called, the result is returned as a JSON string.\n\n |
|
20
|
|
|
* The argument provided by CouchDB has the following structure: |
|
21
|
|
|
@code |
|
22
|
|
|
Array |
|
23
|
|
|
( |
|
24
|
|
|
[0] => Array |
|
25
|
|
|
( |
|
26
|
|
|
[_id] => 32012 |
|
27
|
|
|
[_rev] => 1-f19919e544340438babac6cc86ec61d5 |
|
28
|
|
|
[title] => Visual Modelling with Rational Rose 2000 and UML |
|
29
|
|
|
) |
|
30
|
|
|
) |
|
31
|
|
|
@endcode |
|
32
|
|
|
*/ |
|
33
|
|
|
final class MapDocCmd extends AbstractCmd { |
|
34
|
|
|
use CmdTrait; |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
public static function getName() { |
|
38
|
|
|
return "map_doc"; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
// @brief Converts the array to an object. |
|
43
|
|
|
// @return object |
|
44
|
|
|
public static function arrayToObject($array) { |
|
45
|
|
|
return is_array($array) ? (object)array_map(__METHOD__, $array) : $array; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
public function execute() { |
|
50
|
|
|
$doc = self::arrayToObject(reset($this->args)); |
|
51
|
|
|
|
|
52
|
|
|
$this->server->getMonolog()->addDebug("MAP ".$doc->_id); |
|
53
|
|
|
|
|
54
|
|
|
// We use a closure here, so we can just expose the emit() function to the closure provided by the user. We have |
|
55
|
|
|
// another advantage: the $map variable is defined inside execute(), so we don't need to declare it as class member. |
|
56
|
|
|
$emit = function($key = NULL, $value = NULL) use (&$map) { |
|
|
|
|
|
|
57
|
|
|
$map[] = array($key, $value); |
|
58
|
|
|
}; |
|
59
|
|
|
|
|
60
|
|
|
$closure = NULL; // This initialization is made just to prevent a lint error during development. |
|
61
|
|
|
|
|
62
|
|
|
$result = []; // Every time we map a document against all the registered functions we must reset the result. |
|
63
|
|
|
|
|
64
|
|
View Code Duplication |
foreach ($this->server->getFuncs() as $fn) { |
|
|
|
|
|
|
65
|
|
|
$map = []; // Every time we map a document against a function we must reset the map. |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
// Here we call the closure function stored in the view. The $closure variable contains the function implementation |
|
68
|
|
|
// provided by the user. You can have multiple views in a design document and for every single view you can have |
|
69
|
|
|
// only one map function. |
|
70
|
|
|
// The closure must be declared like: |
|
71
|
|
|
// |
|
72
|
|
|
// function($doc) use ($emit) { ... }; |
|
|
|
|
|
|
73
|
|
|
// |
|
74
|
|
|
// This technique let you use the syntax '$emit($key, $value);' to emit your record. The function doesn't return |
|
75
|
|
|
// any value. You don't need to include any files since the closure's code is executed inside this method. |
|
76
|
|
|
eval("\$closure = ".$fn); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
if (is_callable($closure)) { |
|
79
|
|
|
call_user_func($closure, $doc); |
|
80
|
|
|
$result[] = $map; |
|
81
|
|
|
} |
|
82
|
|
|
else |
|
83
|
|
|
throw new \BadFunctionCallException("The map function is not callable."); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Sends mappings to CouchDB. |
|
87
|
|
|
$this->server->writeln(json_encode($result)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.