Skip to content
Luca Ubiali Web Developer

How to Contribute to Open Source - 6/13 - Working on Reverting

August 15th, 2024
Continuous Learning

Episode - https://laracasts.com/series/how-to-contribute-to-open-source/episodes/6


Luke spent quite some time working on this feature behind the scenes. This confirmed my initial thought at the beginning of the series that this is the most complex part of the project.

First off, as we are running through the steps, we need a way to block the prompt execution. But we cannot do that inside the StepBuilder class as it’s too high in the hierarchy of classes. Have to do the change directly in the Prompt class.

In there, it’s possible to add some logic when CTRL+U is pressed to handle step reverting. (This is a new key shortcut never used before.) With CTRL+U we can set the status of the prompt as error and show a feedback message.

Now the tricky part. From Prompt let StepBuilder know that a step must be reverted.

The whole feature started to click for me when Prompt and StepBuilder had this work-in-progress code:

1// Prompt
2if($key === Key::CTRL_U) {
3 $this->state = 'error';
4 $this->error = 'Reverted';
5 
6 if(self::$revertedUsing) {
7 call_user_func(self::$revertedUsing);
8 }
9 
10 return false;
11}
1// StepBuilder
2public function run() {
3 $index = 0;
4 
5 while($index < count($this->steps)) {
6 [$step, $revert, $key] = $this->steps[$index];
7 
8 Promp::$revertUsing = function() use (&$index) {
9 $index--;
10 }
11 
12 $this->responses[$key ?? $index] = $step($this->responses);
13 }
14}

Before running the step, set the revert function at Prompt level, so when CTRL+U is used it can run the logic defined in StepBuilder.

The final code is more complex than that, to take care of steps that cannot be reverted and such. But the juicy part is the one above.