1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\MultiDomain; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Object; |
6
|
|
|
use SilverStripe\MultiDomain\MultiDomainDomain; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A utility class that provides several static methods for parsing URLs |
10
|
|
|
* and resolving hostnames. |
11
|
|
|
* |
12
|
|
|
* @package silverstripe-multi-domain |
13
|
|
|
* @author Aaron Carlino <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class MultiDomain extends Object |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The key for the "primary" domain |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
const KEY_PRIMARY = 'primary'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Given a url, get the domain that maps to it, e.g. |
26
|
|
|
* |
27
|
|
|
* /company/ -> silverstripe.com |
28
|
|
|
* /company/partners -> silverstripe.com |
29
|
|
|
* /community/forum -> silverstripe.org |
30
|
|
|
* |
31
|
|
|
* @param string $url |
32
|
|
|
* @return MultiDomainDomain |
33
|
|
|
*/ |
34
|
|
|
public static function domain_for_url($url) |
35
|
|
|
{ |
36
|
|
|
$url = trim($url, '/'); |
37
|
|
|
|
38
|
|
|
foreach (self::get_all_domains() as $domain) { |
39
|
|
|
if ($domain->hasURL($url)) { |
40
|
|
|
return $domain; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return self::get_primary_domain(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Gets all the domains that have been configured |
49
|
|
|
* |
50
|
|
|
* @param boolean $includePrimary If true, include the primary domain |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public static function get_all_domains($includePrimary = false) |
54
|
|
|
{ |
55
|
|
|
$domains = array (); |
56
|
|
|
|
57
|
|
|
foreach (self::config()->domains as $key => $config) { |
58
|
|
|
if (!$includePrimary && $key === self::KEY_PRIMARY) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
$domains[] = MultiDomainDomain::create($key, $config); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $domains; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets the domain marked as "primary" |
69
|
|
|
* @return MultiDomainDomain |
70
|
|
|
*/ |
71
|
|
|
public static function get_primary_domain() |
72
|
|
|
{ |
73
|
|
|
return self::get_domain(self::KEY_PRIMARY); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets a domain by its key, e.g. 'org','com' |
78
|
|
|
* @param string $domain |
79
|
|
|
* @return MultiDomainDomain |
80
|
|
|
*/ |
81
|
|
|
public static function get_domain($domain) |
82
|
|
|
{ |
83
|
|
|
if (isset(self::config()->domains[$domain])) { |
84
|
|
|
return MultiDomainDomain::create( |
85
|
|
|
$domain, |
86
|
|
|
self::config()->domains[$domain] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|