Skip to content

Code patch updates #201

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 9 commits into from
Closed

Code patch updates #201

wants to merge 9 commits into from

Conversation

mikeymike
Copy link
Member

  • Patches internal solution

    • Internal solution is patched in temp
    • Solution is ran from temp
    • Eliminates chance of internal solution becoming "broken"
  • Users solution is patched in place

    • Error handler catches app errors and patches are reverted

- Patches internal solution
  - Internal solution is patched in temp
  - Solution is ran from temp
  - Eliminates chance of internal solution becoming "broken"

- Users solution is patched in place
  - Error handler catches app errors and patches are reverted
'%s/../../exercises/%s/solution/solution.php',
dirname((string) (new ReflectionClass(static::class))->getFileName()),
self::normaliseName($this->getName())
if (null === $this->solution) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this was required and probs also for dir solution... the problem being that a patched solution in temp was being overwritten due to the solution being copied on construct and we're constructing fresh on each call to getSolution.

Probably a better fix here, maybe moving the copy to temp dir to somewhere that isn't on solution construct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AydinHassan I need your input on this one bro, can you think of a cleaner approach that for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm not really off the top of my head. I think it's fine as you have it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the only problem I foresee here is the implementation of a solution inside the exercise instead of using the Abstract. e.g if you need a dir solution and you use the static factory 🤔

@mikeymike mikeymike force-pushed the patch-workshop-solution branch from ad0bcb6 to e04dd7f Compare February 5, 2021 20:53
@mikeymike
Copy link
Member Author

note to self, a current failure that needs a failing test case writing up

this happens when an exercise is not selected and a run or verify is attempted

php bin/php8appreciate run exercises/a-match-made-in-heaven/solution/solution.php                                                                                                                                                                         ()
PHP Fatal error:  Uncaught RuntimeException: Can only revert previously patched code in /Users/michael/Projects/PHPSchool/php8-appreciate/vendor/php-school/php-workshop/src/Listener/CodePatchListener.php:66
Stack trace:
#0 /Users/michael/Projects/PHPSchool/php8-appreciate/vendor/php-school/php-workshop/src/Factory/EventDispatcherFactory.php(111): PhpSchool\PhpWorkshop\Listener\CodePatchListener->revert(Object(PhpSchool\PhpWorkshop\Event\Event))
#1 /Users/michael/Projects/PHPSchool/php8-appreciate/vendor/php-school/php-workshop/src/Event/EventDispatcher.php(43): PhpSchool\PhpWorkshop\Factory\EventDispatcherFactory->PhpSchool\PhpWorkshop\Factory\{closure}(Object(PhpSchool\PhpWorkshop\Event\Event))
#2 /Users/michael/Projects/PHPSchool/php8-appreciate/vendor/php-school/php-workshop/src/Application.php(242): PhpSchool\PhpWorkshop\Event\EventDispatcher->dispatch(Object(PhpSchool\PhpWorkshop\Event\Event))
#3 /Users/michael/Projects/PHPSchool/php8-appreciate/bin/php8appreciate(4): PhpSchool\PhpWorkshop\Application->run()
#4 {main}
  thrown in /Users/michael/Projects/PHPSchool/php8-appreciate/vendor/php-school/php-workshop/src/Listener/CodePatchListener.php on line 66

Fatal error: Uncaught RuntimeException: Can only revert previously patched code in /Users/michael/Projects/PHPSchool/php8-appreciate/vendor/php-school/php-workshop/src/Listener/CodePatchListener.php on line 66

@mikeymike mikeymike force-pushed the patch-workshop-solution branch from 41e11de to 3342864 Compare February 23, 2021 21:24
@mikeymike mikeymike marked this pull request as ready for review March 10, 2021 21:24
@@ -227,6 +237,10 @@ public function run(): int
$message = str_replace($basePath, '', $message);
}

$container
->get(EventDispatcher::class)
->dispatch(new Event('application.tear-down'));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I was thinking, if an exception ever happened in a listener then it would bubble up and mask the original exception.

It could be the initial exception is something we WANT to show the user, and thus an exception covering this up during some failed cleanup process is not something we want the user to see really.

@AydinHassan thoughts? We could move this dispatch into a separate method that tries and catches and either ignores or logs and lets the application carry on the closing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeymike I think logging sounds like a good idea

@@ -227,6 +237,10 @@ public function run(): int
$message = str_replace($basePath, '', $message);
}

$container
->get(EventDispatcher::class)
->dispatch(new Event('application.tear-down'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeymike I think logging sounds like a good idea

Comment on lines +36 to +43
$intersection = array_intersect($currentPath, $solutionPath);

if (count($intersection) <= 1) {
$intersection = explode('/', $tempDir);
}

$basename = implode('/', array_diff($solutionPath, $intersection));
$entrypoint = implode('/', array_diff($entryPointPath, $intersection));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually have no idea what this chuck is doing - can you extract it to a method and maybe add a comment or just give it a good name?

$tmpDir = realpath(sys_get_temp_dir());

if (!$tmpDir) {
throw new \RuntimeException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a message to the exception - also we do have our own RuntimeException you could use

$realpath = realpath($path);

if (false === $realpath) {
throw new \RuntimeException(sprintf('Failed to get realpath of "%s"', $path));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, let's use our own RuntimeException

@@ -0,0 +1,119 @@
<?php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there are no tests for this

@AydinHassan
Copy link
Member

@mikeymike Might be worth splitting up and getting some of this work in? Probably the events stuff can go separately, the patches, and the FS utils ?

@mikeymike
Copy link
Member Author

Superseded by multiple PRs

@mikeymike mikeymike closed this May 21, 2021
@AydinHassan AydinHassan deleted the patch-workshop-solution branch June 11, 2021 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants