Skip to content

Update events to use context #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@
InitialCodeListener::class => function (ContainerInterface $c) {
return new InitialCodeListener($c->get('currentWorkingDirectory'), $c->get(LoggerInterface::class));
},
PrepareSolutionListener::class => create(),
PrepareSolutionListener::class => function (ContainerInterface $c) {
return new PrepareSolutionListener(
$c->get(ProcessFactory::class)
);
},
CodePatchListener::class => function (ContainerInterface $c) {
return new CodePatchListener(
$c->get(CodePatcher::class),
Expand Down Expand Up @@ -472,21 +476,24 @@ function (CgiResult $result) use ($c) {
],
],
'prepare-solution' => [
'cli.verify.start' => [
'cli.verify.reference-execute.pre' => [
containerListener(PrepareSolutionListener::class),
],
'cli.run.start' => [
containerListener(PrepareSolutionListener::class),
],
'cgi.verify.start' => [
'cgi.verify.reference-execute.pre' => [
containerListener(PrepareSolutionListener::class),
],
'cgi.run.start' => [
containerListener(PrepareSolutionListener::class),
],
],
'code-patcher' => [
'verify.pre.execute' => [
'cli.verify.start' => [
containerListener(CodePatchListener::class, 'patch'),
],
'cgi.verify.start' => [
containerListener(CodePatchListener::class, 'patch'),
],
'verify.post.execute' => [
Expand Down
10 changes: 6 additions & 4 deletions src/Event/CgiExecuteEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace PhpSchool\PhpWorkshop\Event;

use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\Scenario\CgiScenario;
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
use PhpSchool\PhpWorkshop\Input\Input;
use Psr\Http\Message\RequestInterface;

Expand All @@ -19,17 +21,17 @@ class CgiExecuteEvent extends CgiExerciseRunnerEvent
/**
* @param string $name The event name.
* @param RequestInterface $request The request that will be performed.
* @param array<mixed> $parameters The event parameters.
* @param array<string, mixed> $parameters The event parameters.
*/
public function __construct(
string $name,
ExerciseInterface $exercise,
Input $input,
ExecutionContext $context,
CgiScenario $scenario,
RequestInterface $request,
array $parameters = []
) {
$parameters['request'] = $request;
parent::__construct($name, $exercise, $input, $parameters);
parent::__construct($name, $context, $scenario, $parameters);
$this->request = $request;
}

Expand Down
20 changes: 20 additions & 0 deletions src/Event/CgiExerciseRunnerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@
namespace PhpSchool\PhpWorkshop\Event;

use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\Scenario\CgiScenario;
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
use PhpSchool\PhpWorkshop\Input\Input;

class CgiExerciseRunnerEvent extends ExerciseRunnerEvent
{
private CgiScenario $scenario;

/**
* @param array<string, mixed> $parameters
*/
public function __construct(
string $name,
ExecutionContext $context,
CgiScenario $scenario,
array $parameters = []
) {
$this->scenario = $scenario;
parent::__construct($name, $context, $parameters);
}

public function getScenario(): CgiScenario
{
return $this->scenario;
}
}
24 changes: 13 additions & 11 deletions src/Event/CliExecuteEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace PhpSchool\PhpWorkshop\Event;

use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\Scenario\CliScenario;
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Utils\ArrayObject;
use PhpSchool\PhpWorkshop\Utils\Collection;

/**
* An event to represent events which occur throughout the verification and running process in
Expand All @@ -15,24 +17,24 @@
class CliExecuteEvent extends CliExerciseRunnerEvent
{
/**
* @var ArrayObject<int, string>
* @var Collection<int, string>
*/
private ArrayObject $args;
private Collection $args;

/**
* @param string $name The event name.
* @param ArrayObject<int, string> $args The arguments that should be/have been passed to the program.
* @param array<mixed> $parameters The event parameters.
* @param Collection<int, string> $args The arguments that should be/have been passed to the program.
* @param array<string, mixed> $parameters The event parameters.
*/
public function __construct(
string $name,
ExerciseInterface $exercise,
Input $input,
ArrayObject $args,
ExecutionContext $context,
CliScenario $scenario,
Collection $args,
array $parameters = []
) {
$parameters['args'] = $args;
parent::__construct($name, $exercise, $input, $parameters);
parent::__construct($name, $context, $scenario, $parameters);
$this->args = $args;
}

Expand All @@ -59,9 +61,9 @@ public function appendArg(string $arg): void
/**
* Get the arguments to be passed to the program.
*
* @return ArrayObject<int, string>
* @return Collection<int, string>
*/
public function getArgs(): ArrayObject
public function getArgs(): Collection
{
return $this->args;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Event/CliExerciseRunnerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@
namespace PhpSchool\PhpWorkshop\Event;

use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\Scenario\CliScenario;
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
use PhpSchool\PhpWorkshop\Input\Input;

class CliExerciseRunnerEvent extends ExerciseRunnerEvent
{
private CliScenario $scenario;

/**
* @param array<string, mixed> $parameters
*/
public function __construct(
string $name,
ExecutionContext $context,
CliScenario $scenario,
array $parameters = []
) {
$this->scenario = $scenario;
parent::__construct($name, $context, $parameters);
}

public function getScenario(): CliScenario
{
return $this->scenario;
}
}
4 changes: 0 additions & 4 deletions src/Event/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ class EventDispatcher
* @var array<string, array<callable>>
*/
private array $listeners = [];

/**
* @var ResultAggregator
*/
private ResultAggregator $resultAggregator;

public function __construct(ResultAggregator $resultAggregator)
Expand Down
33 changes: 14 additions & 19 deletions src/Event/ExerciseRunnerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,47 @@
namespace PhpSchool\PhpWorkshop\Event;

use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
use PhpSchool\PhpWorkshop\Input\Input;

/**
* An event which is dispatched during exercise running
*/
class ExerciseRunnerEvent extends Event
{
/**
* @var ExerciseInterface
*/
private $exercise;

/**
* @var Input
*/
private $input;
private ExecutionContext $context;

/**
* @param string $name
* @param ExerciseInterface $exercise
* @param Input $input
* @param array<mixed> $parameters
* @param array<string, mixed> $parameters
*/
public function __construct(string $name, ExerciseInterface $exercise, Input $input, array $parameters = [])
public function __construct(string $name, ExecutionContext $context, array $parameters = [])
{
$parameters['input'] = $input;
$parameters['exercise'] = $exercise;
$this->context = $context;

$parameters['input'] = $context->getInput();
$parameters['exercise'] = $context->getExercise();
parent::__construct($name, $parameters);
}

$this->exercise = $exercise;
$this->input = $input;
public function getContext(): ExecutionContext
{
return $this->context;
}

/**
* @return Input
*/
public function getInput(): Input
{
return $this->input;
return $this->context->getInput();
}

/**
* @return ExerciseInterface
*/
public function getExercise(): ExerciseInterface
{
return $this->exercise;
return $this->context->getExercise();
}
}
5 changes: 3 additions & 2 deletions src/ExerciseCheck/SelfCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpSchool\PhpWorkshop\ExerciseCheck;

use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Result\ResultInterface;

Expand All @@ -21,8 +22,8 @@ interface SelfCheck
* The method is passed the absolute file path to the student's solution and should return a result
* object which indicates the success or not of the check.
*
* @param Input $input The command line arguments passed to the command.
* @param ExecutionContext $context The current execution context.
* @return ResultInterface The result of the check.
*/
public function check(Input $input): ResultInterface;
public function check(ExecutionContext $context): ResultInterface;
}
14 changes: 7 additions & 7 deletions src/ExerciseDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega
$this->requireCheck($requiredCheck);
}

$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.start', $exercise, $input));
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.start', $context));

$this->validateChecks($this->checksToRunBefore, $exercise);
$this->validateChecks($this->checksToRunAfter, $exercise);
Expand All @@ -130,22 +130,22 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega
}
}

$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.pre.execute', $exercise, $input));
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.pre.execute', $context));

try {
$this->results->add($runner->verify($context));
} finally {
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.execute', $exercise, $input));
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.execute', $context));
}

foreach ($this->checksToRunAfter as $check) {
$this->results->add($check->check($context));
}

$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.check', $exercise, $input));
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.check', $context));
$exercise->tearDown();

$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.finish', $exercise, $input));
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.finish', $context));
return $this->results;
}

Expand Down Expand Up @@ -173,14 +173,14 @@ public function run(ExerciseInterface $exercise, Input $input, OutputInterface $
throw CouldNotRunException::fromFailure($result);
}

$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.start', $exercise, $input));
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.start', $context));

try {
$exitStatus = $this->runnerManager
->getRunner($exercise)
->run($context, $output);
} finally {
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.finish', $exercise, $input));
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.finish', $context));
}

return $exitStatus;
Expand Down
Loading
Loading