Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 196c7f4

Browse files
Initial POC of running solutions in temp dir
1 parent d6374ab commit 196c7f4

File tree

3 files changed

+120
-8
lines changed

3 files changed

+120
-8
lines changed

‎src/Solution/DirectorySolution.php‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class DirectorySolution implements SolutionInterface
4040
* @param array<string> $exclusions An array of file names to exclude from the folder.
4141
* @throws InvalidArgumentException If the entry point does not exist in the folder.
4242
*/
43-
public function __construct(string $directory, string $entryPoint, array $exclusions = [])
43+
private function __construct(string $directory, string $entryPoint, array $exclusions = [])
4444
{
4545
$directory = (string) realpath(rtrim($directory, '/'));
4646
$entryPoint = ltrim($entryPoint, '/');
@@ -83,13 +83,18 @@ public function __construct(string $directory, string $entryPoint, array $exclus
8383
* @param string $entryPoint The relative path from the directory of the entry point file.
8484
* @return self
8585
*/
86-
public static function fromDirectory(string $directory, array $exclusions = [], $entryPoint = 'solution.php'): self
87-
{
88-
return new self($directory, $entryPoint, array_merge($exclusions, ['composer.lock', 'vendor']));
86+
public static function fromDirectory(
87+
string $directory,
88+
array $exclusions = [],
89+
$entryPoint = 'solution.php'
90+
): SolutionInterface {
91+
return InMemorySolution::fromSolution(
92+
new self($directory, $entryPoint, array_merge($exclusions, ['composer.lock', 'vendor']))
93+
);
8994
}
9095

9196
/**
92-
* Get the entry point. This is the PHP file that PHO would execute in order to run the
97+
* Get the entry point. This is the PHP file that PHP would execute in order to run the
9398
* program. This should be the absolute path.
9499
*
95100
* @return string

‎src/Solution/InMemorySolution.php‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\PhpWorkshop\Solution;
6+
7+
use Symfony\Component\Filesystem\Filesystem;
8+
9+
class InMemorySolution implements SolutionInterface
10+
{
11+
/**
12+
* @var string
13+
*/
14+
private string $baseDirectory;
15+
16+
/**
17+
* @var SolutionFile[]
18+
*/
19+
private array $files;
20+
21+
private function __construct(SolutionInterface $solution)
22+
{
23+
$fileSystem = new Filesystem();
24+
25+
$currentPath = explode('/', realpath(__DIR__));
26+
$solutionPath = explode('/', realpath($solution->getBaseDirectory()));
27+
$entryPointPath = explode('/', realpath($solution->getEntryPoint()));
28+
29+
$intersection = array_intersect($currentPath, $solutionPath);
30+
31+
$basename = implode('/', array_diff($solutionPath, $intersection));
32+
$entrypoint = implode('/', array_diff($entryPointPath, $intersection));
33+
34+
$this->baseDirectory = sprintf('%s/php-school/%s', sys_get_temp_dir(), $basename);
35+
$this->entryPoint = sprintf('%s/php-school/%s', sys_get_temp_dir(), $entrypoint);
36+
$this->files = array_map(function (SolutionFile $solutionFile) use ($intersection) {
37+
$filePath = explode('/', realpath($solutionFile->__toString()));
38+
$file = implode('/', array_diff($filePath, $intersection));
39+
return SolutionFile::fromFile(sprintf('%s/php-school/%s', sys_get_temp_dir(), $file));
40+
}, $solution->getFiles());
41+
42+
if ($fileSystem->exists($this->baseDirectory)) {
43+
$fileSystem->remove($this->baseDirectory);
44+
}
45+
46+
$fileSystem->mkdir($this->baseDirectory);
47+
48+
$dirIterator = new \RecursiveDirectoryIterator(
49+
$solution->getBaseDirectory(),
50+
\RecursiveDirectoryIterator::SKIP_DOTS
51+
);
52+
$iterator = new \RecursiveIteratorIterator($dirIterator, \RecursiveIteratorIterator::SELF_FIRST);
53+
54+
foreach ($iterator as $file) {
55+
$target = sprintf('%s/%s', $this->baseDirectory, $iterator->getSubPathName());
56+
$file->isDir()
57+
? $fileSystem->mkdir($target)
58+
: $fileSystem->copy($file->getPathname(), $target);
59+
}
60+
}
61+
62+
public static function fromSolution(SolutionInterface $solution)
63+
{
64+
return new self($solution);
65+
}
66+
67+
/**
68+
* Get the entry point. This is the PHP file that PHO would execute in order to run the
69+
* program. This should be the absolute path.
70+
*
71+
* @return string
72+
*/
73+
public function getEntryPoint(): string
74+
{
75+
return $this->entryPoint;
76+
}
77+
78+
/**
79+
* Get all the files which are contained with the solution.
80+
*
81+
* @return array<SolutionFile>
82+
*/
83+
public function getFiles(): array
84+
{
85+
return $this->files;
86+
}
87+
88+
/**
89+
* Get the absolute path to the directory containing the solution.
90+
*
91+
* @return string
92+
*/
93+
public function getBaseDirectory(): string
94+
{
95+
return $this->baseDirectory;
96+
}
97+
98+
/**
99+
* Check whether there is a `composer.lock` file in the base directory.
100+
*
101+
* @return bool
102+
*/
103+
public function hasComposerFile(): bool
104+
{
105+
return file_exists(sprintf('%s/composer.lock', $this->baseDirectory));
106+
}
107+
}

‎src/Solution/SingleFileSolution.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SingleFileSolution implements SolutionInterface
2020
* @param string $file The absolute path of the reference solution.
2121
* @throws InvalidArgumentException If the file does not exist.
2222
*/
23-
public function __construct(string $file)
23+
private function __construct(string $file)
2424
{
2525
if (!file_exists($file)) {
2626
throw new InvalidArgumentException(sprintf('File: "%s" does not exist', $file));
@@ -36,9 +36,9 @@ public function __construct(string $file)
3636
* @return self
3737
* @throws InvalidArgumentException If the file does not exist.
3838
*/
39-
public static function fromFile(string $file): self
39+
public static function fromFile(string $file): SolutionInterface
4040
{
41-
return new self($file);
41+
return InMemorySolution::fromSolution(new self($file));
4242
}
4343

4444
/**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /