Skip to content

Commit 80149a4

Browse files
committed
Refactor to use configurable repos and standards; clean up code
1 parent 8eb9cef commit 80149a4

File tree

16 files changed

+212
-102
lines changed

16 files changed

+212
-102
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
vendor
22
var
3+
!var/tests/repo
34
!var/**/.gitkeep
45
.phpunit.result.cache

composer.lock

Lines changed: 43 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/di.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22
declare(strict_types=1);
33

44
use App\CodeRepository\CodeRepository;
5-
use App\CodeRepository\GithubCodeRepository;
5+
use App\CodeRepository\GitCodeRepository;
6+
use App\Configuration\ConfigurationRepository;
7+
use App\Configuration\XmlConfigurationRepository;
68
use App\Generator\Generator as DocGenerator;
79
use App\Generator\MarkdownGenerator;
810
use App\SniffFinder\FilesystemSniffFinder;
911
use App\SniffFinder\SniffFinder;
12+
use App\Value\Folder;
1013

1114
return [
12-
CodeRepository::class => DI\autowire(GithubCodeRepository::class),
15+
CodeRepository::class => DI\autowire(GitCodeRepository::class),
1316
DocGenerator::class => DI\autowire(MarkdownGenerator::class),
1417
SniffFinder::class => DI\autowire(FilesystemSniffFinder::class),
18+
ConfigurationRepository::class => DI\factory(function () {
19+
return new XmlConfigurationRepository(new Folder(__DIR__ . '/../'));
20+
})
1521
];

generator.xml.dist

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<generator format="markdown">
3-
<sources>
4-
<source path="git@github.com:PHPCompatibility/PHPCompatibility.git">
5-
<standards>
6-
<standard path="PHPCompatibility" />
7-
</standards>
8-
</source>
9-
</sources>
3+
<source path="git@github.com:PHPCompatibility/PHPCompatibility.git">
4+
<standard path="PHPCompatibility" />
5+
</source>
106
</generator>

src/CodeRepository/CodeRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
namespace App\CodeRepository;
55

6+
use App\Configuration\Value\Source;
67
use App\Value\Folder;
78

89
interface CodeRepository
910
{
1011
public const CODE_DOWNLOAD_PATH = 'var/repos/';
1112

12-
public function downloadCode(string $repoName): Folder;
13+
public function getFolder(Source $source): Folder;
1314
}

src/CodeRepository/GitCodeRepository.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
use Symfony\Component\Process\Exception\ProcessFailedException;
99
use Symfony\Component\Process\Process;
1010

11-
class GithubCodeRepository implements CodeRepository
11+
class GitCodeRepository implements CodeRepository
1212
{
13-
public function downloadCode(Source $source): Folder
13+
public function getFolder(Source $source): Folder
1414
{
15-
$localPath = new Folder(self::CODE_DOWNLOAD_PATH . $source->getLocalFolder() . '/');
15+
$this->runProcess($this->getCloneOrPullProcess($source->getLocalFolder(), $source->getPath()));
16+
$this->runProcess($this->getComposerInstallProcess($source->getLocalFolder()));
1617

17-
$this->runProcess($this->getCloneOrPullProcess($localPath, $source->getPath()));
18-
$this->runProcess($this->getComposerInstallProcess($localPath));
19-
20-
return $localPath;
18+
return $source->getLocalFolder();
2119
}
2220

2321
private function runProcess(Process $process): void

src/Configuration/Value/Source.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33

44
namespace App\Configuration\Value;
55

6+
use App\CodeRepository\CodeRepository;
7+
use App\Value\Folder;
8+
use function Stringy\create as s;
9+
610
final class Source
711
{
12+
public const TYPE_GIT = 'git';
13+
public const TYPE_LOCAL = 'local';
14+
815
private string $path;
916
/**
1017
* @var Standard[]
1118
*/
1219
private array $standards;
20+
private Folder $localFolder;
21+
private string $type = self::TYPE_LOCAL;
1322

1423
/**
1524
* @param Standard[] $standards
@@ -18,13 +27,32 @@ public function __construct(string $path, array $standards)
1827
{
1928
$this->path = $path;
2029
$this->standards = $standards;
30+
31+
if (preg_match('/([^.\/]+)\.git$/', $path, $matches)) {
32+
$this->localFolder = $this->createLocalFolder(CodeRepository::CODE_DOWNLOAD_PATH . $matches[1]);
33+
$this->type = self::TYPE_GIT;
34+
return;
35+
}
36+
37+
$this->localFolder = $this->createLocalFolder($path);
38+
}
39+
40+
private function createLocalFolder(string $path): Folder
41+
{
42+
$path = s($path)->ensureRight('/');
43+
return new Folder((string)$path);
2144
}
2245

2346
public function getPath(): string
2447
{
2548
return $this->path;
2649
}
2750

51+
public function getLocalFolder(): Folder
52+
{
53+
return $this->localFolder;
54+
}
55+
2856
/**
2957
* @return Standard[]
3058
*/

src/Configuration/XmlConfigurationRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(Folder $root)
2222

2323
public function getConfig(): Configuration
2424
{
25-
$paths = [$this->root . 'generator.xml', $this->root . '/generator.xml.dist'];
25+
$paths = [$this->root . 'generator.xml', $this->root . 'generator.xml.dist'];
2626
foreach ($paths as $path) {
2727
if (file_exists($path)) {
2828
return $this->parse($path);
@@ -39,7 +39,7 @@ private function parse(string $path): Configuration
3939
$dom = new DOMDocument;
4040
$dom->load($path);
4141

42-
set_error_handler(function(int $number, string $error) use ($path) {
42+
set_error_handler(function (int $number, string $error) use ($path) {
4343
throw new RuntimeException(
4444
sprintf("The configuration file %s is invalid.\n%s", $path, $error)
4545
);

0 commit comments

Comments
 (0)