/wp-content/plugins/loco-translate/src/mvc/FileParams.php

https://gitlab.com/najomie/fit-hippie · PHP · 216 lines · 111 code · 39 blank · 66 comment · 11 complexity · f4c2594f8fd6dd5e37ebbd40ca49aa32 MD5 · raw file

  1. <?php
  2. /**
  3. * Abstracts information about a file into a view parameter object.
  4. */
  5. class Loco_mvc_FileParams extends Loco_mvc_ViewParams {
  6. /**
  7. * File reference from which to take live properties.
  8. * @var Loco_fs_File
  9. */
  10. private $file;
  11. /**
  12. * Print property as a number of bytes in larger denominations
  13. */
  14. public static function renderBytes( $n ){
  15. $i = 0;
  16. $dp = 0;
  17. while( $n >= 1024 ){
  18. $i++;
  19. $dp++;
  20. $n /= 1024;
  21. }
  22. $s = number_format( $n, $dp, '.', ',' );
  23. // trim trailing zeros from decimal places
  24. $a = explode('.',$s);
  25. if( isset($a[1]) ){
  26. $s = $a[0];
  27. $d = trim($a[1],'0') and $s .= '.'.$d;
  28. }
  29. $units = array( ' bytes', ' KB', ' MB', ' GB', ' TB' );
  30. $s .= $units[$i];
  31. return $s;
  32. }
  33. /**
  34. * @return Loco_mvc_FileParams
  35. */
  36. public static function create( Loco_fs_File $file ) {
  37. return new Loco_mvc_FileParams( array(), $file );
  38. }
  39. /**
  40. * Override does lazy property initialization
  41. */
  42. public function __construct( array $props = array(), Loco_fs_File $file ){
  43. parent::__construct( array (
  44. 'name' => '',
  45. 'path' => '',
  46. 'relpath' => '',
  47. 'reltime' => '',
  48. 'bytes' => 0,
  49. 'size' => '',
  50. 'imode' => '',
  51. 'smode' => '',
  52. 'owner' => '',
  53. 'group' => '',
  54. ) + $props );
  55. $this->file = $file;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. * Override to get live information from file object
  60. */
  61. public function offsetGet( $prop ){
  62. $getter = array( $this, '_get_'.$prop );
  63. if( is_callable($getter) ){
  64. return call_user_func( $getter );
  65. }
  66. return parent::offsetGet($prop);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. * Override to ensure all properties populated
  71. */
  72. public function getArrayCopy(){
  73. $a = array();
  74. foreach( $this as $prop => $dflt ){
  75. $a[$prop] = $this[$prop];
  76. }
  77. return $a;
  78. }
  79. /**
  80. * @return string
  81. */
  82. private function _get_name(){
  83. return $this->file->basename();
  84. }
  85. /**
  86. * @return string
  87. */
  88. private function _get_path(){
  89. return $this->file->getPath();
  90. }
  91. /**
  92. * @return string
  93. */
  94. private function _get_relpath(){
  95. $base = loco_constant('WP_CONTENT_DIR');
  96. return $this->file->getRelativePath($base);
  97. }
  98. /**
  99. * Using slightly modified version of WordPress's Human time differencing
  100. * + Added "Just now" when in the last 30 seconds
  101. * TODO possibly replace with custom function that includes "Yesterday" etc..
  102. */
  103. private function _get_reltime(){
  104. $time = $this->file->modified();
  105. $time_diff = time() - $time;
  106. // use same time format as posts listing when in future or more than a day ago
  107. if( $time_diff < 0 || $time_diff >= 86400 ){
  108. return date_i18n( __('Y/m/d','default'), $time );
  109. }
  110. if( $time_diff < 30 ){
  111. // translators: relative time when something happened in the last 30 seconds
  112. return __('Just now','loco');
  113. }
  114. return sprintf( __('%s ago','default'), human_time_diff($time) );
  115. }
  116. /**
  117. * @return int
  118. */
  119. private function _get_bytes(){
  120. return $this->file->size();
  121. }
  122. /**
  123. * @return string
  124. */
  125. private function _get_size(){
  126. return self::renderBytes( $this->_get_bytes() );
  127. }
  128. /**
  129. * Get octal file mode
  130. * @return string
  131. */
  132. private function _get_imode(){
  133. $mode = new Loco_fs_FileMode( $this->file->mode() );
  134. return (string) $mode;
  135. }
  136. /**
  137. * Get rwx file mode
  138. * @return string
  139. */
  140. private function _get_smode(){
  141. $mode = new Loco_fs_FileMode( $this->file->mode() );
  142. return $mode->format();
  143. }
  144. /**
  145. * Get file owner name
  146. * @return string
  147. */
  148. private function _get_owner(){
  149. if( ( $uid = $this->file->uid() ) && function_exists('posix_getpwuid') && ( $a = posix_getpwuid($uid) ) ){
  150. return $a['name'];
  151. }
  152. return sprintf('%u',$uid);
  153. }
  154. /**
  155. * Get group owner name
  156. * @return string
  157. */
  158. private function _get_group(){
  159. if( ( $gid = $this->file->gid() ) && function_exists('posix_getpwuid') && ( $a = posix_getgrgid($gid) ) ){
  160. return $a['name'];
  161. }
  162. return sprintf('%u',$gid);
  163. }
  164. /**
  165. * Print pseudo console line
  166. */
  167. public function ls(){
  168. $this->e('smode');
  169. echo ' ';
  170. $this->e('owner');
  171. echo ':';
  172. $this->e('group');
  173. echo ' ';
  174. $this->e('relpath');
  175. return '';
  176. }
  177. }