Skip to content

Commit

Permalink
Merge pull request #53 from koenhoeijmakers/patch-1
Browse files Browse the repository at this point in the history
Fix for #46 (and format code in the process).
  • Loading branch information
willvincent authored Nov 6, 2018
2 parents 00689db + 758659c commit 120054c
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 172 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
/.idea
composer.phar
composer.lock
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"require": {
"php": ">=5.4.0",
"illuminate/support": "~5.0",
"simplepie/simplepie": "1.5.*"
"simplepie/simplepie": "1.5.*",
"ext-curl": "*"
},
"autoload": {
"psr-4": {
Expand Down
23 changes: 16 additions & 7 deletions src/Facades/FeedsFacade.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
<?php namespace willvincent\Feeds\Facades;
<?php

use Illuminate\Support\Facades\Facade;

class FeedsFacade extends Facade {
namespace willvincent\Feeds\Facades;

protected static function getFacadeAccessor() {
return 'Feeds';
}
use Illuminate\Support\Facades\Facade;

class FeedsFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*
* @throws \RuntimeException
*/
protected static function getFacadeAccessor()
{
return 'Feeds';
}
}
102 changes: 66 additions & 36 deletions src/FeedsFactory.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
<?php namespace willvincent\Feeds;

use SimplePie;
use Illuminate\Support\Arr;
use simplePie;

class FeedsFactory
{

/**
* The config.
*
* @var array
*/
protected $config;

protected $simplepie;
/**
* @var SimplePie
*/
protected $simplePie;

/**
* FeedsFactory constructor.
*
* @param $config
*/
public function __construct($config)
Expand All @@ -18,71 +28,91 @@ public function __construct($config)
}

/**
* @param array $feed_url RSS URL
* @param int $limit items returned per-feed with multifeeds
* @param bool|false $force_feed
*
* @return SimplePie
* @param array $feedUrl RSS URL
* @param int $limit items returned per-feed with multifeeds
* @param bool $forceFeed
* @param null $options
* @return simplePie
*/
public function make($feed_url = [], $limit = 0, $force_feed = false, $options = null)
public function make($feedUrl = [], $limit = 0, $forceFeed = false, $options = null)
{
$this->simplepie = new SimplePie();
$this->simplePie = new simplePie();
$this->configure();
$this->simplepie->set_feed_url($feed_url);
$this->simplepie->set_item_limit($limit);
if ($force_feed === true) {
$this->simplepie->force_feed(true);
$this->simplePie->set_feed_url($feedUrl);
$this->simplePie->set_item_limit($limit);

if ($forceFeed === true) {
$this->simplePie->force_feed(true);
}
if (!$this->config['strip_html_tags.disabled'] && !empty($this->config['strip_html_tags.tags']) && is_array($this->config['strip_html_tags.tags'])) {
$this->simplepie->strip_htmltags($this->config['strip_html_tags.tags']);

$stripHtmlTags = Arr::get($this->config, 'strip_html_tags.disabled', false);

if (! $stripHtmlTags && ! empty($this->config['strip_html_tags.tags']) && is_array($this->config['strip_html_tags.tags'])) {
$this->simplePie->strip_htmltags($this->config['strip_html_tags.tags']);
} else {
$this->simplepie->strip_htmltags(false);
$this->simplePie->strip_htmltags(false);
}
if (!$this->config['strip_attribute.disabled'] && !empty($this->config['strip_attribute.tags']) && is_array($this->config['strip_attribute.tags'])) {
$this->simplepie->strip_attributes($this->config['strip_attribute.tags']);

if (! $stripHtmlTags && ! empty($this->config['strip_attribute.tags']) && is_array($this->config['strip_attribute.tags'])) {
$this->simplePie->strip_attributes($this->config['strip_attribute.tags']);
} else {
$this->simplepie->strip_attributes(false);
$this->simplePie->strip_attributes(false);
}

if (isset($this->config['curl.timeout']) && is_int($this->config['curl.timeout'])) {
$this->simplepie->set_timeout($this->config['curl.timeout']);
$this->simplePie->set_timeout($this->config['curl.timeout']);
}

if (isset($options) && is_array($options)) {
if (isset($options['curl.options']) && is_array($options['curl.options'])) {
$this->simplepie->set_curl_options($this->simplepie->curl_options + $options['curl.options']);
$this->simplePie->set_curl_options($this->simplePie->curl_options + $options['curl.options']);
}

if (isset($options['strip_html_tags.tags']) && is_array($options['strip_html_tags.tags'])) {
$this->simplepie->strip_htmltags($options['strip_html_tags.tags']);
$this->simplePie->strip_htmltags($options['strip_html_tags.tags']);
}

if (isset($options['strip_attribute.tags']) && is_array($options['strip_attribute.tags'])) {
$this->simplepie->strip_attributes($options['strip_attribute.tags']);
$this->simplePie->strip_attributes($options['strip_attribute.tags']);
}

if (isset($options['curl.timeout']) && is_int($options['curl.timeout'])) {
$this->simplepie->set_timeout($options['curl.timeout']);
$this->simplePie->set_timeout($options['curl.timeout']);
}
}
$this->simplepie->init();

return $this->simplepie;
$this->simplePie->init();

return $this->simplePie;
}

/**
* Configure SimplePie.
*
* @return void
*/
protected function configure()
{
$curloptions = [];
$curlOptions = [];

if ($this->config['cache.disabled']) {
$this->simplepie->enable_cache(false);
$this->simplePie->enable_cache(false);
} else {
$this->simplepie->set_cache_location($this->config['cache.location']);
$this->simplepie->set_cache_duration($this->config['cache.life']);
$this->simplePie->set_cache_location($this->config['cache.location']);
$this->simplePie->set_cache_duration($this->config['cache.life']);
}

if (isset($this->config['curl.options']) && is_array($this->config['curl.options'])) {
$curloptions += $this->config['curl.options'];
$curlOptions += $this->config['curl.options'];
}

if ($this->config['ssl_check.disabled']) {
$curloptions[CURLOPT_SSL_VERIFYHOST] = false;
$curloptions[CURLOPT_SSL_VERIFYPEER] = false;
$curlOptions[CURLOPT_SSL_VERIFYHOST] = false;
$curlOptions[CURLOPT_SSL_VERIFYPEER] = false;
}
if (is_array($curloptions)) {
$this->simplepie->set_curl_options($curloptions);

if (is_array($curlOptions)) {
$this->simplePie->set_curl_options($curlOptions);
}
}
}
90 changes: 48 additions & 42 deletions src/FeedsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
<?php namespace willvincent\Feeds;

use Illuminate\Support\ServiceProvider;
use willvincent\Feeds\FeedsFactory;

class FeedsServiceProvider extends ServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

public function boot() {
$this->publishes([
__DIR__ . '/config/feeds.php' => config_path('feeds.php'),
]);
}

/**
* Register the service provider.
*
* @return void
*/
public function register() {
$this->app->singleton('Feeds', function () {
$config = config('feeds');

if (!$config) {
throw new \RunTimeException('Feeds configuration not found. Please run `php artisan vendor:publish`');
}

return new FeedsFactory($config);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return ['Feeds'];
}

class FeedsServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/config/feeds.php' => config_path('feeds.php'),
]);
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('Feeds', function () {
$config = config('feeds');

if (! $config) {
throw new \RunTimeException('Feeds configuration not found. Please run `php artisan vendor:publish`');
}

return new FeedsFactory($config);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['Feeds'];
}
}
Loading

0 comments on commit 120054c

Please sign in to comment.