|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
7
|
|
|
use Doctrine\ORM\Query; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
9
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use TreeHouse\IoBundle\Entity\Feed; |
|
14
|
|
|
use TreeHouse\IoBundle\Model\OriginInterface; |
|
15
|
|
|
|
|
16
|
|
|
class FeedListCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ManagerRegistry |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $doctrine; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param ManagerRegistry $doctrine |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(ManagerRegistry $doctrine) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->doctrine = $doctrine; |
|
29
|
|
|
|
|
30
|
|
|
parent::__construct(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritdoc |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function configure() |
|
37
|
|
|
{ |
|
38
|
|
|
$this |
|
39
|
|
|
->setName('io:feed:list') |
|
40
|
|
|
->setDescription('Queries/lists feed information') |
|
41
|
|
|
->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'The type of feeds to list') |
|
42
|
|
|
->addOption('fields', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The fields to list', ['id', 'type', 'origin', 'transportConfig']) |
|
43
|
|
|
; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
50
|
|
|
{ |
|
51
|
|
|
$repo = $this->doctrine->getRepository('TreeHouseIoBundle:Feed'); |
|
52
|
|
|
|
|
53
|
|
|
$builder = $repo->createQueryBuilder('f'); |
|
54
|
|
|
$builder->select('f'); |
|
55
|
|
|
|
|
56
|
|
|
if ($input->getOption('type')) { |
|
57
|
|
|
$builder |
|
58
|
|
|
->andWhere('f.type = :type') |
|
59
|
|
|
->setParameter('type', $input->getOption('type')) |
|
60
|
|
|
; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->displayFeeds($builder->getQuery(), $output, $input->getOption('fields')); |
|
64
|
|
|
|
|
65
|
|
|
return 0; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param Query $query |
|
70
|
|
|
* @param OutputInterface $output |
|
71
|
|
|
* @param array $fields |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function displayFeeds(Query $query, OutputInterface $output, array $fields) |
|
74
|
|
|
{ |
|
75
|
|
|
$table = new Table($output); |
|
76
|
|
|
$table->setHeaders($fields); |
|
77
|
|
|
|
|
78
|
|
|
/** @var ClassMetadata $meta */ |
|
79
|
|
|
$meta = $this->doctrine->getManager()->getClassMetadata('TreeHouseIoBundle:Feed'); |
|
80
|
|
|
|
|
81
|
|
|
/** @var Feed $feed */ |
|
82
|
|
|
foreach ($query->iterate() as list($feed)) { |
|
83
|
|
|
$row = []; |
|
84
|
|
|
foreach ($fields as $field) { |
|
85
|
|
|
$value = $meta->getFieldValue($feed, $field); |
|
86
|
|
|
$row[$field] = $this->formatValue($value); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$table->addRow($row); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$table->render(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param mixed $value |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function formatValue($value) |
|
101
|
|
|
{ |
|
102
|
|
|
if (is_scalar($value)) { |
|
103
|
|
|
return (string) $value; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (is_object($value)) { |
|
107
|
|
|
if (method_exists($value, '__toString')) { |
|
108
|
|
|
return (string) $value; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if ($value instanceof OriginInterface) { |
|
112
|
|
|
return $value->getTitle(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return json_encode($value, JSON_UNESCAPED_SLASHES); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|