mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-08-09 10:06:33 +02:00
Refonte du code
This commit is contained in:
187
lib/Bridge.php
Normal file
187
lib/Bridge.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
/**
|
||||
* All bridge logic
|
||||
* Note : adapter are store in other place
|
||||
*/
|
||||
|
||||
interface BridgeInterface{
|
||||
public function collectData(array $param);
|
||||
public function getName();
|
||||
public function getURI();
|
||||
public function getCacheDuration();
|
||||
}
|
||||
|
||||
abstract class BridgeAbstract implements BridgeInterface{
|
||||
protected $cache;
|
||||
protected $items = array();
|
||||
|
||||
/**
|
||||
* Launch probative exception
|
||||
*/
|
||||
protected function returnError($message, $code){
|
||||
throw new \HttpException($message, $code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return datas store in the bridge
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDatas(){
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined datas with parameters depending choose bridge
|
||||
* 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){
|
||||
if( !is_null($this->cache) ){
|
||||
$this->cache->prepare($param);
|
||||
$time = $this->cache->getTime();
|
||||
}
|
||||
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{
|
||||
$this->collectData($param);
|
||||
|
||||
if( !is_null($this->cache) ){ // Cache defined ? We go to refresh is memory :D
|
||||
$this->cache->saveData($this->getDatas());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Define default duraction for cache
|
||||
*/
|
||||
public function getCacheDuration(){
|
||||
return 3600;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined cache object to use
|
||||
*/
|
||||
public function setCache(\CacheAbstract $cache){
|
||||
$this->cache = $cache;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
class Bridge{
|
||||
|
||||
static protected $dirBridge;
|
||||
|
||||
public function __construct(){
|
||||
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new bridge object
|
||||
* @param string $nameBridge Defined bridge name you want use
|
||||
* @return Bridge object dedicated
|
||||
*/
|
||||
static public 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.');
|
||||
}
|
||||
|
||||
$pathBridge = self::getDir() . $nameBridge . '.php';
|
||||
|
||||
if( !file_exists($pathBridge) ){
|
||||
throw new \Exception('The bridge you looking for does not exist.');
|
||||
}
|
||||
|
||||
require_once $pathBridge;
|
||||
|
||||
return new $nameBridge();
|
||||
}
|
||||
|
||||
static public function setDir($dirBridge){
|
||||
if( !is_string($dirBridge) ){
|
||||
throw new \InvalidArgumentException('Dir bridge must be a string.');
|
||||
}
|
||||
|
||||
if( !file_exists($dirBridge) ){
|
||||
throw new \Exception('Dir bridge does not exist.');
|
||||
}
|
||||
|
||||
self::$dirBridge = $dirBridge;
|
||||
}
|
||||
|
||||
static public function getDir(){
|
||||
$dirBridge = self::$dirBridge;
|
||||
|
||||
if( is_null($dirBridge) ){
|
||||
throw new \LogicException(__CLASS__ . ' class need to know bridge path !');
|
||||
}
|
||||
|
||||
return $dirBridge;
|
||||
}
|
||||
|
||||
static public function isValidNameBridge($nameBridge){
|
||||
return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameBridge);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read bridge dir and catch informations about each bridge depending annotation
|
||||
* @return array Informations about each bridge
|
||||
*/
|
||||
static public function searchInformation(){
|
||||
$pathDirBridge = self::getDir();
|
||||
|
||||
$listBridge = array();
|
||||
|
||||
$searchCommonPattern = array('description', 'name');
|
||||
|
||||
$dirFiles = scandir($pathDirBridge);
|
||||
if( $dirFiles !== false ){
|
||||
foreach( $dirFiles as $fileName ){
|
||||
if( preg_match('@([^.]+)\.php@U', $fileName, $out) ){ // Is PHP file ?
|
||||
$infos = array(); // Information about the bridge
|
||||
$resParse = token_get_all(file_get_contents($pathDirBridge . $fileName)); // Parse PHP file
|
||||
foreach($resParse as $v){
|
||||
if( is_array($v) && $v[0] == T_DOC_COMMENT ){ // Lexer node is COMMENT ?
|
||||
$commentary = $v[1];
|
||||
foreach( $searchCommonPattern as $name){ // Catch information with common pattern
|
||||
preg_match('#@' . preg_quote($name, '#') . '\s+(.+)#', $commentary, $outComment);
|
||||
if( isset($outComment[1]) ){
|
||||
$infos[$name] = $outComment[1];
|
||||
}
|
||||
}
|
||||
|
||||
preg_match_all('#@use(?<num>[1-9][0-9]*)\s?\((?<args>.+)\)(?:\r|\n)#', $commentary, $outComment); // Catch specific information about "use".
|
||||
if( isset($outComment['args']) && is_array($outComment['args']) ){
|
||||
$infos['use'] = array();
|
||||
foreach($outComment['args'] as $num => $args){ // Each use
|
||||
preg_match_all('#(?<name>[a-z]+)="(?<value>.*)"(?:,|$)#U', $args, $outArg); // Catch arguments for current use
|
||||
if( isset($outArg['name']) ){
|
||||
$usePos = $outComment['num'][$num]; // Current use name
|
||||
if( !isset($infos['use'][$usePos]) ){ // Not information actually for this "use" ?
|
||||
$infos['use'][$usePos] = array();
|
||||
}
|
||||
|
||||
foreach($outArg['name'] as $numArg => $name){ // Each arguments
|
||||
$infos['use'][$usePos][$name] = $outArg['value'][$numArg];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( isset($infos['name']) ){ // If informations containt at least a name
|
||||
$listBridge[$out[1]] = $infos;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $listBridge;
|
||||
}
|
||||
}
|
72
lib/Cache.php
Normal file
72
lib/Cache.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* All cache logic
|
||||
* Note : adapter are store in other place
|
||||
*/
|
||||
|
||||
interface CacheInterface{
|
||||
public function loadData();
|
||||
public function saveData($datas);
|
||||
public function getTime();
|
||||
}
|
||||
|
||||
abstract class CacheAbstract implements CacheInterface{
|
||||
protected $param;
|
||||
|
||||
public function prepare(array $param){
|
||||
$this->param = $param;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
class Cache{
|
||||
|
||||
static protected $dirCache;
|
||||
|
||||
public function __construct(){
|
||||
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
|
||||
}
|
||||
|
||||
static public 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.');
|
||||
}
|
||||
|
||||
$pathCache = self::getDir() . $nameCache . '.php';
|
||||
|
||||
if( !file_exists($pathCache) ){
|
||||
throw new \Exception('The cache you looking for does not exist.');
|
||||
}
|
||||
|
||||
require_once $pathCache;
|
||||
|
||||
return new $nameCache();
|
||||
}
|
||||
|
||||
static public function setDir($dirCache){
|
||||
if( !is_string($dirCache) ){
|
||||
throw new \InvalidArgumentException('Dir cache must be a string.');
|
||||
}
|
||||
|
||||
if( !file_exists($dirCache) ){
|
||||
throw new \Exception('Dir cache does not exist.');
|
||||
}
|
||||
|
||||
self::$dirCache = $dirCache;
|
||||
}
|
||||
|
||||
static public function getDir(){
|
||||
$dirCache = self::$dirCache;
|
||||
|
||||
if( is_null($dirCache) ){
|
||||
throw new \LogicException(__CLASS__ . ' class need to know cache path !');
|
||||
}
|
||||
|
||||
return $dirCache;
|
||||
}
|
||||
|
||||
static public function isValidNameCache($nameCache){
|
||||
return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameCache);
|
||||
}
|
||||
}
|
61
lib/Exceptions.php
Normal file
61
lib/Exceptions.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
class HttpException extends \Exception{}
|
||||
|
||||
/**
|
||||
* Not real http implementation but only utils stuff
|
||||
*/
|
||||
class Http{
|
||||
|
||||
/**
|
||||
* Return message corresponding to Http code
|
||||
*/
|
||||
static public function getMessageForCode($code){
|
||||
$codes = self::getCodes();
|
||||
|
||||
if( isset($codes[$code]) ){
|
||||
return $codes[$code];
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* List of common Http code
|
||||
*/
|
||||
static public function getCodes(){
|
||||
return array(
|
||||
200 => 'OK',
|
||||
201 => 'Created',
|
||||
202 => 'Accepted',
|
||||
300 => 'Multiple Choices',
|
||||
301 => 'Moved Permanently',
|
||||
302 => 'Moved Temporarily',
|
||||
307 => 'Temporary Redirect',
|
||||
310 => 'Too many Redirects',
|
||||
400 => 'Bad Request',
|
||||
401 => 'Unauthorized',
|
||||
402 => 'Payment Required',
|
||||
403 => 'Forbidden',
|
||||
404 => 'Not Found',
|
||||
405 => 'Method Not',
|
||||
406 => 'Not Acceptable',
|
||||
407 => 'Proxy Authentication Required',
|
||||
408 => 'Request Time-out',
|
||||
409 => 'Conflict',
|
||||
410 => 'Gone',
|
||||
411 => 'Length Required',
|
||||
412 => 'Precondition Failed',
|
||||
413 => 'Request Entity Too Large',
|
||||
414 => 'Request-URI Too Long',
|
||||
415 => 'Unsupported Media Type',
|
||||
416 => 'Requested range unsatisfiable',
|
||||
417 => 'Expectation failed',
|
||||
500 => 'Internal Server Error',
|
||||
501 => 'Not Implemented',
|
||||
502 => 'Bad Gateway',
|
||||
503 => 'Service Unavailable',
|
||||
504 => 'Gateway Time-out',
|
||||
508 => 'Loop detected',
|
||||
);
|
||||
}
|
||||
}
|
183
lib/Format.php
Normal file
183
lib/Format.php
Normal file
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
/**
|
||||
* All format logic
|
||||
* Note : adapter are store in other place
|
||||
*/
|
||||
|
||||
interface FormatInterface{
|
||||
public function stringify();
|
||||
public function display();
|
||||
public function setDatas(array $bridge);
|
||||
}
|
||||
|
||||
abstract class FormatAbstract implements FormatInterface{
|
||||
const DEFAULT_CHARSET = 'UTF-8';
|
||||
|
||||
protected
|
||||
$contentType,
|
||||
$charset,
|
||||
$datas,
|
||||
$extraInfos
|
||||
;
|
||||
|
||||
public function setCharset($charset){
|
||||
$this->charset = $charset;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCharset(){
|
||||
$charset = $this->charset;
|
||||
|
||||
return is_null($charset) ? self::DEFAULT_CHARSET : $charset;
|
||||
}
|
||||
|
||||
protected function setContentType($contentType){
|
||||
$this->contentType = $contentType;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function callContentType(){
|
||||
header('Content-Type: ' . $this->contentType);
|
||||
}
|
||||
|
||||
public function display(){
|
||||
echo $this->stringify();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDatas(array $datas){
|
||||
$this->datas = $datas;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDatas(){
|
||||
if( !is_array($this->datas) ){
|
||||
throw new \LogicException('Feed the ' . get_class($this) . ' with "setDatas" method before !');
|
||||
}
|
||||
|
||||
return $this->datas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define common informations can be required by formats and set default value for unknow values
|
||||
* @param array $extraInfos array with know informations (there isn't merge !!!)
|
||||
* @return this
|
||||
*/
|
||||
public function setExtraInfos(array $extraInfos = array()){
|
||||
foreach(array('name', 'uri') as $infoName){
|
||||
if( !isset($extraInfos[$infoName]) ){
|
||||
$extraInfos[$infoName] = '';
|
||||
}
|
||||
}
|
||||
|
||||
$this->extraInfos = $extraInfos;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return extra infos
|
||||
* @return array See "setExtraInfos" detail method to know what extra are disponibles
|
||||
*/
|
||||
public function getExtraInfos(){
|
||||
if( is_null($this->extraInfos) ){ // No extra info ?
|
||||
$this->setExtraInfos(); // Define with default value
|
||||
}
|
||||
|
||||
return $this->extraInfos;
|
||||
}
|
||||
}
|
||||
|
||||
class Format{
|
||||
|
||||
static protected $dirFormat;
|
||||
|
||||
public function __construct(){
|
||||
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
|
||||
}
|
||||
|
||||
static public function create($nameFormat){
|
||||
if( !static::isValidNameFormat($nameFormat) ){
|
||||
throw new \InvalidArgumentException('Name format must be at least one uppercase follow or not by alphabetic characters.');
|
||||
}
|
||||
|
||||
$pathFormat = self::getDir() . $nameFormat . '.php';
|
||||
|
||||
if( !file_exists($pathFormat) ){
|
||||
throw new \Exception('The format you looking for does not exist.');
|
||||
}
|
||||
|
||||
require_once $pathFormat;
|
||||
|
||||
return new $nameFormat();
|
||||
}
|
||||
|
||||
static public function setDir($dirFormat){
|
||||
if( !is_string($dirFormat) ){
|
||||
throw new \InvalidArgumentException('Dir format must be a string.');
|
||||
}
|
||||
|
||||
if( !file_exists($dirFormat) ){
|
||||
throw new \Exception('Dir format does not exist.');
|
||||
}
|
||||
|
||||
self::$dirFormat = $dirFormat;
|
||||
}
|
||||
|
||||
static public function getDir(){
|
||||
$dirFormat = self::$dirFormat;
|
||||
|
||||
if( is_null($dirFormat) ){
|
||||
throw new \LogicException(__CLASS__ . ' class need to know format path !');
|
||||
}
|
||||
|
||||
return $dirFormat;
|
||||
}
|
||||
|
||||
static public function isValidNameFormat($nameFormat){
|
||||
return preg_match('@^[A-Z][a-zA-Z]*$@', $nameFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read format dir and catch informations about each format depending annotation
|
||||
* @return array Informations about each format
|
||||
*/
|
||||
static public function searchInformation(){
|
||||
$pathDirFormat = self::getDir();
|
||||
|
||||
$listFormat = array();
|
||||
|
||||
$searchCommonPattern = array('name');
|
||||
|
||||
$dirFiles = scandir($pathDirFormat);
|
||||
if( $dirFiles !== false ){
|
||||
foreach( $dirFiles as $fileName ){
|
||||
if( preg_match('@([^.]+)\.php@U', $fileName, $out) ){ // Is PHP file ?
|
||||
$infos = array(); // Information about the bridge
|
||||
$resParse = token_get_all(file_get_contents($pathDirFormat . $fileName)); // Parse PHP file
|
||||
foreach($resParse as $v){
|
||||
if( is_array($v) && $v[0] == T_DOC_COMMENT ){ // Lexer node is COMMENT ?
|
||||
$commentary = $v[1];
|
||||
foreach( $searchCommonPattern as $name){ // Catch information with common pattern
|
||||
preg_match('#@' . preg_quote($name, '#') . '\s+(.+)#', $commentary, $outComment);
|
||||
if( isset($outComment[1]) ){
|
||||
$infos[$name] = $outComment[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( isset($infos['name']) ){ // If informations containt at least a name
|
||||
$listFormat[$out[1]] = $infos;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $listFormat;
|
||||
}
|
||||
}
|
16
lib/Item.php
Normal file
16
lib/Item.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
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){
|
||||
$this->$name = $value;
|
||||
}
|
||||
|
||||
public function __get($name){
|
||||
return isset($this->$name) ? $this->$name : null;
|
||||
}
|
||||
}
|
42
lib/RssBridge.php
Normal file
42
lib/RssBridge.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/* rss-bridge library.
|
||||
Foundation functions for rss-bridge project.
|
||||
See https://github.com/sebsauvage/rss-bridge
|
||||
Licence: Public domain.
|
||||
*/
|
||||
|
||||
define('PATH_VENDOR', '/../vendor');
|
||||
|
||||
require __DIR__ . '/Exceptions.php';
|
||||
require __DIR__ . '/Item.php';
|
||||
require __DIR__ . '/Format.php';
|
||||
require __DIR__ . '/Bridge.php';
|
||||
require __DIR__ . '/Cache.php';
|
||||
|
||||
$vendorLibSimpleHtmlDom = __DIR__ . PATH_VENDOR . '/simplehtmldom/simple_html_dom.php';
|
||||
if( !file_exists($vendorLibSimpleHtmlDom) ){
|
||||
throw new \HttpException('"PHP Simple HTML DOM Parser" is missing. Get it from http://simplehtmldom.sourceforge.net and place the script "simple_html_dom.php" in the same folder to allow me to work.', 500);
|
||||
}
|
||||
require_once $vendorLibSimpleHtmlDom;
|
||||
|
||||
/* Example use
|
||||
|
||||
require_once __DIR__ . '/lib/RssBridge.php';
|
||||
|
||||
// Data retrieval
|
||||
Bridge::setDir(__DIR__ . '/bridges/');
|
||||
$bridge = Bridge::create('GoogleSearch');
|
||||
$bridge->collectData($_REQUEST);
|
||||
|
||||
// Data transformation
|
||||
Format::setDir(__DIR__ . '/formats/');
|
||||
$format = Format::create('Atom');
|
||||
$format
|
||||
->setDatas($bridge->getDatas())
|
||||
->setExtraInfos(array(
|
||||
'name' => $bridge->getName(),
|
||||
'uri' => $bridge->getURI(),
|
||||
))
|
||||
->display();
|
||||
|
||||
*/
|
Reference in New Issue
Block a user