Skip to content

Realpath listener & Event Refactor #140

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 6 commits into from
Nov 28, 2016
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
52 changes: 43 additions & 9 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use PhpSchool\PhpWorkshop\Listener\CodePatchListener;
use PhpSchool\PhpWorkshop\Listener\ConfigureCommandListener;
use PhpSchool\PhpWorkshop\Listener\PrepareSolutionListener;
use PhpSchool\PhpWorkshop\Listener\RealPathListener;
use PhpSchool\PhpWorkshop\Listener\SelfCheckListener;
use PhpSchool\PhpWorkshop\MenuItem\ResetProgress;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
Expand Down Expand Up @@ -197,7 +198,8 @@
$c->get(RunnerManager::class)
);
},

RealPathListener::class => object(),

//checks
FileExistsCheck::class => object(),
PhpLintCheck::class => object(),
Expand Down Expand Up @@ -281,6 +283,20 @@
],
'appContributors' => [],
'eventListeners' => [
'realpath-student-submission' => [
'cli.verify.start' => [
containerListener(RealPathListener::class)
],
'cli.run.start' => [
containerListener(RealPathListener::class)
],
'cgi.verify.start' => [
containerListener(RealPathListener::class)
],
'cgi.run.start' => [
containerListener(RealPathListener::class)
]
],
'check-exercise-assigned' => [
'route.pre.resolve.args' => [
containerListener(CheckExerciseAssignedListener::class)
Expand All @@ -292,25 +308,43 @@
],
],
'prepare-solution' => [
'verify.start' => [
'cli.verify.start' => [
containerListener(PrepareSolutionListener::class),
],
'cli.run.start' => [
containerListener(PrepareSolutionListener::class),
],
'cgi.verify.start' => [
containerListener(PrepareSolutionListener::class),
],
'run.start' => [
'cgi.run.start' => [
containerListener(PrepareSolutionListener::class),
],
],
'code-patcher' => [
'run.start' => [
'cli.verify.start' => [
containerListener(CodePatchListener::class, 'patch'),
],
'verify.pre.execute' => [
'cli.verify.finish' => [
containerListener(CodePatchListener::class, 'patch'),
],
'verify.post.execute' => [
containerListener(CodePatchListener::class, 'revert'),
'cli.run.start' => [
containerListener(CodePatchListener::class, 'patch'),
],
'cli.run.finish' => [
containerListener(CodePatchListener::class, 'patch'),
],
'cgi.verify.start' => [
containerListener(CodePatchListener::class, 'patch'),
],
'run.finish' => [
containerListener(CodePatchListener::class, 'revert'),
'cgi.verify.finish' => [
containerListener(CodePatchListener::class, 'patch'),
],
'cgi.run.start' => [
containerListener(CodePatchListener::class, 'patch'),
],
'cgi.run.finish' => [
containerListener(CodePatchListener::class, 'patch'),
],
],
'self-check' => [
Expand Down
10 changes: 1 addition & 9 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,7 @@ public function __construct(
*/
public function __invoke(Input $input)
{
$program = $input->getArgument('program');
if (!file_exists($program)) {
$this->output->printError(
sprintf('Could not run. File: "%s" does not exist', $program)
);
return 1;
}
$program = realpath($program);
$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
$this->exerciseDispatcher->run($exercise, $program, $this->output);
$this->exerciseDispatcher->run($exercise, $input, $this->output);
}
}
9 changes: 0 additions & 9 deletions src/Command/VerifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ public function __construct(
*/
public function __invoke(Input $input)
{
$program = $input->getArgument('program');
if (!file_exists($program)) {
$this->output->printError(
sprintf('Could not verify. File: "%s" does not exist', $program)
);
return 1;
}
$program = realpath($program);

$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
$results = $this->exerciseDispatcher->verify($exercise, $input);

Expand Down
55 changes: 55 additions & 0 deletions src/Event/ExerciseRunnerEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace PhpSchool\PhpWorkshop\Event;

use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Input\Input;

/**
* @author Aydin Hassan <[email protected]>
*/
class ExerciseRunnerEvent extends Event
{

/**
* @var ExerciseInterface
*/
private $exercise;

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

/**
* @param string $name
* @param ExerciseInterface $exercise
* @param Input $input
* @param array $parameters
*/
public function __construct($name, ExerciseInterface $exercise, Input $input, array $parameters = [])
{
$parameters['input'] = $input;
$parameters['exercise'] = $exercise;
parent::__construct($name, $parameters);

$this->exercise = $exercise;
$this->input = $input;
}

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

/**
* @return ExerciseInterface
*/
public function getExercise()
{
return $this->exercise;
}
}
10 changes: 7 additions & 3 deletions src/ExerciseRunner/CgiRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
use PhpSchool\PhpWorkshop\Event\CgiExecuteEvent;
use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
use PhpSchool\PhpWorkshop\Exception\CodeExecutionException;
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
use PhpSchool\PhpWorkshop\Exercise\CgiExercise;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\Result\CgiOutFailure;
use PhpSchool\PhpWorkshop\Result\CgiOutRequestFailure;
use PhpSchool\PhpWorkshop\Result\CgiOutResult;
use PhpSchool\PhpWorkshop\Result\Failure;
Expand Down Expand Up @@ -237,7 +236,8 @@ private function getProcess($fileName, RequestInterface $request)
*/
public function verify(Input $input)
{
return new CgiOutResult(
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.verify.start', $this->exercise, $input));
$result = new CgiOutResult(
$this->getName(),
array_map(
function (RequestInterface $request) use ($input) {
Expand All @@ -246,6 +246,8 @@ function (RequestInterface $request) use ($input) {
$this->exercise->getRequests()
)
);
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.verify.finish', $this->exercise, $input));
return $result;
}

/**
Expand All @@ -267,6 +269,7 @@ function (RequestInterface $request) use ($input) {
*/
public function run(Input $input, OutputInterface $output)
{
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.run.start', $this->exercise, $input));
$success = true;
foreach ($this->exercise->getRequests() as $i => $request) {
$event = $this->eventDispatcher->dispatch(
Expand Down Expand Up @@ -295,6 +298,7 @@ public function run(Input $input, OutputInterface $output)

$output->lineBreak();
}
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.run.finish', $this->exercise, $input));
return $success;
}
}
16 changes: 15 additions & 1 deletion src/ExerciseRunner/CliRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent;
use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
use PhpSchool\PhpWorkshop\Exception\CodeExecutionException;
use PhpSchool\PhpWorkshop\Exception\SolutionExecutionException;
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\Result\Failure;
Expand Down Expand Up @@ -127,6 +127,18 @@ private function getPhpProcess($fileName, ArrayObject $args)
* @return ResultInterface The result of the check.
*/
public function verify(Input $input)
{
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.verify.start', $this->exercise, $input));
$result = $this->doVerify($input);
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.verify.finish', $this->exercise, $input));
return $result;
}

/**
* @param Input $input
* @return ResultInterface
*/
private function doVerify(Input $input)
{
//arrays are not pass-by-ref
$args = new ArrayObject($this->exercise->getArgs());
Expand Down Expand Up @@ -175,6 +187,7 @@ public function verify(Input $input)
*/
public function run(Input $input, OutputInterface $output)
{
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.run.start', $this->exercise, $input));
/** @var CliExecuteEvent $event */
$event = $this->eventDispatcher->dispatch(
new CliExecuteEvent('cli.run.student-execute.pre', new ArrayObject($this->exercise->getArgs()))
Expand All @@ -200,6 +213,7 @@ public function run(Input $input, OutputInterface $output)
$output->writeLine($outputBuffer);
});

$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.run.finish', $this->exercise, $input));
return $process->isSuccessful();
}
}
21 changes: 8 additions & 13 deletions src/Listener/CodePatchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpSchool\PhpWorkshop\CodePatcher;
use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
use PhpSchool\PhpWorkshop\Input\Input;
use RuntimeException;

Expand Down Expand Up @@ -34,34 +35,28 @@ public function __construct(CodePatcher $codePatcher)
}

/**
* @param Event $event
* @param ExerciseRunnerEvent $event
*/
public function patch(Event $event)
public function patch(ExerciseRunnerEvent $event)
{
/** @var Input $input */
$input = $event->getParameter('input');
$fileName = $input->getArgument('program');
$fileName = $event->getInput()->getArgument('program');

$this->originalCode = file_get_contents($fileName);
file_put_contents(
$fileName,
$this->codePatcher->patch($event->getParameter('exercise'), $this->originalCode)
$this->codePatcher->patch($event->getExercise(), $this->originalCode)
);
}

/**
* @param Event $event
* @param ExerciseRunnerEvent $event
*/
public function revert(Event $event)
public function revert(ExerciseRunnerEvent $event)
{
if (null === $this->originalCode) {
throw new RuntimeException('Can only revert previously patched code');
}

/** @var Input $input */
$input = $event->getParameter('input');
$fileName = $input->getArgument('program');

file_put_contents($fileName, $this->originalCode);
file_put_contents($event->getInput()->getArgument('program'), $this->originalCode);
}
}
7 changes: 4 additions & 3 deletions src/Listener/PrepareSolutionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpSchool\PhpWorkshop\Listener;

use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
use RuntimeException;
use Symfony\Component\Process\Process;

Expand All @@ -25,11 +26,11 @@ class PrepareSolutionListener
];

/**
* @param Event $event
* @param ExerciseRunnerEvent $event
*/
public function __invoke(Event $event)
public function __invoke(ExerciseRunnerEvent $event)
{
$solution = $event->getParameter('exercise')->getSolution();
$solution = $event->getExercise()->getSolution();

if ($solution->hasComposerFile()) {
//prepare composer deps
Expand Down
28 changes: 28 additions & 0 deletions src/Listener/RealPathListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace PhpSchool\PhpWorkshop\Listener;

use PhpSchool\PhpWorkshop\CommandDefinition;
use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\UserState;

/**
* @author Aydin Hassan <[email protected]>
*/
class RealPathListener
{

/**
* @param ExerciseRunnerEvent $event
*/
public function __invoke(ExerciseRunnerEvent $event)
{
$program = $event->getInput()->getArgument('program');

if (file_exists($program)) {
$event->getInput()->setArgument('program', realpath($program));
}
}
}
Loading