Skip to content

Commit 1f97a2e

Browse files
committed
Abstracted quote class, documented and cleaned up code.
1 parent 981bdb4 commit 1f97a2e

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

bashquote

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,29 @@
22
<?php
33

44
// Use http_proxy environment variable
5-
stream_context_set_default(array('http' => array(
6-
'proxy' => str_replace("http://", "tcp://", getenv('http_proxy')),
7-
'request_fulluri' => true
8-
)));
5+
if ($proxy = str_replace("http://", "tcp://", getenv('http_proxy')))
6+
stream_context_set_default(array('http' => array('proxy' => $proxy,'request_fulluri' => true)));
97

108
$quotes = new BashOrgQuotes();
119
$quote = $quotes->getQuote();
1210
$quote = $quotes::cleanupQuote($quote);
1311
echo $quote."\n";
1412

15-
class Quotes {
13+
/**
14+
* Quotes class
15+
*
16+
* A generic class for fetching quotes
17+
*/
18+
abstract class Quotes {
1619

1720
private $quotefile = ''; // Quote cache location
1821
private $quotes = array(); // Quotes
1922

23+
/**
24+
* Class constructur
25+
*
26+
* Opens the quote cache.
27+
*/
2028
function __construct() {
2129
$this->quotefile = getenv('HOME')."/.bashqdb";
2230
$this->quotefp = fopen($this->quotefile,"c+");
@@ -26,13 +34,21 @@ class Quotes {
2634
$this->cacheRead();
2735
}
2836

37+
/**
38+
* Class destructor
39+
*
40+
* Fills the cache (if needed), and writes it back to disk.
41+
*/
2942
function __destruct() {
3043
$this->fillIfEmpty();
3144
$this->cacheWrite();
3245
flock($this->quotefp, LOCK_UN);
3346
fclose($this->quotefp);
3447
}
3548

49+
/**
50+
* Read the cache contents
51+
*/
3652
function cacheRead() {
3753
$cache = file($this->quotefile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES | FILE_TEXT);
3854
if ($cache) {
@@ -43,29 +59,43 @@ class Quotes {
4359
}
4460
}
4561

62+
/**
63+
* Write the cache contents to disk
64+
*/
4665
function cacheWrite() {
4766
if (FALSE === file_put_contents($this->quotefile, implode("\n", $this->quotes))) {
4867
throw new Exception('Could not write quote cache');
4968
}
5069
}
5170

71+
/**
72+
* Checks if the cache is empty, and if so, fills it.
73+
*/
5274
function fillIfEmpty() {
5375
if (!$this->quotes)
5476
$this->quotes = $this->fetchQuotes();
5577
}
5678

79+
/**
80+
* Get a single quote
81+
*/
5782
function getQuote() {
5883
$quote = array_pop($this->quotes);
59-
$this->fillIfEmpty();
6084
if (!$quote) { // This is not good enough error handling
61-
$quote = array_pop($this->quotes);
6285
$this->fillIfEmpty();
86+
$quote = array_pop($this->quotes);
6387
}
6488
return $quote;
6589
}
6690
}
6791

92+
/**
93+
* Class for handling quotes from bash.org
94+
*
95+
* Implementation of class Quotes
96+
*/
6897
class BashOrgQuotes extends Quotes {
98+
6999
function fetchQuotes() { // */p[@class='qt']
70100
$matches = array();
71101
$source = file_get_contents("http://bash.org/?random1");
@@ -84,5 +114,5 @@ class BashOrgQuotes extends Quotes {
84114
return $quote;
85115
}
86116
}
87-
// http://www.qdb.us/api
117+
88118
?>

0 commit comments

Comments
 (0)