forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigUtils.php
More file actions
133 lines (112 loc) · 3.83 KB
/
Copy pathConfigUtils.php
File metadata and controls
133 lines (112 loc) · 3.83 KB
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
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary\Configuration;
use Cloudinary\ArrayUtils;
use Cloudinary\StringUtils;
use Cloudinary\Utils;
use InvalidArgumentException;
use UnexpectedValueException;
/**
* Class ConfigUtils
*
* @internal
*/
class ConfigUtils
{
const CLOUDINARY_URL_SCHEME = 'cloudinary';
/**
* Checks whether the supplied string is a valid cloudinary url
*
* @param string $cloudinaryUrl Cloudinary url candidate
*
* @return bool
*/
public static function isCloudinaryUrl($cloudinaryUrl)
{
return Utils::tryParseUrl(self::normalizeCloudinaryUrl($cloudinaryUrl), [self::CLOUDINARY_URL_SCHEME]) ? true
: false;
}
/**
* Parses cloudinary url and fills in array that can be consumed by Configuration.
*
* @param string $cloudinaryUrl The Cloudinary Url
*
* @return array
*/
public static function parseCloudinaryUrl($cloudinaryUrl)
{
if (empty($cloudinaryUrl)) {
throw new InvalidArgumentException(
'CLOUDINARY_URL cannot be empty'
);
}
$uri = Utils::tryParseUrl(self::normalizeCloudinaryUrl($cloudinaryUrl), [self::CLOUDINARY_URL_SCHEME]);
if (! $uri) {
throw new UnexpectedValueException(
/** @lang text */
'Invalid CLOUDINARY_URL, "cloudinary://[<key>:<secret>@]<cloud>" expected'
);
}
$qParams = Utils::tryParseValues(StringUtils::parseQueryString($uri->getQuery()));
$cloud = [CloudConfig::CLOUD_NAME => $uri->getHost()];
$userPass = explode(':', $uri->getUserInfo(), 2);
ArrayUtils::addNonEmpty($cloud, CloudConfig::API_KEY, ArrayUtils::get($userPass, 0));
ArrayUtils::addNonEmpty($cloud, CloudConfig::API_SECRET, ArrayUtils::get($userPass, 1));
$config = array_merge_recursive($qParams, [CloudConfig::CONFIG_NAME => $cloud]);
$isPrivateCdn = ! empty($uri->getPath()) && $uri->getPath() !== '/';
if ($isPrivateCdn) {
$config = array_merge(
$config,
[
UrlConfig::CONFIG_NAME => [
UrlConfig::SECURE_CNAME => substr($uri->getPath(), 1),
UrlConfig::PRIVATE_CDN => $isPrivateCdn,
],
]
);
}
return $config;
}
/**
* Tries to normalize the supplied cloudinary url string.
*
* @param string $cloudinaryUrl Cloudinary url candidate.
*
* @return string
*/
public static function normalizeCloudinaryUrl($cloudinaryUrl)
{
if (! is_string($cloudinaryUrl)) {
return $cloudinaryUrl;
}
return StringUtils::truncatePrefix($cloudinaryUrl, Configuration::CLOUDINARY_URL_ENV_VAR . '=');
}
/**
* Builds the main part of the Cloudinary url (not including query parameters)
*
* @param array $config Configuration array
*
* @return string Resulting Cloudinary Url
*/
public static function buildCloudinaryUrl($config)
{
$res = self::CLOUDINARY_URL_SCHEME . '://';
if (! empty($config[CloudConfig::CONFIG_NAME])) {
$res .= "{$config[CloudConfig::CONFIG_NAME][CloudConfig::API_KEY]}:" .
"{$config[CloudConfig::CONFIG_NAME][CloudConfig::API_SECRET]}@";
}
$res .= ArrayUtils::get($config, [CloudConfig::CONFIG_NAME, CloudConfig::CLOUD_NAME]);
$res = ArrayUtils::implodeFiltered(
'/',
[$res, ArrayUtils::get($config, [UrlConfig::CONFIG_NAME, UrlConfig::SECURE_CNAME])]
);
return $res;
}
}