DrupalApplication.php in CMS Content Sync 2.0.x
File
src/SyncCoreInterface/DrupalApplication.php
View source
<?php
namespace Drupal\cms_content_sync\SyncCoreInterface;
use Drupal\cms_content_sync\Controller\AuthenticationByUser;
use Drupal\cms_content_sync\Controller\ContentSyncSettings;
use Drupal\Core\Cache\Cache;
use EdgeBox\SyncCore\Interfaces\Embed\IEmbedService;
use EdgeBox\SyncCore\Interfaces\IApplicationInterface;
class DrupalApplication implements IApplicationInterface {
public const APPLICATION_ID = 'drupal';
protected static $instance;
public static function get() {
if (!empty(self::$instance)) {
return self::$instance;
}
return self::$instance = new DrupalApplication();
}
public function getSiteBaseUrl() {
return ContentSyncSettings::getInstance()
->getSiteBaseUrl();
}
public function getAuthentication() {
$type = ContentSyncSettings::getInstance()
->getAuthenticationType();
$authentication_provider = AuthenticationByUser::getInstance();
return [
'type' => $type,
'username' => $authentication_provider
->getUsername(),
'password' => $authentication_provider
->getPassword(),
];
}
public function getRelativeReferenceForRestCall(string $flow_machine_name, string $action) {
$url = '/rest/cms-content-sync/v2/' . $flow_machine_name;
if (IApplicationInterface::REST_ACTION_LIST_ENTITIES !== $action) {
$url .= '/[type.namespaceMachineName]/[type.machineName]';
$url .= IApplicationInterface::REST_ACTION_RETRIEVE_ENTITY === $action ? '/[entity.uuid]' : '/[entity.sharedId]';
}
$url .= '?_format=json';
return $url;
}
public function getEmbedBaseUrl(string $feature) {
$export_url = $this
->getSiteBaseUrl();
$name = IEmbedService::REGISTER_SITE === $feature || IEmbedService::SITE_REGISTERED === $feature || IEmbedService::MIGRATE === $feature ? 'site' : $feature;
$url = sprintf('%s/admin/config/services/cms_content_sync/%s', $export_url, $name);
if (IEmbedService::REGISTER_SITE === $feature) {
$url .= '?force=' . IEmbedService::REGISTER_SITE;
}
elseif (IEmbedService::MIGRATE === $feature) {
$url .= '?force=' . IEmbedService::MIGRATE;
}
return $url;
}
public function setSyncCoreUrl(string $set) {
ContentSyncSettings::getInstance()
->setSyncCoreUrl($set);
}
public function getSyncCoreUrl() {
return ContentSyncSettings::getInstance()
->getSyncCoreUrl();
}
public function getRestUrl($pool_id, $type_machine_name, $bundle_machine_name, $version_id, $entity_uuid = null, $manually = null, $as_dependency = null) {
$export_url = $this
->getSiteBaseUrl();
$url = sprintf('%s/rest/cms-content-sync/%s/%s/%s/%s', $export_url, $pool_id, $type_machine_name, $bundle_machine_name, $version_id);
if ($entity_uuid) {
$url .= '/' . $entity_uuid;
}
$url .= '?_format=json';
if ($as_dependency) {
$url .= '&is_dependency=' . $as_dependency;
}
if ($manually) {
$url .= '&is_manual=' . $manually;
}
return $url;
}
public function getSiteName() {
$name = getenv('CONTENT_SYNC_SITE_NAME');
if ($name) {
return $name;
}
try {
return ContentSyncSettings::getInstance()
->getSiteName();
} catch (\Exception $e) {
return \Drupal::config('system.site')
->get('name');
}
}
public function setSiteId($set) {
ContentSyncSettings::getInstance()
->setSiteId($set);
}
public function getSiteId() {
return ContentSyncSettings::getInstance()
->getSiteMachineName();
}
public function getSiteMachineName() {
return ContentSyncSettings::getInstance()
->getSiteMachineName();
}
public function setSiteMachineName($set) {
ContentSyncSettings::getInstance()
->setSiteMachineName($set);
}
public function setSiteUuid(string $set) {
ContentSyncSettings::getInstance()
->setSiteUuid($set);
$bins = Cache::getBins();
$bins['render']
->deleteAll();
$bins['discovery']
->deleteAll();
}
public function getSiteUuid() {
return ContentSyncSettings::getInstance()
->getSiteUuid();
}
public function getApplicationId() {
return self::APPLICATION_ID;
}
public function getApplicationVersion() {
return \Drupal::VERSION;
}
public function getApplicationModuleVersion() {
$version = \Drupal::service('extension.list.module')
->getExtensionInfo('cms_content_sync')['version'];
return $version ? $version : 'dev';
}
public function getHttpClient() {
return \Drupal::httpClient();
}
public function getHttpOptions() {
$options = [];
global $config;
$config_name = 'cms_content_sync.sync_core_request_timeout';
if (!empty($config[$config_name]) && is_int($config[$config_name])) {
$options['timeout'] = $config[$config_name];
}
return $options;
}
const FEATURE_COUNT_ENTITIES = 'count-entities';
public function getFeatureFlags() {
return [
self::FEATURE_COUNT_ENTITIES => 1,
];
}
}