From fa1c763db4ea1cb68f74e518bf32c4d0037f5f64 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 22 May 2014 20:11:25 -0700 Subject: [PATCH] CRM-14478, CRM-14474 - Move XML lookup code into new class --- CRM/Case/XMLProcessor.php | 78 +------------------ CRM/Case/XMLRepository.php | 156 +++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+), 76 deletions(-) create mode 100644 CRM/Case/XMLRepository.php diff --git a/CRM/Case/XMLProcessor.php b/CRM/Case/XMLProcessor.php index c2bbccc044..30968014a5 100644 --- a/CRM/Case/XMLProcessor.php +++ b/CRM/Case/XMLProcessor.php @@ -34,82 +34,8 @@ */ class CRM_Case_XMLProcessor { - static protected $_xml; - - static protected $_hookCache = NULL; - - function retrieve($caseType) { - $caseType = self::mungeCaseType($caseType); - - if (!CRM_Utils_Array::value($caseType, self::$_xml)) { - if (!self::$_xml) { - self::$_xml = array(); - } - - // first check custom templates directory - $fileName = NULL; - $config = CRM_Core_Config::singleton(); - if (isset($config->customTemplateDir) && - $config->customTemplateDir - ) { - // check if the file exists in the custom templates directory - $fileName = implode(DIRECTORY_SEPARATOR, - array( - $config->customTemplateDir, - 'CRM', - 'Case', - 'xml', - 'configuration', - "$caseType.xml", - ) - ); - } - - if (!$fileName || - !file_exists($fileName) - ) { - // check if file exists locally - $fileName = implode(DIRECTORY_SEPARATOR, - array(dirname(__FILE__), - 'xml', - 'configuration', - "$caseType.xml", - ) - ); - - if (!file_exists($fileName)) { - // check if file exists locally - $fileName = implode(DIRECTORY_SEPARATOR, - array(dirname(__FILE__), - 'xml', - 'configuration.sample', - "$caseType.xml", - ) - ); - } - - if (!file_exists($fileName)) { - if (self::$_hookCache === NULL) { - self::$_hookCache = array(); - CRM_Utils_Hook::caseTypes(self::$_hookCache); - } - if (isset(self::$_hookCache[$caseType], self::$_hookCache[$caseType]['file'])) { - $fileName = self::$_hookCache[$caseType]['file']; - } - } - - if (!file_exists($fileName)) { - return FALSE; - } - } - - // read xml file - $dom = new DomDocument(); - $dom->load($fileName); - $dom->xinclude(); - self::$_xml[$caseType] = simplexml_import_dom($dom); - } - return self::$_xml[$caseType]; + public function retrieve($caseType) { + return CRM_Case_XMLRepository::singleton()->retrieve($caseType); } public static function mungeCaseType($caseType) { diff --git a/CRM/Case/XMLRepository.php b/CRM/Case/XMLRepository.php new file mode 100644 index 0000000000..fec0ff2f32 --- /dev/null +++ b/CRM/Case/XMLRepository.php @@ -0,0 +1,156 @@ + + */ + protected $xml = array(); + + /** + * @var array|NULL + */ + protected $hookCache = NULL; + + /** + * @var array|NULL symbolic names of case-types + */ + protected $allCaseTypes = NULL; + + /** + * @param bool $fresh + * @return CRM_Case_XMLProcessor + */ + public static function singleton($fresh = FALSE) { + if (!self::$singleton || $fresh) { + self::$singleton = new static(); + } + return self::$singleton; + } + + /** + * @param array $xml + */ + public function __construct($xml = array()) { + $this->xml = $xml; + } + + /** + * @param string $caseType + * @return SimpleXMLElement|FALSE + */ + public function retrieve($caseType) { + $caseType = CRM_Case_XMLProcessor::mungeCaseType($caseType); + + if (!CRM_Utils_Array::value($caseType, $this->xml)) { + // first check custom templates directory + $fileName = NULL; + $config = CRM_Core_Config::singleton(); + if (isset($config->customTemplateDir) && + $config->customTemplateDir + ) { + // check if the file exists in the custom templates directory + $fileName = implode(DIRECTORY_SEPARATOR, + array( + $config->customTemplateDir, + 'CRM', + 'Case', + 'xml', + 'configuration', + "$caseType.xml", + ) + ); + } + + if (!$fileName || + !file_exists($fileName) + ) { + // check if file exists locally + $fileName = implode(DIRECTORY_SEPARATOR, + array( + dirname(__FILE__), + 'xml', + 'configuration', + "$caseType.xml", + ) + ); + + if (!file_exists($fileName)) { + // check if file exists locally + $fileName = implode(DIRECTORY_SEPARATOR, + array( + dirname(__FILE__), + 'xml', + 'configuration.sample', + "$caseType.xml", + ) + ); + } + + if (!file_exists($fileName)) { + $caseTypesViaHook = $this->getCaseTypesViaHook(); + if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) { + $fileName = $caseTypesViaHook[$caseType]['file']; + } + } + + if (!file_exists($fileName)) { + return FALSE; + } + } + + // read xml file + $dom = new DomDocument(); + $dom->load($fileName); + $dom->xinclude(); + $this->xml[$caseType] = simplexml_import_dom($dom); + } + return $this->xml[$caseType]; + } + + /** + * @return array + * @see CRM_Utils_Hook::caseTypes + */ + public function getCaseTypesViaHook() { + if ($this->hookCache === NULL) { + $this->hookCache = array(); + CRM_Utils_Hook::caseTypes($this->hookCache); + } + return $this->hookCache; + } +} -- 2.25.1