Completed
Push — master ( 9cc640...40928c )
by Daniel
9s
created
tests/MultiDomainDomainTest.php 1 patch
Indentation   +164 added lines, -164 removed lines patch added patch discarded remove patch
@@ -2,168 +2,168 @@
 block discarded – undo
2 2
 
3 3
 class MultiDomainDomainTest extends SapphireTest
4 4
 {
5
-    /**
6
-     * Set up some test domain data for testing
7
-     *
8
-     * {@inheritDoc}
9
-     */
10
-    public function setUp()
11
-    {
12
-        parent::setUp();
13
-        Config::nest();
14
-
15
-        Config::inst()->remove('MultiDomain', 'domains');
16
-        Config::inst()->update('MultiDomain', 'domains', array(
17
-            'primary' => array(
18
-                'hostname' => 'example.com'
19
-            ),
20
-            'store' => array(
21
-                'hostname' => 'example-store.com',
22
-                'resolves_to' => 'shop/store',
23
-                'allow' => array(
24
-                    'admin/*',
25
-                    'Security/*',
26
-                    'my-custom-webhook/'
27
-                )
28
-            ),
29
-            'configurable' => array(
30
-                'hostname' => 'MY_CONSTANT_HOSTNAME'
31
-            ),
32
-            'forceful' => array(
33
-                'hostname' => 'forced.com',
34
-                'force' => array(
35
-                    'buy-now/*'
36
-                )
37
-            )
38
-        ));
39
-    }
40
-
41
-    /**
42
-     * Test that a hostname defined in a constant will override the default configuration, otherwise the default
43
-     * configuration for the domain is returned
44
-     */
45
-    public function testGetHostname()
46
-    {
47
-        $configurableDomain = MultiDomain::get_domain('configurable');
48
-        define('MY_CONSTANT_HOSTNAME', 'I am a constant');
49
-        $this->assertSame('I am a constant', $configurableDomain->getHostname());
50
-
51
-        $storeDomain = MultiDomain::get_domain('store');
52
-        $this->assertSame('example-store.com', $storeDomain->getHostname());
53
-    }
54
-
55
-    /**
56
-     * Test that the domain's "resolves to" property is returned for the URL if it is defined, otherwise null
57
-     */
58
-    public function testGetUrl()
59
-    {
60
-        $primaryDomain = MultiDomain::get_domain('primary');
61
-        $this->assertNull($primaryDomain->getURL());
62
-
63
-        $storeDomain = MultiDomain::get_domain('store');
64
-        $this->assertSame('shop/store', $storeDomain->getURL());
65
-    }
66
-
67
-    /**
68
-     * Test that a domain can be identified as the primary domain or otherwise
69
-     */
70
-    public function testIsPrimary()
71
-    {
72
-        $this->assertTrue(MultiDomain::get_primary_domain()->isPrimary());
73
-        $this->assertFalse(MultiDomain::get_domain('store')->isPrimary());
74
-    }
75
-
76
-    /**
77
-     * When the request URI matches one of the allowed rules for a domain, the isActive method should return false
78
-     */
79
-    public function testIsActiveReturnsFalseWhenRequestUriIsAllowedPath()
80
-    {
81
-        $domain = MultiDomain::get_domain('store');
82
-        $domain->setRequestUri('/Security/login');
83
-        $this->assertFalse($domain->isActive());
84
-    }
85
-
86
-    /**
87
-     * When a subdomain is "allowed" and is requested, subdomains should be allowed through "isActive" as well
88
-     * as the primary domain
89
-     */
90
-    public function testSubdomainsAllowedInIsActiveWhenConfigured()
91
-    {
92
-        Config::inst()->update('MultiDomain', 'allow_subdomains', true);
93
-
94
-        $domain = MultiDomain::get_domain('store')
95
-            ->setRequestUri('/some/page')
96
-            ->setHttpHost('api.example-store.com');
97
-
98
-        $this->assertTrue($domain->isActive());
99
-    }
100
-
101
-    /**
102
-     * The default behaviour would be that if the current host from the request matchese that of the domain model
103
-     * then isActive should be true
104
-     */
105
-    public function testReturnActiveIfCurrentHostMatchesDomainsHostname()
106
-    {
107
-        $domain = MultiDomain::get_domain('primary')
108
-            ->setRequestUri('/another/page')
109
-            ->setHttpHost('example.com');
110
-
111
-        $this->assertTrue($domain->isActive());
112
-    }
113
-
114
-    /**
115
-     * getNativeUrl should not be used on the primary domain
116
-     *
117
-     * @expectedException Exception
118
-     * @expectedExceptionMessage Cannot convert a native URL on the primary domain
119
-     */
120
-    public function testGetNativeUrlThrowsExceptionOnPrimaryDomain()
121
-    {
122
-        MultiDomain::get_primary_domain()->getNativeUrl('foo');
123
-    }
124
-
125
-    /**
126
-     * Test that a URL segment can be added to the domain's URL and returned as a "native URL"
127
-     */
128
-    public function testGetNativeUrl()
129
-    {
130
-        $domain = MultiDomain::get_domain('store');
131
-        $this->assertSame('shop/store/foo/bar', $domain->getNativeUrl('foo/bar'));
132
-    }
133
-
134
-    /**
135
-     * "Allowed" and "forced" URLs should just be returned from getNativeUrl as is
136
-     */
137
-    public function testGetNativeUrlReturnsInputWhenUrlIsAllowedOrForced()
138
-    {
139
-        $domain = MultiDomain::get_domain('store');
140
-        $this->assertSame('my-custom-webhook/', $domain->getNativeUrl('my-custom-webhook/'));
141
-
142
-        $domain = MultiDomain::get_domain('forceful');
143
-        $this->assertSame('buy-now/whatever', $domain->getNativeUrl('buy-now/whatever'));
144
-    }
145
-
146
-    /**
147
-     * The primary domain and "allowed" route matches should be returned as it
148
-     */
149
-    public function testGetVanityUrlReturnsInputWhenUrlIsAllowedOrIsPrimaryDomain()
150
-    {
151
-        $this->assertSame('/pages/info', MultiDomain::get_primary_domain()->getVanityUrl('/pages/info'));
152
-        $this->assertSame('/Security/login', MultiDomain::get_domain('store')->getVanityUrl('/Security/login'));
153
-    }
154
-
155
-    /**
156
-     * Non-primary domains and un-allowed route matches should be returned without their URL for vanity
157
-     */
158
-    public function testGetVanityUrl()
159
-    {
160
-        $this->assertSame('partners/', MultiDomain::get_domain('store')->getVanityUrl('shop/store/partners/'));
161
-        $this->assertSame('foo/bar', MultiDomain::get_domain('store')->getVanityUrl('shop/store/foo/bar'));
162
-    }
163
-
164
-    public function tearDown()
165
-    {
166
-        Config::unnest();
167
-        parent::tearDown();
168
-    }
5
+	/**
6
+	 * Set up some test domain data for testing
7
+	 *
8
+	 * {@inheritDoc}
9
+	 */
10
+	public function setUp()
11
+	{
12
+		parent::setUp();
13
+		Config::nest();
14
+
15
+		Config::inst()->remove('MultiDomain', 'domains');
16
+		Config::inst()->update('MultiDomain', 'domains', array(
17
+			'primary' => array(
18
+				'hostname' => 'example.com'
19
+			),
20
+			'store' => array(
21
+				'hostname' => 'example-store.com',
22
+				'resolves_to' => 'shop/store',
23
+				'allow' => array(
24
+					'admin/*',
25
+					'Security/*',
26
+					'my-custom-webhook/'
27
+				)
28
+			),
29
+			'configurable' => array(
30
+				'hostname' => 'MY_CONSTANT_HOSTNAME'
31
+			),
32
+			'forceful' => array(
33
+				'hostname' => 'forced.com',
34
+				'force' => array(
35
+					'buy-now/*'
36
+				)
37
+			)
38
+		));
39
+	}
40
+
41
+	/**
42
+	 * Test that a hostname defined in a constant will override the default configuration, otherwise the default
43
+	 * configuration for the domain is returned
44
+	 */
45
+	public function testGetHostname()
46
+	{
47
+		$configurableDomain = MultiDomain::get_domain('configurable');
48
+		define('MY_CONSTANT_HOSTNAME', 'I am a constant');
49
+		$this->assertSame('I am a constant', $configurableDomain->getHostname());
50
+
51
+		$storeDomain = MultiDomain::get_domain('store');
52
+		$this->assertSame('example-store.com', $storeDomain->getHostname());
53
+	}
54
+
55
+	/**
56
+	 * Test that the domain's "resolves to" property is returned for the URL if it is defined, otherwise null
57
+	 */
58
+	public function testGetUrl()
59
+	{
60
+		$primaryDomain = MultiDomain::get_domain('primary');
61
+		$this->assertNull($primaryDomain->getURL());
62
+
63
+		$storeDomain = MultiDomain::get_domain('store');
64
+		$this->assertSame('shop/store', $storeDomain->getURL());
65
+	}
66
+
67
+	/**
68
+	 * Test that a domain can be identified as the primary domain or otherwise
69
+	 */
70
+	public function testIsPrimary()
71
+	{
72
+		$this->assertTrue(MultiDomain::get_primary_domain()->isPrimary());
73
+		$this->assertFalse(MultiDomain::get_domain('store')->isPrimary());
74
+	}
75
+
76
+	/**
77
+	 * When the request URI matches one of the allowed rules for a domain, the isActive method should return false
78
+	 */
79
+	public function testIsActiveReturnsFalseWhenRequestUriIsAllowedPath()
80
+	{
81
+		$domain = MultiDomain::get_domain('store');
82
+		$domain->setRequestUri('/Security/login');
83
+		$this->assertFalse($domain->isActive());
84
+	}
85
+
86
+	/**
87
+	 * When a subdomain is "allowed" and is requested, subdomains should be allowed through "isActive" as well
88
+	 * as the primary domain
89
+	 */
90
+	public function testSubdomainsAllowedInIsActiveWhenConfigured()
91
+	{
92
+		Config::inst()->update('MultiDomain', 'allow_subdomains', true);
93
+
94
+		$domain = MultiDomain::get_domain('store')
95
+			->setRequestUri('/some/page')
96
+			->setHttpHost('api.example-store.com');
97
+
98
+		$this->assertTrue($domain->isActive());
99
+	}
100
+
101
+	/**
102
+	 * The default behaviour would be that if the current host from the request matchese that of the domain model
103
+	 * then isActive should be true
104
+	 */
105
+	public function testReturnActiveIfCurrentHostMatchesDomainsHostname()
106
+	{
107
+		$domain = MultiDomain::get_domain('primary')
108
+			->setRequestUri('/another/page')
109
+			->setHttpHost('example.com');
110
+
111
+		$this->assertTrue($domain->isActive());
112
+	}
113
+
114
+	/**
115
+	 * getNativeUrl should not be used on the primary domain
116
+	 *
117
+	 * @expectedException Exception
118
+	 * @expectedExceptionMessage Cannot convert a native URL on the primary domain
119
+	 */
120
+	public function testGetNativeUrlThrowsExceptionOnPrimaryDomain()
121
+	{
122
+		MultiDomain::get_primary_domain()->getNativeUrl('foo');
123
+	}
124
+
125
+	/**
126
+	 * Test that a URL segment can be added to the domain's URL and returned as a "native URL"
127
+	 */
128
+	public function testGetNativeUrl()
129
+	{
130
+		$domain = MultiDomain::get_domain('store');
131
+		$this->assertSame('shop/store/foo/bar', $domain->getNativeUrl('foo/bar'));
132
+	}
133
+
134
+	/**
135
+	 * "Allowed" and "forced" URLs should just be returned from getNativeUrl as is
136
+	 */
137
+	public function testGetNativeUrlReturnsInputWhenUrlIsAllowedOrForced()
138
+	{
139
+		$domain = MultiDomain::get_domain('store');
140
+		$this->assertSame('my-custom-webhook/', $domain->getNativeUrl('my-custom-webhook/'));
141
+
142
+		$domain = MultiDomain::get_domain('forceful');
143
+		$this->assertSame('buy-now/whatever', $domain->getNativeUrl('buy-now/whatever'));
144
+	}
145
+
146
+	/**
147
+	 * The primary domain and "allowed" route matches should be returned as it
148
+	 */
149
+	public function testGetVanityUrlReturnsInputWhenUrlIsAllowedOrIsPrimaryDomain()
150
+	{
151
+		$this->assertSame('/pages/info', MultiDomain::get_primary_domain()->getVanityUrl('/pages/info'));
152
+		$this->assertSame('/Security/login', MultiDomain::get_domain('store')->getVanityUrl('/Security/login'));
153
+	}
154
+
155
+	/**
156
+	 * Non-primary domains and un-allowed route matches should be returned without their URL for vanity
157
+	 */
158
+	public function testGetVanityUrl()
159
+	{
160
+		$this->assertSame('partners/', MultiDomain::get_domain('store')->getVanityUrl('shop/store/partners/'));
161
+		$this->assertSame('foo/bar', MultiDomain::get_domain('store')->getVanityUrl('shop/store/foo/bar'));
162
+	}
163
+
164
+	public function tearDown()
165
+	{
166
+		Config::unnest();
167
+		parent::tearDown();
168
+	}
169 169
 }
Please login to merge, or discard this patch.
tests/MultiDomainTest.php 1 patch
Indentation   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -2,96 +2,96 @@
 block discarded – undo
2 2
 
3 3
 class MultiDomainTest extends SapphireTest
4 4
 {
5
-    /**
6
-     * Set up some test domain configuration
7
-     *
8
-     * {@inheritDoc}
9
-     */
10
-    public function setUp()
11
-    {
12
-        parent::setUp();
13
-        Config::nest();
5
+	/**
6
+	 * Set up some test domain configuration
7
+	 *
8
+	 * {@inheritDoc}
9
+	 */
10
+	public function setUp()
11
+	{
12
+		parent::setUp();
13
+		Config::nest();
14 14
 
15
-        Config::inst()->remove('MultiDomain', 'domains');
16
-        Config::inst()->update('MultiDomain', 'domains', array(
17
-            'primary' => array(
18
-                'hostname' => 'foo.bar',
19
-                'resolves_to' => 'bar.baz'
20
-            ),
21
-            'secondary' => array(
22
-                'hostname' => 'localhost',
23
-                'resolves_to' => 'local.dev'
24
-            )
25
-        ));
26
-    }
15
+		Config::inst()->remove('MultiDomain', 'domains');
16
+		Config::inst()->update('MultiDomain', 'domains', array(
17
+			'primary' => array(
18
+				'hostname' => 'foo.bar',
19
+				'resolves_to' => 'bar.baz'
20
+			),
21
+			'secondary' => array(
22
+				'hostname' => 'localhost',
23
+				'resolves_to' => 'local.dev'
24
+			)
25
+		));
26
+	}
27 27
 
28
-    /**
29
-     * Test that a MultiDomainDomain can be returned from the configured domains
30
-     */
31
-    public function testGetDomain()
32
-    {
33
-        $this->assertNull(MultiDomain::get_domain('does-not-exist'));
34
-        $this->assertInstanceOf(MultiDomainDomain::class, MultiDomain::get_domain('primary'));
35
-    }
28
+	/**
29
+	 * Test that a MultiDomainDomain can be returned from the configured domains
30
+	 */
31
+	public function testGetDomain()
32
+	{
33
+		$this->assertNull(MultiDomain::get_domain('does-not-exist'));
34
+		$this->assertInstanceOf(MultiDomainDomain::class, MultiDomain::get_domain('primary'));
35
+	}
36 36
 
37
-    /**
38
-     * Test that all domains can be returned, with or without the primary domain
39
-     *
40
-     * @dataProvider getAllDomainsProvider
41
-     * @param bool $withPrimary
42
-     */
43
-    public function testGetAllDomains($withPrimary)
44
-    {
45
-        $result = MultiDomain::get_all_domains($withPrimary);
46
-        $this->assertInternalType('array', $result);
47
-        $this->assertNotEmpty($result);
37
+	/**
38
+	 * Test that all domains can be returned, with or without the primary domain
39
+	 *
40
+	 * @dataProvider getAllDomainsProvider
41
+	 * @param bool $withPrimary
42
+	 */
43
+	public function testGetAllDomains($withPrimary)
44
+	{
45
+		$result = MultiDomain::get_all_domains($withPrimary);
46
+		$this->assertInternalType('array', $result);
47
+		$this->assertNotEmpty($result);
48 48
 
49
-        $expectedCount = $withPrimary ? 2 : 1;
50
-        $this->assertCount($expectedCount, $result);
51
-    }
49
+		$expectedCount = $withPrimary ? 2 : 1;
50
+		$this->assertCount($expectedCount, $result);
51
+	}
52 52
 
53
-    /**
54
-     * @return array[]
55
-     */
56
-    public function getAllDomainsProvider()
57
-    {
58
-        return array(
59
-            array(true),
60
-            array(false)
61
-        );
62
-    }
53
+	/**
54
+	 * @return array[]
55
+	 */
56
+	public function getAllDomainsProvider()
57
+	{
58
+		return array(
59
+			array(true),
60
+			array(false)
61
+		);
62
+	}
63 63
 
64
-    /**
65
-     * Test that the primary domain can be returned
66
-     */
67
-    public function testGetPrimaryDomain()
68
-    {
69
-        $result = MultiDomain::get_primary_domain();
70
-        $this->assertInstanceOf(MultiDomainDomain::class, $result);
71
-        $this->assertTrue($result->isPrimary());
72
-    }
64
+	/**
65
+	 * Test that the primary domain can be returned
66
+	 */
67
+	public function testGetPrimaryDomain()
68
+	{
69
+		$result = MultiDomain::get_primary_domain();
70
+		$this->assertInstanceOf(MultiDomainDomain::class, $result);
71
+		$this->assertTrue($result->isPrimary());
72
+	}
73 73
 
74
-    /**
75
-     * Test that the correct domain can be returned by a provided URL
76
-     */
77
-    public function testDomainForUrl()
78
-    {
79
-        $result = MultiDomain::domain_for_url('foo.bar/my-page');
80
-        $this->assertInstanceOf(MultiDomainDomain::class, $result);
81
-        $this->assertSame('primary', $result->getKey());
82
-    }
74
+	/**
75
+	 * Test that the correct domain can be returned by a provided URL
76
+	 */
77
+	public function testDomainForUrl()
78
+	{
79
+		$result = MultiDomain::domain_for_url('foo.bar/my-page');
80
+		$this->assertInstanceOf(MultiDomainDomain::class, $result);
81
+		$this->assertSame('primary', $result->getKey());
82
+	}
83 83
 
84
-    /**
85
-     * Test that if a URL doesn't match any domain then the primary domain is returned
86
-     */
87
-    public function testDomainForUrlDefaultsToPrimaryDomain()
88
-    {
89
-        $this->assertTrue(MultiDomain::domain_for_url('does-not-exist.com')->isPrimary());
90
-    }
84
+	/**
85
+	 * Test that if a URL doesn't match any domain then the primary domain is returned
86
+	 */
87
+	public function testDomainForUrlDefaultsToPrimaryDomain()
88
+	{
89
+		$this->assertTrue(MultiDomain::domain_for_url('does-not-exist.com')->isPrimary());
90
+	}
91 91
 
92
-    public function tearDown()
93
-    {
94
-        Config::unnest();
95
-        parent::tearDown();
96
-    }
92
+	public function tearDown()
93
+	{
94
+		Config::unnest();
95
+		parent::tearDown();
96
+	}
97 97
 }
Please login to merge, or discard this patch.
code/MultiDomainRequestFilter.php 1 patch
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -9,60 +9,60 @@
 block discarded – undo
9 9
 class MultiDomainRequestFilter implements RequestFilter
10 10
 {
11 11
 
12
-    /**
13
-     * Gets the active domain, and sets its URL to the native one, with a vanity
14
-     * URL in the request
15
-     *
16
-     * @param  SS_HTTPRequest $request
17
-     * @param  Session        $session
18
-     * @param  DataModel      $model
19
-     */
20
-    public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model)
21
-    {
12
+	/**
13
+	 * Gets the active domain, and sets its URL to the native one, with a vanity
14
+	 * URL in the request
15
+	 *
16
+	 * @param  SS_HTTPRequest $request
17
+	 * @param  Session        $session
18
+	 * @param  DataModel      $model
19
+	 */
20
+	public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model)
21
+	{
22 22
 
23
-        if (Director::is_cli()) {
24
-            return;
25
-        }
23
+		if (Director::is_cli()) {
24
+			return;
25
+		}
26 26
         
27
-        // Not the best place for validation, but _config.php is too early.
28
-        if (!MultiDomain::get_primary_domain()) {
29
-            throw new Exception('MultiDomain must define a "'.MultiDomain::KEY_PRIMARY.'" domain in the config, under "domains"');
30
-        }
27
+		// Not the best place for validation, but _config.php is too early.
28
+		if (!MultiDomain::get_primary_domain()) {
29
+			throw new Exception('MultiDomain must define a "'.MultiDomain::KEY_PRIMARY.'" domain in the config, under "domains"');
30
+		}
31 31
                     
32
-        foreach (MultiDomain::get_all_domains() as $domain) {
33
-            if (!$domain->isActive()) {
34
-                continue;
35
-            }
32
+		foreach (MultiDomain::get_all_domains() as $domain) {
33
+			if (!$domain->isActive()) {
34
+				continue;
35
+			}
36 36
             
37
-            $url = $this->createNativeURLForDomain($domain);
38
-            $parts = explode('?', $url);
39
-            $request->setURL($parts[0]);
40
-        }
41
-    }
37
+			$url = $this->createNativeURLForDomain($domain);
38
+			$parts = explode('?', $url);
39
+			$request->setURL($parts[0]);
40
+		}
41
+	}
42 42
 
43
-    /**
44
-     * Post request noop
45
-     * @param  SS_HTTPRequest  $request
46
-     * @param  SS_HTTPResponse $response
47
-     * @param  DataModel       $model
48
-     */
49
-    public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model)
50
-    {
51
-    }
43
+	/**
44
+	 * Post request noop
45
+	 * @param  SS_HTTPRequest  $request
46
+	 * @param  SS_HTTPResponse $response
47
+	 * @param  DataModel       $model
48
+	 */
49
+	public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model)
50
+	{
51
+	}
52 52
 
53
-    /**
54
-     * Creates a native URL for a domain. This functionality is abstracted so
55
-     * that other modules can overload it, e.g. translatable modules that
56
-     * have their own custom URLs.
57
-     *
58
-     * @param  MultiDomainDomain $domain
59
-     * @return string
60
-     */
61
-    protected function createNativeURLForDomain(MultiDomainDomain $domain)
62
-    {
63
-        return Controller::join_links(
64
-            Director::baseURL(),
65
-            $domain->getNativeURL($_SERVER['REQUEST_URI'])
66
-        );
67
-    }
53
+	/**
54
+	 * Creates a native URL for a domain. This functionality is abstracted so
55
+	 * that other modules can overload it, e.g. translatable modules that
56
+	 * have their own custom URLs.
57
+	 *
58
+	 * @param  MultiDomainDomain $domain
59
+	 * @return string
60
+	 */
61
+	protected function createNativeURLForDomain(MultiDomainDomain $domain)
62
+	{
63
+		return Controller::join_links(
64
+			Director::baseURL(),
65
+			$domain->getNativeURL($_SERVER['REQUEST_URI'])
66
+		);
67
+	}
68 68
 }
Please login to merge, or discard this patch.
code/MultiDomain.php 2 patches
Indentation   +63 added lines, -63 removed lines patch added patch discarded remove patch
@@ -10,75 +10,75 @@
 block discarded – undo
10 10
 class MultiDomain extends Object
11 11
 {
12 12
 
13
-    /**
14
-     * The key for the "primary" domain
15
-     */
16
-    const KEY_PRIMARY = 'primary';
13
+	/**
14
+	 * The key for the "primary" domain
15
+	 */
16
+	const KEY_PRIMARY = 'primary';
17 17
 
18
-    /**
19
-     * Given a url, get the domain that maps to it, e.g.
20
-     *
21
-     * /company/ -> silverstripe.com
22
-     * /company/partners -> silverstripe.com
23
-     * /community/forum -> silverstripe.org
24
-     *
25
-     * @param  string $url
26
-     * @return MultiDomainDomain
27
-     */
28
-    public static function domain_for_url($url)
29
-    {
30
-        $url = trim($url, '/');
18
+	/**
19
+	 * Given a url, get the domain that maps to it, e.g.
20
+	 *
21
+	 * /company/ -> silverstripe.com
22
+	 * /company/partners -> silverstripe.com
23
+	 * /community/forum -> silverstripe.org
24
+	 *
25
+	 * @param  string $url
26
+	 * @return MultiDomainDomain
27
+	 */
28
+	public static function domain_for_url($url)
29
+	{
30
+		$url = trim($url, '/');
31 31
 
32
-        foreach (self::get_all_domains() as $domain) {
33
-            if ($domain->hasURL($url)) {
34
-                return $domain;
35
-            }
36
-        }
32
+		foreach (self::get_all_domains() as $domain) {
33
+			if ($domain->hasURL($url)) {
34
+				return $domain;
35
+			}
36
+		}
37 37
 
38
-        return self::get_primary_domain();
39
-    }
38
+		return self::get_primary_domain();
39
+	}
40 40
 
41
-    /**
42
-     * Gets all the domains that have been configured
43
-     *
44
-     * @param  boolean $includePrimary If true, include the primary domain
45
-     * @return array
46
-     */
47
-    public static function get_all_domains($includePrimary = false)
48
-    {
49
-        $domains = array ();
41
+	/**
42
+	 * Gets all the domains that have been configured
43
+	 *
44
+	 * @param  boolean $includePrimary If true, include the primary domain
45
+	 * @return array
46
+	 */
47
+	public static function get_all_domains($includePrimary = false)
48
+	{
49
+		$domains = array ();
50 50
 
51
-        foreach (self::config()->domains as $key => $config) {
52
-            if (!$includePrimary && $key === self::KEY_PRIMARY) {
53
-                continue;
54
-            }
55
-            $domains[] = MultiDomainDomain::create($key, $config);
56
-        }
51
+		foreach (self::config()->domains as $key => $config) {
52
+			if (!$includePrimary && $key === self::KEY_PRIMARY) {
53
+				continue;
54
+			}
55
+			$domains[] = MultiDomainDomain::create($key, $config);
56
+		}
57 57
 
58
-        return $domains;
59
-    }
58
+		return $domains;
59
+	}
60 60
 
61
-    /**
62
-     * Gets the domain marked as "primary"
63
-     * @return MultiDomainDomain
64
-     */
65
-    public static function get_primary_domain()
66
-    {
67
-        return self::get_domain(self::KEY_PRIMARY);
68
-    }
61
+	/**
62
+	 * Gets the domain marked as "primary"
63
+	 * @return MultiDomainDomain
64
+	 */
65
+	public static function get_primary_domain()
66
+	{
67
+		return self::get_domain(self::KEY_PRIMARY);
68
+	}
69 69
 
70
-    /**
71
-     * Gets a domain by its key, e.g. 'org','com'
72
-     * @param  string $domain
73
-     * @return MultiDomainDomain
74
-     */
75
-    public static function get_domain($domain)
76
-    {
77
-        if (isset(self::config()->domains[$domain])) {
78
-            return MultiDomainDomain::create(
79
-                $domain,
80
-                self::config()->domains[$domain]
81
-            );
82
-        }
83
-    }
70
+	/**
71
+	 * Gets a domain by its key, e.g. 'org','com'
72
+	 * @param  string $domain
73
+	 * @return MultiDomainDomain
74
+	 */
75
+	public static function get_domain($domain)
76
+	{
77
+		if (isset(self::config()->domains[$domain])) {
78
+			return MultiDomainDomain::create(
79
+				$domain,
80
+				self::config()->domains[$domain]
81
+			);
82
+		}
83
+	}
84 84
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -46,7 +46,7 @@
 block discarded – undo
46 46
      */
47 47
     public static function get_all_domains($includePrimary = false)
48 48
     {
49
-        $domains = array ();
49
+        $domains = array();
50 50
 
51 51
         foreach (self::config()->domains as $key => $config) {
52 52
             if (!$includePrimary && $key === self::KEY_PRIMARY) {
Please login to merge, or discard this patch.
code/MultiDomainDomain.php 2 patches
Indentation   +271 added lines, -271 removed lines patch added patch discarded remove patch
@@ -9,275 +9,275 @@
 block discarded – undo
9 9
 class MultiDomainDomain extends Object
10 10
 {
11 11
 
12
-    /**
13
-     * The hostname of the domain, e.g. silverstripe.org
14
-     * @var string
15
-     */
16
-    protected $hostname;
17
-
18
-    /**
19
-     * The path that the hostname resolves to
20
-     * @var string
21
-     */
22
-    protected $url;
23
-
24
-    /**
25
-     * The identifier of the domain, e.g. 'org','com'
26
-     * @var string
27
-     */
28
-    protected $key;
29
-
30
-    /**
31
-     * Paths that are allowed to be accessed on the primary domain
32
-     * @var array
33
-     */
34
-    protected $allowedPaths;
35
-
36
-    /**
37
-     * Paths that are forced from the primary domain into a vanity one,
38
-     * outside the resolves_to path
39
-     *
40
-     * @var array
41
-     */
42
-    protected $forcedPaths;
43
-
44
-    /**
45
-     * The request URI
46
-     *
47
-     * @var string
48
-     */
49
-    protected $requestUri;
50
-
51
-    /**
52
-     * The request HTTP HOST
53
-     *
54
-     * @var string
55
-     */
56
-    protected $httpHost;
57
-
58
-    /**
59
-     * Constructor. Takes a key for the domain and its array of settings from the config
60
-     * @param string $key
61
-     * @param array $config
62
-     */
63
-    public function __construct($key, $config)
64
-    {
65
-        $this->key = $key;
66
-        $this->hostname = $config['hostname'];
67
-        $this->url = isset($config['resolves_to']) ? $config['resolves_to'] : null;
68
-
69
-        $globalAllowed = (array) Config::inst()->get('MultiDomain', 'allow');
70
-        $globalForced = (array) Config::inst()->get('MultiDomain', 'force');
71
-        $myAllowed = isset($config['allow']) ? $config['allow'] : array ();
72
-        $myForced = isset($config['force']) ? $config['force'] : array ();
73
-        $this->allowedPaths = array_merge($globalAllowed, $myAllowed);
74
-        $this->forcedPaths = array_merge($globalForced, $myForced);
75
-
76
-        parent::__construct();
77
-    }
78
-
79
-    /**
80
-     * Gets the hostname for the domain
81
-     * @return string
82
-     */
83
-    public function getHostname()
84
-    {
85
-        return defined($this->hostname) ? constant($this->hostname) : $this->hostname;
86
-    }
87
-
88
-    /**
89
-     * Gets the path that the hostname resolves to
90
-     * @return string
91
-     */
92
-    public function getURL()
93
-    {
94
-        return $this->url;
95
-    }
96
-
97
-    /**
98
-     * Returns true if the domain is currently in the HTTP_HOST
99
-     * @return boolean
100
-     */
101
-    public function isActive()
102
-    {
103
-        if ($this->isAllowedPath($this->getRequestUri())) {
104
-            return false;
105
-        }
106
-
107
-        $currentHost = $this->getHttpHost();
108
-        if (strpos(':', $currentHost) !== false) {
109
-            list($currentHost, $currentPort) = explode(':', $currentHost, 2);
110
-        }
111
-
112
-        $allow_subdomains = MultiDomain::config()->allow_subdomains;
113
-        $hostname = $this->getHostname();
114
-
115
-        return $allow_subdomains ?
116
-                    (bool) preg_match('/(\.|^)'.$hostname.'$/', $currentHost) :
117
-                    ($currentHost == $hostname);
118
-    }
119
-
120
-    /**
121
-     * Returns true if this domain is the primary domain
122
-     * @return boolean
123
-     */
124
-    public function isPrimary()
125
-    {
126
-        return $this->key === MultiDomain::KEY_PRIMARY;
127
-    }
128
-
129
-    /**
130
-     * Gets the native URL for a vanity domain, e.g. /partners/ for .com
131
-     * returns /company/partners when .com is mapped to /company/.
132
-     *
133
-     * @param  string $url
134
-     * @return string
135
-     */
136
-    public function getNativeURL($url)
137
-    {
138
-        if ($this->isPrimary()) {
139
-            throw new Exception("Cannot convert a native URL on the primary domain");
140
-        }
141
-
142
-        if ($this->isAllowedPath($url) || $this->isForcedPath($url)) {
143
-            return $url;
144
-        }
145
-
146
-        return Controller::join_links($this->getURL(), $url);
147
-    }
148
-
149
-    /**
150
-     * Gets the vanity URL given a native URL. /company/partners returns /partners/
151
-     * when .com is mapped to /company/.
152
-     *
153
-     * @param  string $url
154
-     * @return string
155
-     */
156
-    public function getVanityURL($url)
157
-    {
158
-        if ($this->isPrimary() || $this->isAllowedPath($url)) {
159
-            return $url;
160
-        }
161
-
162
-        $domainUrl = str_replace('/', '\/', $this->getURL());
163
-        return preg_replace('/^\/?' . $domainUrl . '\//', '', $url);
164
-    }
165
-
166
-    /**
167
-     * Return true if this domain contains the given URL
168
-     * @param  string  $url
169
-     * @return boolean
170
-     */
171
-    public function hasURL($url)
172
-    {
173
-        if ($this->isForcedPath($url)) {
174
-            return true;
175
-        }
176
-        $domainBaseURL = trim($this->getURL(), '/');
177
-        if (preg_match('/^'.$domainBaseURL.'/', $url)) {
178
-            return true;
179
-        }
180
-
181
-        return false;
182
-    }
183
-
184
-    /**
185
-     * Returns the key/identifier for this domain
186
-     *
187
-     * @return string
188
-     */
189
-    public function getKey()
190
-    {
191
-        return $this->key;
192
-    }
193
-
194
-    /**
195
-     * Set the request URI
196
-     *
197
-     * @param  string $requestUri
198
-     * @return $this
199
-     */
200
-    public function setRequestUri($requestUri)
201
-    {
202
-        $this->requestUri = (string) $requestUri;
203
-        return $this;
204
-    }
205
-
206
-    /**
207
-     * Return the current request URI, defaulting to retrieving it from the $_SERVER superglobal
208
-     *
209
-     * @return string
210
-     */
211
-    public function getRequestUri()
212
-    {
213
-        return $this->requestUri ?: $_SERVER['REQUEST_URI'];
214
-    }
215
-
216
-    /**
217
-     * Set the HTTP host in the request
218
-     *
219
-     * @param  string $httpHost
220
-     * @return $this
221
-     */
222
-    public function setHttpHost($httpHost)
223
-    {
224
-        $this->httpHost = (string) $httpHost;
225
-        return $this;
226
-    }
227
-
228
-    /**
229
-     * Return the current HTTP host, defaulting to retrieving it from the $_SERVER superglobal
230
-     *
231
-     * @return string
232
-     */
233
-    public function getHttpHost()
234
-    {
235
-        return $this->httpHost ?: $_SERVER['HTTP_HOST'];
236
-    }
237
-
238
-    /**
239
-     * Checks a given list of wildcard patterns to see if a path is allowed
240
-     * @param  string  $url
241
-     * @return boolean
242
-     */
243
-    protected function isAllowedPath($url)
244
-    {
245
-        return self::match_url($url, $this->allowedPaths);
246
-    }
247
-
248
-    /**
249
-     * Checks a given list of wildcard patterns to see if a path is allowed
250
-     * @param  string  $url
251
-     * @return boolean
252
-     */
253
-    protected function isForcedPath($url)
254
-    {
255
-        return self::match_url($url, $this->forcedPaths);
256
-    }
257
-
258
-    /**
259
-     * Matches a URL against a list of wildcard patterns
260
-     * @param  string $url
261
-     * @param  array $patterns
262
-     * @return boolean
263
-     */
264
-    protected static function match_url($url, $patterns)
265
-    {
266
-        if (!is_array($patterns)) {
267
-            return false;
268
-        }
269
-
270
-        $url = ltrim($url, '/');
271
-        if (substr($url, -1) !== '/') {
272
-            $url .= '/';
273
-        }
274
-
275
-        foreach ($patterns as $pattern) {
276
-            if (fnmatch($pattern, $url)) {
277
-                return true;
278
-            }
279
-        }
280
-
281
-        return false;
282
-    }
12
+	/**
13
+	 * The hostname of the domain, e.g. silverstripe.org
14
+	 * @var string
15
+	 */
16
+	protected $hostname;
17
+
18
+	/**
19
+	 * The path that the hostname resolves to
20
+	 * @var string
21
+	 */
22
+	protected $url;
23
+
24
+	/**
25
+	 * The identifier of the domain, e.g. 'org','com'
26
+	 * @var string
27
+	 */
28
+	protected $key;
29
+
30
+	/**
31
+	 * Paths that are allowed to be accessed on the primary domain
32
+	 * @var array
33
+	 */
34
+	protected $allowedPaths;
35
+
36
+	/**
37
+	 * Paths that are forced from the primary domain into a vanity one,
38
+	 * outside the resolves_to path
39
+	 *
40
+	 * @var array
41
+	 */
42
+	protected $forcedPaths;
43
+
44
+	/**
45
+	 * The request URI
46
+	 *
47
+	 * @var string
48
+	 */
49
+	protected $requestUri;
50
+
51
+	/**
52
+	 * The request HTTP HOST
53
+	 *
54
+	 * @var string
55
+	 */
56
+	protected $httpHost;
57
+
58
+	/**
59
+	 * Constructor. Takes a key for the domain and its array of settings from the config
60
+	 * @param string $key
61
+	 * @param array $config
62
+	 */
63
+	public function __construct($key, $config)
64
+	{
65
+		$this->key = $key;
66
+		$this->hostname = $config['hostname'];
67
+		$this->url = isset($config['resolves_to']) ? $config['resolves_to'] : null;
68
+
69
+		$globalAllowed = (array) Config::inst()->get('MultiDomain', 'allow');
70
+		$globalForced = (array) Config::inst()->get('MultiDomain', 'force');
71
+		$myAllowed = isset($config['allow']) ? $config['allow'] : array ();
72
+		$myForced = isset($config['force']) ? $config['force'] : array ();
73
+		$this->allowedPaths = array_merge($globalAllowed, $myAllowed);
74
+		$this->forcedPaths = array_merge($globalForced, $myForced);
75
+
76
+		parent::__construct();
77
+	}
78
+
79
+	/**
80
+	 * Gets the hostname for the domain
81
+	 * @return string
82
+	 */
83
+	public function getHostname()
84
+	{
85
+		return defined($this->hostname) ? constant($this->hostname) : $this->hostname;
86
+	}
87
+
88
+	/**
89
+	 * Gets the path that the hostname resolves to
90
+	 * @return string
91
+	 */
92
+	public function getURL()
93
+	{
94
+		return $this->url;
95
+	}
96
+
97
+	/**
98
+	 * Returns true if the domain is currently in the HTTP_HOST
99
+	 * @return boolean
100
+	 */
101
+	public function isActive()
102
+	{
103
+		if ($this->isAllowedPath($this->getRequestUri())) {
104
+			return false;
105
+		}
106
+
107
+		$currentHost = $this->getHttpHost();
108
+		if (strpos(':', $currentHost) !== false) {
109
+			list($currentHost, $currentPort) = explode(':', $currentHost, 2);
110
+		}
111
+
112
+		$allow_subdomains = MultiDomain::config()->allow_subdomains;
113
+		$hostname = $this->getHostname();
114
+
115
+		return $allow_subdomains ?
116
+					(bool) preg_match('/(\.|^)'.$hostname.'$/', $currentHost) :
117
+					($currentHost == $hostname);
118
+	}
119
+
120
+	/**
121
+	 * Returns true if this domain is the primary domain
122
+	 * @return boolean
123
+	 */
124
+	public function isPrimary()
125
+	{
126
+		return $this->key === MultiDomain::KEY_PRIMARY;
127
+	}
128
+
129
+	/**
130
+	 * Gets the native URL for a vanity domain, e.g. /partners/ for .com
131
+	 * returns /company/partners when .com is mapped to /company/.
132
+	 *
133
+	 * @param  string $url
134
+	 * @return string
135
+	 */
136
+	public function getNativeURL($url)
137
+	{
138
+		if ($this->isPrimary()) {
139
+			throw new Exception("Cannot convert a native URL on the primary domain");
140
+		}
141
+
142
+		if ($this->isAllowedPath($url) || $this->isForcedPath($url)) {
143
+			return $url;
144
+		}
145
+
146
+		return Controller::join_links($this->getURL(), $url);
147
+	}
148
+
149
+	/**
150
+	 * Gets the vanity URL given a native URL. /company/partners returns /partners/
151
+	 * when .com is mapped to /company/.
152
+	 *
153
+	 * @param  string $url
154
+	 * @return string
155
+	 */
156
+	public function getVanityURL($url)
157
+	{
158
+		if ($this->isPrimary() || $this->isAllowedPath($url)) {
159
+			return $url;
160
+		}
161
+
162
+		$domainUrl = str_replace('/', '\/', $this->getURL());
163
+		return preg_replace('/^\/?' . $domainUrl . '\//', '', $url);
164
+	}
165
+
166
+	/**
167
+	 * Return true if this domain contains the given URL
168
+	 * @param  string  $url
169
+	 * @return boolean
170
+	 */
171
+	public function hasURL($url)
172
+	{
173
+		if ($this->isForcedPath($url)) {
174
+			return true;
175
+		}
176
+		$domainBaseURL = trim($this->getURL(), '/');
177
+		if (preg_match('/^'.$domainBaseURL.'/', $url)) {
178
+			return true;
179
+		}
180
+
181
+		return false;
182
+	}
183
+
184
+	/**
185
+	 * Returns the key/identifier for this domain
186
+	 *
187
+	 * @return string
188
+	 */
189
+	public function getKey()
190
+	{
191
+		return $this->key;
192
+	}
193
+
194
+	/**
195
+	 * Set the request URI
196
+	 *
197
+	 * @param  string $requestUri
198
+	 * @return $this
199
+	 */
200
+	public function setRequestUri($requestUri)
201
+	{
202
+		$this->requestUri = (string) $requestUri;
203
+		return $this;
204
+	}
205
+
206
+	/**
207
+	 * Return the current request URI, defaulting to retrieving it from the $_SERVER superglobal
208
+	 *
209
+	 * @return string
210
+	 */
211
+	public function getRequestUri()
212
+	{
213
+		return $this->requestUri ?: $_SERVER['REQUEST_URI'];
214
+	}
215
+
216
+	/**
217
+	 * Set the HTTP host in the request
218
+	 *
219
+	 * @param  string $httpHost
220
+	 * @return $this
221
+	 */
222
+	public function setHttpHost($httpHost)
223
+	{
224
+		$this->httpHost = (string) $httpHost;
225
+		return $this;
226
+	}
227
+
228
+	/**
229
+	 * Return the current HTTP host, defaulting to retrieving it from the $_SERVER superglobal
230
+	 *
231
+	 * @return string
232
+	 */
233
+	public function getHttpHost()
234
+	{
235
+		return $this->httpHost ?: $_SERVER['HTTP_HOST'];
236
+	}
237
+
238
+	/**
239
+	 * Checks a given list of wildcard patterns to see if a path is allowed
240
+	 * @param  string  $url
241
+	 * @return boolean
242
+	 */
243
+	protected function isAllowedPath($url)
244
+	{
245
+		return self::match_url($url, $this->allowedPaths);
246
+	}
247
+
248
+	/**
249
+	 * Checks a given list of wildcard patterns to see if a path is allowed
250
+	 * @param  string  $url
251
+	 * @return boolean
252
+	 */
253
+	protected function isForcedPath($url)
254
+	{
255
+		return self::match_url($url, $this->forcedPaths);
256
+	}
257
+
258
+	/**
259
+	 * Matches a URL against a list of wildcard patterns
260
+	 * @param  string $url
261
+	 * @param  array $patterns
262
+	 * @return boolean
263
+	 */
264
+	protected static function match_url($url, $patterns)
265
+	{
266
+		if (!is_array($patterns)) {
267
+			return false;
268
+		}
269
+
270
+		$url = ltrim($url, '/');
271
+		if (substr($url, -1) !== '/') {
272
+			$url .= '/';
273
+		}
274
+
275
+		foreach ($patterns as $pattern) {
276
+			if (fnmatch($pattern, $url)) {
277
+				return true;
278
+			}
279
+		}
280
+
281
+		return false;
282
+	}
283 283
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -5 removed lines patch added patch discarded remove patch
@@ -68,8 +68,8 @@  discard block
 block discarded – undo
68 68
 
69 69
         $globalAllowed = (array) Config::inst()->get('MultiDomain', 'allow');
70 70
         $globalForced = (array) Config::inst()->get('MultiDomain', 'force');
71
-        $myAllowed = isset($config['allow']) ? $config['allow'] : array ();
72
-        $myForced = isset($config['force']) ? $config['force'] : array ();
71
+        $myAllowed = isset($config['allow']) ? $config['allow'] : array();
72
+        $myForced = isset($config['force']) ? $config['force'] : array();
73 73
         $this->allowedPaths = array_merge($globalAllowed, $myAllowed);
74 74
         $this->forcedPaths = array_merge($globalForced, $myForced);
75 75
 
@@ -113,8 +113,7 @@  discard block
 block discarded – undo
113 113
         $hostname = $this->getHostname();
114 114
 
115 115
         return $allow_subdomains ?
116
-                    (bool) preg_match('/(\.|^)'.$hostname.'$/', $currentHost) :
117
-                    ($currentHost == $hostname);
116
+                    (bool) preg_match('/(\.|^)'.$hostname.'$/', $currentHost) : ($currentHost == $hostname);
118 117
     }
119 118
 
120 119
     /**
@@ -160,7 +159,7 @@  discard block
 block discarded – undo
160 159
         }
161 160
 
162 161
         $domainUrl = str_replace('/', '\/', $this->getURL());
163
-        return preg_replace('/^\/?' . $domainUrl . '\//', '', $url);
162
+        return preg_replace('/^\/?'.$domainUrl.'\//', '', $url);
164 163
     }
165 164
 
166 165
     /**
Please login to merge, or discard this patch.