Merge pull request #14033 from eileenmcnaughton/recur_cancel_api
[civicrm-core.git] / CRM / Core / Resources / Strings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Manage translatable strings on behalf of resource files.
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 */
35 class CRM_Core_Resources_Strings {
36
37 /**
38 * @var CRM_Utils_Cache_Interface|NULL
39 */
40 private $cache = NULL;
41
42 /**
43 * @param CRM_Utils_Cache_Interface $cache
44 * Localization cache.
45 */
46 public function __construct($cache) {
47 $this->cache = $cache;
48 }
49
50 /**
51 * Flush the cache of translated strings.
52 */
53 public function flush() {
54 $this->cache->flush();
55 }
56
57 /**
58 * Get the strings from a file, using a cache if available.
59 *
60 * @param string $bucket
61 * The name of a cache-row which includes strings for this file.
62 * @param string $file
63 * File path.
64 * @param string $format
65 * Type of file (e.g. 'text/javascript', 'text/html').
66 * @return array
67 * List of translatable strings.
68 */
69 public function get($bucket, $file, $format) {
70 // array($file => array(...strings...))
71 $stringsByFile = $this->cache->get($bucket);
72 if (!$stringsByFile) {
73 $stringsByFile = [];
74 }
75 if (!isset($stringsByFile[$file])) {
76 if ($file && is_readable($file)) {
77 $stringsByFile[$file] = $this->extract($file, $format);
78 }
79 else {
80 $stringsByFile[$file] = [];
81 }
82 $this->cache->set($bucket, $stringsByFile);
83 }
84 return $stringsByFile[$file];
85 }
86
87 /**
88 * Extract a list of strings from a file.
89 *
90 * @param string $file
91 * File path.
92 * @param string $format
93 * Type of file (e.g. 'text/javascript', 'text/html').
94 * @return array
95 * List of translatable strings.
96 * @throws Exception
97 */
98 public function extract($file, $format) {
99 switch ($format) {
100 case 'text/javascript':
101 return CRM_Utils_JS::parseStrings(file_get_contents($file));
102
103 case 'text/html':
104 // Magic! The JS parser works with HTML! See CRM_Utils_HTMLTest.
105 return CRM_Utils_JS::parseStrings(file_get_contents($file));
106
107 default:
108 throw new Exception("Cannot extract strings: Unrecognized file type.");
109 }
110 }
111
112 }