Skip to content

Commit f8510f3

Browse files
authored
Merge pull request #135 from php-school/input-object
Input object
2 parents c905eb1 + bc82383 commit f8510f3

33 files changed

+468
-173
lines changed

app/config.php

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
new CommandDefinition('credits', [], CreditsCommand::class)
9090
],
9191
'menu',
92+
$c->get(EventDispatcher::class),
9293
$c
9394
);
9495
},

src/Check/CodeParseCheck.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Parser;
99
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1010
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
11+
use PhpSchool\PhpWorkshop\Input\Input;
1112
use PhpSchool\PhpWorkshop\Result\Failure;
1213
use PhpSchool\PhpWorkshop\Result\ResultInterface;
1314
use PhpSchool\PhpWorkshop\Result\Success;
@@ -51,18 +52,18 @@ public function getName()
5152
* by the parser, it is treated as a failure.
5253
*
5354
* @param ExerciseInterface $exercise The exercise to check against.
54-
* @param string $fileName The absolute path to the student's solution.
55+
* @param Input $input The command line arguments passed to the command.
5556
* @return ResultInterface The result of the check.
5657
*/
57-
public function check(ExerciseInterface $exercise, $fileName)
58+
public function check(ExerciseInterface $exercise, Input $input)
5859
{
5960

60-
$code = file_get_contents($fileName);
61+
$code = file_get_contents($input->getArgument('program'));
6162

6263
try {
6364
$this->parser->parse($code);
6465
} catch (Error $e) {
65-
return Failure::fromCheckAndCodeParseFailure($this, $e, $fileName);
66+
return Failure::fromCheckAndCodeParseFailure($this, $e, $input->getArgument('program'));
6667
}
6768

6869
return Success::fromCheck($this);
@@ -76,7 +77,7 @@ public function check(ExerciseInterface $exercise, $fileName)
7677
*/
7778
public function canRun(ExerciseType $exerciseType)
7879
{
79-
return true;
80+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
8081
}
8182

8283
/**

src/Check/ComposerCheck.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
77
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
88
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
9+
use PhpSchool\PhpWorkshop\Input\Input;
910
use PhpSchool\PhpWorkshop\Result\Failure;
1011
use PhpSchool\PhpWorkshop\Result\ResultInterface;
1112
use PhpSchool\PhpWorkshop\Result\Success;
@@ -35,24 +36,24 @@ public function getName()
3536
* a success is returned.
3637
*
3738
* @param ExerciseInterface $exercise The exercise to check against.
38-
* @param string $fileName The absolute path to the student's solution.
39+
* @param Input $input The command line arguments passed to the command.
3940
* @return ResultInterface The result of the check.
4041
*/
41-
public function check(ExerciseInterface $exercise, $fileName)
42+
public function check(ExerciseInterface $exercise, Input $input)
4243
{
4344
if (!$exercise instanceof ComposerExerciseCheck) {
4445
throw new \InvalidArgumentException;
4546
}
4647

47-
if (!file_exists(sprintf('%s/composer.json', dirname($fileName)))) {
48+
if (!file_exists(sprintf('%s/composer.json', dirname($input->getArgument('program'))))) {
4849
return new Failure($this->getName(), 'No composer.json file found');
4950
}
5051

51-
if (!file_exists(sprintf('%s/composer.lock', dirname($fileName)))) {
52+
if (!file_exists(sprintf('%s/composer.lock', dirname($input->getArgument('program'))))) {
5253
return new Failure($this->getName(), 'No composer.lock file found');
5354
}
5455

55-
$lockFile = new LockFileParser(sprintf('%s/composer.lock', dirname($fileName)));
56+
$lockFile = new LockFileParser(sprintf('%s/composer.lock', dirname($input->getArgument('program'))));
5657
$missingPackages = array_filter($exercise->getRequiredPackages(), function ($package) use ($lockFile) {
5758
return !$lockFile->hasInstalledPackage($package);
5859
});
@@ -78,7 +79,7 @@ public function check(ExerciseInterface $exercise, $fileName)
7879
*/
7980
public function canRun(ExerciseType $exerciseType)
8081
{
81-
return true;
82+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
8283
}
8384

8485
/**

src/Check/FileExistsCheck.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
66
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
7+
use PhpSchool\PhpWorkshop\Input\Input;
78
use PhpSchool\PhpWorkshop\Result\Failure;
89
use PhpSchool\PhpWorkshop\Result\ResultInterface;
910
use PhpSchool\PhpWorkshop\Result\Success;
@@ -30,16 +31,16 @@ public function getName()
3031
* Simply check that the file exists.
3132
*
3233
* @param ExerciseInterface $exercise The exercise to check against.
33-
* @param string $fileName The absolute path to the student's solution.
34+
* @param Input $input The command line arguments passed to the command.
3435
* @return ResultInterface The result of the check.
3536
*/
36-
public function check(ExerciseInterface $exercise, $fileName)
37+
public function check(ExerciseInterface $exercise, Input $input)
3738
{
38-
if (file_exists($fileName)) {
39+
if (file_exists($input->getArgument('program'))) {
3940
return Success::fromCheck($this);
4041
}
4142

42-
return Failure::fromCheckAndReason($this, sprintf('File: "%s" does not exist', $fileName));
43+
return Failure::fromCheckAndReason($this, sprintf('File: "%s" does not exist', $input->getArgument('program')));
4344
}
4445

4546
/**
@@ -50,7 +51,7 @@ public function check(ExerciseInterface $exercise, $fileName)
5051
*/
5152
public function canRun(ExerciseType $exerciseType)
5253
{
53-
return true;
54+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
5455
}
5556

5657
/**

src/Check/FunctionRequirementsCheck.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1010
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
1111
use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck;
12+
use PhpSchool\PhpWorkshop\Input\Input;
1213
use PhpSchool\PhpWorkshop\NodeVisitor\FunctionVisitor;
1314
use PhpSchool\PhpWorkshop\Result\Failure;
1415
use PhpSchool\PhpWorkshop\Result\FunctionRequirementsFailure;
@@ -54,10 +55,10 @@ public function getName()
5455
* are pulled from the exercise.
5556
*
5657
* @param ExerciseInterface $exercise The exercise to check against.
57-
* @param string $fileName The absolute path to the student's solution.
58+
* @param Input $input The command line arguments passed to the command.
5859
* @return ResultInterface The result of the check.
5960
*/
60-
public function check(ExerciseInterface $exercise, $fileName)
61+
public function check(ExerciseInterface $exercise, Input $input)
6162
{
6263
if (!$exercise instanceof FunctionRequirementsExerciseCheck) {
6364
throw new \InvalidArgumentException;
@@ -66,12 +67,12 @@ public function check(ExerciseInterface $exercise, $fileName)
6667
$requiredFunctions = $exercise->getRequiredFunctions();
6768
$bannedFunctions = $exercise->getBannedFunctions();
6869

69-
$code = file_get_contents($fileName);
70+
$code = file_get_contents($input->getArgument('program'));
7071

7172
try {
7273
$ast = $this->parser->parse($code);
7374
} catch (Error $e) {
74-
return Failure::fromCheckAndCodeParseFailure($this, $e, $fileName);
75+
return Failure::fromCheckAndCodeParseFailure($this, $e, $input->getArgument('program'));
7576
}
7677

7778
$visitor = new FunctionVisitor($requiredFunctions, $bannedFunctions);
@@ -107,7 +108,7 @@ public function check(ExerciseInterface $exercise, $fileName)
107108
*/
108109
public function canRun(ExerciseType $exerciseType)
109110
{
110-
return true;
111+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
111112
}
112113

113114
/**

src/Check/PhpLintCheck.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
66
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
7+
use PhpSchool\PhpWorkshop\Input\Input;
8+
use PhpSchool\PhpWorkshop\Result\ResultInterface;
79
use PhpSchool\PhpWorkshop\Result\Success;
810
use PhpSchool\PhpWorkshop\Result\Failure;
911
use Symfony\Component\Process\Process;
@@ -32,12 +34,12 @@ public function getName()
3234
* Simply check the student's solution can be linted with `php -l`.
3335
*
3436
* @param ExerciseInterface $exercise The exercise to check against.
35-
* @param string $fileName The absolute path to the student's solution.
37+
* @param Input $input The command line arguments passed to the command.
3638
* @return ResultInterface The result of the check.
3739
*/
38-
public function check(ExerciseInterface $exercise, $fileName)
40+
public function check(ExerciseInterface $exercise, Input $input)
3941
{
40-
$process = new Process(sprintf('%s -l %s', PHP_BINARY, $fileName));
42+
$process = new Process(sprintf('%s -l %s', PHP_BINARY, $input->getArgument('program')));
4143
$process->run();
4244

4345
if ($process->isSuccessful()) {
@@ -55,7 +57,7 @@ public function check(ExerciseInterface $exercise, $fileName)
5557
*/
5658
public function canRun(ExerciseType $exerciseType)
5759
{
58-
return true;
60+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
5961
}
6062

6163
/**

src/Check/SimpleCheckInterface.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
66
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
7+
use PhpSchool\PhpWorkshop\Input\Input;
78
use PhpSchool\PhpWorkshop\Result\ResultInterface;
89

910
/**
@@ -47,10 +48,10 @@ public function canRun(ExerciseType $exerciseType);
4748
* should be returned.
4849
*
4950
* @param ExerciseInterface $exercise The exercise to check against.
50-
* @param string $fileName The absolute path to the student's solution.
51+
* @param Input $input The command line arguments passed to the command.
5152
* @return ResultInterface The result of the check.
5253
*/
53-
public function check(ExerciseInterface $exercise, $fileName);
54+
public function check(ExerciseInterface $exercise, Input $input);
5455

5556
/**
5657
* Either `static::CHECK_BEFORE` | `static::CHECK_AFTER`.

src/Command/RunCommand.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
66
use PhpSchool\PhpWorkshop\ExerciseRepository;
7+
use PhpSchool\PhpWorkshop\Input\Input;
78
use PhpSchool\PhpWorkshop\Output\OutputInterface;
89
use PhpSchool\PhpWorkshop\UserState;
910
use PhpSchool\PhpWorkshop\UserStateSerializer;
@@ -62,13 +63,13 @@ public function __construct(
6263
}
6364

6465
/**
65-
* @param string $appName
66-
* @param string $program
66+
* @param Input $input The command line arguments passed to the command.
6767
*
6868
* @return int|void
6969
*/
70-
public function __invoke($appName, $program)
70+
public function __invoke(Input $input)
7171
{
72+
$program = $input->getArgument('program');
7273
if (!file_exists($program)) {
7374
$this->output->printError(
7475
sprintf('Could not run. File: "%s" does not exist', $program)

src/Command/VerifyCommand.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
66
use PhpSchool\PhpWorkshop\ExerciseRepository;
77
use PhpSchool\PhpWorkshop\ExerciseRunner;
8+
use PhpSchool\PhpWorkshop\Input\Input;
89
use PhpSchool\PhpWorkshop\Output\OutputInterface;
910
use PhpSchool\PhpWorkshop\ResultRenderer\ResultsRenderer;
1011
use PhpSchool\PhpWorkshop\UserState;
@@ -72,13 +73,13 @@ public function __construct(
7273
}
7374

7475
/**
75-
* @param string $appName
76-
* @param string $program
76+
* @param Input $input The command line arguments passed to the command.
7777
*
7878
* @return int|void
7979
*/
80-
public function __invoke($appName, $program)
80+
public function __invoke(Input $input)
8181
{
82+
$program = $input->getArgument('program');
8283
if (!file_exists($program)) {
8384
$this->output->printError(
8485
sprintf('Could not verify. File: "%s" does not exist', $program)
@@ -93,7 +94,7 @@ public function __invoke($appName, $program)
9394
}
9495

9596
$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
96-
$results = $this->exerciseDispatcher->verify($exercise, $program);
97+
$results = $this->exerciseDispatcher->verify($exercise, $input);
9798

9899
if ($results->isSuccessful()) {
99100
$this->userState->addCompletedExercise($exercise->getName());

0 commit comments

Comments
 (0)