1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DH\NavigationBundle\Command; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DH\NavigationBundle\Model\Routing\Summary; |
7
|
|
|
use DH\NavigationBundle\NavigationManager; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
10
|
|
|
use Symfony\Component\Console\Helper\Table; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
|
16
|
|
|
class RoutingCommand extends Command |
17
|
|
|
{ |
18
|
|
|
protected static $defaultName = 'navigation:routing'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var NavigationManager |
22
|
|
|
*/ |
23
|
|
|
private $manager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param NavigationManager $manager |
27
|
|
|
*/ |
28
|
|
|
public function __construct(NavigationManager $manager) |
29
|
|
|
{ |
30
|
|
|
$this->manager = $manager; |
31
|
|
|
|
32
|
|
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
protected function configure() |
39
|
|
|
{ |
40
|
|
|
$this |
41
|
|
|
->setName('navigation:routing') |
42
|
|
|
->setDescription('Computes a route') |
43
|
|
|
->addOption('provider', null, InputOption::VALUE_REQUIRED) |
44
|
|
|
->addOption('waypoint', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Point de passage') |
45
|
|
|
->addOption('departure', null, InputOption::VALUE_REQUIRED, 'Departure date and time (YYYY-MM-DDD HH:II:SS)') |
46
|
|
|
->addOption('arrival', null, InputOption::VALUE_REQUIRED, 'Arrival date and time (YYYY-MM-DDD HH:II:SS)') |
47
|
|
|
->addOption('traffic', null, InputOption::VALUE_REQUIRED, 'Traffic mode (enabled/disabled/default depending on provider)') |
48
|
|
|
->addOption('language', null, InputOption::VALUE_REQUIRED, 'Language (fr-FR, en-US, etc.)') |
49
|
|
|
->setHelp(<<<'EOF' |
50
|
|
|
The <info>navigation:routing</info> command will compute a route from the given addresses. |
51
|
|
|
|
52
|
|
|
You can choose a provider with the "provider" option. |
53
|
|
|
|
54
|
|
|
<info>php bin/console navigation:routing --waypoint="45.834278,1.260816" --waypoint="44.830109,-0.603649" --provider=here</info> |
55
|
|
|
EOF |
56
|
|
|
) |
57
|
|
|
; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
64
|
|
|
{ |
65
|
|
|
if (\count($input->getOption('waypoint')) < 2) { |
|
|
|
|
66
|
|
|
throw new InvalidArgumentException('A route needs at least two waypoints (start and end locations).'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($input->getOption('provider')) { |
70
|
|
|
$this->manager->using($input->getOption('provider')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$query = $this->manager->createRoutingQuery(); |
74
|
|
|
|
75
|
|
|
foreach ($input->getOption('waypoint') as $waypoint) { |
76
|
|
|
$query->addWaypoint($waypoint); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($input->getOption('departure')) { |
80
|
|
|
$query->setDepartureTime(new DateTime($input->getOption('departure'))); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($input->getOption('arrival')) { |
84
|
|
|
$query->setArrivalTime(new DateTime($input->getOption('arrival'))); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($input->getOption('traffic')) { |
88
|
|
|
$query->setTrafficMode($input->getOption('traffic')); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($input->getOption('language')) { |
92
|
|
|
$query->setLanguage($input->getOption('language')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$response = $query->execute(); |
96
|
|
|
|
97
|
|
|
$io = new SymfonyStyle($input, $output); |
98
|
|
|
|
99
|
|
|
$routes = $response->getRoutes(); |
100
|
|
|
|
101
|
|
|
$io->section('Summary'); |
102
|
|
|
|
103
|
|
|
$data = []; |
104
|
|
|
foreach ($routes as $index => $route) { |
105
|
|
|
/** |
106
|
|
|
* @var Summary |
107
|
|
|
*/ |
108
|
|
|
$summary = $route->getSummary(); |
109
|
|
|
$data[] = [ |
110
|
|
|
$index + 1, |
111
|
|
|
$summary->getDistance()->getFormattedValue(2), |
112
|
|
|
$summary->getTravelTime()->getFormattedValue(2), |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$table = new Table($output); |
117
|
|
|
$table |
118
|
|
|
->setHeaders(['route', 'distance', 'duration']) |
119
|
|
|
->setRows($data) |
120
|
|
|
; |
121
|
|
|
$table->render(); |
122
|
|
|
|
123
|
|
|
$io->newLine(); |
124
|
|
|
|
125
|
|
|
foreach ($routes as $routeIndex => $route) { |
126
|
|
|
$io->section('Route #'.($routeIndex + 1)); |
127
|
|
|
|
128
|
|
|
$legs = $route->getLegs(); |
129
|
|
|
foreach ($legs as $legIndex => $leg) { |
130
|
|
|
$steps = $leg->getSteps(); |
131
|
|
|
$io->writeln(sprintf('<comment>Leg #%d</comment> - %d steps.', $legIndex + 1, \count($steps))); |
132
|
|
|
|
133
|
|
|
$data = []; |
134
|
|
|
foreach ($steps as $stepIndex => $step) { |
135
|
|
|
$data[] = [ |
136
|
|
|
$stepIndex + 1, |
137
|
|
|
implode(', ', $step->getPosition()), |
138
|
|
|
$step->getDistance()->getFormattedValue(2), |
139
|
|
|
$step->getDuration()->getFormattedValue(2), |
140
|
|
|
$step->getInstruction(), |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$table = new Table($output); |
145
|
|
|
$table |
146
|
|
|
->setHeaders(['step', 'position', 'distance', 'duration', 'instruction']) |
147
|
|
|
->setRows($data) |
148
|
|
|
->setColumnMaxWidth(4, 100) |
149
|
|
|
; |
150
|
|
|
$table->render(); |
151
|
|
|
$io->newLine(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return 0; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|