Skip to content

Commit a95e9c7

Browse files
mazhalaieddielau
authored andcommitted
MAGETWO-31850: [GITHUB] install.log can not be created with open_basedir restriction
- moved install.log to var/log
1 parent 24a7e92 commit a95e9c7

File tree

2 files changed

+37
-57
lines changed

2 files changed

+37
-57
lines changed

setup/module/Magento/Setup/src/Controller/DatabaseCheck.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
namespace Magento\Setup\Controller;
66

7+
use Magento\Framework\Filesystem;
78
use Magento\Setup\Model\InstallerFactory;
89
use Magento\Setup\Model\WebLogger;
910
use Zend\Json\Json;
@@ -19,14 +20,23 @@ class DatabaseCheck extends AbstractActionController
1920
*/
2021
private $installerFactory;
2122

23+
/**
24+
* Filesystem to access log
25+
*
26+
* @var Filesystem
27+
*/
28+
private $filesystem;
29+
2230
/**
2331
* Constructor
2432
*
2533
* @param InstallerFactory $installerFactory
34+
* @param Filesystem $filesystem
2635
*/
27-
public function __construct(InstallerFactory $installerFactory)
36+
public function __construct(InstallerFactory $installerFactory, Filesystem $filesystem)
2837
{
2938
$this->installerFactory = $installerFactory;
39+
$this->filesystem = $filesystem;
3040
}
3141

3242
/**
@@ -38,7 +48,7 @@ public function indexAction()
3848
{
3949
$params = Json::decode($this->getRequest()->getContent(), Json::TYPE_ARRAY);
4050
try {
41-
$installer = $this->installerFactory->create(new WebLogger());
51+
$installer = $this->installerFactory->create(new WebLogger($this->filesystem));
4252
$password = isset($params['password']) ? $params['password'] : '';
4353
$installer->checkDatabaseConnection($params['name'], $params['host'], $params['user'], $password);
4454
return new JsonModel(['success' => true]);

setup/module/Magento/Setup/src/Model/WebLogger.php

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace Magento\Setup\Model;
77

8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Filesystem;
10+
811
/**
912
* Web UI Logger
1013
*
@@ -22,16 +25,17 @@ class WebLogger implements LoggerInterface
2225
/**
2326
* Currently open file resource
2427
*
25-
* @var resource
28+
* @var Filesystem
2629
*/
27-
protected $resource;
30+
protected $filesystem;
31+
2832

2933
/**
30-
* Whether the log contains an error message
34+
* Currently open file resource
3135
*
32-
* @var bool
36+
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
3337
*/
34-
protected $hasError = false;
38+
protected $directory;
3539

3640
/**
3741
* Indicator of whether inline output is started
@@ -42,31 +46,11 @@ class WebLogger implements LoggerInterface
4246

4347
/**
4448
* Constructor
49+
* @param Filesystem $filesystem
4550
*/
46-
public function __construct()
47-
{
48-
$this->logFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $this->logFile;
49-
}
50-
51-
/**
52-
* Opens log file in the specified mode
53-
*
54-
* @param string $mode
55-
* @return void
56-
*/
57-
private function open($mode)
58-
{
59-
$this->resource = fopen($this->logFile, $mode);
60-
}
61-
62-
/**
63-
* Closes the log file
64-
*
65-
* @return void
66-
*/
67-
private function close()
51+
public function __construct(Filesystem $filesystem)
6852
{
69-
fclose($this->resource);
53+
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::LOG);
7054
}
7155

7256
/**
@@ -84,7 +68,14 @@ public function logSuccess($message)
8468
public function logError(\Exception $e)
8569
{
8670
$this->terminateLine();
87-
$this->writeToFile('<span class="text-danger">[ERROR] ' . $e . '<span><br/>');
71+
$stackTrace = $e->getTrace();
72+
$this->writeToFile('<span class="text-danger">[ERROR] ' . $e->getMessage() . '<br/>');
73+
foreach ($stackTrace as $errorLine) {
74+
if (isset($errorLine['file'])) {
75+
$this->writeToFile($errorLine['file'] . ' ' . $errorLine['line'] . '<br/>');
76+
}
77+
}
78+
$this->writeToFile('<span><br/>');
8879
}
8980

9081
/**
@@ -122,19 +113,7 @@ public function logMeta($message)
122113
*/
123114
private function writeToFile($message)
124115
{
125-
$this->open('a+');
126-
fwrite($this->resource, $message);
127-
$this->close();
128-
}
129-
130-
/**
131-
* Whether there is an error in the log
132-
*
133-
* @return bool
134-
*/
135-
public function hasError()
136-
{
137-
return $this->hasError;
116+
$this->directory->writeFile($this->logFile, $message, 'a+');
138117
}
139118

140119
/**
@@ -144,17 +123,8 @@ public function hasError()
144123
*/
145124
public function get()
146125
{
147-
$this->open('r+');
148-
fseek($this->resource, 0);
149-
$messages = [];
150-
while (($string = fgets($this->resource)) !== false) {
151-
if (strpos($string, '[ERROR]') !== false) {
152-
$this->hasError = true;
153-
}
154-
$messages[] = $string;
155-
}
156-
$this->close();
157-
return $messages;
126+
$fileContents = explode('\n', $this->directory->readFile($this->logFile));
127+
return $fileContents;
158128
}
159129

160130
/**
@@ -164,8 +134,8 @@ public function get()
164134
*/
165135
public function clear()
166136
{
167-
if (file_exists($this->logFile)) {
168-
unlink($this->logFile);
137+
if ($this->directory->isExist($this->logFile)) {
138+
$this->directory->delete($this->logFile);
169139
}
170140
}
171141

0 commit comments

Comments
 (0)