Path: blob/master/src/infrastructure/internationalization/scope/PhabricatorLocaleScopeGuard.php
12242 views
<?php12/**3* Change the effective locale for the lifetime of this guard.4*5* Use @{method:PhabricatorEnv::beginScopedLocale} to acquire a guard.6* Guards are released when they exit scope.7*/8final class PhabricatorLocaleScopeGuard9extends Phobject {1011private static $stack = array();12private $key;13private $destroyed;1415public function __construct($code) {16// If this is the first time we're building a guard, push the default17// locale onto the bottom of the stack. We'll never remove it.18if (empty(self::$stack)) {19self::$stack[] = PhabricatorEnv::getLocaleCode();20}2122// If there's no locale, use the server default locale.23if (!$code) {24$code = self::$stack[0];25}2627// Push this new locale onto the stack and set it as the active locale.28// We keep track of which key this guard owns, in case guards are destroyed29// out-of-order.30self::$stack[] = $code;31$this->key = last_key(self::$stack);3233PhabricatorEnv::setLocaleCode($code);34}3536public function __destruct() {37if ($this->destroyed) {38return;39}40$this->destroyed = true;4142// Remove this locale from the stack and set the new active locale. Usually,43// we're the last item on the stack, so this shortens the stack by one item44// and sets the locale underneath. However, it's possible that guards are45// being destroyed out of order, so we might just be removing an item46// somewhere in the middle of the stack. In this case, we won't actually47// change the locale, just set it to its current value again.4849unset(self::$stack[$this->key]);50PhabricatorEnv::setLocaleCode(end(self::$stack));51}5253}545556