Ejemplo n.º 1
0
 /**
  * Register Translator (i18n) Component
  * @throws BootstrapException
  */
 protected function registerTranslator()
 {
     if (!property_exists($this->config->app, "translations")) {
         throw new BootstrapException(__METHOD__, '"translations" node must be set under "app" node', 2231);
     }
     if (!property_exists($this->config->app->translations, "path")) {
         throw new BootstrapException(__METHOD__, 'Var "path" to translations files must be set under "app.translations" node', 2232);
     }
     if (!property_exists($this->config->app->translations, "fallBack")) {
         throw new BootstrapException(__METHOD__, 'Var "fall_back" language must be set under "app.translations" node', 2233);
     }
     if (!isset($this->session)) {
         throw new BootstrapException(__METHOD__, '"Translator" requires "Session" component', 2234);
     }
     // Cache of compiled language files?
     $cache = false;
     if (property_exists($this->config->app->translations, "cache") && $this->config->app->translations->cache === true) {
         $cache = true;
     }
     try {
         // Set translator service/instance
         $this->translator = $this->container->get("Translator");
         $this->translator->setLanguagesPath($this->rootPath . self::DS . $this->config->app->translations->path);
         // Get Session
         $bag = $this->session->getSession()->getBags()->getBag("Comely")->getBag("Framework");
         /** @var $currentLocale string|null */
         $currentLocale = $bag->get("language") ?? $this->config->app->translations->fallBack;
         // Source current and fallBack of language
         if ($cache) {
             // Load compiled language from cache
             $locale = $this->getCachedLanguage($currentLocale);
             $fallBack = $locale;
             if ($currentLocale !== $this->config->app->translations->fallBack) {
                 $fallBack = $this->getCachedLanguage($this->config->app->translations->fallBack);
             }
         } else {
             // Fresh compile language files from Yaml source
             $locale = $this->translator->language($currentLocale);
             $fallBack = $locale;
             if ($currentLocale !== $this->config->app->translations->fallBack) {
                 $fallBack = $this->translator->language($this->config->app->translations->fallBack);
             }
         }
         $this->translator->bindLanguage($locale);
         $this->translator->bindFallbackLanguage($fallBack);
     } catch (\ComelyException $e) {
         throw new BootstrapException(__METHOD__, $e->getMessage(), $e->getCode());
     }
 }
Ejemplo n.º 2
0
 /**
  * Prepares session after waking up
  *
  * @param int $expiry
  * @param string $salt
  * @param int $cost
  * @return bool
  * @throws SessionException
  */
 public function decodeData(int $expiry, string $salt, int $cost) : bool
 {
     // Check if this session needs decoding
     if (is_array($this->data) || empty($this->encoded)) {
         throw new SessionException(__METHOD__, "Session is already decoded", 1502);
     }
     // Check validity
     $span = microtime(true) - $this->timeStamp["last"];
     if ($span >= $expiry) {
         // Session has expired
         return false;
     }
     // Checksum
     if (!hash_equals(Session::saltedHash($this->encoded, $salt, $cost), $this->hash)) {
         throw new SessionException(__METHOD__, "Session checksum failed", 1503);
     }
     // Decode data
     $this->data = unserialize($this->encoded, ["allowed_classes" => ["Comely\\IO\\Session\\ComelySession\\Bag"]]);
     if (!$this->data instanceof Bag) {
         throw new SessionException(__METHOD__, "Failed to un-serialize data bags", 1504);
     }
     // Release "encoded" property
     $this->encoded = null;
     // Successfully decoded
     return true;
 }