Example #1
0
 /**
  * Fetch an L10n content string
  *
  * @param $key      string  YAML key of the desired text string
  * @param $language string  Optionally override the desired language
  * @return mixed
  */
 public static function fetch($key, $language = null, $lower = false)
 {
     $app = \Slim\Slim::getInstance();
     $language = $language ? $language : Config::getCurrentLanguage();
     $value = $key;
     /*
     |--------------------------------------------------------------------------
     | Check for new language
     |--------------------------------------------------------------------------
     |
     | English is loaded by default. If requesting a language not already
     | cached, go grab it.
     |
     */
     if (!isset($app->config['_translations'][$language])) {
         $app->config['_translations'][$language] = YAML::parse(Config::getTranslation($language));
     }
     /*
     |--------------------------------------------------------------------------
     | Resolve translation
     |--------------------------------------------------------------------------
     |
     | If the set language is found and the key exists, return it. Falls back to
     | English, and then falls back to the slug-style key itself.
     |
     */
     if (array_get($app->config['_translations'][$language]['translations'], $value, false)) {
         $value = array_get($app->config['_translations'][$language]['translations'], $value);
     } else {
         $value = array_get($app->config['_translations']['en']['translations'], $value, $value);
     }
     return $lower ? strtolower($value) : $value;
 }
Example #2
0
 /**
  * Load the config (yaml) files in a specified order:
  *
  * 1. Loose per-site configs
  * 2. Routes
  * 3. Settings
  * 4. Theme overrides
  */
 public static function loadAllConfigs($admin = false)
 {
     $hash = Debug::markStart('config', 'finding');
     /*
     |--------------------------------------------------------------------------
     | YAML Mode
     |--------------------------------------------------------------------------
     |
     | We need to know the YAML mode first (loose, strict, transitional),
     | so we parse the settings file once to check before doing anything else.
     |
     */
     $preload_config = YAML::parse(Config::getConfigPath() . '/settings.yaml');
     $yaml_mode = array_get($preload_config, '_yaml_mode', 'loose');
     /*
     |--------------------------------------------------------------------------
     | Default Settings
     |--------------------------------------------------------------------------
     |
     | We keep a set of default options that the user config overrides, allowing
     | us to always have clean defaults.
     |
     */
     $settings_to_parse = File::get(Config::getAppConfigPath() . '/default.settings.yaml');
     /*
     |--------------------------------------------------------------------------
     | User Site Settings
     |--------------------------------------------------------------------------
     |
     | Next we parse and override the user's settings.
     |
     */
     $settings_to_parse .= "\n\n" . File::get(Config::getConfigPath() . '/settings.yaml');
     /*
     |--------------------------------------------------------------------------
     | Routes and vanity URLs
     |--------------------------------------------------------------------------
     |
     | Any URL can be manipulated by routes or vanity urls. We need this info
     | early on, before content parsing begins.
     |
     */
     $settings_to_parse .= "\n\n_routes:\n  " . trim(preg_replace("/\n/", "\n  ", File::get(Config::getConfigPath() . '/routes.yaml')));
     $settings_to_parse .= "\n\n_vanity_urls:\n  " . trim(preg_replace("/\n/", "\n  ", File::get(Config::getConfigPath() . '/vanity.yaml')));
     /*
     |--------------------------------------------------------------------------
     | Global Variables
     |--------------------------------------------------------------------------
     |
     | We parse all the yaml files in the root (except settings and routes) of
     | the config folder and make them available as global template variables.
     |
     */
     if (Folder::exists($config_files_location = Config::getConfigPath())) {
         $files = glob($config_files_location . '/*.yaml');
         if ($files) {
             foreach ($files as $file) {
                 if (strpos($file, 'routes.yaml') !== false || strpos($file, 'vanity.yaml') !== false || strpos($file, 'settings.yaml')) {
                     continue;
                 }
                 $settings_to_parse .= "\n\n" . File::get($file);
             }
         }
     }
     /*
     |--------------------------------------------------------------------------
     | Parse settings up until now
     |--------------------------------------------------------------------------
     |
     | Parses the concatenated settings string we've made so far.
     |
     */
     $config = YAML::parse($settings_to_parse, $yaml_mode);
     /*
     |--------------------------------------------------------------------------
     | Theme Variables
     |--------------------------------------------------------------------------
     |
     | Theme variables need to specifically parsed later so they can override
     | any site/global defaults.
     |
     */
     $themes_path = array_get($config, '_themes_path', '_themes');
     $theme_name = array_get($config, '_theme', 'acadia');
     // reset
     $settings_to_parse = '';
     if (Folder::exists($theme_files_location = Path::assemble(BASE_PATH, $themes_path, $theme_name))) {
         $theme_files = glob(Path::tidy($theme_files_location . '/*.yaml'));
         if ($theme_files) {
             foreach ($theme_files as $file) {
                 $settings_to_parse .= "\n\n" . File::get($file);
             }
         }
     }
     Debug::markEnd($hash);
     // parse theme settings if any
     if ($settings_to_parse) {
         $config = YAML::parse($settings_to_parse, $yaml_mode) + $config;
     }
     /*
     |--------------------------------------------------------------------------
     | Load Environment Configs and Variables
     |--------------------------------------------------------------------------
     |
     | Environments settings explicitly overwrite any existing settings, and
     | therefore must be loaded late. We also set a few helper variables
     | to make working with environments even easier.
     |
     */
     _Environment::establish($config);
     /*
     |--------------------------------------------------------------------------
     | MIME Types
     |--------------------------------------------------------------------------
     */
     $config = array('_mimes' => require Config::getAppConfigPath() . '/mimes.php') + $config;
     /*
     |--------------------------------------------------------------------------
     | Localization
     |--------------------------------------------------------------------------
     |
     | We load up English by default. We're American after all. Doesn't the
     | world revolve around us? Hello? Bueller? More hamburgers please.
     |
     */
     $config['_translations'] = array();
     $config['_translations']['en'] = YAML::parse(Config::getAppConfigPath() . '/default.en.yaml');
     if ($lang = array_get($config, '_language', false)) {
         if (File::exists(Config::getTranslation($lang))) {
             $translation = YAML::parse(Config::getTranslation($lang));
             $config['_translations'][$lang] = Helper::arrayCombineRecursive($config['_translations']['en'], $translation);
         }
     }
     $finder = new Finder();
     // clear previous Finder interator results
     try {
         $translation_files = $finder->files()->in(BASE_PATH . Config::getAddonsPath() . '/*/translations')->name($lang . '.*.yaml')->depth(0)->followLinks();
         foreach ($translation_files as $file) {
             $translation = YAML::parse($file->getRealPath());
             $config['_translations'][$lang] = Helper::arrayCombineRecursive($translation, $config['_translations'][$lang]);
         }
     } catch (Exception $e) {
         // meh. not important.
     }
     /*
     |--------------------------------------------------------------------------
     | Set Slim Config
     |--------------------------------------------------------------------------
     |
     | Slim needs to be initialized with a set of config options, so these
     | need to be set earlier than the set_default_tags() method.
     |
     */
     // $config['view'] = new Statamic_View();
     $config['cookies.lifetime'] = $config['_cookies.lifetime'];
     if ($admin) {
         $admin_theme = array_get($config, '_admin_theme', 'ascent');
         if (!Folder::exists(BASE_PATH . Path::tidy('/' . $config['_admin_path'] . '/' . 'themes/' . $admin_theme))) {
             $admin_theme = 'ascent';
         }
         $theme_path = Path::tidy('/' . $config['_admin_path'] . '/' . 'themes/' . $admin_theme . '/');
         $config['theme_path'] = $theme_path;
         $config['templates.path'] = '.' . $theme_path;
     } else {
         $public_path = isset($config['_public_path']) ? $config['_public_path'] : '';
         $config['theme_path'] = $themes_path . '/' . $config['_theme'] . '/';
         $config['templates.path'] = Path::tidy($public_path . $themes_path . '/' . $config['_theme'] . '/');
     }
     if (!array_get($config, '_display_debug_panel', false)) {
         Debug::disable();
     }
     return $config;
 }