1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
11
|
|
|
use TreeHouse\IoBundle\Export\FeedExporter; |
12
|
|
|
use TreeHouse\IoBundle\Export\FeedType\FeedTypeInterface; |
13
|
|
|
|
14
|
|
|
class ExportValidateCommand extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var FeedExporter |
18
|
|
|
*/ |
19
|
|
|
protected $exporter; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \XMLReader |
23
|
|
|
*/ |
24
|
|
|
protected $reader; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $currentItem; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param FeedExporter $exporter |
33
|
|
|
*/ |
34
|
|
|
public function __construct(FeedExporter $exporter) |
35
|
|
|
{ |
36
|
|
|
$this->exporter = $exporter; |
37
|
|
|
|
38
|
|
|
parent::__construct(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
protected function configure() |
45
|
|
|
{ |
46
|
|
|
$this->addArgument('type', InputArgument::REQUIRED, 'The type for which the feed needs to be validated'); |
47
|
|
|
$this->setName('io:export:validate'); |
48
|
|
|
$this->setDescription('Validate an exported feed'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
55
|
|
|
{ |
56
|
|
|
$type = $this->exporter->getType($input->getArgument('type')); |
|
|
|
|
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$this->validate($type, $output); |
60
|
|
|
} catch (\RuntimeException $exception) { |
61
|
|
|
$output->writeln(''); |
62
|
|
|
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage())); |
63
|
|
|
$output->writeln(''); |
64
|
|
|
$output->writeln('Node:'); |
65
|
|
|
$output->writeln($this->currentItem); |
66
|
|
|
|
67
|
|
|
return 1; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$output->writeln(''); |
71
|
|
|
$output->writeln(sprintf('<info>Feed "%s" is valid!</info>', $type->getName())); |
72
|
|
|
|
73
|
|
|
return 0; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param FeedTypeInterface $type |
78
|
|
|
* @param OutputInterface $output |
79
|
|
|
* |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
|
|
protected function validate(FeedTypeInterface $type, OutputInterface $output) |
83
|
|
|
{ |
84
|
|
|
$file = $this->exporter->getFeedFilename($type); |
85
|
|
|
|
86
|
|
|
if (!file_exists($file)) { |
87
|
|
|
throw new FileNotFoundException(sprintf('<error>Feed "%s" has not yet been exported</error>', $type->getName())); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$options = LIBXML_NOENT | LIBXML_COMPACT | LIBXML_PARSEHUGE | LIBXML_NOERROR | LIBXML_NOWARNING; |
91
|
|
|
$this->reader = new \XMLReader($options); |
|
|
|
|
92
|
|
|
$this->reader->open($file); |
93
|
|
|
$this->reader->setParserProperty(\XMLReader::SUBST_ENTITIES, true); |
94
|
|
|
// foreach ($type->getNamespaces() as $name => $location) { |
95
|
|
|
// $this->reader->setSchema($location); |
96
|
|
|
// } |
97
|
|
|
|
98
|
|
|
libxml_clear_errors(); |
99
|
|
|
libxml_use_internal_errors(true); |
100
|
|
|
libxml_disable_entity_loader(true); |
101
|
|
|
|
102
|
|
|
$progress = new ProgressBar($output); |
103
|
|
|
$progress->start(); |
104
|
|
|
|
105
|
|
|
// go through the whole thing |
106
|
|
|
while ($this->reader->read()) { |
107
|
|
|
if ($this->reader->nodeType === \XMLReader::ELEMENT && $this->reader->name === $type->getItemNode()) { |
108
|
|
|
$progress->advance(); |
109
|
|
|
$this->currentItem = $this->reader->readOuterXml(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($error = libxml_get_last_error()) { |
113
|
|
|
throw new \RuntimeException( |
114
|
|
|
sprintf( |
115
|
|
|
'[%s %s] %s (in %s - line %d, column %d)', |
116
|
|
|
LIBXML_ERR_WARNING === $error->level ? 'WARNING' : 'ERROR', |
117
|
|
|
$error->code, |
118
|
|
|
trim($error->message), |
119
|
|
|
$error->file ? $error->file : 'n/a', |
120
|
|
|
$error->line, |
121
|
|
|
$error->column |
122
|
|
|
) |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$progress->finish(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.