1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ODM\MongoDB\Tools\Console\Command; |
21
|
|
|
|
22
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
23
|
|
|
use Symfony\Component\Console\Input\InputOption; |
24
|
|
|
use Symfony\Component\Console; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Command to query mongodb and inspect the outputted results from your document classes. |
28
|
|
|
* |
29
|
|
|
* @since 1.0 |
30
|
|
|
* @author Jonathan Wage <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class QueryCommand extends Console\Command\Command |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @see Console\Command\Command |
36
|
|
|
*/ |
37
|
|
|
protected function configure() |
38
|
|
|
{ |
39
|
|
|
$this |
40
|
|
|
->setName('odm:query') |
41
|
|
|
->setDescription('Query mongodb and inspect the outputted results from your document classes.') |
42
|
|
|
->setDefinition(array( |
43
|
|
|
new InputArgument( |
44
|
|
|
'class', InputArgument::REQUIRED, |
45
|
|
|
'The class to query.' |
46
|
|
|
), |
47
|
|
|
new InputArgument( |
48
|
|
|
'query', InputArgument::REQUIRED, |
49
|
|
|
'The query to execute and output the results for.' |
50
|
|
|
), |
51
|
|
|
new InputOption( |
52
|
|
|
'hydrate', null, InputOption::VALUE_NONE, |
53
|
|
|
'Whether or not to hydrate the results in to document objects.' |
54
|
|
|
), |
55
|
|
|
new InputOption( |
56
|
|
|
'skip', null, InputOption::VALUE_REQUIRED, |
57
|
|
|
'The number of documents to skip in the cursor.' |
58
|
|
|
), |
59
|
|
|
new InputOption( |
60
|
|
|
'limit', null, InputOption::VALUE_REQUIRED, |
61
|
|
|
'The number of documents to return.' |
62
|
|
|
), |
63
|
|
|
new InputOption( |
64
|
|
|
'depth', null, InputOption::VALUE_REQUIRED, |
65
|
|
|
'Dumping depth of Document graph.', 7 |
66
|
|
|
) |
67
|
|
|
)) |
68
|
|
|
->setHelp(<<<EOT |
69
|
|
|
Execute a query and output the results. |
70
|
|
|
EOT |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @see Console\Command\Command |
76
|
|
|
*/ |
77
|
|
|
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) |
78
|
|
|
{ |
79
|
|
|
$dm = $this->getHelper('documentManager')->getDocumentManager(); |
80
|
|
|
$qb = $dm->getRepository($input->getArgument('class'))->createQueryBuilder(); |
81
|
|
|
$qb->setQueryArray((array) json_decode($input->getArgument('query'))); |
82
|
|
|
$qb->hydrate((bool) $input->getOption('hydrate')); |
83
|
|
|
|
84
|
|
|
$depth = $input->getOption('depth'); |
85
|
|
|
|
86
|
|
|
if ( ! is_numeric($depth)) { |
87
|
|
|
throw new \LogicException("Option 'depth' must contain an integer value"); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
if (($skip = $input->getOption('skip')) !== null) { |
|
|
|
|
91
|
|
|
if ( ! is_numeric($skip)) { |
92
|
|
|
throw new \LogicException("Option 'skip' must contain an integer value"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$qb->skip((int) $skip); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
if (($limit = $input->getOption('limit')) !== null) { |
|
|
|
|
99
|
|
|
if ( ! is_numeric($limit)) { |
100
|
|
|
throw new \LogicException("Option 'limit' must contain an integer value"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$qb->limit((int) $limit); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
foreach ($qb->getQuery() as $result) { |
107
|
|
|
\Doctrine\Common\Util\Debug::dump($result, $depth); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.