View source
<?php
function pwa_requirements($phase) {
$requirements = [];
if ($phase !== 'runtime') {
return $requirements;
}
$t = get_t();
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' || isset($_SERVER["REQUEST_SCHEME"]) && $_SERVER["REQUEST_SCHEME"] === 'https') {
$requirements['pwa'] = array(
'title' => $t('Progressive Web App'),
'value' => $t('HTTPS on'),
'severity' => REQUIREMENT_OK,
'description' => $t('Please make sure the certificate of %domain is valid for offline functionality to work.', [
'%domain' => $_SERVER['HTTP_HOST'],
]),
);
}
elseif (in_array($_SERVER['HTTP_HOST'], [
'localhost',
'127.0.0.1',
])) {
$requirements['pwa'] = array(
'title' => $t('Progressive Web App'),
'value' => 'localhost',
'severity' => REQUIREMENT_WARNING,
'description' => $t('You will need to configure HTTPS on your production site for the Progressive Web App to function.'),
);
}
else {
$requirements['pwa'] = array(
'title' => $t('Progressive Web App'),
'value' => $t('HTTPS off'),
'severity' => REQUIREMENT_ERROR,
'description' => $t('HTTPS needs to be configured to enable your Progressive Web App. Without a secure connection, the Service Worker will not install itself.'),
);
}
return $requirements;
}
function pwa_install() {
$directory = file_default_scheme() . '://pwa';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
variable_set('pwa_sw_cache_exclude', implode("\n", PWA_SW_CACHE_EXCLUDE));
variable_set('pwa_sw_cache_urls', implode("\n", [
'/',
'/offline',
variable_get('pwa_start_url', ''),
]));
pwa_flush_caches();
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, [
'access pwa',
]);
}
function pwa_uninstall() {
db_delete('variable')
->condition('name', 'pwa_%', 'LIKE')
->execute();
cache_clear_all('variables', 'cache_bootstrap');
$scheme = file_default_scheme();
$directory = $scheme . '://pwa';
$path = $directory . '/manifest.json';
file_unmanaged_delete($path);
}