Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. class FtpFileAccessor implements FileAccessorInterface
  2. {
  3. /**
  4. * @var string
  5. */
  6. protected $currentPosition;
  7.  
  8. /**
  9. * @var FtpClient
  10. */
  11. protected $ftp;
  12.  
  13. /**
  14. * @var array
  15. */
  16. protected static $filesList, $dirsList;
  17.  
  18. /**
  19. * @param FtpClient $ftp
  20. */
  21. public function __construct(FtpClient $ftp)
  22. {
  23. $this->ftp = $ftp;
  24. }
  25.  
  26. /**
  27. * @param $ip
  28. * @param $username
  29. * @param $password
  30. * @return $this
  31. */
  32. public function init($ip, $username, $password)
  33. {
  34. try {
  35. $this->ftp->connect($ip);
  36. $this->ftp->login($username, $password);
  37. }
  38. catch (\Exception $e) {
  39. // ...
  40. }
  41.  
  42. return $this;
  43. }
  44.  
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function listFiles()
  49. {
  50. if (!isset(self::$filesList[$this->currentPosition])) {
  51. $this->updateListing();
  52. }
  53.  
  54. return self::$filesList[$this->currentPosition];
  55. }
  56.  
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function listDirs()
  61. {
  62. if (!isset(self::$dirsList[$this->currentPosition])) {
  63. $this->updateListing();
  64. }
  65.  
  66. return self::$dirsList[$this->currentPosition];
  67. }
  68.  
  69. /**
  70. * Update list of files and directories for current folder.
  71. */
  72. protected function updateListing()
  73. {
  74. try {
  75. foreach ($this->ftp->scanDir($this->currentPosition) as $fileInfo) {
  76. switch ($fileInfo['type']) {
  77. case "directory":
  78. self::$dirsList[$this->currentPosition][] = $fileInfo['name'];
  79. break;
  80.  
  81. case "file":
  82. self::$filesList[$this->currentPosition][] = $fileInfo['name'];
  83. break;
  84. }
  85. }
  86. }
  87. catch (\Exception $e) {
  88. // ...
  89. }
  90. }
  91.  
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function changeDir($path)
  96. {
  97. // handling directory change and save it into $this->currentPosition
  98.  
  99. return $this;
  100. }
  101.  
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function showFile($fileName)
  106. {
  107. // reading file
  108.  
  109. return $content; // string file contents
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement