Skip to content

Add a call to onSelected() when an exercise is selected from the menu #130

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions src/Exercise/AbstractExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ abstract class AbstractExercise
*/
abstract public function getName();

/**
* Allow to perform some action when an exercise is selected from the menu.
*/
public function onSelected()
{
}

/**
* This returns a single file solution named `solution.php` which
* should exist in `workshop-root/exercises/<exercise-name>/solution/`.
Expand Down
7 changes: 7 additions & 0 deletions src/Exercise/ExerciseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ public function getProblem();
* @return void
*/
public function tearDown();

/**
* Allow to perform some action when an exercise is selected from the menu.
*
* @return void
*/
public function onSelected();
}
7 changes: 6 additions & 1 deletion src/Factory/MenuFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PhpSchool\PhpWorkshop\Command\CreditsCommand;
use PhpSchool\PhpWorkshop\Command\HelpCommand;
use PhpSchool\PhpWorkshop\Command\MenuCommandInvoker;
use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\ExerciseRenderer;
use PhpSchool\PhpWorkshop\ExerciseRepository;
Expand Down Expand Up @@ -47,7 +49,10 @@ public function __invoke(ContainerInterface $c)
->addItems(array_map(function (ExerciseInterface $exercise) use ($exerciseRenderer, $userState) {
return [
$exercise->getName(),
$exerciseRenderer,
function (CliMenu $menu) use ($exerciseRenderer, $exercise) {
$exercise->onSelected();
$exerciseRenderer($menu);
},
$userState->completedExercise($exercise->getName())
];
}, $exerciseRepository->findAll()))
Expand Down
10 changes: 10 additions & 0 deletions test/Asset/ComposerExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,14 @@ public function configure(ExerciseDispatcher $dispatcher)
{
$dispatcher->requireCheck(ComposerCheck::class);
}

/**
* Allow to perform some action when an exercise is selected from the menu.
*
* @return void
*/
public function onSelected()
{
// TODO: Implement onSelected() method.
}
}
10 changes: 10 additions & 0 deletions test/Asset/FunctionRequirementsExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,14 @@ public function getBannedFunctions()
{
return ['file'];
}

/**
* Allow to perform some action when an exercise is selected from the menu.
*
* @return void
*/
public function onSelected()
{
// TODO: Implement onSelected() method.
}
}
10 changes: 10 additions & 0 deletions test/Asset/PatchableExercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,14 @@ public function configure(ExerciseDispatcher $dispatcher)
{
// TODO: Implement configure() method.
}

/**
* Allow to perform some action when an exercise is selected from the menu.
*
* @return void
*/
public function onSelected()
{
// TODO: Implement onSelected() method.
}
}