View source
<?php
namespace Drupal\warden\Service;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\Extension\InfoParser;
use WardenApi\Api;
use Drupal\Core\Extension\Extension;
class WardenManager extends Api {
protected $infoParser;
protected $siteName;
protected $baseUrl;
protected $coreVersion;
protected $themes;
protected $modules;
protected $libraries;
protected $includeContrib;
protected $includeCustom;
protected $includeLibrary;
protected $customLibraries;
protected $customModuleRegex;
protected $contribModuleRegex;
protected $localToken;
protected $time;
public function __construct(ConfigFactory $configFactory) {
$warden_settings = $configFactory
->get('warden.settings');
$this->siteName = $configFactory
->get('system.site')
->get('name');
$this->contribModuleRegex = $warden_settings
->get('warden_preg_match_contrib');
$this->customModuleRegex = $warden_settings
->get('warden_preg_match_custom');
$this->includeCustom = $warden_settings
->get('warden_match_custom');
$this->includeContrib = $warden_settings
->get('warden_match_contrib');
$this->includeLibrary = $warden_settings
->get('warden_match_library');
$this->customLibraries = $warden_settings
->get('warden_list_libraries');
$local_token = $warden_settings
->get('warden_token');
if (!empty($local_token)) {
$this->localToken = $local_token;
}
$this->coreVersion = \Drupal::VERSION;
parent::__construct($warden_settings
->get('warden_server_host_path'), $warden_settings
->get('warden_http_username'), $warden_settings
->get('warden_http_password'), $warden_settings
->get('warden_certificate_path'));
}
public function generateSiteData() {
$result = [
'core' => [
'drupal' => [
'version' => $this
->getCoreVersion(),
],
],
'contrib' => [],
'custom' => [],
'library' => [],
'url' => $this
->getBaseUrl(),
'site_name' => $this
->getSiteName(),
'key' => $this
->getLocalToken(),
'time' => $this
->getTime(),
];
if (!$this
->includeCustomModules()) {
$result['custom'] = 'disabled';
}
if (!$this
->includeContribModules()) {
$result['contrib'] = 'disabled';
}
if (!$this
->includeLibrary()) {
$result['library'] = 'disabled';
}
if ($this
->includeContribModules()) {
foreach ($this
->getThemes() as $theme) {
if (isset($theme->info['package']) && $theme->info['package'] == 'Core') {
continue;
}
if (isset($theme->info['version'])) {
$result['contrib'][$theme->name] = [
'version' => $theme->info['version'],
];
}
}
}
foreach ($this
->getModules() as $module => $module_info) {
$filename = $module_info
->getPathname();
if ($this
->includeCustomModules() && preg_match($this
->getCustomRegex(), $filename)) {
$result['custom'] = array_merge($result['custom'], $this
->getModuleDetails($module, $filename));
}
if ($this
->includeContribModules() && preg_match($this
->getContribRegex(), $filename)) {
$result['contrib'] = array_merge($result['contrib'], $this
->getModuleDetails($module, $filename));
}
}
if ($this
->includeLibrary()) {
if (isset($this->customLibraries)) {
$result['library'] = $this->customLibraries;
}
else {
foreach ($this
->getLibraries() as $library_uri => $library_info) {
$result['library'] = array_merge($result['library'], $this
->getLibraryDetails($library_uri));
}
}
}
return $result;
}
public function updateWarden() {
$data = $this
->generateSiteData();
$this
->postSiteData($data);
}
public function getLocalToken() {
if (!isset($this->localToken)) {
$this->localToken = $this
->generateNewLocalToken();
}
return $this->localToken;
}
protected function generateNewLocalToken() {
$local_token = hash('sha256', mt_rand());
\Drupal::configFactory()
->getEditable('warden.settings')
->set('warden_token', $local_token)
->save();
return $local_token;
}
protected function getModuleDetails($module, $filename) {
$details = $this
->getInfoParser()
->parse($filename);
if (!empty($details['project'])) {
return [
$details['project'] => [
'version' => isset($details['version']) ? $details['version'] : '',
],
];
}
else {
return [
$module => [
'version' => isset($details['version']) ? $details['version'] : '',
],
];
}
}
protected function getLibraryDetails($filename) {
$details = Json::decode(file_get_contents($filename));
return [
$details['name'] => isset($details['version']) ? $details['version'] : '',
];
}
protected function getSiteName() {
return $this->siteName;
}
protected function getCoreVersion() {
return $this->coreVersion;
}
protected function getContribRegex() {
return $this->contribModuleRegex;
}
protected function getCustomRegex() {
return $this->customModuleRegex;
}
protected function includeCustomModules() {
return $this->includeCustom;
}
protected function includeContribModules() {
return $this->includeContrib;
}
protected function includeLibrary() {
return $this->includeLibrary;
}
public function setThemes(array $themes) {
$this->themes = $themes;
return $this;
}
protected function getThemes() {
if (!isset($this->themes)) {
$this
->setThemes(system_list('theme'));
}
return $this->themes;
}
public function setModules(array $modules) {
$this->modules = $modules;
return $this;
}
protected function getModules() {
if (!isset($this->modules)) {
$listing = new ExtensionDiscovery(\Drupal::root());
$this
->setModules($listing
->scan('module'));
}
return $this->modules;
}
public function setLibraries(array $libraries) {
$this->libraries = $libraries;
return $this;
}
protected function getLibraries() {
if (!isset($this->libraries)) {
$libraryDir = \Drupal::root() . '/libraries';
if (file_exists($libraryDir)) {
$listing = file_scan_directory($libraryDir, '/^package.json/');
$this
->setLibraries($listing);
}
}
return $this->libraries;
}
protected function getInfoParser() {
if (empty($this->infoParser)) {
$this
->setInfoParser(new InfoParser());
}
return $this->infoParser;
}
public function setInfoParser(InfoParser $infoParser) {
$this->infoParser = $infoParser;
return $this;
}
protected function getBaseUrl() {
if (!isset($this->baseUrl)) {
global $base_url;
$this
->setBaseUrl($base_url);
}
return $this->baseUrl;
}
public function setBaseUrl($baseUrl) {
$this->baseUrl = $baseUrl;
return $this;
}
protected function getTime() {
if (!isset($this->time)) {
$this
->setTime(time());
}
return $this->time;
}
public function setTime($time) {
$this->time = $time;
return $this;
}
}