[ PHP ] FTP転送とSMBファイル転送をBuilderパターンで実装する

Pocket

ここでは、FTP転送とSMBファイル転送(通常のファイル転送)をデザインパターンの1つである Builder パターンで実装するサンプルスクリプトを掲載しています。

スポンサーリンク

Builder パターン

Builder パターンとは、オブジェクトの生成過程を抽象化することによって、動的なオブジェクトの生成を可能にします。

Builder パターンで重要な点は、オブジェクトの生成手順を把握しているオブジェクトが1つであるということです。つまり、個々のオブジェクトの生成手順は同一となります(同一となるように実装する)。

FTP転送とSMBファイル転送の実装サンプル

次は、FTPファイル転送とSMBファイル転送を Builder パターンで実装するサンプルスクリプトになります。同一の手順で呼び出し、個々のオブジェクトがそれぞれの処理を行っています。

ちょっとダラダラと長くなりました (;^_^A

// Builder によって生成される結果オブジェクト
class result
{
    private $_result;

    public function setResult($result) { $this->_result = $result; }
    public function getResult() { return $this->_result; }
}

// ファイル転送 Builder インターフェース
interface fileSendBulderInterface
{
    public function open();
    public function send($filepath);
    public function close();
    public function getResult();
}

// FTP ファイル転送 Builder
class ftpFileSendBuilder implements fileSendBulderInterface
{
    private $_result = false;
    private $_conn;

    public function open()
    {
        $this->_conn = @ftp_connect('接続先');
        if($this->_conn == false) { return; }
        $this->_result = @ftp_login($this->_conn, 'user', 'password');
    }

    public function send($local_filepath)
    {
        if($this->_result == false) { return; }
        
        $remote_filename = basename($local_filepath);
        $ret = @ftp_put($this->_conn, $remote_filename, $local_filepath, FTP_ASCII);
    }

    public function close()
    {
        @ftp_close($this->_conn);
    }

    public function getResult()
    {
        $result = new result();
        $result->setResult($this->_result);

        return $result;
    }
}

// SMB ファイル転送 Builder
class smbFileSendBuilder implements fileSendBulderInterface
{
    private $_result = false;
    const REMOTE_DIR_PATH = '/pth/to/remote/dir';

    public function open()
    {
        // 転送先ディレクトリがあるか確認する
        $this->_result = is_dir(self::REMOTE_DIR_PATH);
    }

    public function send($local_filepath)
    {
        // open に失敗していたらリターン
        if($this->_result == false) { return; }
        
        $remote_filepath = self::REMOTE_DIR_PATH . '/' . basename($local_filepath);
        $this->_result = copy($local_filepath, $remote_filepath);
    }

    public function close()
    {
        // 何もしない
    }

    public function getResult()
    {
        $result = new result();
        $result->setResult($this->_result);

        return $result;
    }
}

// オブジェクトの生成手順を把握するディレクタークラス
class fileSendDirector
{
    private $_builder;  // fileSendBulderInterface

    public function __construct($builder)
    {
        $this->_builder = $builder;
    }

    // ファイル転送手順を実施し、結果オブジェクトをリターンする
    public function sendFile($filepath)
    {
        $this->_builder->open();
        $this->_builder->send($filepath);
        $this->_builder->close();

        return $this->_builder->getResult();
    }
}

上記の Builder パターンで実装された処理を次のように呼び出します。

// 送信ファイルパス
$filepath = '/path/to/local/file';

// FTP でファイルを送信する場合(builder に ftpFileSendBuilder を設定)
$director = new fileSendDirector(new ftpFileSendBuilder());
$result = $director->sendFile($filepath);

// SMB でファイルを送信する場合(builder に smbFileSendBuilder を設定)
$director = new fileSendDirector(new smbFileSendBuilder());
$result = $director->sendFile($filepath);
スポンサーリンク


Pocket

Leave a Comment

Your email address will not be published. Required fields are marked *