/** * initialize method * * Merge settings and set Config.language to a valid locale * * @return void * @access public */ function initialize(&$Controller, $config = array()) { App::import('Vendor', 'Mi.MiCache'); $lang = MiCache::setting('Site.lang'); if (!$lang) { if (!defined('DEFAULT_LANGUAGE')) { return; } $lang = DEFAULT_LANGUAGE; } elseif (!defined('DEFAULT_LANGUAGE')) { define('DEFAULT_LANGUAGE', $lang); } Configure::write('Config.language', $lang); App::import('Core', 'I18n'); $I18n =& I18n::getInstance(); $I18n->domain = 'default_' . $lang; $I18n->__lang = $lang; $I18n->l10n->get($lang); if (!empty($Controller->plugin)) { $config['plugins'][] = Inflector::underscore($Controller->plugin); } if (!empty($config['plugins'])) { $plugins = array_intersect(MiCache::mi('plugins'), $config['plugins']); $Inst = App::getInstance(); foreach ($plugins as $path => $name) { $Inst->locales[] = $path . DS . 'locale' . DS; } } }
/** * viewPaths method * * @param mixed $locale null * @param mixed $plugin null * @return void * @access protected */ protected function _viewPaths($plugin = null, $locale = null) { if (!$locale) { App::import('Vendor', 'Mi.MiCache'); $locale = MiCache::setting('Site.lang'); if (!$locale) { $locale = Configure::read('Config.language'); if (!$locale && defined('DEFAULT_LANGUAGE')) { $locale = 'DEFAULT_LANGUAGE'; } } if ($locale) { Configure::write('Config.language', $locale); App::import('Core', 'I18n'); $I18n =& I18n::getInstance(); $I18n->domain = 'default_' . $locale; $I18n->__lang = $locale; $I18n->l10n->get($locale); } } if (Mi::cakeVersion() === '1.2') { $viewPaths = Configure::read('viewPaths'); $localePaths = Configure::read('localePaths'); } else { $viewPaths = App::path('views'); $localePaths = App::path('locales'); } if (!class_exists('I18n')) { App::import('Core', array('I18n')); } $languagePaths = array_filter(array_unique(I18n::getInstance()->l10n->languagePath)); $paths = array(); if ($plugin) { $count = count($viewPaths); for ($i = 0; $i < $count; $i++) { $base = $viewPaths[$i] . 'plugins' . DS . $plugin . DS; if ($locale) { foreach ($localePaths as $path) { foreach ($localePaths as $_locale) { $paths[] = $_locale . $locale . DS . 'views' . DS . 'plugins' . DS . $plugin . DS; } } } $paths[] = $base; } if (Mi::cakeVersion() === '1.2') { $pluginPaths = Configure::read('pluginPaths'); } else { $pluginPaths = App::path('plugins'); } $count = count($pluginPaths); for ($i = 0; $i < $count; $i++) { $base = $pluginPaths[$i] . $plugin . DS; if ($locale) { foreach ($localePaths as $path) { foreach ($languagePaths as $_locale) { $paths[] = $pluginPaths[$i] . $plugin . DS . 'locale' . DS . $_locale . DS . 'views' . DS; } } } $paths[] = $base . 'views' . DS; } } if ($locale) { foreach ($localePaths as $path) { foreach ($languagePaths as $_locale) { $paths[] = $path . $_locale . DS . 'views' . DS; } } } if ($paths) { $viewPaths = array_merge($paths, $viewPaths); } if (!empty($theme)) { $themePaths = array(); $count = count($paths); for ($i = 0; $i < $count; $i++) { if (strpos($paths[$i], DS . 'plugins' . DS) === false && strpos($paths[$i], DS . 'libs' . DS . 'view') === false) { if ($plugin) { $themePaths[] = $paths[$i] . 'themed' . DS . $theme . DS . 'plugins' . DS . $plugin . DS; } $themePaths[] = $paths[$i] . 'themed' . DS . $theme . DS; } } $viewPaths = array_merge($themePaths, $viewPaths); } return array_unique($viewPaths); }
/** * testConfigureOverrides method * * Run the tests twice to ensure the first and cached results are the same * * @return void * @access public */ public function testConfigureOverrides() { $negatives = array('array' => array(1, 2, 3), 'false' => true, 'null' => 'you can\'t override with null', 'string' => 'not empty', 'zero' => 1, 'zeroString' => '1'); Configure::write('MiCacheTest.negatives', $negatives); $negatives = array('array' => array(), 'false' => false, 'null' => null, 'string' => '', 'zero' => 0, 'zeroString' => '0'); $Setting = ClassRegistry::init('MiSettings.Setting'); $Setting->store('MiCacheTest.negatives', $negatives); $stored = $Setting->find('list', array('conditions' => array('id LIKE' => 'MiCacheTest.%'))); $expected = array('MiCacheTest.negatives.array' => '[]', 'MiCacheTest.negatives.false' => '', 'MiCacheTest.negatives.null' => null, 'MiCacheTest.negatives.string' => '', 'MiCacheTest.negatives.zero' => '0', 'MiCacheTest.negatives.zeroString' => '0'); $this->assertIdentical($stored, $expected); $expected = $negatives; $return = MiCache::setting('MiCacheTest.negatives'); $this->assertIdentical($return, $expected); $expected = array(); $return = MiCache::setting('MiCacheTest.negatives.array'); $this->assertIdentical($return, $expected); $return = MiCache::setting('MiCacheTest.negatives.array'); $this->assertIdentical($return, $expected); $expected = false; $return = MiCache::setting('MiCacheTest.negatives.false'); $this->assertIdentical($return, $expected); $return = MiCache::setting('MiCacheTest.negatives.false'); $this->assertIdentical($return, $expected); $expected = 'you can\'t override with null'; $return = MiCache::setting('MiCacheTest.negatives.null'); $this->assertIdentical($return, $expected); $return = MiCache::setting('MiCacheTest.negatives.null'); $this->assertIdentical($return, $expected); $expected = ''; $return = MiCache::setting('MiCacheTest.negatives.string'); $this->assertIdentical($return, $expected); $return = MiCache::setting('MiCacheTest.negatives.string'); $this->assertIdentical($return, $expected); $expected = 0; $return = MiCache::setting('MiCacheTest.negatives.zero'); $this->assertIdentical($return, $expected); $return = MiCache::setting('MiCacheTest.negatives.zero'); $this->assertIdentical($return, $expected); $expected = '0'; $return = MiCache::setting('MiCacheTest.negatives.zeroString'); $this->assertIdentical($return, $expected); $return = MiCache::setting('MiCacheTest.negatives.zeroString'); $this->assertIdentical($return, $expected); $Setting->deleteAll(array('id LIKE' => 'MiCacheTest%')); }
/** * setting method * * Query the application setting model, if there is no result query Configure::read - and cache * the result * * @param string $cacheKey '' * @param mixed $dummy null * @return void * @access public */ public static function setting($id = '', $aroId = null) { if (MiCache::$setting === null) { MiCache::config(); } if (MiCache::$settings[MiCache::$setting]['batchLoadSettings']) { if (strpos($id, '.')) { $keys = explode('.', $id); $mainId = array_shift($keys); if (!array_key_exists($aroId . '_' . $mainId, MiCache::$_appSettingCache)) { MiCache::$_appSettingCache[$aroId . '_' . $mainId] = MiCache::setting($mainId, $aroId); } $array = MiCache::$_appSettingCache[$aroId . '_' . $mainId]; $j = count($keys); $return = null; if (is_array($array)) { foreach ($keys as $i => $key) { if (!array_key_exists($key, $array)) { $array = null; break; } $array = $array[$key]; } if ($i == $j - 1) { $return = $array; } } if ($return !== null) { return $return; } } } else { if (strpos($id, '.')) { $keys = explode('.', $id, 1); $mainId = array_shift($keys); if (array_key_exists($aroId . '_' . $mainId, MiCache::$_appSettingCache) && array_key_exists($id, MiCache::$_appSettingCache[$aroId . '_' . $mainId])) { return MiCache::$_appSettingCache[$aroId . '_' . $mainId][$id]; } } } if (MiCache::_hasSettingsTable()) { $return = MiCache::data('MiSettings.Setting', 'data', $id, $aroId); if ($return !== null) { return $return; } } $return = Configure::read($id); $cacheKey = MiCache::key(array('MiSettings.Setting', 'data', $id, $aroId)); MiCache::write($cacheKey, $return, MiCache::$setting); return $return; }
/** * removeStopWords from a string. if $splitOnStopWord is true, the following occurs: * input "apples bananas pears and red cars" * output array('apples bananas pears', 'red cars') * * If the passed string doesn't contain the seperator, or after stripping out stop words there's * nothing left - the original input is returned (in the desired format) * * Therefore passing "contain" will return immediately array('contain') * Passing "contain this text" will return array('text') * both contain and this are stop words * Passing "contain this" will return array('contain this') * * @param mixed $Model * @param mixed $string string or array of words * @param array $params * @return mixed * @access public */ public function removeStopWords(&$Model, $string = '', $params = array()) { if (!$string) { return $string; } $seperator = ' '; $splitOnStopWord = true; $return = 'array'; $originalIfEmpty = true; extract($params); if (!class_exists('MiCache')) { App::import('Vendor', 'Mi.MiCache'); } if (!empty($this->settings[$Model->alias]['language'])) { $lang = $this->settings[$Model->alias]['language']; } else { $lang = MiCache::setting('Site.lang'); if (!$lang) { $lang = 'eng'; } $this->settings[$Model->alias]['language'] = $lang; } if (!array_key_exists($lang, $this->stopWords)) { ob_start(); if (!App::import('Vendor', 'stop_words/' . $lang, array('file' => "stop_words/{$lang}.txt"))) { App::import('Vendor', 'stop_words/' . $lang, array('plugin' => 'Mi', 'file' => "stop_words/{$lang}.txt")); } $stopWords = preg_replace('@/\\*.*\\*/@', '', ob_get_clean()); $this->stopWords[$lang] = array_filter(array_map('trim', explode("\n", $stopWords))); } if (is_array($string)) { $originalTerms = $terms = $string; foreach ($terms as $i => &$term) { $term = trim(preg_replace('@[^\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}]@u', $seperator, $term), $seperator); } $lTerms = array_map('mb_strtolower', $terms); $lTerms = array_diff($lTerms, $this->stopWords[$lang]); $terms = array_intersect_key($terms, $lTerms); } else { if (!strpos($string, $seperator)) { if ($return === 'array') { return array($string); } return $string; } $string = preg_replace('@[^\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}]@u', $seperator, $string); $originalTerms = $terms = array_filter(array_map('trim', explode($seperator, $string))); if ($splitOnStopWord) { $terms = $chunk = array(); $snippet = ''; foreach ($originalTerms as $term) { $lterm = strtolower($term); if (in_array($lterm, $this->stopWords[$lang])) { if ($chunk) { $terms[] = $chunk; $chunk = array(); } continue; } $chunk[] = $term; } if ($chunk) { $terms[] = $chunk; } foreach ($terms as &$phrase) { $phrase = implode(' ', $phrase); } } else { $lTerms = array_map('mb_strtolower', $terms); $lTerms = array_diff($lTerms, $this->stopWords[$lang]); $terms = array_intersect_key($terms, $lTerms); } } if (!$terms && $originalIfEmpty) { $terms = array(implode(' ', $originalTerms)); } if ($return === 'array') { return array_values(array_unique($terms)); } return implode($seperator, $terms); }
/** * If code and domain are not passed explicitly - use the MiCache setting class * to determine which code and domain to use * * @param mixed $code null * @param mixed $domain null * @return string the code to inject * @access protected */ protected function _codeBlock($code = null, $domain = null) { if ($this->settings['element']) { $element = $this->settings['element'] === true ? 'analytics' : $this->settings['element']; return $this->View->element($element, compact('code', 'domain')); } if (is_null($code)) { $code = MiCache::setting('Site.analyticsCode'); if (!$code) { return; } } if (is_null($domain)) { $domain = MiCache::setting('Site.analyticsDomain'); } $this->_template(); $return = String::insert($this->_template, compact('code', 'domain')); if ($domain) { $return = preg_replace('#(<<<domainStart|domainEnd>>>)\\s*#s', '', $return); } else { $return = preg_replace('#<<<domainStart.*domainEnd>>>\\s*#s', '', $return); } return trim($return); }