File sendTo functionality

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

File sendTo functionality

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

just out of my mind:
  • PrintSendTo
  • IcqSendTo
  • ScpSendTo
  • DevNullSendTo ;)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

I think scp is a must, i would use that more than ftp.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

DirectorySendTo - backup scripts?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

YourFavouriteCompressionAlgoSendTo ? :)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Yeah, I just have to finish up a few things. Actually, let me do that now. Work stuff can wait for once.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

exactly. ;)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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().
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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?
Post Reply