/app/code/core/Mage/ImportExport/Model/Abstract.php

https://gitlab.com/LisovyiEvhenii/ismextensions · PHP · 135 lines · 54 code · 9 blank · 72 comment · 5 complexity · 40838be4ecbacd9411cce97a8e33fd59 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_ImportExport
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Operation abstract class
  28. *
  29. * @category Mage
  30. * @package Mage_ImportExport
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_ImportExport_Model_Abstract extends Varien_Object
  34. {
  35. /**
  36. * Log directory
  37. *
  38. */
  39. const LOG_DIRECTORY = 'log/import_export/';
  40. /**
  41. * Enable loging
  42. *
  43. * @var boolean
  44. */
  45. protected $_debugMode = false;
  46. /**
  47. * Loger instance
  48. * @var Mage_Core_Model_Log_Adapter
  49. */
  50. protected $_logInstance;
  51. /**
  52. * Fields that should be replaced in debug with '***'
  53. *
  54. * @var array
  55. */
  56. protected $_debugReplacePrivateDataKeys = array();
  57. /**
  58. * Contains all log information
  59. *
  60. * @var array
  61. */
  62. protected $_logTrace = array();
  63. /**
  64. * Log debug data to file.
  65. * Log file dir: var/log/import_export/%Y/%m/%d/%time%_%operation_type%_%entity_type%.log
  66. *
  67. * @param mixed $debugData
  68. * @return Mage_ImportExport_Model_Abstract
  69. */
  70. public function addLogComment($debugData)
  71. {
  72. if (is_array($debugData)) {
  73. $this->_logTrace = array_merge($this->_logTrace, $debugData);
  74. } else {
  75. $this->_logTrace[] = $debugData;
  76. }
  77. if (!$this->_debugMode) {
  78. return $this;
  79. }
  80. if (!$this->_logInstance) {
  81. $dirName = date('Y' . DS .'m' . DS .'d' . DS);
  82. $fileName = join('_', array(
  83. str_replace(':', '-', $this->getRunAt()),
  84. $this->getScheduledOperationId(),
  85. $this->getOperationType(),
  86. $this->getEntity()
  87. ));
  88. $dirPath = Mage::getBaseDir('var') . DS . self::LOG_DIRECTORY
  89. . $dirName;
  90. if (!is_dir($dirPath)) {
  91. mkdir($dirPath, 0750, true);
  92. }
  93. $fileName = substr(strstr(self::LOG_DIRECTORY, DS), 1)
  94. . $dirName . $fileName . '.log';
  95. $this->_logInstance = Mage::getModel('core/log_adapter', $fileName)
  96. ->setFilterDataKeys($this->_debugReplacePrivateDataKeys);
  97. }
  98. $this->_logInstance->log($debugData);
  99. return $this;
  100. }
  101. /**
  102. * Return human readable debug trace.
  103. *
  104. * @return array
  105. */
  106. public function getFormatedLogTrace()
  107. {
  108. $trace = '';
  109. $lineNumber = 1;
  110. foreach ($this->_logTrace as &$info) {
  111. $trace .= $lineNumber++ . ': ' . $info . "\n";
  112. }
  113. return $trace;
  114. }
  115. /**
  116. * Sets debug mode
  117. *
  118. * @param bool $mode
  119. * @return Mage_ImportExport_Model_Abstract
  120. */
  121. public function setDebugMode($mode = true)
  122. {
  123. $this->_debugMode = (bool)$mode;
  124. return $this;
  125. }
  126. }