Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FtpFileAccessor implements FileAccessorInterface
- {
- /**
- * @var string
- */
- protected $currentPosition;
- /**
- * @var FtpClient
- */
- protected $ftp;
- /**
- * @var array
- */
- protected static $filesList, $dirsList;
- /**
- * @param FtpClient $ftp
- */
- public function __construct(FtpClient $ftp)
- {
- $this->ftp = $ftp;
- }
- /**
- * @param $ip
- * @param $username
- * @param $password
- * @return $this
- */
- public function init($ip, $username, $password)
- {
- try {
- $this->ftp->connect($ip);
- $this->ftp->login($username, $password);
- }
- catch (\Exception $e) {
- // ...
- }
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function listFiles()
- {
- if (!isset(self::$filesList[$this->currentPosition])) {
- $this->updateListing();
- }
- return self::$filesList[$this->currentPosition];
- }
- /**
- * {@inheritdoc}
- */
- public function listDirs()
- {
- if (!isset(self::$dirsList[$this->currentPosition])) {
- $this->updateListing();
- }
- return self::$dirsList[$this->currentPosition];
- }
- /**
- * Update list of files and directories for current folder.
- */
- protected function updateListing()
- {
- try {
- foreach ($this->ftp->scanDir($this->currentPosition) as $fileInfo) {
- switch ($fileInfo['type']) {
- case "directory":
- self::$dirsList[$this->currentPosition][] = $fileInfo['name'];
- break;
- case "file":
- self::$filesList[$this->currentPosition][] = $fileInfo['name'];
- break;
- }
- }
- }
- catch (\Exception $e) {
- // ...
- }
- }
- /**
- * {@inheritdoc}
- */
- public function changeDir($path)
- {
- // handling directory change and save it into $this->currentPosition
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function showFile($fileName)
- {
- // reading file
- return $content; // string file contents
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement