1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FFCMS\Models; |
4
|
|
|
|
5
|
|
|
use FFCMS\{Traits, Mappers, Exceptions}; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Assets Model Class. |
9
|
|
|
* |
10
|
|
|
* @author Vijay Mahrra <[email protected]> |
11
|
|
|
* @copyright (c) Copyright 2016 Vijay Mahrra |
12
|
|
|
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) |
13
|
|
|
*/ |
14
|
|
|
class Assets extends DB |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \FFCMS\Mappers\Assets mapper for asset |
19
|
|
|
*/ |
20
|
|
|
public $mapper; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* initialize with array of params, 'db' and 'logger' can be injected |
24
|
|
|
* |
25
|
|
|
* @param null|\Log $logger |
26
|
|
|
* @param null|\DB\SQL $db |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $params = [], \Log $logger = null, \DB\SQL $db = null) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($params, $logger, $db); |
31
|
|
|
|
32
|
|
|
$this->mapper = new Mappers\Assets; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the associated asset mapper |
37
|
|
|
* |
38
|
|
|
* @return \FFCMS\Mappers\Assets |
39
|
|
|
*/ |
40
|
|
|
public function &getMapper() |
41
|
|
|
{ |
42
|
|
|
return $this->mapper; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Return the URL path to the asset |
47
|
|
|
* |
48
|
|
|
* @param string $assetPath the path in assets, must prefix slash, no trailing slash |
49
|
|
|
* @return string return the url path or false if not exists |
50
|
|
|
*/ |
51
|
|
|
public function assetUrlPath($assetPath): string |
52
|
|
|
{ |
53
|
|
|
$f3 = \Base::instance(); |
54
|
|
|
return $f3->get('assets.url') . $assetPath; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create if needed, and return the dir to the asset path |
59
|
|
|
* |
60
|
|
|
* @return string string $assetPath the path in assets |
61
|
|
|
*/ |
62
|
|
|
public function assetDirPath($assetPath): string |
63
|
|
|
{ |
64
|
|
|
$f3 = \Base::instance(); |
65
|
|
|
$dir = $f3->get('assets.dir') . $assetPath; |
66
|
|
|
if (!file_exists($dir)) { |
67
|
|
|
mkdir($dir, 0777, true); |
68
|
|
|
} |
69
|
|
|
return $dir . '/'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create if needed, and return the path to the asset file path |
74
|
|
|
* |
75
|
|
|
* @param string $dirPath dir for the $filename |
76
|
|
|
* @param string $filename filename for asset |
77
|
|
|
* @return string $path to the asset |
78
|
|
|
*/ |
79
|
|
|
public function assetFilePath($dirPath, $filename): string |
80
|
|
|
{ |
81
|
|
|
return $this->assetDirPath($dirPath) . $filename; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return the URL path to the asset if exists or false |
86
|
|
|
* |
87
|
|
|
* @param string $dirPath dir for the $filename |
88
|
|
|
* @return string $path to the asset |
89
|
|
|
* @return bool true if the asset exists |
|
|
|
|
90
|
|
|
*/ |
91
|
|
|
public function assetExists($dirPath, $filename) |
92
|
|
|
{ |
93
|
|
|
return file_exists($this->assetFilePath($dirPath, $filename)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return the URL path to the asset if exists or false |
98
|
|
|
* |
99
|
|
|
* @param string $dirPath dir for the $filename |
100
|
|
|
* @param string $uuid the user uuid |
|
|
|
|
101
|
|
|
* @return false|string return the url path or false if not exists |
102
|
|
|
*/ |
103
|
|
|
public function assetUrl($dirPath, $filename) |
104
|
|
|
{ |
105
|
|
|
$url = $this->assetExists($dirPath, $filename) ? $this->assetUrlPath($dirPath . '/' . $filename) : false; |
106
|
|
|
if (empty($url)) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
return $url . '?' . filesize($this->assetFilePath($dirPath, $filename)); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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.