Skip to content

Commit

Permalink
Merge pull request epochblue#2 from druid628/feature/addsPidSupport
Browse files Browse the repository at this point in the history
Added PID support
  • Loading branch information
epochblue committed Oct 19, 2012
2 parents 1616b0d + 9f96138 commit 528b067
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $> php composer.phar install -v
```

Once this is complete, your directory should have a few new items (a `composer.lock` file, and
a `vendors` directory) in it, and you shoudl be ready to go. All that's left is to create the
a `vendors` directory) in it, and you should be ready to go. All that's left is to create the
the bot. You can name your bot whatever you want, though `bot.php` is nice and easy.
Here's a basic example:

Expand Down
29 changes: 29 additions & 0 deletions src/Philip/Philip.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Philip
/** @var Logger $log The log to write to, if debug is enabled */
private $log;

/** @var string $pidfile The location to write to, if write_pidfile is enabled */
private $pidfile;

/**
* Constructor.
*
Expand All @@ -42,20 +45,46 @@ public function __construct($config = array())
$this->dispatcher = new EventDispatcher();

$this->setupLogger();
$this->setupPidfile();
$this->addDefaultHandlers();
}

/**
* Destructor; ensure the socket gets closed.
* Destroys pid file if set in config.
*/
public function __destruct()
{
if (isset($this->socket)) {
fclose($this->socket);
}

if ( isset($this->config['write_pidfile']) ) {
if ( $this->config['write_pidfile'] === true ) {
unlink( $this->pidfile );
}
}
}


/**
* Creates a pid file if 'pid' is set in configuration
*/
public function setupPidfile()
{
if(isset($this->config['write_pidfile']) && $this->config['write_pidfile'] === true ) {
if( isset($this->config['pidfile'])) {
$this->pidfile = $this->config['pidfile'];
} else {
$this->pidfile = sprintf("%s/philip.pid", __DIR__);
}

$pidfile = fopen($this->pidfile, 'w') or die("can't open file");
fwrite($pidfile, getmypid());
fclose($pidfile);
}
}

/**
* Adds an event handler to the list for when someone talks in a channel.
*
Expand Down

0 comments on commit 528b067

Please sign in to comment.