示例#1
0
 public function testSetLocale()
 {
     Session::start();
     $currentLocale = I18n::getCurrentLocale();
     $_REQUEST['_locale'] = 'en';
     I18n::staticReset();
     $this->assertEquals('en', I18n::getCurrentLocale(), 'Unable to set locale via request');
     $_REQUEST['_locale'] = $currentLocale;
     I18n::staticReset();
     unset($_REQUEST['_locale']);
     I18n::setLocale('en');
     $this->assertEquals('en', I18n::getCurrentLocale(), 'Unable to set locale via setLocale()');
     I18n::setLocale($currentLocale);
     $this->assertEquals($currentLocale, I18n::getCurrentLocale(), 'Unable to set locale via setLocale()');
     I18n::setLocale('non-existing');
     $this->assertEquals($currentLocale, I18n::getCurrentLocale(), 'Unable to restore default locale via setLocale()');
     Session::end();
     I18n::clearCache(true);
     Cookies::set('locale', 'en');
     Session::start();
     I18n::staticReset();
     $this->assertEquals('en', I18n::getCurrentLocale(), 'Unable to set locale via cookie');
     $_REQUEST['_locale'] = $currentLocale;
     I18n::staticReset();
     unset($_REQUEST['_locale']);
     Session::set('current_locale', 'en');
     I18n::staticReset();
     $this->assertEquals('en', I18n::getCurrentLocale(), 'Unable to set locale via session');
     Session::set('current_locale', null);
     I18n::staticReset();
     $this->assertEquals($currentLocale, I18n::getCurrentLocale(), 'Unable to restore default locale');
     Session::end();
 }
示例#2
0
 public function setUp()
 {
     $_SERVER['SCRIPT_NAME'] = '/index.php';
     $_SERVER['PHWOOLCON_PHALCON_VERSION'] = Version::getId();
     /* @var Di $di */
     $di = $this->di = Di::getDefault();
     Events::register($di);
     DiFix::register($di);
     Db::register($di);
     Cache::register($di);
     Log::register($di);
     Config::register($di);
     Counter::register($this->di);
     Aliases::register($di);
     I18n::register($di);
     Cookies::register($di);
     Session::register($di);
     Cache::flush();
     Config::clearCache();
     parent::setUp();
     $class = get_class($this);
     Log::debug("================== Running {$class}::{$this->getName()}() ... ==================");
     Timer::start();
 }
示例#3
0
 public static function dispatch($uri = null)
 {
     try {
         static::$router === null and static::$router = static::$di->getShared('router');
         $router = static::$router;
         // @codeCoverageIgnoreStart
         if (!$uri && $router->_sitePathLength && $router->_uriSource == self::URI_SOURCE_GET_URL) {
             list($uri) = explode('?', $_SERVER['REQUEST_URI']);
             $uri = str_replace(basename($_SERVER['SCRIPT_FILENAME']), '', $uri);
             if (substr($uri, 0, $router->_sitePathLength) == $router->_sitePathPrefix) {
                 $uri = substr($uri, $router->_sitePathLength);
             }
         }
         // @codeCoverageIgnoreEnd
         Events::fire('router:before_dispatch', $router, ['uri' => $uri]);
         $router->handle($uri);
         $route = $router->getMatchedRoute() or static::throw404Exception();
         static::$disableSession or Session::start();
         static::$disableCsrfCheck or static::checkCsrfToken();
         if (($controllerClass = $router->getControllerName()) instanceof Closure) {
             $response = $controllerClass();
             if (!$response instanceof Response) {
                 /* @var Response $realResponse */
                 $realResponse = static::$di->getShared('response');
                 $realResponse->setContent($response);
                 $response = $realResponse;
             }
         } else {
             /* @var Controller $controller */
             $controller = new $controllerClass();
             method_exists($controller, 'initialize') and $controller->initialize();
             method_exists($controller, $method = $router->getActionName()) or static::throw404Exception();
             $controller->{$method}();
             $response = $controller->response;
         }
         Events::fire('router:after_dispatch', $router, ['response' => $response]);
         Session::end();
         return $response;
     } catch (HttpException $e) {
         Log::exception($e);
         return static::$runningUnitTest ? $e : $e->toResponse();
     }
 }
示例#4
0
 public function testSessionCsrfToken()
 {
     Config::set('session.default', 'native');
     Session::register($this->di);
     Session::start();
     $this->assertNotEmpty($csrf = Session::generateCsrfToken(), 'Unable to generate CSRF token');
     $this->assertEquals($csrf, Session::getCsrfToken(), 'Unable to check CSRF token');
     Session::clear();
     $this->assertNotEmpty($newCsrf = Session::getCsrfToken(), 'Unable to regenerate CSRF token');
     $this->assertNotEquals($csrf, $newCsrf, 'Unable to regenerate unique CSRF token');
     Session::end();
 }
示例#5
0
use Phalcon\Version;
use Phwoolcon\Aliases;
use Phwoolcon\Cache;
use Phwoolcon\Config;
use Phwoolcon\Cookies;
use Phwoolcon\Db;
use Phwoolcon\DiFix;
use Phwoolcon\Events;
use Phwoolcon\I18n;
use Phwoolcon\Log;
use Phwoolcon\Queue;
use Phwoolcon\Router;
use Phwoolcon\Session;
use Phwoolcon\Util\Counter;
use Phwoolcon\View;
$_SERVER['PHWOOLCON_PHALCON_VERSION'] = Version::getId();
Events::register($di);
DiFix::register($di);
Db::register($di);
Cache::register($di);
Log::register($di);
Config::register($di);
Counter::register($di);
Aliases::register($di);
Router::register($di);
I18n::register($di);
Cookies::register($di);
Session::register($di);
View::register($di);
Queue::register($di);
$loader->registerNamespaces(Config::get('app.autoload.namespaces', []), true);
示例#6
0
 /**
  * @param User $user
  * @return $this
  */
 public function setUserAsLoggedIn($user)
 {
     $this->user = $user;
     $user and Session::set($this->sessionKey . '.uid', $user->getId());
     return $this;
 }
示例#7
0
 protected function prepareDebugObservers()
 {
     Events::attach('router:after_dispatch', function (Event $event) {
         $this->debugData['session-id'] = json_encode(Session::getId());
         $this->debugData['session-data'] = json_encode(isset($_SESSION) ? $_SESSION : null);
     });
 }
示例#8
0
 public function reset()
 {
     if (!$this->multiLocale) {
         return;
     }
     $this->request or $this->request = static::$di->getShared('request');
     if ($locale = $this->request->get('_locale')) {
         $this->_setLocale($locale);
         return;
     }
     if (($cookie = Cookies::get('locale')) && ($locale = $cookie->useEncryption(false)->getValue())) {
         $this->_setLocale($locale);
         $cookie->setHttpOnly(false)->delete();
         return;
     }
     if ($locale = Session::get('current_locale')) {
         $this->loadLocale($this->currentLocale = $locale);
         return;
     }
     // @codeCoverageIgnoreStart
     if ($this->detectClientLocale) {
         $this->_setLocale($this->request->getBestLanguage());
         return;
     }
     // @codeCoverageIgnoreEnd
     $this->loadLocale($this->currentLocale = $this->defaultLocale);
 }