-
Notifications
You must be signed in to change notification settings - Fork 4
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
Code patch updates #201
Changes from all commits
60e154c
e04dd7f
0e0c44a
abb89cd
3342864
33dfe13
fcbf0bf
41e9746
f45b6f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like there are no tests for this |
||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpSchool\PhpWorkshop\Solution; | ||
|
||
use PhpSchool\PhpWorkshop\Utils\System; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
class InTempSolution implements SolutionInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $baseDirectory; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $entryPoint; | ||
|
||
/** | ||
* @var SolutionFile[] | ||
*/ | ||
private $files; | ||
|
||
private function __construct(SolutionInterface $solution) | ||
{ | ||
$fileSystem = new Filesystem(); | ||
|
||
$tempDir = System::tempDir(); | ||
$currentPath = explode('/', System::realpath(__DIR__)); | ||
$solutionPath = explode('/', System::realpath($solution->getBaseDirectory())); | ||
$entryPointPath = explode('/', System::realpath($solution->getEntryPoint())); | ||
|
||
$intersection = array_intersect($currentPath, $solutionPath); | ||
|
||
if (count($intersection) <= 1) { | ||
$intersection = explode('/', $tempDir); | ||
} | ||
|
||
$basename = implode('/', array_diff($solutionPath, $intersection)); | ||
$entrypoint = implode('/', array_diff($entryPointPath, $intersection)); | ||
Comment on lines
+36
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
|
||
$this->baseDirectory = sprintf('%s/php-school/%s', $tempDir, $basename); | ||
$this->entryPoint = sprintf('%s/php-school/%s', $tempDir, $entrypoint); | ||
|
||
if ($fileSystem->exists($this->baseDirectory)) { | ||
$fileSystem->remove($this->baseDirectory); | ||
} | ||
|
||
$fileSystem->mkdir($this->baseDirectory); | ||
|
||
$dirIterator = new \RecursiveDirectoryIterator( | ||
$solution->getBaseDirectory(), | ||
\RecursiveDirectoryIterator::SKIP_DOTS | ||
); | ||
$iterator = new \RecursiveIteratorIterator($dirIterator, \RecursiveIteratorIterator::SELF_FIRST); | ||
|
||
foreach ($iterator as $file) { | ||
$target = sprintf('%s/%s', $this->baseDirectory, $iterator->getSubPathName()); | ||
$file->isDir() | ||
? $fileSystem->mkdir($target) | ||
: $fileSystem->copy($file->getPathname(), $target); | ||
} | ||
|
||
$this->files = array_map(function (SolutionFile $solutionFile) use ($intersection, $tempDir) { | ||
$filePath = explode('/', System::realpath($solutionFile->__toString())); | ||
$file = implode('/', array_diff($filePath, $intersection)); | ||
return SolutionFile::fromFile(sprintf('%s/php-school/%s', $tempDir, $file)); | ||
}, $solution->getFiles()); | ||
} | ||
|
||
public static function fromSolution(SolutionInterface $solution): SolutionInterface | ||
{ | ||
return new self($solution); | ||
} | ||
|
||
/** | ||
* Get the entry point. This is the PHP file that PHP would execute in order to run the | ||
* program. This should be the absolute path. | ||
* | ||
* @return string | ||
*/ | ||
public function getEntryPoint(): string | ||
{ | ||
return $this->entryPoint; | ||
} | ||
|
||
/** | ||
* Get all the files which are contained with the solution. | ||
* | ||
* @return array<SolutionFile> | ||
*/ | ||
public function getFiles(): array | ||
{ | ||
return $this->files; | ||
} | ||
|
||
/** | ||
* Get the absolute path to the directory containing the solution. | ||
* | ||
* @return string | ||
*/ | ||
public function getBaseDirectory(): string | ||
{ | ||
return $this->baseDirectory; | ||
} | ||
|
||
/** | ||
* Check whether there is a `composer.lock` file in the base directory. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasComposerFile(): bool | ||
{ | ||
return file_exists(sprintf('%s/composer.lock', $this->baseDirectory)); | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🤔