From 758659c8e642646b6ac2b4278a7b6aa9dee99783 Mon Sep 17 00:00:00 2001 From: Koen Hoeijmakers Date: Fri, 5 Oct 2018 19:48:37 +0200 Subject: [PATCH] Prevent FeedFactory from throwing a "Undefined index: ssl_check.disabled" error for anybody upgrading, and reformat some stuff. --- src/FeedsFactory.php | 102 ++++++++++++++++++++++------------- src/FeedsServiceProvider.php | 90 ++++++++++++++++--------------- 2 files changed, 114 insertions(+), 78 deletions(-) diff --git a/src/FeedsFactory.php b/src/FeedsFactory.php index 308cba7..53c9c6c 100644 --- a/src/FeedsFactory.php +++ b/src/FeedsFactory.php @@ -1,15 +1,25 @@ 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); } } } diff --git a/src/FeedsServiceProvider.php b/src/FeedsServiceProvider.php index 1caa72d..6c3237f 100644 --- a/src/FeedsServiceProvider.php +++ b/src/FeedsServiceProvider.php @@ -1,47 +1,53 @@ 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']; + } }