-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Definitions.php
51 lines (38 loc) · 1.15 KB
/
Definitions.php
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
<?php
namespace Spatie\SchemaOrg\Generator;
use RuntimeError;
use Symfony\Component\DomCrawler\Crawler;
class Definitions
{
/** @var array */
protected $sources;
/** @var string */
protected $tempDir = __DIR__.'/temp';
public function __construct(array $sources)
{
$this->sources = $sources;
}
public function preload()
{
foreach ($this->sources as $sourceId => $sourcePath) {
$this->loadSource($sourceId, false);
}
}
public function query(string $selector): Crawler
{
return (new Crawler($this->loadSource('core')))->filter($selector);
}
protected function loadSource(string $sourceId, bool $fromCache = true): string
{
if (! isset($this->sources[$sourceId])) {
throw new RuntimeError("Source `{$sourceId}` doesn't exist");
}
$cachePath = $this->tempDir.'/'.$sourceId.'.rdfa';
if ($fromCache && file_exists($cachePath)) {
return file_get_contents($cachePath);
}
$rdfa = file_get_contents($this->sources[$sourceId]);
file_put_contents($cachePath, $rdfa);
return $rdfa;
}
}