diff --git a/setup/module/Magento/Setup/src/Controller/DatabaseCheck.php b/setup/module/Magento/Setup/src/Controller/DatabaseCheck.php index 00765648b030c..34284da0a7e6f 100644 --- a/setup/module/Magento/Setup/src/Controller/DatabaseCheck.php +++ b/setup/module/Magento/Setup/src/Controller/DatabaseCheck.php @@ -4,6 +4,7 @@ */ namespace Magento\Setup\Controller; +use Magento\Framework\Filesystem; use Magento\Setup\Model\InstallerFactory; use Magento\Setup\Model\WebLogger; use Zend\Json\Json; @@ -19,14 +20,23 @@ class DatabaseCheck extends AbstractActionController */ private $installerFactory; + /** + * Filesystem to access log + * + * @var Filesystem + */ + private $filesystem; + /** * Constructor * * @param InstallerFactory $installerFactory + * @param Filesystem $filesystem */ - public function __construct(InstallerFactory $installerFactory) + public function __construct(InstallerFactory $installerFactory, Filesystem $filesystem) { $this->installerFactory = $installerFactory; + $this->filesystem = $filesystem; } /** @@ -38,7 +48,7 @@ public function indexAction() { $params = Json::decode($this->getRequest()->getContent(), Json::TYPE_ARRAY); try { - $installer = $this->installerFactory->create(new WebLogger()); + $installer = $this->installerFactory->create(new WebLogger($this->filesystem)); $password = isset($params['password']) ? $params['password'] : ''; $installer->checkDatabaseConnection($params['name'], $params['host'], $params['user'], $password); return new JsonModel(['success' => true]); diff --git a/setup/module/Magento/Setup/src/Model/WebLogger.php b/setup/module/Magento/Setup/src/Model/WebLogger.php index 28dde3a0b2b76..328d8823df51c 100644 --- a/setup/module/Magento/Setup/src/Model/WebLogger.php +++ b/setup/module/Magento/Setup/src/Model/WebLogger.php @@ -5,6 +5,9 @@ namespace Magento\Setup\Model; +use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\Filesystem; + /** * Web UI Logger * @@ -22,16 +25,17 @@ class WebLogger implements LoggerInterface /** * Currently open file resource * - * @var resource + * @var Filesystem */ - protected $resource; + protected $filesystem; + /** - * Whether the log contains an error message + * Currently open file resource * - * @var bool + * @var \Magento\Framework\Filesystem\Directory\WriteInterface */ - protected $hasError = false; + protected $directory; /** * Indicator of whether inline output is started @@ -42,31 +46,11 @@ class WebLogger implements LoggerInterface /** * Constructor + * @param Filesystem $filesystem */ - public function __construct() - { - $this->logFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $this->logFile; - } - - /** - * Opens log file in the specified mode - * - * @param string $mode - * @return void - */ - private function open($mode) - { - $this->resource = fopen($this->logFile, $mode); - } - - /** - * Closes the log file - * - * @return void - */ - private function close() + public function __construct(Filesystem $filesystem) { - fclose($this->resource); + $this->directory = $filesystem->getDirectoryWrite(DirectoryList::LOG); } /** @@ -84,7 +68,14 @@ public function logSuccess($message) public function logError(\Exception $e) { $this->terminateLine(); - $this->writeToFile('[ERROR] ' . $e . '
'); + $stackTrace = $e->getTrace(); + $this->writeToFile('[ERROR] ' . $e->getMessage() . '
'); + foreach ($stackTrace as $errorLine) { + if (isset($errorLine['file'])) { + $this->writeToFile($errorLine['file'] . ' ' . $errorLine['line'] . '
'); + } + } + $this->writeToFile('
'); } /** @@ -122,19 +113,7 @@ public function logMeta($message) */ private function writeToFile($message) { - $this->open('a+'); - fwrite($this->resource, $message); - $this->close(); - } - - /** - * Whether there is an error in the log - * - * @return bool - */ - public function hasError() - { - return $this->hasError; + $this->directory->writeFile($this->logFile, $message, 'a+'); } /** @@ -144,17 +123,8 @@ public function hasError() */ public function get() { - $this->open('r+'); - fseek($this->resource, 0); - $messages = []; - while (($string = fgets($this->resource)) !== false) { - if (strpos($string, '[ERROR]') !== false) { - $this->hasError = true; - } - $messages[] = $string; - } - $this->close(); - return $messages; + $fileContents = explode('\n', $this->directory->readFile($this->logFile)); + return $fileContents; } /** @@ -164,8 +134,8 @@ public function get() */ public function clear() { - if (file_exists($this->logFile)) { - unlink($this->logFile); + if ($this->directory->isExist($this->logFile)) { + $this->directory->delete($this->logFile); } }