|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EmanueleMinotto\DomainParserBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Parses an URL or a host. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Emanuele Minotto <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class ParseCommand extends ContainerAwareCommand |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Pdp Parser. |
|
20
|
|
|
* |
|
21
|
|
|
* @var \Pdp\Parser |
|
22
|
|
|
*/ |
|
23
|
|
|
private $parser; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
6 |
|
protected function configure() |
|
29
|
|
|
{ |
|
30
|
6 |
|
$this |
|
31
|
6 |
|
->setName('domain-parser:parse') |
|
32
|
6 |
|
->setDescription('Parses URL') |
|
33
|
6 |
|
->addArgument('url', InputArgument::REQUIRED, 'URL to parse') |
|
34
|
6 |
|
->addOption( |
|
35
|
6 |
|
'host-only', |
|
36
|
6 |
|
null, |
|
37
|
6 |
|
InputOption::VALUE_NONE, |
|
38
|
|
|
'Parses host only' |
|
39
|
6 |
|
); |
|
40
|
6 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
6 |
|
protected function initialize( |
|
46
|
|
|
InputInterface $input, |
|
47
|
|
|
OutputInterface $output |
|
48
|
|
|
) { |
|
49
|
6 |
|
$container = $this->getContainer(); |
|
50
|
6 |
|
$this->parser = $container->get('pdp.parser'); |
|
51
|
6 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
57
|
|
|
{ |
|
58
|
6 |
|
$parsed = $this->parse( |
|
59
|
6 |
|
$input->getArgument('url'), |
|
60
|
6 |
|
$input->getOption('host-only') |
|
61
|
6 |
|
); |
|
62
|
6 |
|
$output->writeln(sprintf('Parsed: <info>%s</info>', $parsed)); |
|
63
|
|
|
|
|
64
|
6 |
|
foreach ($parsed->toArray() as $key => $val) { |
|
65
|
6 |
|
$output->writeln(sprintf(' <comment>%s</comment>: %s', $key, $val)); |
|
66
|
6 |
|
} |
|
67
|
6 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Argument parsing. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $argument Url or domain |
|
73
|
|
|
* @param bool $hostOnly |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Pdp\Uri\Url|\Pdp\Uri\Url\Host |
|
76
|
|
|
*/ |
|
77
|
6 |
|
private function parse($argument, $hostOnly = false) |
|
78
|
|
|
{ |
|
79
|
6 |
|
$parsed = $this->parser->parseUrl($argument); |
|
80
|
|
|
|
|
81
|
6 |
|
if ($hostOnly) { |
|
82
|
3 |
|
return $parsed->host; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
return $parsed; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|