Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Csvfile.php
Go to the documentation of this file.
1 <?php
9 
12 
13 class Csvfile extends AbstractOutput
14 {
15  const DEFAULT_FILEPATH = '/var/log/profiler.csv';
16 
21  protected $_filePath;
22 
26  protected $_delimiter;
27 
31  protected $_enclosure;
32 
38  public function __construct(array $config = null)
39  {
40  parent::__construct($config);
41  $this->_filePath = $this->_parseFilePath($config);
42  $this->_delimiter = isset($config['delimiter']) ? $config['delimiter'] : ',';
43  $this->_enclosure = isset($config['enclosure']) ? $config['enclosure'] : '"';
44  }
45 
52  protected function _parseFilePath(array $config = null)
53  {
54  $result = isset($config['filePath']) ? $config['filePath'] : self::DEFAULT_FILEPATH;
55  if (isset($config['baseDir'])) {
56  $result = rtrim($config['baseDir'], '/') . '/' . ltrim($result, '/');
57  }
58  return $result;
59  }
60 
68  public function display(Stat $stat)
69  {
70  $fileHandle = fopen($this->_filePath, 'w');
71  if (!$fileHandle) {
72  throw new \RuntimeException(sprintf('Can not open a file "%s".', $this->_filePath));
73  }
74 
75  $lockRequired = strpos($this->_filePath, 'php://') !== 0;
76  $isLocked = false;
77  while ($lockRequired && !$isLocked) {
78  $isLocked = flock($fileHandle, LOCK_EX);
79  }
80  $this->_writeFileContent($fileHandle, $stat);
81  if ($isLocked) {
82  flock($fileHandle, LOCK_UN);
83  }
84  fclose($fileHandle);
85  }
86 
94  protected function _writeFileContent($fileHandle, Stat $stat)
95  {
96  foreach ($this->_getTimerIds($stat) as $timerName) {
97  $row = [];
98  foreach ($this->_columns as $column) {
99  $row[] = $this->_renderColumnValue($stat->fetch($timerName, $column), $column);
100  }
101  fputcsv($fileHandle, $row, $this->_delimiter, $this->_enclosure);
102  }
103  }
104 }
$config
Definition: fraud_order.php:17