You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.7 KiB

  1. <?php
  2. /**
  3. * @author Alexander Demidov <demidov@dimti.ru>
  4. * Full paths to directories and files contains slash as first symbol
  5. * Dir name not contains slashes on first or last symbols
  6. */
  7. /**
  8. * Class File
  9. */
  10. abstract class File
  11. {
  12. public $path;
  13. public $filename;
  14. private $logger;
  15. private $error_stream;
  16. private $owner;
  17. public function __construct()
  18. {
  19. }
  20. public function setOwner($owner)
  21. {
  22. $this->owner = $owner;
  23. }
  24. public function getOwner()
  25. {
  26. return $this->owner;
  27. }
  28. public function log($message)
  29. {
  30. if (Config::get('LOGGING')) {
  31. if (is_null($this->logger)) {
  32. $this->logger = Logger::getInstance();
  33. }
  34. $this->logger->log($message);
  35. } else {
  36. $this->error_stream = Config::get('ErrorStream', 'php://stderr');
  37. file_put_contents($this->error_stream, PHP_EOL . 'Log ' . '#' . __CLASS__ . ': ' . $message . PHP_EOL, FILE_APPEND);
  38. }
  39. }
  40. /**
  41. * @param array|string|null $data
  42. * @return Image
  43. * @throws ErrorException
  44. */
  45. public static function getInstance($data = null)
  46. {
  47. $instance = new static;
  48. if (!(is_null($data) || $data == '')) {
  49. if (!is_array($data)) {
  50. $data = json_decode($data, true);
  51. if (json_last_error() != JSON_ERROR_NONE) {
  52. $instance->log('Unable to convert json-string to array. Data ' . print_r($data, true));
  53. }
  54. }
  55. if (is_array($data)) {
  56. foreach ($data as $attribute_name => $attribute_value) {
  57. if (property_exists($instance, $attribute_name)) {
  58. $instance->{$attribute_name} = $attribute_value;
  59. }
  60. }
  61. }
  62. }
  63. return $instance;
  64. }
  65. /**
  66. * @param $size string
  67. * @param $force_create bool
  68. * @return ImageVariant
  69. */
  70. public function getImageVariant($size, $force_create = false)
  71. {
  72. /**
  73. * @var $this Image
  74. */
  75. if (!array_key_exists($size, $this->variants)) {
  76. $original_file_path = Config::get('PATH_WEB_ROOT') . '/' . $this->getWebName();
  77. if ($force_create && $this->getIsNoEmpty() && file_exists($original_file_path)) {
  78. Upload::imageVariant($this, $size);
  79. } else {
  80. $this->variants[$size] = new ImageVariant();
  81. }
  82. } else {
  83. if (!is_object($this->variants[$size])) {
  84. $this->variants[$size] = ImageVariant::getInstance($this->variants[$size]);
  85. }
  86. }
  87. return $this->variants[$size];
  88. }
  89. /**
  90. * Если объект содержит информацию о файле/изображение вернет - true
  91. * Если объект пуст - вернет false
  92. * @return bool
  93. */
  94. public function getIsNoEmpty()
  95. {
  96. return (bool) $this->size;
  97. }
  98. public function delete()
  99. {
  100. /**
  101. * @var $image Image
  102. */
  103. if ($this->getIsNoEmpty()) {
  104. @unlink($this->getRealPath());
  105. }
  106. if (get_parent_class($this) == 'Image') {
  107. $image = $this;
  108. foreach ($image->getSizes() as $size) {
  109. $image_variant = $image->getVariant($size);
  110. if ($image_variant->getIsNoEmpty()) {
  111. @unlink($image->getRealPath());
  112. }
  113. }
  114. }
  115. }
  116. public function getRealPath()
  117. {
  118. return PATH_WEB_ROOT . '/' . $this->path . '/' . $this->filename;
  119. }
  120. /**
  121. * @return string
  122. */
  123. public function getWebName()
  124. {
  125. return (($this->getIsNoEmpty()) ? ($this->path . '/' . $this->filename) : '_.gif');
  126. }
  127. public function __toString()
  128. {
  129. return FileHelper::toString($this);
  130. }
  131. }