PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/application/protected/extensions/yiiext/components/filesystem/EFileMetaData.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 154 lines | 73 code | 2 blank | 79 comment | 4 complexity | 38cb2af275894682f87c25c761317013 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * EFileMetaData represents the meta-data for an EFile class.
  4. *
  5. * @author Veaceslav Medvedev <slavcopost@gmail.com>
  6. * @version 0.1
  7. * @package yiiext.filesystem
  8. *
  9. * @property $name
  10. * @property $path
  11. * @property $dirName
  12. * @property $type
  13. * @property $extension
  14. * @property $mime
  15. * @property $isDir
  16. * @property $size
  17. * @property $permission
  18. * @property $modifiedTime
  19. * @property $accessedTime
  20. */
  21. class EFileMetaData {
  22. /**
  23. *
  24. */
  25. public static $attributeLabels = array(
  26. 'name', 'path', 'dirName', 'type', 'extension', 'mime', 'isDir',
  27. 'size', 'permissions', 'modifiedTime', 'accessedTime'
  28. );
  29. /**
  30. *
  31. */
  32. public $attributes = array();
  33. /**
  34. *
  35. */
  36. protected $_file;
  37. /**
  38. *
  39. */
  40. protected $_filePath;
  41. /**
  42. * @return array
  43. */
  44. public function toArray() {
  45. $this->loadAttributes();
  46. return $this->attributes;
  47. }
  48. /**
  49. * @param $file
  50. * @param boolean $loadAttributes
  51. * @return void
  52. */
  53. public function __construct($file, $loadAttributes = FALSE) {
  54. $this->_file = $file;
  55. $this->_filePath = $file->getFilePath();
  56. clearstatcache(TRUE, $this->_filePath);
  57. if ($loadAttributes) {
  58. $this->loadAttributes();
  59. }
  60. }
  61. /**
  62. * @return EFileMetaData
  63. */
  64. public function loadAttributes() {
  65. foreach (self::$attributeLabels as $attribute) {
  66. $this->getAttribute($attribute);
  67. }
  68. return $this;
  69. }
  70. /**
  71. * @param $attribute
  72. * @return array
  73. */
  74. public function getAttribute($attribute) {
  75. if (!isset($this->attributes[$attribute])) {
  76. $getter = 'get' . $attribute;
  77. if (method_exists($this, $getter)) {
  78. $this->attributes[$attribute] = $this->$getter();
  79. }
  80. else {
  81. return NULL;
  82. }
  83. }
  84. return $this->attributes[$attribute];
  85. }
  86. /**
  87. * @return string
  88. */
  89. public function getIsDir() {
  90. return is_dir($this->_filePath);
  91. }
  92. /**
  93. * @return string
  94. */
  95. public function getName() {
  96. return basename($this->_filePath);
  97. }
  98. /**
  99. * @return string
  100. */
  101. public function getPath() {
  102. return $this->_filePath;
  103. }
  104. /**
  105. * @return string
  106. */
  107. public function getDirName() {
  108. return dirname($this->_filePath);
  109. }
  110. /**
  111. * @return string
  112. */
  113. public function getType() {
  114. return filetype($this->_filePath);
  115. }
  116. /**
  117. * @return string
  118. */
  119. public function getExtension() {
  120. return EFileHelper::fileExtension($this->_filePath);
  121. }
  122. /**
  123. * @return string
  124. */
  125. public function getMime() {
  126. return EFileHelper::getMimeType($this->_filePath);
  127. }
  128. /**
  129. * @return string
  130. */
  131. public function getSize() {
  132. return EFileHelper::fileSize($this->_filePath);
  133. }
  134. /**
  135. * @return integer
  136. */
  137. public function getPermissions() {
  138. return fileperms($this->_filePath);
  139. }
  140. /**
  141. * @return integer
  142. */
  143. public function getModifiedTime() {
  144. return filemtime($this->_filePath);
  145. }
  146. /**
  147. * @return integer
  148. */
  149. public function getAccessedTime() {
  150. return fileatime($this->_filePath);
  151. }
  152. }