AntonLV 8 лет назад
Родитель
Сommit
43999a9bd1
2 измененных файлов с 154 добавлено и 29 удалено
  1. 110 0
      inc/classes/BxDolLog.php
  2. 44 29
      studio/classes/BxDolStudioInstallerUtils.php

+ 110 - 0
inc/classes/BxDolLog.php

@@ -0,0 +1,110 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+class BxDolLog extends BxDolFactory implements iBxDolSingleton
+{
+    protected $_sFile;
+    protected $_sName;
+
+    protected function __construct()
+    {
+        if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
+            trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
+
+        parent::__construct();
+    }
+
+    /**
+     * Prevent cloning the instance
+     */
+    public function __clone()
+    {
+        if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
+            trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
+    }
+
+    /**
+     * Get singleton instance of the class
+     */
+    public static function getInstance()
+    {
+        if (!isset($GLOBALS['bxDolClasses'][__CLASS__]))
+            $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolLog();
+
+        $GLOBALS['bxDolClasses'][__CLASS__]->reset();
+        return $GLOBALS['bxDolClasses'][__CLASS__];
+    }
+
+    /**
+     * Reset custom log file and log name 
+     * to be empty to ready for future usage.
+     */
+    public function reset()
+    {
+        $this->_sFile = '';
+        $this->_sName = '';
+    }
+
+    /**
+     * Set custom log file. The file should have necessary permissions.
+     * @param string $sFile - full path to custom log file.
+     */
+    public function setFile($sFile)
+    {
+        $this->_sFile = $sFile;
+    }
+
+    /**
+     * Set log name. Log file will be created\updated in standard log directory.
+     * @param string $sName - log file name without path and extension.
+     */
+    public function setName($sName)
+    {
+        $this->_sName =  $sName;
+    }
+
+    /**
+     * Writes content to a log file. Can get any number of arguments to be written 
+     * in one write session. Each argument can be a string, an array or an object.
+     */
+    public function write()
+    {
+        if(func_num_args() == 0)
+            return;
+
+        try {
+            $sFile = '';
+
+            if(!empty($this->_sFile))
+                $sFile = $this->_sFile;
+            else 
+                $sFile = BX_DIRECTORY_PATH_LOGS . '/' . $this->_sName . '.log';
+
+            if(empty($sFile))
+                return;
+
+            file_put_contents($sFile, date('m-d H:i:s') . ":\n", FILE_APPEND);
+
+            $aArgs = func_get_args();
+            foreach($aArgs as $mixedArg) {
+                if(is_array($mixedArg))
+                    $mixedArg = var_export($mixedArg, true);	
+                else if(is_object($mixedArg))
+                    $mixedArg = json_encode($mixedArg);
+
+                file_put_contents($sFile, $mixedArg . "\n", FILE_APPEND);
+            }
+        }
+        catch (Exception $oException) {
+            echo 'Error: ' . $oException->getMessage();
+        }
+    }
+}
+
+/** @} */

+ 44 - 29
studio/classes/BxDolStudioInstallerUtils.php

@@ -259,44 +259,59 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
 
     public function performModulesUpgrade($bDirectly = true, $bEmailNotify = true)
     {
-    	if(!defined('BX_DOL_CRON_EXECUTE') && !$bDirectly)
-    		return $this->addTransientJob(self::getNamePerformModulesUpgrade(), 'perform_modules_upgrade', array($bEmailNotify));   	
+        if(!defined('BX_DOL_CRON_EXECUTE') && !$bDirectly)
+            return $this->addTransientJob(self::getNamePerformModulesUpgrade(), 'perform_modules_upgrade', array($bEmailNotify));   	
+
+        $oLog = BxDolLog::getInstance();
+        $oLog->setName('upgrade_modules');
 
         $aFailed = array();
-		$aUpdates = $this->checkUpdates();
-		if(empty($aUpdates) || !is_array($aUpdates))
-			return true;
+        $aUpdates = $this->checkUpdates();
+        if(empty($aUpdates) || !is_array($aUpdates)) {
+            $oLog->write('Cannot get a list of modules which require to be updated.');
+            return true;
+        }
 
-	    foreach($aUpdates as $aUpdate) {
-	    	$mixedResult = $this->downloadUpdatePublic($aUpdate['name']);
-	        if($mixedResult !== true) {
-	        	$aFailed[$aUpdate['name']] = $mixedResult;
-				continue;
-			}
-		}
+        foreach($aUpdates as $aUpdate) {
+            $mixedResult = $this->downloadUpdatePublic($aUpdate['name']);
+            if($mixedResult !== true) {
+                $aFailed[$aUpdate['name']] = $mixedResult;
+                continue;
+            }
+        }
 
-		$aSuccess = array();
-		$aUpdates = $this->getUpdates();
-		if(empty($aUpdates) || !is_array($aUpdates))
-			return true;
+        $aSuccess = array();
+        $aUpdates = $this->getUpdates();
+        if(empty($aUpdates) || !is_array($aUpdates)) {
+            $oLog->write('Cannot find update scripts for modules. They are damaged or were not downloaded.');
+            return true;
+        }
 
-		foreach($aUpdates as $aUpdate) {
-			$aResult = $this->perform($aUpdate['dir'], 'update');
-			if((int)$aResult['code'] != 0) {
-				$aFailed[$aUpdate['module_name']] = $aResult['message'];
-				continue;
-			}
+        foreach($aUpdates as $aUpdate) {
+            $aResult = $this->perform($aUpdate['dir'], 'update');
+            if((int)$aResult['code'] != 0) {
+                $aFailed[$aUpdate['module_name']] = $aResult['message'];
+                continue;
+            }
 
-			$aSuccess[$aUpdate['module_name']] = $aUpdate['version_to'];
-		}
+            $aSuccess[$aUpdate['module_name']] = $aUpdate['version_to'];
+        }
 
-    	if($bEmailNotify && !empty($aSuccess))
-    		$this->emailNotifyModulesUpgrade('success', $aSuccess);
+        if(!empty($aFailed)) {
+            $oLog->write('Failed to update modules:', $aFailed);
 
-        if($bEmailNotify && !empty($aFailed))
-        	$this->emailNotifyModulesUpgrade('failed', $aFailed);
+            if($bEmailNotify)
+                $this->emailNotifyModulesUpgrade('failed', $aFailed);
+        }
+
+        if(!empty($aSuccess)) {
+            $oLog->write('Successfully updated modules:', $aSuccess);
+
+            if($bEmailNotify)
+                $this->emailNotifyModulesUpgrade('success', $aSuccess);
+        }
 
-		return empty($aFailed);
+        return empty($aFailed);
     }
 
     public function checkModules($bAuthorizedAccess = false)