de.forumzfd.twinglecampaign/CRM/TwingleCampaign/Utils/ExtensionCache.php
2020-10-29 21:19:01 +01:00

99 lines
No EOL
2.1 KiB
PHP

<?php
namespace CRM\TwingleCampaign\Utils;
use Civi;
use CRM\TwingleCampaign\BAO\CustomField as CustomField;
use CRM_TwingleCampaign_ExtensionUtil as E;
use Exception;
/**
* A singleton that caches mappings and settings
*
* @package CRM\TwingleCampaign\Utilities
*/
class ExtensionCache {
protected static $_instance = NULL;
private $customFieldMapping;
private $translations;
private $campaigns;
private $templates;
public static function getInstance() {
if (null === self::$_instance) {
self::$_instance = new self;
}
return self::$_instance;
}
protected function __construct() {
// Get a mapping of custom fields
$this->customFieldMapping = CustomField::getMapping();
// Initialize json files as arrays
$file_paths = [
'translations' => '/CRM/TwingleCampaign/resources/dictionary.json',
'templates' => '/CRM/TwingleCampaign/resources/twingle_api_templates.json',
'campaigns' => '/CRM/TwingleCampaign/resources/campaigns.json',
];
foreach ($file_paths as $key => $file_path) {
// Get array from json file
$file_path = E::path() . $file_path;
$json_file = file_get_contents($file_path);
$json_file_name = pathinfo($file_path)['filename'];
$array = json_decode($json_file, TRUE);
// Throw and log an error if json file can't be read
if (!$array) {
$message = ($json_file_name)
? "Could not read json file $json_file_name"
: "Could not locate json file in path: $file_path";
Civi::log()->error($message);
throw new Exception($message);
}
// Set attribute
$this->$key = $array;
}
}
protected function __clone() {}
/**
* @return array
*/
public function getCustomFieldMapping(): array {
return $this->customFieldMapping;
}
/**
* @return mixed
*/
public function getTranslations() {
return $this->translations;
}
/**
* @return mixed
*/
public function getCampaigns() {
return $this->campaigns;
}
/**
* @return mixed
*/
public function getTemplates() {
return $this->templates;
}
}