private $cache_timeout = 24;
private $feed_url = null;
private $posts = [];
+ private $stream_opts = [
+ 'http' => [
+ 'timeout' => 5
+ ]
+ ];
/**
* Default constructor
*/
public function __construct($url, $nocache = false)
{
- $this->feed_url = $url;
+ $this->feed_url = $this->getFeedURL($url);
//only if cache should be used
if ($nocache === false && GALETTE_MODE !== 'DEV') {
);
}
- $opts = [
- 'http' => [
- 'timeout' => 5
- ]
- ];
- $context = stream_context_create($opts);
+ $context = stream_context_create($this->stream_opts);
$data = file_get_contents($this->feed_url, false, $context);
if (!$data) {
throw new \Exception();
{
return $this->posts;
}
+
+ /**
+ * Get feed url, handle Galette website to check available langs
+ *
+ * @param string $url Requested URL
+ *
+ * @return string
+ */
+ public function getFeedURL($url)
+ {
+ global $i18n;
+
+ if (strpos($url, 'galette.eu') !== false || trim($url) == '') {
+ $url = 'https://galette.eu/site';
+ } elseif (strpos($url, 'localhost:4000') !== false) {
+ $url = 'http://localhost:4000/site';
+ } else {
+ return $url;
+ }
+
+ $galette_website_langs = $url . '/langs.json';
+ $context = stream_context_create($this->stream_opts);
+ $langs = json_decode(file_get_contents($galette_website_langs, false, $context));
+
+ if ($i18n->getAbbrev() != 'en' && in_array($i18n->getAbbrev(), $langs)) {
+ $url .= '/' . $i18n->getAbbrev();
+ }
+ $url .= '/feed.xml';
+
+ return $url;
+ }
}
*/
class News extends atoum
{
+ private $i18n;
+
+ /**
+ * Set up tests
+ *
+ * @param string $testMethod Method name
+ *
+ * @return void
+ */
+ public function beforeTestMethod($testMethod)
+ {
+ $this->i18n = new \Galette\Core\I18n();
+ global $i18n;
+ $i18n = $this->i18n;
+ }
+
/**
* Test news loading
*
//ensure allow_url_fopen is on
ini_set('allow_url_fopen', true);
//load news without caching
- $news = new \Galette\IO\News('http://galette.eu/dc/index.php/feed/atom', true);
+ $news = new \Galette\IO\News('https://galette.eu/site/feed.xml', true);
$posts = $news->getPosts();
$this->array($posts)
->size->isGreaterThan(0);
*/
public function testCacheNews()
{
- $file = GALETTE_CACHE_DIR . md5('http://galette.eu/dc/index.php/feed/atom') . '.cache';
+ //will use default lang to build RSS URL
+ $file = GALETTE_CACHE_DIR . md5('https://galette.eu/site/fr/feed.xml') . '.cache';
//ensure file does not exists
$this->boolean(file_exists($file))->isFalse;
//load news with caching
- $news = new \Galette\IO\News('http://galette.eu/dc/index.php/feed/atom');
+ $news = new \Galette\IO\News('https://galette.eu/site/feed.xml');
$posts = $news->getPosts();
$this->array($posts)
$touched = touch($file, $expired->getTimestamp());
$this->boolean($touched)->isTrue;
- $news = new \Galette\IO\News('http://galette.eu/dc/index.php/feed/atom');
+ $news = new \Galette\IO\News('https://galette.eu/site/feed.xml');
$mnewdate = \DateTime::createFromFormat(
$dformat,
date(
{
$this->assert('News cannot be loaded')
->if($this->function->ini_get = 0)
- ->given($news = new \Galette\IO\News('http://galette.eu/dc/index.php/feed/atom', true))
+ ->given($news = new \Galette\IO\News('https://galette.eu/site/feed.xml', true))
->then
->array($news->getPosts())
->hasSize(0);