2012-01-20 18:10:48 +01:00
|
|
|
<?php
|
2015-09-10 23:13:48 +02:00
|
|
|
|
2017-01-04 23:16:12 +01:00
|
|
|
namespace SilverStripe\EnvironmentCheck\Checks;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
|
|
|
|
|
2012-01-20 18:10:48 +01:00
|
|
|
/**
|
|
|
|
* Check that a given URL is functioning, by default, the homepage.
|
2017-01-04 23:16:12 +01:00
|
|
|
*
|
2012-01-20 18:10:48 +01:00
|
|
|
* Note that Director::test() will be used rather than a CURL check.
|
2017-01-04 23:16:12 +01:00
|
|
|
*
|
|
|
|
* @package environmentcheck
|
2012-01-20 18:10:48 +01:00
|
|
|
*/
|
2015-11-21 07:18:35 +01:00
|
|
|
class URLCheck implements EnvironmentCheck
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $url;
|
2015-09-10 23:13:48 +02:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $testString;
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2017-03-21 11:57:07 +01:00
|
|
|
/**
|
2015-11-21 07:18:35 +01:00
|
|
|
* @param string $url The URL to check, relative to the site (homepage is '').
|
|
|
|
* @param string $testString An optional piece of text to search for on the homepage.
|
|
|
|
*/
|
|
|
|
public function __construct($url = '', $testString = '')
|
|
|
|
{
|
|
|
|
$this->url = $url;
|
|
|
|
$this->testString = $testString;
|
|
|
|
}
|
2015-09-10 23:13:48 +02:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
2017-01-04 23:16:12 +01:00
|
|
|
* {@inheritDoc}
|
2015-11-21 07:18:35 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2017-01-04 23:16:12 +01:00
|
|
|
* @throws HTTPResponse_Exception
|
2015-11-21 07:18:35 +01:00
|
|
|
*/
|
|
|
|
public function check()
|
|
|
|
{
|
|
|
|
$response = Director::test($this->url);
|
2012-01-20 18:10:48 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
if ($response->getStatusCode() != 200) {
|
2017-03-21 11:57:07 +01:00
|
|
|
return [
|
2015-11-21 07:18:35 +01:00
|
|
|
EnvironmentCheck::ERROR,
|
|
|
|
sprintf('Error retrieving "%s" (Code: %d)', $this->url, $response->getStatusCode())
|
2017-03-21 11:57:07 +01:00
|
|
|
];
|
2015-11-21 07:18:35 +01:00
|
|
|
} elseif ($this->testString && (strpos($response->getBody(), $this->testString) === false)) {
|
2017-03-21 11:57:07 +01:00
|
|
|
return [
|
2015-11-21 07:18:35 +01:00
|
|
|
EnvironmentCheck::WARNING,
|
|
|
|
sprintf('Success retrieving "%s", but string "%s" not found', $this->url, $this->testString)
|
2017-03-21 11:57:07 +01:00
|
|
|
];
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2017-03-21 11:57:07 +01:00
|
|
|
return [
|
2017-01-04 23:16:12 +01:00
|
|
|
EnvironmentCheck::OK,
|
|
|
|
sprintf('Success retrieving "%s"', $this->url)
|
2017-03-21 11:57:07 +01:00
|
|
|
];
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2015-09-10 23:13:48 +02:00
|
|
|
}
|