1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace allejo\stakx\Markup; |
4
|
|
|
|
5
|
|
|
use allejo\stakx\Document\ContentItem; |
6
|
|
|
use allejo\stakx\Filesystem\File; |
7
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
8
|
|
|
use allejo\stakx\Manager\AssetManager; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This trait provides the functionality for registering linked files with our AssetManager. |
12
|
|
|
* |
13
|
|
|
* @since 0.2.1 |
14
|
|
|
*/ |
15
|
|
|
trait AssetHandlerTrait |
16
|
|
|
{ |
17
|
|
|
/** @var AssetManager */ |
18
|
|
|
protected $assetManager; |
19
|
|
|
|
20
|
|
|
/** @var ContentItem */ |
21
|
|
|
protected $contentItem; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get a File object from a local path relative to the ContentItem. |
25
|
|
|
* |
26
|
|
|
* @param string $localPath |
27
|
|
|
* |
28
|
|
|
* @return File |
29
|
|
|
*/ |
30
|
|
|
private function getFileFromPath($localPath) |
31
|
|
|
{ |
32
|
|
|
$path = fs::path($this->contentItem->getAbsoluteFilePath()) |
33
|
|
|
->getParentDirectory() |
34
|
|
|
->generatePath($localPath); |
35
|
|
|
|
36
|
|
|
return new File($path); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the permalink this file would belong at. |
41
|
|
|
* |
42
|
|
|
* This is taken from the ContentItem's target path and puts the asset at the same location as a sibling. |
43
|
|
|
* |
44
|
|
|
* @param File $file |
45
|
|
|
* |
46
|
|
|
* @return string |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
private function getPermalinkFromFile(File $file) |
49
|
|
|
{ |
50
|
|
|
$folder = fs::path($this->contentItem->getTargetFile())->getParentDirectory(); |
51
|
|
|
|
52
|
|
|
return fs::getRelativePath($folder->generatePath($file->getFilename())); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check if a given string is a valid URL. |
57
|
|
|
* |
58
|
|
|
* @param string $url |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
private function isValidURL($url) |
63
|
|
|
{ |
64
|
|
|
return filter_var($url, FILTER_VALIDATE_URL); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Given a URL to a local path, register this function with the AssetManager so it can be available at compile time. |
69
|
|
|
* |
70
|
|
|
* @since 0.2.1 |
71
|
|
|
* |
72
|
|
|
* @param string $path |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
protected function registerAsset($path) |
77
|
|
|
{ |
78
|
|
|
if ($this->isValidURL($path)) |
79
|
|
|
{ |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$asset = $this->getFileFromPath($path); |
84
|
|
|
$permalink = $this->getPermalinkFromFile($asset); |
85
|
|
|
|
86
|
|
|
$this->assetManager->addExplicitAsset($permalink, $asset); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.