Page 1 of 1

File sendTo functionality

Posted: Mon Sep 13, 2004 1:01 am
by jason
So I have:

Code: Select all

<?php
$File = File::create(); // create temporary file
$File->name("send_to_file_name.zip"); // name temporary file
$File->sendTo(new EmailSendTo($to, $subject, $email_content, $headers));
?>
and...

Code: Select all

<?php
$File = File::create();
$File->name("send_to_file_name.rar");
$File->sendTo(new FtpSendTo($host, $user, $pass, $dir));
?>
Basically, the idea is that with File, you can sendTo() something. Now, anyone can create a SendTo object that a file can be sent to, and the object will do something with it.

I built these so that I can easily do things to files that normally are a pain. FTPing a file quickly and easily, and Emailing a file as an attachment are both things I need to do often enough that it makes sense.

So, I am looking for some other ideas of things you think I could send a file to.

Posted: Mon Sep 13, 2004 1:50 am
by Weirdan
just out of my mind:
  • PrintSendTo
  • IcqSendTo
  • ScpSendTo
  • DevNullSendTo ;)

Posted: Mon Sep 13, 2004 11:05 am
by kettle_drum
I think scp is a must, i would use that more than ftp.

Posted: Mon Sep 13, 2004 11:26 am
by jason

Posted: Mon Sep 13, 2004 11:48 am
by McGruff
DirectorySendTo - backup scripts?

Posted: Mon Sep 13, 2004 11:52 am
by feyd
YourFavouriteCompressionAlgoSendTo ? :)

Posted: Mon Sep 13, 2004 11:57 am
by jason
McGruff wrote:DirectorySendTo - backup scripts?
DirectorySendTo? That falls more into the copyTo() or moveTo() realm. Heck, I would allow:

Code: Select all

<?php
$File->copyTo("ftp://user:pass@example.com/home/example");
?>
If it would work, but it's not really a good idea to do it that way. (Many people have their FTP usernames as something like: user@example.com, making the above: ftp://user@example.com:pass@example.com/home/example, which doesn't parse correctly).

However, their will be a FileCollection object, while will let you handle server files in one object. And you will be able to do:

Code: Select all

<?php
// Returns a File object, obviously, because files.zip is a single file
$File =& $FileCollection->storeAs("files.zip");
$File->moveTo("/backup/directory");
?>
However, I still have to work out the details on that one.

FileCollection would also need something like this:

Code: Select all

<?php
$FileCollection = FileCollection::createFrom("files.zip");
?>
FileCollection needs some more work...but you get the idea.

Posted: Mon Sep 13, 2004 1:53 pm
by McGruff
Rgr. If it suits the way you're working would be interesting to look into the EclipseCE CVS now and then to see where you're going?

Posted: Mon Sep 13, 2004 2:03 pm
by jason
Yeah, I just have to finish up a few things. Actually, let me do that now. Work stuff can wait for once.

Posted: Mon Sep 13, 2004 2:08 pm
by McGruff
feyd wrote:YourFavouriteCompressionAlgoSendTo ? :)
This could easily expand into sendTo initiating a Chain of Responsibility. Don't know if I can come up with a good use for that off the top of my head though. Maybe:

- ChangeWindowsLineBreaksToLinux
- CompressFile
- Ftp

Posted: Mon Sep 13, 2004 2:10 pm
by feyd
exactly. ;)

Posted: Mon Sep 13, 2004 3:46 pm
by jason
You know, I really was going to work on it. And then I found we had an extra video card lying around, and plugged it in, and setup another monitor on my work computer.

And then an interesting problem reared it's head, and now I am solving that.

Damn...okay, but this stuff is cool, and I am getting some good ideas. Now, here is the big question, McGruff (or anyone else), how do you see the interface for that looking?

Posted: Mon Sep 13, 2004 4:01 pm
by timvw
jason wrote:
McGruff wrote:DirectorySendTo - backup scripts?
DirectorySendTo? That falls more into the copyTo() or moveTo() realm. Heck, I would allow:

Code: Select all

<?php
$File->copyTo("ftp://user:pass@example.com/home/example");
?>
If it would work, but it's not really a good idea to do it that way. (Many people have their FTP usernames as something like: user@example.com, making the above: ftp://user@example.com:pass@example.com/home/example, which doesn't parse correctly).
You could try to url-encode the username and password, if i'm not mistaken, that should work.

Posted: Mon Sep 13, 2004 4:53 pm
by Christopher
A different implementation i s code SendTo as a Factory, e.g.

$SendTo = new SendTo('ftp');
$SendTo = new SendTo('email');
$SendTo = new SendTo('tar');

Then you could set the sources and destinations and it would sort out how to handle them. Give it a a string and a file as sources and an address as the destination to an' email' SentTo and it will build an email with an attachment and mail it. Use the same idea with copying files or building archives or printing. You just need addSource(), addDestination() and send().

Posted: Tue Sep 14, 2004 3:01 am
by McGruff
jason wrote:Now, here is the big question, McGruff (or anyone else), how do you see the interface for that looking?
I'm interpreting Chain of Responsibility as a filter chain which isn't exactly GoF but the two are more or less the same.

Maybe something along these lines:

Code: Select all

/*
    CLASS Handler

    Handler superclass.

*/
class Handler
{
    /*
        param (object) - the target object passed down the chain of handlers/filters
    */
    function execute(&$target)
    {
        $this->target =& $target;
        $this->_apply();

        if($this->_haveMoreHandlers())
        {
            $this->_next_handler->execute($this->target);       
        }
    }

    /*
        return (bool)
    */
    function _haveMoreHandlers()
    {
        return !is_null($this->_next_handler = array_shift($this->_handlers));
    }

}

/*
    CLASS FooHandler

    A concrete handler.
*/
class FooHandler extends Handler
{
    /*
        param (array) - an array of handler objects
    */
    function FooHandler(&$handlers)
    {
        $this->_handlers =& $handlers;
    }

    function _apply()
    {
        // do something with or to $target object
    }

}


    // the File sendTo method:
    function sendTo(&$handlers) 
    {
        if(!is_null($handler = array_shift($handlers)))
        {
            $handler->execute($this);
        
        } else {
        
            trigger_error('Who ate all the handlers?');
        }
    }

// in use:

$handlers = array();
$handlers[] =& new FooHandler($handlers);
$handlers[] =& new BarHandler($handlers);
// etc

$file->sendTo($handlers);
Actually the $handlers array might be better as a FilterManager type object. You could store errors or etc along the way then continue with the usual client output code.

Another approach: what about passing File to a Sender rather than telling File to send itself?