|
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 implements \ArrayAccess |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var Builder */ |
|
21
|
|
|
protected $builder; |
|
22
|
|
|
/** @var Config */ |
|
23
|
|
|
protected $config; |
|
24
|
|
|
/** @var array */ |
|
25
|
|
|
protected $data = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Loads a file. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Builder |
|
31
|
|
|
* @param string $path |
|
32
|
|
|
* @param array|null $options |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(Builder $builder, string $path, array $options = null) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->builder = $builder; |
|
37
|
|
|
$this->config = $builder->getConfig(); |
|
38
|
|
|
|
|
39
|
|
|
if (false === $filePath = $this->findFile($path)) { |
|
40
|
|
|
throw new Exception(sprintf('Asset file "%s" doesn\'t exist.', $path)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// handles options |
|
44
|
|
|
$attributs = null; // html attributes |
|
45
|
|
|
extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
|
46
|
|
|
|
|
47
|
|
|
// set data |
|
48
|
|
|
$this->data['file'] = $filePath; |
|
49
|
|
|
$this->data['path'] = '/'.ltrim($path, '/'); |
|
50
|
|
|
$this->data['ext'] = pathinfo($filePath, PATHINFO_EXTENSION); |
|
51
|
|
|
$this->data['type'] = explode('/', mime_content_type($filePath))[0]; |
|
52
|
|
|
$this->data['content'] = ''; |
|
53
|
|
|
if ($this->data['type'] == 'text') { |
|
54
|
|
|
$this->data['content'] = file_get_contents($filePath); |
|
55
|
|
|
} |
|
56
|
|
|
$this->data['attributs'] = $attributs; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __toString(): string |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->data['path']; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Implements \ArrayAccess. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function offsetSet($offset, $value) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!is_null($offset)) { |
|
73
|
|
|
$this->data[$offset] = $value; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Implements \ArrayAccess. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function offsetExists($offset) |
|
81
|
|
|
{ |
|
82
|
|
|
return isset($this->data[$offset]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Implements \ArrayAccess. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function offsetUnset($offset) |
|
89
|
|
|
{ |
|
90
|
|
|
unset($this->data[$offset]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Implements \ArrayAccess. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function offsetGet($offset) |
|
97
|
|
|
{ |
|
98
|
|
|
return isset($this->data[$offset]) ? $this->data[$offset] : null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Try to find a static file (in site or theme(s)) if exists or returns false. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $path |
|
105
|
|
|
* |
|
106
|
|
|
* @return string|false |
|
107
|
|
|
*/ |
|
108
|
|
|
private function findFile(string $path) |
|
109
|
|
|
{ |
|
110
|
|
|
$filePath = Util::joinFile($this->config->getStaticPath(), $path); |
|
111
|
|
|
if (Util::getFS()->exists($filePath)) { |
|
112
|
|
|
return $filePath; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// checks in each theme |
|
116
|
|
|
foreach ($this->config->getTheme() as $theme) { |
|
117
|
|
|
$filePath = Util::joinFile($this->config->getThemeDirPath($theme, 'static'), $path); |
|
118
|
|
|
if (Util::getFS()->exists($filePath)) { |
|
119
|
|
|
return $filePath; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|