| 1 | <?php |
| 2 | |
| 3 | namespace Civi\Angular\Page; |
| 4 | |
| 5 | /** |
| 6 | * This page aggregates data from Angular modules. |
| 7 | * |
| 8 | * Example: Aggregate metadata about all modules in JSON format. |
| 9 | * civicrm/ajax/angular-modules?format=json |
| 10 | * |
| 11 | * Example: Aggregate metadata for crmUi and crmUtil modules. |
| 12 | * civicrm/ajax/angular-modules?format=json&modules=crmUi,crmUtil |
| 13 | * |
| 14 | * Example: Aggregate *.js files for all modules. |
| 15 | * civicrm/ajax/angular-modules?format=js |
| 16 | * |
| 17 | * Example: Aggregate *.css files for all modules. |
| 18 | * civicrm/ajax/angular-modules?format=css |
| 19 | */ |
| 20 | class Modules extends \CRM_Core_Page { |
| 21 | |
| 22 | /** |
| 23 | * Generate asset content (when accessed via older, custom |
| 24 | * "civicrm/ajax/anulgar-modules" route). |
| 25 | * |
| 26 | * @deprecated |
| 27 | * |
| 28 | * @throws \CRM_Core_Exception |
| 29 | */ |
| 30 | public function run() { |
| 31 | /** |
| 32 | * @var \Civi\Angular\Manager $angular |
| 33 | */ |
| 34 | $angular = \Civi::service('angular'); |
| 35 | $moduleNames = $this->parseModuleNames(\CRM_Utils_Request::retrieve('modules', 'String'), $angular); |
| 36 | |
| 37 | switch (\CRM_Utils_Request::retrieve('format', 'String')) { |
| 38 | case 'json': |
| 39 | case '': |
| 40 | $this->send( |
| 41 | 'application/javascript', |
| 42 | json_encode($this->getMetadata($moduleNames, $angular)) |
| 43 | ); |
| 44 | break; |
| 45 | |
| 46 | case 'js': |
| 47 | $this->send( |
| 48 | 'application/javascript', |
| 49 | $this->digestJs($angular->getResources($moduleNames, 'js', 'path')) |
| 50 | ); |
| 51 | break; |
| 52 | |
| 53 | case 'css': |
| 54 | $this->send( |
| 55 | 'text/css', |
| 56 | \CRM_Utils_File::concat($angular->getResources($moduleNames, 'css', 'path'), "\n") |
| 57 | ); |
| 58 | break; |
| 59 | |
| 60 | default: |
| 61 | throw new \CRM_Core_Exception("Unrecognized format"); |
| 62 | } |
| 63 | |
| 64 | \CRM_Utils_System::civiExit(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Generate asset content (when accessed via AssetBuilder). |
| 69 | * |
| 70 | * @param \Civi\Core\Event\GenericHookEvent $event |
| 71 | * @see CRM_Utils_hook::buildAsset() |
| 72 | * @see \Civi\Core\AssetBuilder |
| 73 | */ |
| 74 | public static function buildAngularModules($event) { |
| 75 | $page = new Modules(); |
| 76 | $angular = \Civi::service('angular'); |
| 77 | |
| 78 | switch ($event->asset) { |
| 79 | case 'angular-modules.json': |
| 80 | $moduleNames = $page->parseModuleNames($event->params['modules'] ?? NULL, $angular); |
| 81 | $event->mimeType = 'application/json'; |
| 82 | $event->content = json_encode($page->getMetadata($moduleNames, $angular)); |
| 83 | break; |
| 84 | |
| 85 | case 'angular-modules.js': |
| 86 | $moduleNames = $page->parseModuleNames($event->params['modules'] ?? NULL, $angular); |
| 87 | $event->mimeType = 'application/javascript'; |
| 88 | $event->content = $page->digestJs($angular->getResources($moduleNames, 'js', 'path')); |
| 89 | break; |
| 90 | |
| 91 | case 'angular-modules.css': |
| 92 | $moduleNames = $page->parseModuleNames($event->params['modules'] ?? NULL, $angular); |
| 93 | $event->mimeType = 'text/css'; |
| 94 | $event->content = \CRM_Utils_File::concat($angular->getResources($moduleNames, 'css', 'path'), "\n"); |
| 95 | |
| 96 | default: |
| 97 | // Not our problem. |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @param array $files |
| 103 | * File paths. |
| 104 | * @return string |
| 105 | */ |
| 106 | public function digestJs($files) { |
| 107 | $scripts = []; |
| 108 | foreach ($files as $file) { |
| 109 | $scripts[] = \CRM_Utils_JS::stripComments(file_get_contents($file)); |
| 110 | } |
| 111 | $scripts = \CRM_Utils_JS::dedupeClosures( |
| 112 | $scripts, |
| 113 | ['angular', '$', '_'], |
| 114 | ['angular', 'CRM.$', 'CRM._'] |
| 115 | ); |
| 116 | return implode("\n", $scripts); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param string $modulesExpr |
| 121 | * Comma-separated list of module names. |
| 122 | * @param \Civi\Angular\Manager $angular |
| 123 | * @return array |
| 124 | * Any well-formed module names. All if moduleExpr is blank. |
| 125 | */ |
| 126 | public function parseModuleNames($modulesExpr, $angular) { |
| 127 | if ($modulesExpr) { |
| 128 | $moduleNames = preg_grep( |
| 129 | '/^[a-zA-Z0-9\-_\.]+$/', |
| 130 | explode(',', $modulesExpr) |
| 131 | ); |
| 132 | return $moduleNames; |
| 133 | } |
| 134 | else { |
| 135 | $moduleNames = array_keys($angular->getModules()); |
| 136 | return $moduleNames; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param array $moduleNames |
| 142 | * List of module names. |
| 143 | * @param \Civi\Angular\Manager $angular |
| 144 | * @return array |
| 145 | */ |
| 146 | public function getMetadata($moduleNames, $angular) { |
| 147 | $modules = $angular->getModules(); |
| 148 | $result = []; |
| 149 | foreach ($moduleNames as $moduleName) { |
| 150 | if (isset($modules[$moduleName])) { |
| 151 | $result[$moduleName] = []; |
| 152 | $result[$moduleName]['domain'] = $modules[$moduleName]['ext']; |
| 153 | $result[$moduleName]['js'] = $angular->getResources($moduleName, 'js', 'rawUrl'); |
| 154 | $result[$moduleName]['css'] = $angular->getResources($moduleName, 'css', 'rawUrl'); |
| 155 | $result[$moduleName]['partials'] = $angular->getPartials($moduleName); |
| 156 | $result[$moduleName]['strings'] = $angular->getTranslatedStrings($moduleName); |
| 157 | } |
| 158 | } |
| 159 | return $result; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Send a response. |
| 164 | * |
| 165 | * @param string $type |
| 166 | * Content type. |
| 167 | * @param string $data |
| 168 | * Content. |
| 169 | */ |
| 170 | public function send($type, $data) { |
| 171 | // Encourage browsers to cache for a long time - 1 year |
| 172 | $ttl = 60 * 60 * 24 * 364; |
| 173 | \CRM_Utils_System::setHttpHeader('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $ttl)); |
| 174 | \CRM_Utils_System::setHttpHeader("Content-Type", $type); |
| 175 | \CRM_Utils_System::setHttpHeader("Cache-Control", "max-age=$ttl, public"); |
| 176 | echo $data; |
| 177 | } |
| 178 | |
| 179 | } |