Skip to content

Commit d49b10e

Browse files
committed
blah
1 parent 14073d4 commit d49b10e

38 files changed

+533
-501
lines changed

app/config.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use PhpSchool\PhpWorkshop\ExerciseRenderer;
4040
use PhpSchool\PhpWorkshop\ExerciseRepository;
4141
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContextFactory;
42+
use PhpSchool\PhpWorkshop\ExerciseRunner\EnvironmentManager;
4243
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CgiRunnerFactory;
4344
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CliRunnerFactory;
4445
use PhpSchool\PhpWorkshop\ExerciseRunner\Factory\CustomVerifyingRunnerFactory;
@@ -188,11 +189,23 @@
188189
EventDispatcher::class => factory(EventDispatcherFactory::class),
189190
EventDispatcherFactory::class => create(),
190191

192+
EnvironmentManager::class => function (ContainerInterface $c) {
193+
return new EnvironmentManager($c->get(Filesystem::class));
194+
},
195+
191196
//Exercise Runners
192197
RunnerManager::class => function (ContainerInterface $c) {
193198
$manager = new RunnerManager();
194-
$manager->addFactory(new CliRunnerFactory($c->get(EventDispatcher::class), $c->get(ProcessFactory::class)));
195-
$manager->addFactory(new CgiRunnerFactory($c->get(EventDispatcher::class), $c->get(ProcessFactory::class)));
199+
$manager->addFactory(new CliRunnerFactory(
200+
$c->get(EventDispatcher::class),
201+
$c->get(ProcessFactory::class),
202+
$c->get(EnvironmentManager::class)
203+
));
204+
$manager->addFactory(new CgiRunnerFactory(
205+
$c->get(EventDispatcher::class),
206+
$c->get(ProcessFactory::class),
207+
$c->get(EnvironmentManager::class)
208+
));
196209
$manager->addFactory(new CustomVerifyingRunnerFactory());
197210
return $manager;
198211
},

src/ExerciseDispatcher.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
1212
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
1313
use PhpSchool\PhpWorkshop\Exception\CheckNotApplicableException;
14+
use PhpSchool\PhpWorkshop\Exception\CouldNotRunException;
1415
use PhpSchool\PhpWorkshop\Exception\ExerciseNotConfiguredException;
1516
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
1617
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
17-
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContextFactory;
18+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
1819
use PhpSchool\PhpWorkshop\ExerciseRunner\RunnerManager;
1920
use PhpSchool\PhpWorkshop\Input\Input;
2021
use PhpSchool\PhpWorkshop\Output\OutputInterface;
2122
use PhpSchool\PhpWorkshop\Result\FailureInterface;
22-
use PhpSchool\PhpWorkshop\Exception\CouldNotRunException;
2323

2424
/**
2525
* This class is used to verify/run a student's solution to an exercise. It routes to the correct
@@ -49,7 +49,6 @@ public function __construct(
4949
private ResultAggregator $results,
5050
private EventDispatcher $eventDispatcher,
5151
private CheckRepository $checkRepository,
52-
private ExecutionContextFactory $executionContextFactory,
5352
) {
5453
}
5554

@@ -108,7 +107,7 @@ public function requireCheck(string $requiredCheck): void
108107
*/
109108
public function verify(ExerciseInterface $exercise, Input $input): ResultAggregator
110109
{
111-
$context = $this->executionContextFactory->fromInputAndExercise($input, $exercise);
110+
$context = ExecutionContext::fromInputAndExercise($input, $exercise);
112111
$runner = $this->runnerManager->getRunner($exercise);
113112

114113
$exercise->defineListeners($this->eventDispatcher);
@@ -161,7 +160,7 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega
161160
*/
162161
public function run(ExerciseInterface $exercise, Input $input, OutputInterface $output): bool
163162
{
164-
$context = $this->executionContextFactory->fromInputAndExercise($input, $exercise);
163+
$context = ExecutionContext::fromInputAndExercise($input, $exercise);
165164

166165
$exercise->defineListeners($this->eventDispatcher);
167166

src/ExerciseRunner/CgiRunner.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class CgiRunner implements ExerciseRunnerInterface
6565
public function __construct(
6666
private CgiExercise $exercise,
6767
private EventDispatcher $eventDispatcher,
68-
private ProcessFactory $processFactory
68+
private ProcessFactory $processFactory,
69+
private EnvironmentManager $environmentManager,
6970
) {
7071
}
7172

@@ -109,6 +110,10 @@ public function verify(ExecutionContext $context): ResultInterface
109110
$scenario = $this->exercise->defineTestScenario();
110111

111112
$this->eventDispatcher->dispatch(new CgiExerciseRunnerEvent('cgi.verify.start', $context, $scenario));
113+
114+
$this->environmentManager->prepareStudent($context, $scenario);
115+
$this->environmentManager->prepareSolution($context, $scenario);
116+
112117
$result = new CgiResult(
113118
array_map(
114119
function (RequestInterface $request) use ($context, $scenario) {
@@ -117,6 +122,9 @@ function (RequestInterface $request) use ($context, $scenario) {
117122
$scenario->getExecutions()
118123
)
119124
);
125+
126+
$this->environmentManager->cleanup($context, $scenario);
127+
120128
$this->eventDispatcher->dispatch(new CgiExerciseRunnerEvent('cgi.verify.finish', $context, $scenario));
121129
return $result;
122130
}
@@ -296,6 +304,9 @@ public function run(ExecutionContext $context, OutputInterface $output): bool
296304
$scenario = $this->exercise->defineTestScenario();
297305

298306
$this->eventDispatcher->dispatch(new CgiExerciseRunnerEvent('cgi.run.start', $context, $scenario));
307+
308+
$this->environmentManager->prepareStudent($context, $scenario);
309+
299310
$success = true;
300311
foreach ($scenario->getExecutions() as $i => $request) {
301312
/** @var CgiExecuteEvent $event */
@@ -333,6 +344,9 @@ public function run(ExecutionContext $context, OutputInterface $output): bool
333344
new CgiExecuteEvent('cgi.run.student-execute.post', $context, $scenario, $request)
334345
);
335346
}
347+
348+
$this->environmentManager->cleanup($context, $scenario);
349+
336350
$this->eventDispatcher->dispatch(new CgiExerciseRunnerEvent('cgi.run.finish', $context, $scenario));
337351
return $success;
338352
}

src/ExerciseRunner/CliRunner.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class CliRunner implements ExerciseRunnerInterface
6363
public function __construct(
6464
private CliExercise $exercise,
6565
private EventDispatcher $eventDispatcher,
66-
private ProcessFactory $processFactory
66+
private ProcessFactory $processFactory,
67+
private EnvironmentManager $environmentManager,
6768
) {
6869
}
6970

@@ -106,6 +107,10 @@ public function verify(ExecutionContext $context): ResultInterface
106107
$scenario = $this->exercise->defineTestScenario();
107108

108109
$this->eventDispatcher->dispatch(new CliExerciseRunnerEvent('cli.verify.start', $context, $scenario));
110+
111+
$this->environmentManager->prepareStudent($context, $scenario);
112+
$this->environmentManager->prepareSolution($context, $scenario);
113+
109114
$result = new CliResult(
110115
array_map(
111116
function (Collection $args) use ($context, $scenario) {
@@ -114,6 +119,9 @@ function (Collection $args) use ($context, $scenario) {
114119
$scenario->getExecutions()
115120
)
116121
);
122+
123+
$this->environmentManager->cleanup($context, $scenario);
124+
117125
$this->eventDispatcher->dispatch(new CliExerciseRunnerEvent('cli.verify.finish', $context, $scenario));
118126
return $result;
119127
}
@@ -123,7 +131,6 @@ function (Collection $args) use ($context, $scenario) {
123131
*/
124132
private function doVerify(ExecutionContext $context, CliScenario $scenario, Collection $args): CliResultInterface
125133
{
126-
127134
try {
128135
/** @var CliExecuteEvent $event */
129136
$event = $this->eventDispatcher->dispatch(
@@ -203,6 +210,9 @@ public function run(ExecutionContext $context, OutputInterface $output): bool
203210
$scenario = $this->exercise->defineTestScenario();
204211

205212
$this->eventDispatcher->dispatch(new CliExerciseRunnerEvent('cli.run.start', $context, $scenario));
213+
214+
$this->environmentManager->prepareStudent($context, $scenario);
215+
206216
$success = true;
207217
foreach ($scenario->getExecutions() as $i => $args) {
208218
/** @var CliExecuteEvent $event */
@@ -238,6 +248,8 @@ public function run(ExecutionContext $context, OutputInterface $output): bool
238248
);
239249
}
240250

251+
$this->environmentManager->cleanup($context, $scenario);
252+
241253
$this->eventDispatcher->dispatch(new CliExerciseRunnerEvent('cli.run.finish', $context, $scenario));
242254
return $success;
243255
}

src/ExerciseRunner/Context/ExecutionContext.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ public function __construct(
1717
) {
1818
}
1919

20+
public static function fromInputAndExercise(Input $input, ExerciseInterface $exercise): ExecutionContext
21+
{
22+
$program = $input->hasArgument('program') ? dirname($input->getRequiredArgument('program')) : (string) getcwd();
23+
24+
return new self(
25+
$program,
26+
System::randomTempDir(),
27+
$exercise,
28+
$input
29+
);
30+
}
31+
2032
public function getExercise(): ExerciseInterface
2133
{
2234
return $this->exercise;

src/ExerciseRunner/Context/ExecutionContextFactory.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/ExerciseRunner/Context/StaticExecutionContextFactory.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)