Csvfile.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Class that represents profiler output in CSV-file format
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Profiler\Driver\Standard\Output;
  9. use Magento\Framework\Profiler\Driver\Standard\AbstractOutput;
  10. use Magento\Framework\Profiler\Driver\Standard\Stat;
  11. class Csvfile extends AbstractOutput
  12. {
  13. const DEFAULT_FILEPATH = '/var/log/profiler.csv';
  14. /**
  15. *
  16. * @var string
  17. */
  18. protected $_filePath;
  19. /**
  20. * @var string
  21. */
  22. protected $_delimiter;
  23. /**
  24. * @var string
  25. */
  26. protected $_enclosure;
  27. /**
  28. * Constructor
  29. *
  30. * @param array|null $config
  31. */
  32. public function __construct(array $config = null)
  33. {
  34. parent::__construct($config);
  35. $this->_filePath = $this->_parseFilePath($config);
  36. $this->_delimiter = isset($config['delimiter']) ? $config['delimiter'] : ',';
  37. $this->_enclosure = isset($config['enclosure']) ? $config['enclosure'] : '"';
  38. }
  39. /**
  40. * Parses file path
  41. *
  42. * @param array|null $config
  43. * @return string
  44. */
  45. protected function _parseFilePath(array $config = null)
  46. {
  47. $result = isset($config['filePath']) ? $config['filePath'] : self::DEFAULT_FILEPATH;
  48. if (isset($config['baseDir'])) {
  49. $result = rtrim($config['baseDir'], '/') . '/' . ltrim($result, '/');
  50. }
  51. return $result;
  52. }
  53. /**
  54. * Write profiling results to CSV-file
  55. *
  56. * @param Stat $stat
  57. * @return void
  58. * @throws \RuntimeException if output file cannot be opened
  59. */
  60. public function display(Stat $stat)
  61. {
  62. $fileHandle = fopen($this->_filePath, 'w');
  63. if (!$fileHandle) {
  64. throw new \RuntimeException(sprintf('Can not open a file "%s".', $this->_filePath));
  65. }
  66. $lockRequired = strpos($this->_filePath, 'php://') !== 0;
  67. $isLocked = false;
  68. while ($lockRequired && !$isLocked) {
  69. $isLocked = flock($fileHandle, LOCK_EX);
  70. }
  71. $this->_writeFileContent($fileHandle, $stat);
  72. if ($isLocked) {
  73. flock($fileHandle, LOCK_UN);
  74. }
  75. fclose($fileHandle);
  76. }
  77. /**
  78. * Write content into an opened file handle
  79. *
  80. * @param resource $fileHandle
  81. * @param Stat $stat
  82. * @return void
  83. */
  84. protected function _writeFileContent($fileHandle, Stat $stat)
  85. {
  86. foreach ($this->_getTimerIds($stat) as $timerName) {
  87. $row = [];
  88. foreach ($this->_columns as $column) {
  89. $row[] = $this->_renderColumnValue($stat->fetch($timerName, $column), $column);
  90. }
  91. fputcsv($fileHandle, $row, $this->_delimiter, $this->_enclosure);
  92. }
  93. }
  94. }