-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDetector.php
More file actions
71 lines (59 loc) · 1.66 KB
/
Detector.php
File metadata and controls
71 lines (59 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace Utopia\VCS\Detector;
class Detector
{
/**
* @var Adapter[]
*/
protected array $detectors = [];
/**
* @var string[]
*/
protected array $files;
/**
* @var string[]
*/
protected array $languages;
/**
* @param string[] $files
* @param string[] $languages
*/
public function __construct(array $files = [], array $languages = [])
{
$this->files = $files;
$this->languages = $languages;
}
public function addDetector(Adapter $detector): self
{
$this->detectors[] = $detector;
return $this;
}
public function detect(): ?string
{
// 1. Look for specific files
foreach ($this->detectors as $detector) {
$detectorFiles = $detector->getFiles();
$matches = \array_intersect($detectorFiles, $this->files);
if (\count($matches) > 0) {
return $detector->getRuntime();
}
}
// 2. Look for files with extension
foreach ($this->detectors as $detector) {
foreach ($this->files as $file) {
if (\in_array(pathinfo($file, PATHINFO_EXTENSION), $detector->getFileExtensions())) {
return $detector->getRuntime();
}
}
}
// 3. Look for match with language detected by Git
foreach ($this->detectors as $detector) {
foreach ($this->languages as $language) {
if (\in_array($language, $detector->getLanguages())) {
return $detector->getRuntime();
}
}
}
return null;
}
}