|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\LearnYouPhp\Exercise; |
|
4
|
|
|
|
|
5
|
|
|
use Hoa\Core\Exception\Exception; |
|
6
|
|
|
use PhpSchool\LearnYouPhp\TcpSocketFactory; |
|
7
|
|
|
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent; |
|
8
|
|
|
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
|
9
|
|
|
use PhpSchool\PhpWorkshop\Exercise\CliExercise; |
|
10
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
|
11
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
|
12
|
|
|
use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
|
13
|
|
|
use PhpSchool\PhpWorkshop\Result\Failure; |
|
14
|
|
|
use PhpSchool\PhpWorkshop\Result\StdOutFailure; |
|
15
|
|
|
use PhpSchool\PhpWorkshop\Result\Success; |
|
16
|
|
|
use Psr\Http\Message\RequestInterface; |
|
17
|
|
|
use Symfony\Component\Process\Process; |
|
18
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
|
19
|
|
|
use Zend\Diactoros\Request; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class TimeServer |
|
23
|
|
|
* @package PhpSchool\LearnYouPhp\Exercise |
|
24
|
|
|
* @author Aydin Hassan <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class TimeServer extends AbstractExercise implements ExerciseInterface, CliExercise |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var TcpSocketFactory |
|
31
|
|
|
*/ |
|
32
|
|
|
private $socketFactory; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* TimeServer constructor. |
|
36
|
|
|
* @param TcpSocketFactory $socketFactory |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(TcpSocketFactory $socketFactory) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->socketFactory = $socketFactory; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getName() |
|
47
|
|
|
{ |
|
48
|
|
|
return 'Time Server'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getDescription() |
|
55
|
|
|
{ |
|
56
|
|
|
return 'Build a Time Server!'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param ExerciseDispatcher $exerciseDispatcher |
|
61
|
|
|
*/ |
|
62
|
|
|
public function configure(ExerciseDispatcher $exerciseDispatcher) |
|
63
|
|
|
{ |
|
64
|
|
|
$eventDispatcher = $exerciseDispatcher->getEventDispatcher(); |
|
65
|
|
|
|
|
66
|
|
|
$eventDispatcher->listen('cli.verify.solution-execute.pre', function (CliExecuteEvent $event) { |
|
67
|
|
|
$event->appendArg('127.0.0.1'); |
|
68
|
|
|
$event->appendArg($this->getRandomPort()); |
|
69
|
|
|
}); |
|
70
|
|
|
|
|
71
|
|
|
$eventDispatcher->listen('cli.verify.user-execute.pre', function (CliExecuteEvent $event) { |
|
72
|
|
|
$event->appendArg('127.0.0.1'); |
|
73
|
|
|
$event->appendArg($this->getRandomPort()); |
|
74
|
|
|
}); |
|
75
|
|
|
|
|
76
|
|
|
$eventDispatcher->listen('cli.verify.solution.executing', function (CliExecuteEvent $event) { |
|
77
|
|
|
$args = $event->getArgs()->getArrayCopy(); |
|
78
|
|
|
$client = $this->socketFactory->createClient(...$args); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
//wait for server to boot |
|
81
|
|
|
usleep(100000); |
|
82
|
|
|
|
|
83
|
|
|
$client->connect(); |
|
84
|
|
|
$client->readAll(); |
|
85
|
|
|
|
|
86
|
|
|
//wait for shutdown |
|
87
|
|
|
usleep(100000); |
|
88
|
|
|
}); |
|
89
|
|
|
|
|
90
|
|
|
$eventDispatcher->insertVerifier('cli.verify.user.executing', function (CliExecuteEvent $event) { |
|
91
|
|
|
$args = $event->getArgs()->getArrayCopy(); |
|
92
|
|
|
$client = $this->socketFactory->createClient(...$args); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
//wait for server to boot |
|
95
|
|
|
usleep(100000); |
|
96
|
|
|
|
|
97
|
|
|
try { |
|
98
|
|
|
$client->connect(); |
|
99
|
|
|
} catch (Exception $e) { |
|
100
|
|
|
return Failure::fromNameAndReason($this->getName(), $e->getMessage()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$out = $client->readAll(); |
|
104
|
|
|
|
|
105
|
|
|
//wait for shutdown |
|
106
|
|
|
usleep(100000); |
|
107
|
|
|
|
|
108
|
|
|
$date = new \DateTime; |
|
109
|
|
|
|
|
110
|
|
|
//match the current date but any seconds |
|
111
|
|
|
//since we can't mock time in PHP easily |
|
112
|
|
|
if (!preg_match(sprintf('/^%s:([0-5][0-9]|60)\n$/', $date->format('Y-m-d H:i')), $out)) { |
|
113
|
|
|
return new StdOutFailure($this->getName(), $date->format("Y-m-d H:i:s\n"), $out); |
|
114
|
|
|
} |
|
115
|
|
|
return new Success($this->getName()); |
|
116
|
|
|
}); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
private function getRandomPort() |
|
123
|
|
|
{ |
|
124
|
|
|
return (string) mt_rand(1025, 65535); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return ExerciseType |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getType() |
|
131
|
|
|
{ |
|
132
|
|
|
return ExerciseType::CLI(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return string[] |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getArgs() |
|
139
|
|
|
{ |
|
140
|
|
|
return []; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
This check looks for function calls that miss required arguments.