Skip to content

Commit

Permalink
Merge pull request #10 from robbieaverill/feature/extension-in-get-se…
Browse files Browse the repository at this point in the history
…rver-name

Feature/extension in get server name
  • Loading branch information
zanderwar authored Dec 5, 2016
2 parents 9b3ccd2 + 34c1d1e commit 009bec6
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 35 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# For more information about the properties used in
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_size = 2
57 changes: 36 additions & 21 deletions code/CloudFlare.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class CloudFlare
class CloudFlare extends Object
{
/**
* @var string
Expand All @@ -16,17 +16,12 @@ class CloudFlare
protected static $ready = false;

/**
* @var \CloudFlare
*/
protected static $singleton;

/**
* Instance
* Get a singleton instance. Use the default Object functionality
* @return \CloudFlare
*/
public static function inst()
{
return (is_object(static::$singleton)) ? static::$singleton : static::$singleton = new CloudFlare();
return self::singleton();
}

/**
Expand Down Expand Up @@ -301,6 +296,38 @@ public function purgeImages()

}

/**
* Gathers the current server name, which will be used as the CloudFlare zone ID
*
* @return string
*/
public function getServerName()
{
$serverName = '';
if (!empty($_SERVER['SERVER_NAME'])) {
$server = \Convert::raw2xml($_SERVER); // "Fixes" #1
$serverName = $server['SERVER_NAME'];
}

// CI support
if (getenv('TRAVIS')) {
$serverName = getenv('CLOUDFLARE_DUMMY_SITE');
}

// Remove protocols, etc
$replaceWith = array(
'www.' => '',
'http://' => '',
'https://' => ''
);
$serverName = str_replace(array_keys($replaceWith), array_values($replaceWith), $serverName);

// Allow extensions to modify or replace the server name if required
$this->extend('updateCloudFlareServerName', $serverName);

return $serverName;
}

/**
* Gets the CF Zone ID for the current domain.
*
Expand All @@ -316,19 +343,7 @@ public function fetchZoneID()
return $cache;
}

$replaceWith = array(
"www." => "",
"http://" => "",
"https://" => ""
);

$server = Convert::raw2xml($_SERVER); // "Fixes" #1

$serverName = str_replace(array_keys($replaceWith), array_values($replaceWith), $server[ 'SERVER_NAME' ]);

if (getenv('TRAVIS')) {
$serverName = getenv('CLOUDFLARE_DUMMY_SITE');
}
$serverName = $this->getServerName();

if ($serverName == 'localhost') {
$this->setAlert(
Expand Down
61 changes: 47 additions & 14 deletions tests/CloudFlareTest.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,62 @@
<?php

/**
* @todo
* Class CloudFlareTest
*
* @todo
* @coversDefaultClass CloudFlare
*/
class CloudFlareTest extends SapphireTest {

class CloudFlareTest extends SapphireTest
{
/**
* Tests CloudFlare::inst()->getUrlVariants()
* @covers ::getUrlVariants
*/
public function testGetUrlVariants() {
public function testGetUrlVariants()
{
$urls = array(
"http://www.example.com",
'http://www.example.com',
);

$this->assertEquals(
CloudFlare::inst()->getUrlVariants($urls),
array(
"http://www.example.com",
"https://www.example.com",
"http://www.example.com?stage=Stage",
"https://www.example.com?stage=Stage"
'http://www.example.com',
'https://www.example.com',
'http://www.example.com?stage=Stage',
'https://www.example.com?stage=Stage'
)
);
}


}
/**
* Ensures that the server name can be retrieved as expected from environment variables or an extension attached
* @covers ::getServerName
*/
public function testGetServerName()
{
// Ensures the CI environment can be factored in
putenv('TRAVIS=1');
putenv('CLOUDFLARE_DUMMY_SITE=https://www.sometest.dev');
$this->assertSame('sometest.dev', CloudFlare::inst()->getServerName());

// Apply a test extension, get a new instance of the CF class and test again to ensure the hook works
CloudFlare::add_extension('CloudFlareTest_Extension');
$this->assertSame('extended.dev', CloudFlare::create()->getServerName());
}
}

/**
* A stub extension applied to CloudFlare as needed to test extension hooks
*/
class CloudFlareTest_Extension extends Extension implements TestOnly
{
/**
* Set a dummy server name
*
* @see CloudFlare::getServerName
* @var string $serverName
*/
public function updateCloudFlareServerName(&$serverName)
{
$serverName = 'extended.dev';
}
}

0 comments on commit 009bec6

Please sign in to comment.