|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Auto-register "templates/" folder. |
| 5 | + * |
| 6 | + * @mixinName smarty-v2 |
| 7 | + * @mixinVersion 1.0.3 |
| 8 | + * @since 5.59 |
| 9 | + * |
| 10 | + * @deprecated - it turns out that the mixin is not version specific so the 'smarty' |
| 11 | + * mixin is preferred over smarty-v2 (they are the same but not having the version |
| 12 | + * in the name is less misleading.) |
| 13 | + * |
| 14 | + * @param CRM_Extension_MixInfo $mixInfo |
| 15 | + * On newer deployments, this will be an instance of MixInfo. On older deployments, Civix may polyfill with a work-a-like. |
| 16 | + * @param \CRM_Extension_BootCache $bootCache |
| 17 | + * On newer deployments, this will be an instance of MixInfo. On older deployments, Civix may polyfill with a work-a-like. |
| 18 | + */ |
| 19 | +return function ($mixInfo, $bootCache) { |
| 20 | + $dir = $mixInfo->getPath('templates'); |
| 21 | + if (!file_exists($dir)) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + $register = function($newDirs) { |
| 26 | + $smarty = CRM_Core_Smarty::singleton(); |
| 27 | + $v2 = isset($smarty->_version) && version_compare($smarty->_version, 3, '<'); |
| 28 | + $templateDirs = (array) ($v2 ? $smarty->template_dir : $smarty->getTemplateDir()); |
| 29 | + $templateDirs = array_merge($newDirs, $templateDirs); |
| 30 | + $templateDirs = array_unique(array_map(function($v) { |
| 31 | + $v = str_replace(DIRECTORY_SEPARATOR, '/', $v); |
| 32 | + $v = rtrim($v, '/') . '/'; |
| 33 | + return $v; |
| 34 | + }, $templateDirs)); |
| 35 | + if ($v2) { |
| 36 | + $smarty->template_dir = $templateDirs; |
| 37 | + } |
| 38 | + else { |
| 39 | + $smarty->setTemplateDir($templateDirs); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + // Let's figure out what environment we're in -- so that we know the best way to call $register(). |
| 44 | + |
| 45 | + if (!empty($GLOBALS['_CIVIX_MIXIN_POLYFILL'])) { |
| 46 | + // Polyfill Loader (v<=5.45): We're already in the middle of firing `hook_config`. |
| 47 | + if ($mixInfo->isActive()) { |
| 48 | + $register([$dir]); |
| 49 | + } |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (CRM_Extension_System::singleton()->getManager()->extensionIsBeingInstalledOrEnabled($mixInfo->longName)) { |
| 54 | + // New Install, Standard Loader: The extension has just been enabled, and we're now setting it up. |
| 55 | + // System has already booted. New templates may be needed for upcoming installation steps. |
| 56 | + $register([$dir]); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Typical Pageview, Standard Loader: Defer the actual registration for a moment -- to ensure that Smarty is online. |
| 61 | + // We need to bundle-up all dirs -- Smarty 3/4/5 is inefficient with processing repeated calls to `getTemplateDir()`+`setTemplateDir()` |
| 62 | + if (!isset(Civi::$statics[__FILE__]['event'])) { |
| 63 | + Civi::$statics[__FILE__]['event'] = 'civi.smarty-v2.addPaths.' . md5(__FILE__); |
| 64 | + Civi::dispatcher()->addListener('hook_civicrm_config', function() use ($register) { |
| 65 | + $dirs = []; |
| 66 | + $event = \Civi\Core\Event\GenericHookEvent::create(['dirs' => &$dirs]); |
| 67 | + Civi::dispatcher()->dispatch(Civi::$statics[__FILE__]['event'], $event); |
| 68 | + $register($dirs); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + Civi::dispatcher()->addListener(Civi::$statics[__FILE__]['event'], function($event) use ($mixInfo, $dir) { |
| 73 | + if ($mixInfo->isActive()) { |
| 74 | + array_unshift($event->dirs, $dir); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | +}; |
0 commit comments