This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCdnAssetManager.php
169 lines (152 loc) · 4.7 KB
/
CdnAssetManager.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* @copyright Copyright (c) 2015 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\cdn;
use Yii;
use CException;
use CFileHelper;
use CAssetManager;
use Aws\CloudFront\CloudFrontClient;
/**
* CdnAssetManager
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\cdn
*/
class CdnAssetManager extends CAssetManager
{
use AssetManagerTrait;
/**
* @var string the CDN distribution id
*/
public $distribution;
/**
* @var string keeps the id of the CDN distribution. Used for invalidation requests.
*/
protected $cdn;
/**
* @inheritdoc
* @throws CException
*/
public function init()
{
if ($this->isDeveloperEnvironment === false && $this->host === null) {
throw new CException(
'"CdnAssetManager.host" cannot be null.'
);
}
parent::init();
}
/**
* Returns the CloudFrontClient
*
* @return CloudFrontClient|string
*/
public function getCloudFrontClient()
{
if ($this->cdn === null) {
$this->cdn = CloudFrontClient::factory(
[
'key' => $this->key,
'secret' => $this->secret
]
);
}
return $this->cdn;
}
/**
* @inheritdoc
*/
public function publish($path, $hashByName = false, $level = -1, $forceCopy = null)
{
$path = parent::publish($path, $hashByName, $level, $forceCopy);
if (!$this->isDeveloperEnvironment) {
$schema = Yii::app()->getRequest()->isSecureConnection ? 'https' : 'http';
$path = "{$schema}://{$this->host}{$path}";
}
return $path;
}
/**
* Invalidates specified asset files from CDN.
*
* Important: In CloudFront cache invalidation is a costly operation with various restrictions. First of all, you
* can run only 3 invalidation requests at any given time. Second, in each validation request you can included
* maximum of 1000 files. Third, invalidation takes time propagate across all edge locations (5~10 minutes).
* The change in CloudFront distribution will eventually be consistent but not immediately. In terms of cost, in a
* given month invalidation of 1000 files is free after that you have to pay per file for each file listed in your
* invalidation requests.
*
* @param array $assetsPaths
*
* @throws CException
*/
public function invalidateAssetsOnCloudFront($assetsPaths = [])
{
$paths = [];
$webRootStrLength = strlen(Yii::getPathOfAlias('webroot'));
$pathGroups = [];
// get assets to invalidate and store the relative path
foreach ($assetsPaths as $path) {
$files = CFileHelper::findFiles(Yii::getPathOfAlias($path));
foreach ($files as $file) {
$paths[] = substr($file, $webRootStrLength);
if (count($paths) >= 1000) {
$pathGroups[] = $paths;
$paths = [];
}
}
}
if (!empty($pathGroups)) {
if (count($pathGroups) > 3) {
throw new \CException(
"You can only invalidate up to 3000 object URLs at one time. Number requested: " . count(
$pathGroups
)
);
}
foreach ($pathGroups as $p) {
$this->createInvalidation($p);
}
} else {
$this->createInvalidation($paths);
}
}
/**
* Executes the CreateInvalidation operation.
*
* @param array $paths the relative
*
* @return \Guzzle\Service\Resource\Model
*/
protected function createInvalidation($paths)
{
$cdn = $this->getCloudFrontClient();
return $cdn->createInvalidation(
[
'DistributionId' => $this->distribution,
'Paths' => [
'Quantity' => count($paths),
'Items' => $paths
],
'CallerReference' => $this->distribution . date('U')
]
);
}
/**
* Override in order to hash always by name.
*
* @param string $file
* @param bool $hashByName
*
* @return string
*/
protected function generatePath($file, $hashByName = false)
{
return $this->isDeveloperEnvironment ? parent::generatePath($file, $hashByName) : $this->hash(basename($file));
}
}