Saturday, January 12. 2008phpurple new oo design,
and so it will be in the future ...
Namely, i had the excellent oo design suggestion from the Alexey on the pecl mailing list (and it is better, than I could invent), and it works fully for php 5.3 and unfortunately partly for php 5.2. So lets take a look on it ... just get the latest version ... CODE: user@host~# svn co https://phpurple.svn.sourceforge.net/svnroot/phpurple/trunk phpurple
The principle is: we inherit our stuff from the Client class, which has firmly callback methods, and we must overload them, to implement our own behavior. I've compiled it on debian+php5.2.0+libpurple2.3.1 and gentoo64+php5.2.4+libpurple2.2.1 and it works. I've compiled the both with php 5.3 too, wich supports the namespaces and it works too. We need glib (which is needed for libpurple) and pcre. Currently we can connect to msn and icq, but in the first release i would enclose at least irc support too. If you compile the libpurple from scratch, you could use the following configure options: CODE: user@host~# ./configure --enable-gnutls=yes --enable-nss=no --disable-gtkui \
--disable-consoleui --disable-screensaver --disable-sm --without-x --prefix=/usr this will compile only libpurple. Otherwise, if you have already pidgin installed, the libpurple is normally present in the /usr/lib or /usr/local/lib Now the extension must be compiled: CODE: user@host~# phpize CODE: user@host~# ./configure --enable-purple CODE: user@host~# make Don't do "make install" yet. At this point we can check the things: PHP: <?php if(!extension_loaded('purple')) { dl('purple.' . PHP_SHLIB_SUFFIX); } //comment in the namspaces for the php version >= v5.3 /*use Purple::Client; use Purple::Account; use Purple::Conversation; use Purple::Connection;*/ /** * @class PurpleClient * * The method names are firmly and must be overloaded to work * For php <= 5.3 the child class name is firmly too */ class PurpleClient extends Client { private $someVar; private $someOtherVar; /** * Initalize internal class stuff * Since we have protected final Client::__construct, * the initInternal() is called within the Client::__construct instead * @access protected * @return void */ protected function initInternal() { $this->someVar = "Hello World"; $this->someOtherVar = time(); $this->addAccount("msn://login@hotmail.com:password"); $this->connectToSignal("signed-on"); $this->runLoop(); } /** * @access protected * @param object $conversation * @param string $who * @param string $alias * @param string $message * @param integer $time * @return void */ protected function writeConv($conversation, $who, $alias, $message, $flags, $time) { if ($alias) { $name = $alias; } else if ($who) { $name = $who; } else { $name = NULL; } if(PurpleClient::MESSAGE_RECV == $flags) { printf( "(%s) %s %s: %s\n", $conversation->getName(), date("H:i:s", $time), $name, $message ); // may be here comes your database stuff if(preg_match("/.*hello.*/i", strtolower($message)) || preg_match("/.*hi.*/i", strtolower($message))) { $conversation->sendIM("hi there!"); } else if(preg_match("/.*\/date.*/i", strtolower($message))) { $conversation->sendIM(shell_exec("date")); } else if(preg_match("/.*\/version.*/i", strtolower($message))) { $conversation->sendIM("I'm the bot example version 0.0.0.0.1"); } else if(preg_match("/.*\/engine_version.*/i", strtolower($message))) { $conversation->sendIM("libpurple v" . Client::getCoreVersion()); } else if(preg_match("/.*\/show_var.*/i", strtolower($message))) { $conversation->sendIM($this->somevar); } else if(preg_match("/.*\/fun (.*)/i", strtolower($message), $matches)) { $conversation->sendIM($this->justForFun($matches[1])); } else { $conversation->sendIM("sorry, i don't understand you!"); } } } /** * this method is not inherited from the Client * @param mixed $param */ public function justForFun($param) { return "just for fun, the param is: $param"; } } try { // print "Available protocols:\n"; // print_r(PurpleClient::getProtocols()); $client = PurpleClient::getInstance(); //we could instantiate accounts and other stuff here, instead of in the initInternal /* $client->addAccount("msn://trickitracki@hotmail.com:VeryGoodMess_42"); $client->connectToSignal("signed-on"); $client->runLoop();*/ } catch (Exception $e) { echo "[Purple]: " . $e->getMessage() . "\n"; die(); } ?> Stay in the ext dir and run the script: CODE: user@host~# php -n -d extension_dir=./modules/ -d purple.debug_enabled=1 -f test.php Of course you could make "make install" and try to use some other ext together with purple (for example some database). I'm normally using pidgin, therefore i must prepend "/say" before i'm writing some command(eq. "/say /date"). Because the libpurple is designed for a single threaded client, the singleton pattern for the oo design is implemented. Therefore we can't overload the "protected final __construct()" and the "public static final getInstance()" methods. But for the initialization purposes the initInternal() method can be used (which is invoked directly after PurpleClient initialization within getInstance) It would be interesting to me, how the thing behaves within your web server environment. It's because the glib loop is used to make the whole thing work, and it seems to ignore the php.ini time limit .....: hm ... So as before i would willingly hear the criticisms, but i see the current oo design version as the best and the base for the further development. It's the third design variant and all together it costs 2-3 month for me. Please turn your suggestions in the direction of the new (and also further) design. All the interested persons, as extension writers, unit test writers, windows build makers, documentors, translators or anything else are welcome to this project. Please announce yourself here, or on the sf.net .. (but there i'm more rarely) I've not yet implemented the Buddy class and DateTime class (as suggested by Alexey), but this article is evenly not a release, but an announce. In conclusion i wanna thank all the folks who helps on the on the pecl mailing list and efnet #php.pecl irc channel ... it's really very big help ...
Posted by Anatoliy Belsky
in C/C++, Linux, PHP5, PHP5 extentions
at
19:43
| Comments (0)
| Trackbacks (0)
Trackbacks
Trackback specific URI for this entry
No Trackbacks
|
CategoriesQuicksearch |