forked from php-embed/Embed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.php
More file actions
50 lines (41 loc) · 1.48 KB
/
Code.php
File metadata and controls
50 lines (41 loc) · 1.48 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
<?php
declare(strict_types = 1);
namespace Embed\Adapters\Github\Detectors;
use Embed\Detectors\Code as Detector;
use Embed\EmbedCode;
use function Embed\html;
use function Embed\matchPath;
/**
* @extends Detector<\Embed\Adapters\Github\Extractor>
*/
class Code extends Detector
{
public function detect(): ?EmbedCode
{
$result = parent::detect();
return $result !== null ? $result : $this->fallback();
}
private function fallback(): ?EmbedCode
{
$uri = $this->extractor->getUri();
$path = $uri->getPath();
if (!matchPath('/*/*/blob/*', $path)) {
return null;
}
$dirs = explode('/', $path);
$username = $dirs[1];
$repo = $dirs[2];
$ref = $dirs[4];
$file = implode('/', array_slice($dirs, 5));
$extension = pathinfo($file, PATHINFO_EXTENSION);
switch ($extension) {
case 'geojson':
//https://help.github.com/articles/mapping-geojson-files-on-github/#embedding-your-map-elsewhere
return new EmbedCode(html('script', ['src' => "https://embed.githubusercontent.com/view/geojson/{$username}/{$repo}/{$ref}/{$file}"]));
case 'stl':
//https://help.github.com/articles/3d-file-viewer/#embedding-your-model-elsewhere
return new EmbedCode(html('script', ['src' => "https://embed.githubusercontent.com/view/3d/{$username}/{$repo}/{$ref}/{$file}"]));
}
return null;
}
}