Browse Source

Replacing global $app with a property.

Julio Montoya 11 years ago
parent
commit
8777c5542b

+ 3 - 0
main/inc/global.inc.php

@@ -511,7 +511,10 @@ $app->before(
         // Starting the session for more info see: http://silex.sensiolabs.org/doc/providers/session.html
         $request->getSession()->start();
 
+        // Setting session obj
 
+        Session::setSession($app['session']);
+        UserManager::setEntityManager($app['orm.em']);
 
         /** @var ChamiloLMS\Component\DataFilesystem\DataFilesystem $filesystem */
         $filesystem = $app['chamilo.filesystem'];

+ 27 - 17
main/inc/lib/chamilo_session.class.php

@@ -2,15 +2,27 @@
 
 /**
  * ChamiloSession class definition
- * @todo don't use global
- */
+  */
 class ChamiloSession
 {
+    public static $session;
 
+    /**
+     * @param $session
+     */
+    public static function setSession($session)
+    {
+        self::$session = $session;
+    }
+
+    /**
+     * @param $variable
+     * @param null $default
+     * @return null
+     */
     public static function read($variable, $default = null)
     {
-        global $app;
-        $result = $app['session']->get($variable);
+        $result = self::$session->get($variable);
         // check if the value exists in the $_SESSION array
         if (empty($result)) {
             return isset($_SESSION[$variable]) ? $_SESSION[$variable] : $default;
@@ -19,19 +31,24 @@ class ChamiloSession
         }
     }
 
+    /**
+     * @param $variable
+     * @param $value
+     */
     public static function write($variable, $value)
     {
-        global $app;
         // Writing the session in 2 instances because
         $_SESSION[$variable] = $value;
-        $app['session']->set($variable, $value);
+        self::$session->set($variable, $value);
     }
 
+    /**
+     * @param $variable
+     */
     public static function erase($variable)
     {
-        global $app;
         $variable = (string) $variable;
-        $app['session']->remove($variable);
+        self::$session->remove($variable);
 
         if (isset($GLOBALS[$variable])) {
             unset($GLOBALS[$variable]);
@@ -46,9 +63,7 @@ class ChamiloSession
      */
     public static function clear()
     {
-        global $app;
-        $app['session']->clear();
-        //$_SESSION = array();
+        self::$session->clear();
     }
 
     /**
@@ -56,11 +71,6 @@ class ChamiloSession
      */
     public static function destroy()
     {
-        global $app;
-        $app['session']->invalidate();
-        /*
-        session_unset();
-        $_SESSION = array();
-        session_destroy();*/
+        self::$session->invalidate();
     }
 }

+ 7 - 18
main/inc/lib/usermanager.lib.php

@@ -42,12 +42,11 @@ class UserManager
         'hr_dept_id'
     );
 
-    /**
-     * Empty constructor. This class is mostly static.
-     */
-    public function __construct ()
-    {
+    public static $em;
 
+    public static function setEntityManager($em)
+    {
+        self::$em = $em;
     }
 
     /**
@@ -167,17 +166,6 @@ class UserManager
                 UrlManager::add_user_to_url($user_id, 1);
             }
 
-            global $app;
-            /*
-            $em = $app['orm.ems']['db_write'];
-            // @var Entity\User $user
-            $user = $em->getRepository('Entity\User')->findOneById($user_id);
-            $status = $em->getRepository('Entity\Role')->findOneById($status);
-            $user->roles->add($status);
-            $em->persist($user);
-            $em->flush();*/
-
-
             //saving extra fields
             $field_value = new ExtraFieldValue('user');
             $params['user_id'] = $user_id;
@@ -348,10 +336,11 @@ class UserManager
                 UrlManager::add_user_to_url($return, 1);
             }
 
-            global $app;
+
             // Adding user
             /** @var Entity\User $user */
-            $em = $app['orm.ems']['db_write'];
+            $em = self::$em;
+
             $user = $em->getRepository('Entity\User')->find($return);
             $role = $em->getRepository('Entity\Role')->find($status);
 

+ 4 - 0
src/ChamiloLMS/Component/Auth/LoginListener.php

@@ -18,6 +18,10 @@ class LoginListener
         var_dump($event->getRequest()->getUser());
         */
 
+        $request = $event->getRequest();
+        $session = $request->getSession();
+        \ChamiloSession::setSession($session);
+
         $session = $event->getRequest()->getSession();
         //$session->remove('partThatShouldNotCarryOver');
     }

+ 3 - 2
src/ChamiloLMS/Component/Auth/LoginSuccessHandler.php

@@ -39,9 +39,10 @@ class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
         $user = $token->getUser();
         $userId = $user->getUserId();
 
-        event_login($user);
-
         $session = $request->getSession();
+        \ChamiloSession::setSession($session);
+
+        event_login($user);
 
         // Setting last login datetime
         $session->set('user_last_login_datetime', api_get_utc_datetime());

+ 3 - 0
src/ChamiloLMS/Component/Auth/LogoutSuccessHandler.php

@@ -33,6 +33,9 @@ class LogoutSuccessHandler implements LogoutSuccessHandlerInterface
      */
     public function onLogoutSuccess(Request $request)
     {
+        $session = $request->getSession();
+        \ChamiloSession::setSession($session);
+        
         // Chamilo logout
         $userId = api_get_user_id();
         \Online::logout($userId, false);