1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cecil/Cecil package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cecil\Assets; |
12
|
|
|
|
13
|
|
|
use Cecil\Builder; |
14
|
|
|
use Cecil\Config; |
15
|
|
|
use Cecil\Exception\Exception; |
16
|
|
|
use Cecil\Util; |
17
|
|
|
|
18
|
|
|
class Asset |
19
|
|
|
{ |
20
|
|
|
/** @var Builder */ |
21
|
|
|
protected $builder; |
22
|
|
|
/** @var Config */ |
23
|
|
|
protected $config; |
24
|
|
|
/** @var array */ |
25
|
|
|
protected $properties = []; |
26
|
|
|
|
27
|
|
|
const CACHE_ASSETS_DIR = 'assets'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Loads a file. |
31
|
|
|
* |
32
|
|
|
* @param Builder |
33
|
|
|
* @param string $path |
34
|
|
|
* @param array|null $options |
35
|
|
|
*/ |
36
|
|
|
public function __construct(Builder $builder, string $path, array $options = null) |
37
|
|
|
{ |
38
|
|
|
$this->builder = $builder; |
39
|
|
|
$this->config = $builder->getConfig(); |
40
|
|
|
|
41
|
|
|
if (false === $filePath = $this->getFile($path)) { |
42
|
|
|
throw new Exception(sprintf('Asset file "%s" doesn\'t exist.', $path)); |
43
|
|
|
} |
44
|
|
|
$fileInfo = new \SplFileInfo($filePath); |
45
|
|
|
|
46
|
|
|
$baseurl = (string) $this->config->get('baseurl'); |
47
|
|
|
$base = ''; |
48
|
|
|
|
49
|
|
|
// handles options |
50
|
|
|
$canonical = null; |
51
|
|
|
$attributs = null; |
52
|
|
|
extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
53
|
|
|
|
54
|
|
|
// prepares properties |
55
|
|
|
$this->properties['path'] = '/'.ltrim($path, '/'); |
56
|
|
|
if ((bool) $this->config->get('canonicalurl') || $canonical === true) { |
57
|
|
|
$base = rtrim($baseurl, '/'); |
58
|
|
|
} |
59
|
|
|
if ($canonical === false) { |
60
|
|
|
$base = ''; |
61
|
|
|
} |
62
|
|
|
$this->properties['url'] = $base.'/'.ltrim($path, '/'); |
63
|
|
|
$this->properties['ext'] = $fileInfo->getExtension(); |
64
|
|
|
$this->properties['type'] = explode('/', mime_content_type($fileInfo->getPathname()))[0]; |
65
|
|
|
if ($this->properties['type'] == 'text') { |
66
|
|
|
$this->properties['content'] = file_get_contents($fileInfo->getPathname()); |
67
|
|
|
} |
68
|
|
|
$this->properties['attributs'] = $attributs; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function __toString(): string |
75
|
|
|
{ |
76
|
|
|
return $this->properties['path']; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns as HTML tag. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getHtml(): string |
85
|
|
|
{ |
86
|
|
|
if ($this->properties['type'] == 'image') { |
87
|
|
|
return \sprintf( |
88
|
|
|
'<img src="%s" title="%s" alt="%s">', |
89
|
|
|
$this->properties['path'], |
90
|
|
|
$this->properties['attributs']['title'], |
91
|
|
|
$this->properties['attributs']['alt'] |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
switch ($this->properties['ext']) { |
96
|
|
|
case 'css': |
97
|
|
|
return \sprintf('<link rel="stylesheet" href="%s">', $this->properties['path']); |
98
|
|
|
case 'js': |
99
|
|
|
return \sprintf('<script src="%s"></script>', $this->properties['path']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
throw new Exception(\sprintf('%s is available only with CSS, JS and images files.', '.html')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns file's content. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getInline(): string |
111
|
|
|
{ |
112
|
|
|
if (!array_key_exists('content', $this->properties)) { |
113
|
|
|
throw new Exception(\sprintf('%s is available only with CSS et JS files.', '.inline')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->properties['content']; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get a static file (in site or theme(s)) if exists or false. |
121
|
|
|
* |
122
|
|
|
* @param string $path |
123
|
|
|
* |
124
|
|
|
* @return string|false |
125
|
|
|
*/ |
126
|
|
|
private function getFile(string $path) |
127
|
|
|
{ |
128
|
|
|
$filePath = Util::joinFile($this->config->getStaticPath(), $path); |
129
|
|
|
if (Util::getFS()->exists($filePath)) { |
130
|
|
|
return $filePath; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// checks in each theme |
134
|
|
|
foreach ($this->config->getTheme() as $theme) { |
135
|
|
|
$filePath = Util::joinFile($this->config->getThemeDirPath($theme, 'static'), $path); |
136
|
|
|
if (Util::getFS()->exists($filePath)) { |
137
|
|
|
return $filePath; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|