Respect PHP Coding Standards.

PSR-1 and PSR-2.

PSR-0 need more modifications.
This commit is contained in:
Benoit.S « Benpro » 2014-02-06 11:34:19 +01:00
parent 6b9074da2a
commit fd4af15ce6
29 changed files with 604 additions and 505 deletions

View File

@ -8,9 +8,10 @@
* @description Returns the N most recent documents.
* @use1(n="number")
*/
class CryptomeBridge extends BridgeAbstract{
public function collectData(array $param){
class CryptomeBridge extends BridgeAbstract
{
public function collectData(array $param)
{
$html = '';
$num = 90;
$link = 'http://cryptome.org/';
@ -34,15 +35,18 @@ class CryptomeBridge extends BridgeAbstract{
}
}
public function getName(){
public function getName()
{
return 'Cryptome';
}
public function getURI(){
public function getURI()
{
return 'https://secure.netsolhost.com/cryptome.org/';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 21600; // 6 hours
}
}

View File

@ -7,9 +7,10 @@
* @name DansTonChat Bridge
* @description Returns latest quotes from DansTonChat.
*/
class DansTonChatBridge extends BridgeAbstract{
public function collectData(array $param){
class DansTonChatBridge extends BridgeAbstract
{
public function collectData(array $param)
{
$html = '';
$link = 'http://danstonchat.com/latest.html';
@ -24,15 +25,18 @@ class DansTonChatBridge extends BridgeAbstract{
}
}
public function getName(){
public function getName()
{
return 'DansTonChat';
}
public function getURI(){
public function getURI()
{
return 'http://danstonchat.com';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 21600; // 6 hours
}
}

View File

@ -8,9 +8,10 @@
* @description Returns most recent results from DuckDuckGo.
* @use1(u="keyword")
*/
class DuckDuckGoBridge extends BridgeAbstract{
public function collectData(array $param){
class DuckDuckGoBridge extends BridgeAbstract
{
public function collectData(array $param)
{
$html = '';
$link = 'https://duckduckgo.com/html/?q='.$param[u].'+sort:date';
@ -25,15 +26,18 @@ class DuckDuckGoBridge extends BridgeAbstract{
}
}
public function getName(){
public function getName()
{
return 'DuckDuckGo';
}
public function getURI(){
public function getURI()
{
return 'https://duckduckgo.com';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 21600; // 6 hours
}
}

View File

@ -6,22 +6,23 @@
* @name Futurasciences
* @description Returns the 20 newest posts from FS (full text)
*/
class FSBridge extends BridgeAbstract{
public function collectData(array $param){
function FS_StripCDATA($string) {
class FSBridge extends BridgeAbstract
{
public function collectData(array $param)
{
public function FS_StripCDATA($string)
{
$string = str_replace('<![CDATA[', '', $string);
$string = str_replace(']]>', '', $string);
return $string;
}
function FS_ExtractContent($url) {
public function FS_ExtractContent($url)
{
$html2 = file_get_html($url);
$text = $html2->find('div.fiche-actualite', 0)->innertext;
return $text;
}
$html = file_get_html('http://www.futura-sciences.com/rss/actualites.xml') or $this->returnError('Could not request Futura Sciences.', 404);
@ -38,18 +39,20 @@ class FSBridge extends BridgeAbstract{
$limit++;
}
}
}
public function getName(){
public function getName()
{
return 'Futura Sciences';
}
public function getURI(){
public function getURI()
{
return 'http://www.futura-sciences.com/';
}
public function getCacheDuration(){
public function getCacheDuration()
{
// return 3600; // 1 hour
return 0; // 1 hour
}

View File

@ -6,9 +6,10 @@
* @name Flickr Explore
* @description Returns the latest interesting images from Flickr
*/
class FlickrExploreBridge extends BridgeAbstract{
public function collectData(array $param){
class FlickrExploreBridge extends BridgeAbstract
{
public function collectData(array $param)
{
$html = file_get_html('http://www.flickr.com/explore') or $this->returnError('Could not request Flickr.', 404);
foreach ($html->find('span.photo_container') as $element) {
@ -21,15 +22,18 @@ class FlickrExploreBridge extends BridgeAbstract{
}
}
public function getName(){
public function getName()
{
return 'Flickr Explore';
}
public function getURI(){
public function getURI()
{
return 'http://www.flickr.com/explore';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 21600; // 6 hours
}
}

View File

@ -13,18 +13,18 @@
* @description Returns most recent results from Google search.
* @use1(q="keyword")
*/
class GoogleSearchBridge extends BridgeAbstract{
class GoogleSearchBridge extends BridgeAbstract
{
private $request;
public function collectData(array $param){
public function collectData(array $param)
{
$html = '';
if (isset($param['q'])) { /* keyword search mode */
$this->request = $param['q'];
$html = file_get_html('http://www.google.com/search?q=' . urlencode($this->request) . '&num=100&complete=0&tbs=qdr:y,sbd:1') or $this->returnError('No results for this query.', 404);
}
else{
} else {
$this->returnError('You must specify a keyword (?q=...).', 400);
}
@ -45,15 +45,18 @@ class GoogleSearchBridge extends BridgeAbstract{
}
}
public function getName(){
public function getName()
{
return (!empty($this->request) ? $this->request .' - ' : '') .'Google search';
}
public function getURI(){
public function getURI()
{
return 'http://google.com';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 1800; // 30 minutes
}
}

View File

@ -6,22 +6,23 @@
* @name GuruMed
* @description Returns the 20 newest posts from Gurumed (full text)
*/
class GuruMedBridge extends BridgeAbstract{
public function collectData(array $param){
function GurumedStripCDATA($string) {
class GuruMedBridge extends BridgeAbstract
{
public function collectData(array $param)
{
public function GurumedStripCDATA($string)
{
$string = str_replace('<![CDATA[', '', $string);
$string = str_replace(']]>', '', $string);
return $string;
}
function GurumedExtractContent($url) {
public function GurumedExtractContent($url)
{
$html2 = file_get_html($url);
$text = $html2->find('div.entry', 0)->innertext;
return $text;
}
$html = file_get_html('http://gurumed.org/feed') or $this->returnError('Could not request Gurumed.', 404);
@ -38,18 +39,20 @@ class GuruMedBridge extends BridgeAbstract{
$limit++;
}
}
}
public function getName(){
public function getName()
{
return 'Gurumed';
}
public function getURI(){
public function getURI()
{
return 'http://gurumed.org/';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 3600; // 1 hour
}
}

View File

@ -6,17 +6,17 @@
* @description Returns user timelines
* @use1(u="username")
*/
class IdenticaBridge extends BridgeAbstract{
class IdenticaBridge extends BridgeAbstract
{
private $request;
public function collectData(array $param){
public function collectData(array $param)
{
$html = '';
if (isset($param['u'])) { /* user timeline mode */
$this->request = $param['u'];
$html = file_get_html('https://identi.ca/'.urlencode($this->request)) or $this->returnError('Requested username can\'t be found.', 404);
}
else {
} else {
$this->returnError('You must specify an Identica username (?u=...).', 400);
}
@ -30,15 +30,18 @@ class IdenticaBridge extends BridgeAbstract{
}
}
public function getName(){
public function getName()
{
return (!empty($this->request) ? $this->request .' - ' : '') .'Identica Bridge';
}
public function getURI(){
public function getURI()
{
return 'https://identica.com';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 300; // 5 minutes
}
}

View File

@ -7,31 +7,29 @@
* @description Returns the newest images
* @use1(u="username")
*/
class InstagramBridge extends BridgeAbstract{
class InstagramBridge extends BridgeAbstract
{
private $request;
public function collectData(array $param){
public function collectData(array $param)
{
$html = '';
if (isset($param['u'])) { /* user timeline mode */
$this->request = $param['u'];
$html = file_get_html('http://instagram.com/'.urlencode($this->request)) or $this->returnError('Could not request Instagram.', 404);
}
else {
} else {
$this->returnError('You must specify a Instagram username (?u=...).', 400);
}
$innertext = null;
foreach($html->find('script') as $script)
{
foreach ($html->find('script') as $script) {
if ('' === $script->innertext) {
continue;
}
$pos = strpos(trim($script->innertext), 'window._sharedData');
if (0 !== $pos)
{
if (0 !== $pos) {
continue;
}
@ -39,23 +37,18 @@ class InstagramBridge extends BridgeAbstract{
break;
}
$json = trim(substr($innertext, $pos+18), ' =;');
$data = json_decode($json);
$userMedia = $data->entry_data->UserProfile[0]->userMedia;
foreach($userMedia as $media)
{
foreach ($userMedia as $media) {
$image = $media->images->standard_resolution;
$item = new \Item();
$item->uri = $media->link;
$item->content = '<img src="' . htmlentities($image->url) . '" width="'.htmlentities($image->width).'" height="'.htmlentities($image->height).'" />';
if (isset($media->caption))
{
if (isset($media->caption)) {
$item->title = $media->caption->text;
} else {
$item->title = basename($image->url);
@ -66,15 +59,18 @@ class InstagramBridge extends BridgeAbstract{
}
}
public function getName(){
public function getName()
{
return (!empty($this->request) ? $this->request .' - ' : '') .'Instagram Bridge';
}
public function getURI(){
public function getURI()
{
return 'http://instagram.com/';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 3600;
}
}

View File

@ -8,9 +8,10 @@
* @description Returns latest tutorials from OpenClassrooms.
* @use1(u="informatique or sciences")
*/
class OpenClassroomsBridge extends BridgeAbstract{
public function collectData(array $param){
class OpenClassroomsBridge extends BridgeAbstract
{
public function collectData(array $param)
{
$html = '';
$link = 'http://fr.openclassrooms.com/'.$param[u].'/cours?title=&sort=updatedAt+desc';
@ -25,15 +26,18 @@ class OpenClassroomsBridge extends BridgeAbstract{
}
}
public function getName(){
public function getName()
{
return 'OpenClassrooms';
}
public function getURI(){
public function getURI()
{
return 'http://fr.openclassrooms.com';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 21600; // 6 hours
}
}

View File

@ -8,31 +8,27 @@
* @use1(u="username",b="board")
* @use2(q="keyword")
*/
class PinterestBridge extends BridgeAbstract{
class PinterestBridge extends BridgeAbstract
{
private $username;
private $board;
private $query;
public function collectData(array $param){
public function collectData(array $param)
{
$html = '';
if (isset($param['u']) && isset($param['b'])) {
$this->username = $param['u'];
$this->board = $param['b'];
$html = file_get_html($this->getURI().'/'.urlencode($this->username).'/'.urlencode($this->board)) or $this->returnError('Could not request Pinterest.', 404);
} else if (isset($param['q']))
{
} elseif (isset($param['q'])) {
$this->query = $param['q'];
$html = file_get_html($this->getURI().'/search/?q='.urlencode($this->query)) or $this->returnError('Could not request Pinterest.', 404);
}
else {
} else {
$this->returnError('You must specify a Pinterest username and a board name (?u=...&b=...).', 400);
}
foreach($html->find('div.pinWrapper') as $div)
{
foreach ($html->find('div.pinWrapper') as $div) {
$a = $div->find('a.pinImageWrapper',0);
$img = $a->find('img', 0);
@ -41,9 +37,7 @@ class PinterestBridge extends BridgeAbstract{
$item->uri = $this->getURI().$a->getAttribute('href');
$item->content = '<img src="' . htmlentities($img->getAttribute('src')) . '" alt="" />';
if (isset($this->query))
{
if (isset($this->query)) {
$avatar = $div->find('img.creditImg', 0);
$username = $div->find('span.creditName', 0);
$board = $div->find('span.creditTitle', 0);
@ -68,21 +62,22 @@ class PinterestBridge extends BridgeAbstract{
}
}
public function getName(){
if (isset($this->query))
public function getName()
{
if (isset($this->query)) {
return $this->query .' - Pinterest';
} else {
return $this->username .' - '. $this->board.' - Pinterest';
}
}
public function getURI(){
public function getURI()
{
return 'http://www.pinterest.com';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 0;
}
}

View File

@ -6,12 +6,12 @@
* @name Se Coucher Moins Bête Bridge
* @description Returns the newest anecdotes.
*/
class ScmbBridge extends BridgeAbstract{
public function collectData(array $param){
class ScmbBridge extends BridgeAbstract
{
public function collectData(array $param)
{
$html = '';
$html = file_get_html('http://secouchermoinsbete.fr/') or $this->returnError('Could not request Se Coucher Moins Bete.', 404);
foreach ($html->find('article') as $article) {
$item = new \Item();
$item->uri = 'http://secouchermoinsbete.fr'.$article->find('p.summary a',0)->href;
@ -29,21 +29,23 @@ class ScmbBridge extends BridgeAbstract{
$timestamp = mktime($h,$i,0,$m,$d,$y);
$item->timestamp = $timestamp;
$item->content = $content;
$this->items[] = $item;
}
}
public function getName(){
public function getName()
{
return 'Se Coucher Moins Bête Bridge';
}
public function getURI(){
public function getURI()
{
return 'http://secouchermoinsbete.fr/';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 21600; // 6 hours
}
}

View File

@ -8,21 +8,20 @@
* @use1(q="keyword or #hashtag")
* @use2(u="username")
*/
class TwitterBridge extends BridgeAbstract{
class TwitterBridge extends BridgeAbstract
{
private $request;
public function collectData(array $param){
public function collectData(array $param)
{
$html = '';
if (isset($param['q'])) { /* keyword search mode */
$this->request = $param['q'];
$html = file_get_html('http://twitter.com/search/realtime?q='.urlencode($this->request).'+include:retweets&src=typd') or $this->returnError('No results for this query.', 404);
}
elseif (isset($param['u'])) { /* user timeline mode */
} elseif (isset($param['u'])) { /* user timeline mode */
$this->request = $param['u'];
$html = file_get_html('http://twitter.com/'.urlencode($this->request)) or $this->returnError('Requested username can\'t be found.', 404);
}
else {
} else {
$this->returnError('You must specify a keyword (?q=...) or a Twitter username (?u=...).', 400);
}
@ -40,15 +39,18 @@ class TwitterBridge extends BridgeAbstract{
}
}
public function getName(){
public function getName()
{
return (!empty($this->request) ? $this->request .' - ' : '') .'Twitter Bridge';
}
public function getURI(){
public function getURI()
{
return 'http://twitter.com';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 300; // 5 minutes
}
}

View File

@ -6,9 +6,10 @@
* @name Wikipedia EN "Today's Featured Article..."
* @description Returns the highlighted en.wikipedia.org article.
*/
class WikipediaENBridge extends BridgeAbstract{
public function collectData(array $param){
class WikipediaENBridge extends BridgeAbstract
{
public function collectData(array $param)
{
$html = '';
$host = 'http://en.wikipedia.org';
// If you want HTTPS access instead, uncomment the following line:
@ -27,15 +28,18 @@ class WikipediaENBridge extends BridgeAbstract{
$this->items[] = $item;
}
public function getName(){
public function getName()
{
return 'Wikipedia EN "Today\'s Featued Article"';
}
public function getURI(){
public function getURI()
{
return 'https://en.wikipedia.org/wiki/Main_Page';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 3600*4; // 4 hours
}
}

View File

@ -6,9 +6,10 @@
* @name Wikipedia EO "Artikolo de la semajno"
* @description Returns the highlighted eo.wikipedia.org article.
*/
class WikipediaEOBridge extends BridgeAbstract{
public function collectData(array $param){
class WikipediaEOBridge extends BridgeAbstract
{
public function collectData(array $param)
{
$html = '';
$host = 'http://eo.wikipedia.org';
// If you want HTTPS access instead, uncomment the following line:
@ -27,15 +28,18 @@ class WikipediaEOBridge extends BridgeAbstract{
$this->items[] = $item;
}
public function getName(){
public function getName()
{
return 'Wikipedia EO "Artikolo de la semajno"';
}
public function getURI(){
public function getURI()
{
return 'https://eo.wikipedia.org/wiki/Vikipedio:%C4%88efpa%C4%9Do';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 3600*12; // 12 hours
}
}

View File

@ -6,9 +6,10 @@
* @name Wikipedia FR "Lumière sur..."
* @description Returns the highlighted fr.wikipedia.org article.
*/
class WikipediaFRBridge extends BridgeAbstract{
public function collectData(array $param){
class WikipediaFRBridge extends BridgeAbstract
{
public function collectData(array $param)
{
$html = '';
$host = 'http://fr.wikipedia.org';
// If you want HTTPS access instead, uncomment the following line:
@ -25,15 +26,18 @@ class WikipediaFRBridge extends BridgeAbstract{
$this->items[] = $item;
}
public function getName(){
public function getName()
{
return 'Wikipedia FR "Lumière sur..."';
}
public function getURI(){
public function getURI()
{
return 'https://fr.wikipedia.org/wiki/Wikip%C3%A9dia:Accueil_principal';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 3600*4; // 4 hours
}
}

View File

@ -7,21 +7,20 @@
* @description Returns the newest videos
* @use1(u="username")
*/
class YoutubeBridge extends BridgeAbstract{
class YoutubeBridge extends BridgeAbstract
{
private $request;
public function collectData(array $param){
public function collectData(array $param)
{
$html = '';
if (isset($param['u'])) { /* user timeline mode */
$this->request = $param['u'];
$html = file_get_html('https://www.youtube.com/user/'.urlencode($this->request).'/videos') or $this->returnError('Could not request Youtube.', 404);
}
else {
} else {
$this->returnError('You must specify a Youtbe username (?u=...).', 400);
}
foreach ($html->find('li.channels-content-item') as $element) {
$item = new \Item();
$item->uri = 'https://www.youtube.com'.$element->find('a',0)->href;
@ -32,15 +31,18 @@ class YoutubeBridge extends BridgeAbstract{
}
}
public function getName(){
public function getName()
{
return (!empty($this->request) ? $this->request .' - ' : '') .'Youtube Bridge';
}
public function getURI(){
public function getURI()
{
return 'https://www.youtube.com/';
}
public function getCacheDuration(){
public function getCacheDuration()
{
return 21600; // 6 hours
}
}

View File

@ -2,10 +2,12 @@
/**
* Cache with file system
*/
class FileCache extends CacheAbstract{
class FileCache extends CacheAbstract
{
protected $cacheDirCreated; // boolean to avoid always chck dir cache existance
public function loadData(){
public function loadData()
{
$this->isPrepareCache();
$datas = json_decode(file_get_contents($this->getCacheFile()),true);
@ -21,7 +23,8 @@ class FileCache extends CacheAbstract{
return $items;
}
public function saveData($datas){
public function saveData($datas)
{
$this->isPrepareCache();
file_put_contents($this->getCacheFile(), json_encode($datas));
@ -29,7 +32,8 @@ class FileCache extends CacheAbstract{
return $this;
}
public function getTime(){
public function getTime()
{
$this->isPrepareCache();
$cacheFile = $this->getCacheFile();
@ -45,7 +49,8 @@ class FileCache extends CacheAbstract{
* Note : Cache name is based on request information, then cache must be prepare before use
* @return \Exception|true
*/
protected function isPrepareCache(){
protected function isPrepareCache()
{
if ( is_null($this->param) ) {
throw new \Exception('Please feed "prepare" method before try to load');
}
@ -57,7 +62,8 @@ class FileCache extends CacheAbstract{
* Return cache path (and create if not exist)
* @return string Cache path
*/
protected function getCachePath(){
protected function getCachePath()
{
$cacheDir = __DIR__ . '/../cache/'; // FIXME : configuration ?
// FIXME : implement recursive dir creation
@ -75,7 +81,8 @@ class FileCache extends CacheAbstract{
* Get the file name use for cache store
* @return string Path to the file cache
*/
protected function getCacheFile(){
protected function getCacheFile()
{
return $this->getCachePath() . $this->getCacheName();
}
@ -83,10 +90,12 @@ class FileCache extends CacheAbstract{
* Determines file name for store the cache
* return string
*/
protected function getCacheName(){
protected function getCacheName()
{
$this->isPrepareCache();
$stringToEncode = $_SERVER['REQUEST_URI'] . http_build_query($this->param);
return hash('sha1', $stringToEncode) . '.cache';
}
}

View File

@ -5,9 +5,10 @@
*
* @name Atom
*/
class AtomFormat extends FormatAbstract{
public function stringify(){
class AtomFormat extends FormatAbstract
{
public function stringify()
{
/* Datas preparation */
$https = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '' );
$httpHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
@ -75,10 +76,12 @@ EOD;
// So we use mb_convert_encoding instead:
ini_set('mbstring.substitute_character', 'none');
$toReturn= mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8');
return $toReturn;
}
public function display(){
public function display()
{
$this
->setContentType('application/atom+xml; charset=utf8') // We force UTF-8 in ATOM output.
->callContentType();

View File

@ -5,9 +5,10 @@
*
* @name Html
*/
class HtmlFormat extends FormatAbstract{
public function stringify(){
class HtmlFormat extends FormatAbstract
{
public function stringify()
{
/* Datas preparation */
$extraInfos = $this->getExtraInfos();
$title = htmlspecialchars($extraInfos['name']);
@ -51,7 +52,8 @@ EOD;
return $toReturn;
}
public function display() {
public function display()
{
$this
->setContentType('text/html; charset=' . $this->getCharset())
->callContentType();

View File

@ -5,16 +5,18 @@
*
* @name Json
*/
class JsonFormat extends FormatAbstract{
public function stringify(){
class JsonFormat extends FormatAbstract
{
public function stringify()
{
// FIXME : sometime content can be null, transform to empty string
$datas = $this->getDatas();
return json_encode($datas);
}
public function display(){
public function display()
{
$this
->setContentType('application/json')
->callContentType();

View File

@ -5,14 +5,17 @@
*
* @name Plaintext
*/
class PlaintextFormat extends FormatAbstract{
public function stringify(){
class PlaintextFormat extends FormatAbstract
{
public function stringify()
{
$datas = $this->getDatas();
return print_r($datas, true);
}
public function display(){
public function display()
{
$this
->setContentType('text/plain;charset=' . $this->getCharset())
->callContentType();

View File

@ -57,17 +57,16 @@ try{
break;
}
}
}
catch(HttpException $e){
} catch (HttpException $e) {
header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode()));
header('Content-Type: text/plain');
die($e->getMessage());
}
catch(\Exception $e){
} catch (\Exception $e) {
die($e->getMessage());
}
function getHelperButtonFormat($value, $name){
function getHelperButtonFormat($value, $name)
{
return '<button type="submit" name="format" value="' . $value . '">' . $name . '</button>';
}

View File

@ -4,21 +4,24 @@
* Note : adapter are store in other place
*/
interface BridgeInterface{
interface BridgeInterface
{
public function collectData(array $param);
public function getName();
public function getURI();
public function getCacheDuration();
}
abstract class BridgeAbstract implements BridgeInterface{
abstract class BridgeAbstract implements BridgeInterface
{
protected $cache;
protected $items = array();
/**
* Launch probative exception
*/
protected function returnError($message, $code){
protected function returnError($message, $code)
{
throw new \HttpException($message, $code);
}
@ -26,7 +29,8 @@ abstract class BridgeAbstract implements BridgeInterface{
* Return datas store in the bridge
* @return mixed
*/
public function getDatas(){
public function getDatas()
{
return $this->items;
}
@ -35,19 +39,18 @@ abstract class BridgeAbstract implements BridgeInterface{
* Note : you can defined a cache before with "setCache"
* @param array $param $_REQUEST, $_GET, $_POST, or array with bridge expected paramters
*/
public function setDatas(array $param){
public function setDatas(array $param)
{
if ( !is_null($this->cache) ) {
$this->cache->prepare($param);
$time = $this->cache->getTime();
}
else{
} else {
$time = false; // No cache ? No time !
}
if ( $time !== false && ( time() - $this->getCacheDuration() < $time ) ) { // Cache file has not expired. Serve it.
$this->items = $this->cache->loadData();
}
else{
} else {
$this->collectData($param);
if ( !is_null($this->cache) ) { // Cache defined ? We go to refresh is memory :D
@ -59,25 +62,28 @@ abstract class BridgeAbstract implements BridgeInterface{
/**
* Define default duraction for cache
*/
public function getCacheDuration(){
public function getCacheDuration()
{
return 3600;
}
/**
* Defined cache object to use
*/
public function setCache(\CacheAbstract $cache){
public function setCache(\CacheAbstract $cache)
{
$this->cache = $cache;
return $this;
}
}
class Bridge{
class Bridge
{
protected static $dirBridge;
static protected $dirBridge;
public function __construct(){
public function __construct()
{
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
}
@ -86,7 +92,8 @@ class Bridge{
* @param string $nameBridge Defined bridge name you want use
* @return Bridge object dedicated
*/
static public function create($nameBridge){
public static function create($nameBridge)
{
if ( !static::isValidNameBridge($nameBridge) ) {
throw new \InvalidArgumentException('Name bridge must be at least one uppercase follow or not by alphanumeric or dash characters.');
}
@ -102,7 +109,8 @@ class Bridge{
return new $nameBridge();
}
static public function setDir($dirBridge){
public static function setDir($dirBridge)
{
if ( !is_string($dirBridge) ) {
throw new \InvalidArgumentException('Dir bridge must be a string.');
}
@ -114,7 +122,8 @@ class Bridge{
self::$dirBridge = $dirBridge;
}
static public function getDir(){
public static function getDir()
{
$dirBridge = self::$dirBridge;
if ( is_null($dirBridge) ) {
@ -124,7 +133,8 @@ class Bridge{
return $dirBridge;
}
static public function isValidNameBridge($nameBridge){
public static function isValidNameBridge($nameBridge)
{
return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameBridge);
}
@ -132,7 +142,8 @@ class Bridge{
* Read bridge dir and catch informations about each bridge depending annotation
* @return array Informations about each bridge
*/
static public function searchInformation(){
public static function searchInformation()
{
$pathDirBridge = self::getDir();
$listBridge = array();

View File

@ -4,31 +4,36 @@
* Note : adapter are store in other place
*/
interface CacheInterface{
interface CacheInterface
{
public function loadData();
public function saveData($datas);
public function getTime();
}
abstract class CacheAbstract implements CacheInterface{
abstract class CacheAbstract implements CacheInterface
{
protected $param;
public function prepare(array $param){
public function prepare(array $param)
{
$this->param = $param;
return $this;
}
}
class Cache{
class Cache
{
protected static $dirCache;
static protected $dirCache;
public function __construct(){
public function __construct()
{
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
}
static public function create($nameCache){
public static function create($nameCache)
{
if ( !static::isValidNameCache($nameCache) ) {
throw new \InvalidArgumentException('Name cache must be at least one uppercase follow or not by alphanumeric or dash characters.');
}
@ -44,7 +49,8 @@ class Cache{
return new $nameCache();
}
static public function setDir($dirCache){
public static function setDir($dirCache)
{
if ( !is_string($dirCache) ) {
throw new \InvalidArgumentException('Dir cache must be a string.');
}
@ -56,7 +62,8 @@ class Cache{
self::$dirCache = $dirCache;
}
static public function getDir(){
public static function getDir()
{
$dirCache = self::$dirCache;
if ( is_null($dirCache) ) {
@ -66,7 +73,8 @@ class Cache{
return $dirCache;
}
static public function isValidNameCache($nameCache){
public static function isValidNameCache($nameCache)
{
return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameCache);
}
}

View File

@ -4,12 +4,13 @@ class HttpException extends \Exception{}
/**
* Not real http implementation but only utils stuff
*/
class Http{
class Http
{
/**
* Return message corresponding to Http code
*/
static public function getMessageForCode($code){
public static function getMessageForCode($code)
{
$codes = self::getCodes();
if ( isset($codes[$code]) ) {
@ -22,7 +23,8 @@ class Http{
/**
* List of common Http code
*/
static public function getCodes(){
public static function getCodes()
{
return array(
200 => 'OK',
201 => 'Created',

View File

@ -4,57 +4,65 @@
* Note : adapter are store in other place
*/
interface FormatInterface{
interface FormatInterface
{
public function stringify();
public function display();
public function setDatas(array $bridge);
}
abstract class FormatAbstract implements FormatInterface{
abstract class FormatAbstract implements FormatInterface
{
const DEFAULT_CHARSET = 'UTF-8';
protected
$contentType,
protected $contentType,
$charset,
$datas,
$extraInfos
;
public function setCharset($charset){
public function setCharset($charset)
{
$this->charset = $charset;
return $this;
}
public function getCharset(){
public function getCharset()
{
$charset = $this->charset;
return is_null($charset) ? self::DEFAULT_CHARSET : $charset;
}
protected function setContentType($contentType){
protected function setContentType($contentType)
{
$this->contentType = $contentType;
return $this;
}
protected function callContentType(){
protected function callContentType()
{
header('Content-Type: ' . $this->contentType);
}
public function display(){
public function display()
{
echo $this->stringify();
return $this;
}
public function setDatas(array $datas){
public function setDatas(array $datas)
{
$this->datas = $datas;
return $this;
}
public function getDatas(){
public function getDatas()
{
if ( !is_array($this->datas) ) {
throw new \LogicException('Feed the ' . get_class($this) . ' with "setDatas" method before !');
}
@ -67,7 +75,8 @@ abstract class FormatAbstract implements FormatInterface{
* @param array $extraInfos array with know informations (there isn't merge !!!)
* @return this
*/
public function setExtraInfos(array $extraInfos = array()){
public function setExtraInfos(array $extraInfos = array())
{
foreach (array('name', 'uri') as $infoName) {
if ( !isset($extraInfos[$infoName]) ) {
$extraInfos[$infoName] = '';
@ -83,7 +92,8 @@ abstract class FormatAbstract implements FormatInterface{
* Return extra infos
* @return array See "setExtraInfos" detail method to know what extra are disponibles
*/
public function getExtraInfos(){
public function getExtraInfos()
{
if ( is_null($this->extraInfos) ) { // No extra info ?
$this->setExtraInfos(); // Define with default value
}
@ -109,15 +119,17 @@ abstract class FormatAbstract implements FormatInterface{
}
}
class Format{
class Format
{
protected static $dirFormat;
static protected $dirFormat;
public function __construct(){
public function __construct()
{
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
}
static public function create($nameFormat){
public static function create($nameFormat)
{
if ( !static::isValidNameFormat($nameFormat) ) {
throw new \InvalidArgumentException('Name format must be at least one uppercase follow or not by alphabetic characters.');
}
@ -133,7 +145,8 @@ class Format{
return new $nameFormat();
}
static public function setDir($dirFormat){
public static function setDir($dirFormat)
{
if ( !is_string($dirFormat) ) {
throw new \InvalidArgumentException('Dir format must be a string.');
}
@ -145,7 +158,8 @@ class Format{
self::$dirFormat = $dirFormat;
}
static public function getDir(){
public static function getDir()
{
$dirFormat = self::$dirFormat;
if ( is_null($dirFormat) ) {
@ -155,7 +169,8 @@ class Format{
return $dirFormat;
}
static public function isValidNameFormat($nameFormat){
public static function isValidNameFormat($nameFormat)
{
return preg_match('@^[A-Z][a-zA-Z]*$@', $nameFormat);
}
@ -163,7 +178,8 @@ class Format{
* Read format dir and catch informations about each format depending annotation
* @return array Informations about each format
*/
static public function searchInformation(){
public static function searchInformation()
{
$pathDirFormat = self::getDir();
$listFormat = array();

View File

@ -5,12 +5,15 @@ interface ItemInterface{}
* Object to store datas collect informations
* FIXME : not sur this logic is the good, I think recast all is necessary
*/
class Item implements ItemInterface{
public function __set($name, $value){
class Item implements ItemInterface
{
public function __set($name, $value)
{
$this->$name = $value;
}
public function __get($name){
public function __get($name)
{
return isset($this->$name) ? $this->$name : null;
}
}