public function Vcl::execute in Fastly 7.2
Main execute function, takes values inserted into constructor, builds requests and sends them via Fastly API.
@activate bool
Return value
bool
File
- ./
fastly.vcl.inc, line 116
Class
- Vcl
- Class to control the VCL handling.
Code
public function execute($activate = FALSE) {
// Check if last version is fetched.
if ($this->_last_version_data === FALSE) {
$this
->add_error(t('Last version does not exist'));
return FALSE;
}
// Check if any of the data is set.
if (empty($this->_vcl_data) && empty($this->_condition_data) && empty($this->_setting_data) && empty($this->_response_object_data)) {
$this
->add_error(t('No update data set, please specify, vcl, condition or setting data'));
return FALSE;
}
try {
if (FALSE === $this
->clone_last_active_version()) {
$this
->add_error(t('Unable to clone last version'));
return FALSE;
}
$requests = array();
if (!empty($this->_vcl_data)) {
$requests = array_merge($requests, $this
->prepare_vcl());
}
if (!empty($this->_condition_data)) {
$conditions = $this
->prepare_condition();
if (FALSE === $conditions) {
$this
->add_error(t('Unable to insert new condition'));
return FALSE;
}
$requests = array_merge($requests, $conditions);
}
if (!empty($this->_setting_data)) {
$requests = array_merge($requests, $this
->prepare_setting());
}
if (!empty($this->_response_object_data)) {
$requests = array_merge($requests, $this
->prepare_response_object());
}
if (!$this
->validate_version()) {
$this
->add_error(t('Version not validated'));
return FALSE;
}
// Set Request Headers.
foreach ($requests as $key => $request) {
if (in_array($request['type'], array(
"POST",
"PUT",
))) {
$requests[$key]['headers'] = $this->_headers_post;
}
else {
$requests[$key]['headers'] = $this->_headers_get;
}
}
// Send Requests.
$responses = [];
foreach ($requests as $key => $value) {
if (!isset($value['type'])) {
continue;
}
$url = $value['url'];
$data = $value['data'];
$type = $value['type'];
$headers = $value['headers'];
$response = $this->_api
->makeRequest($url, $data, $type, $headers);
$responses[] = $response;
}
$pass = TRUE;
foreach ($responses as $response) {
if ($response->code != 404) {
$success = TRUE;
}
else {
$success = FALSE;
}
if (!$success) {
$pass = FALSE;
$this
->add_error(t('Some of the API requests failed, enable debugging and check logs for more information.'));
$this
->add_error(t('VCL update failed : ' . $response->data));
}
}
// Activate version if vcl is successfully uploaded.
if ($pass && $activate) {
$request = $this
->prepare_activate_version();
$response = $this->_api
->makeRequest($request['url'], array(), $request['type'], $request['headers']);
if (!$response->code == 200) {
$pass = FALSE;
$this
->add_error(t('Some of the API requests failed, enable debugging and check logs for more information.'));
$this
->add_error(t('Activation of new version failed : ' . $response->data));
}
else {
$this
->add_message(t('VCL updated, version activated : ' . $this->_last_cloned_version));
}
}
elseif ($pass && !$activate) {
$this
->add_message(t('VCL updated, but not activated.'));
}
} catch (Exception $e) {
$this
->add_error(t('Some of the API requests failed, enable debugging and check logs for more information.'));
$this
->add_error(t('VCL update failed : ' . $e
->getMessage()));
return FALSE;
}
return $pass;
}