1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Ember Db - An embeddable document database for php. |
4
|
|
|
* Copyright (C) 2016 Alexander During |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
* |
19
|
|
|
* @link http://github.com/alexanderduring/php-ember-db |
20
|
|
|
* @copyright Copyright (C) 2016 Alexander During |
21
|
|
|
* @license http://www.gnu.org/licenses GNU General Public License v3.0 |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace EmberDb\Client\Parser; |
25
|
|
|
|
26
|
|
|
use EmberDb\Client\Exception; |
27
|
|
|
use EmberDb\Document; |
28
|
|
|
use EmberDb\DocumentManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The responsibility of the class Parser is parsing the |
32
|
|
|
* input line and finding the right command and parameters. |
33
|
|
|
* Currently it also executes the command by invoking it on |
34
|
|
|
* the document manager. It also renders a help view. |
35
|
|
|
*/ |
36
|
|
|
class Parser |
37
|
|
|
{ |
38
|
|
|
/** @var \EmberDb\DocumentManager */ |
39
|
|
|
private $documentManager; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param \EmberDb\DocumentManager $documentManager |
45
|
|
|
*/ |
46
|
|
|
public function injectDocumentManager(DocumentManager $documentManager) |
47
|
|
|
{ |
48
|
|
|
$this->documentManager = $documentManager; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
public function execute($inputLine) |
54
|
|
|
{ |
55
|
|
|
$output = ''; |
56
|
|
|
|
57
|
|
|
$tokens = explode(' ', $inputLine); |
58
|
|
|
$command = array_shift($tokens); |
59
|
|
|
$parameters = $tokens; |
60
|
|
|
|
61
|
|
|
switch ($command) { |
62
|
|
|
case 'insert': |
63
|
|
|
try { |
64
|
|
|
if (count($parameters) != 2) { |
65
|
|
|
throw new Exception('Incorrect number of parameters for command "insert".'); |
66
|
|
|
} |
67
|
|
|
$collection = $parameters[0]; |
68
|
|
|
$documentData = json_decode($parameters[1], true); |
69
|
|
|
if ($documentData !== null) { |
70
|
|
|
$document = new Document($documentData); |
71
|
|
|
} else { |
72
|
|
|
throw new Exception('The description of the document is not a valid json.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->documentManager->insert($collection, $document); |
76
|
|
|
$output .= "Inserted document in the $collection collection.\n"; |
77
|
|
|
} |
78
|
|
|
catch (Exception $exception) { |
79
|
|
|
$output .= "Error: ".$exception->getMessage()."\n"; |
80
|
|
|
} |
81
|
|
|
break; |
82
|
|
|
case 'find': |
83
|
|
|
$collection = $parameters[0]; |
84
|
|
|
$filter = count($parameters) == 2 ? json_decode($parameters[1], true) : array(); |
85
|
|
|
$documents = $this->documentManager->find($collection, $filter); |
86
|
|
|
foreach ($documents as $document) { |
87
|
|
|
$output .= json_encode($document)."\n"; |
88
|
|
|
} |
89
|
|
|
break; |
90
|
|
|
case 'pwd': |
91
|
|
|
$output .= $this->documentManager->getDatabasePath()."\n"; |
92
|
|
|
break; |
93
|
|
|
case 'help': |
94
|
|
|
$output .= "Available commands:\n"; |
95
|
|
|
$output .= " insert <collection> <document> Insert the document <document> into the collection <collection>.\n"; |
96
|
|
|
$output .= " find <collection> <filter> Find all documents into the collection <collection> that match <filter>.\n"; |
97
|
|
|
$output .= " pwd Print working directory.\n"; |
98
|
|
|
$output .= " help Display this help.\n"; |
99
|
|
|
$output .= " exit Exit this client.\n"; |
100
|
|
|
break; |
101
|
|
|
default: |
102
|
|
|
$output .= "Syntax error: Unknown command '".$command."'.\n"; |
103
|
|
|
} |
104
|
|
|
$output .= "\n"; |
105
|
|
|
|
106
|
|
|
return $output; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|