From b4a1fd1d98d4fb17e1ed2e9843d150760e7bb68d Mon Sep 17 00:00:00 2001 From: Alexandr Kozlov Date: Wed, 23 May 2018 22:01:03 +0200 Subject: [PATCH 1/2] add iblock element caching class --- IblockElement.php | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 IblockElement.php diff --git a/IblockElement.php b/IblockElement.php new file mode 100644 index 0000000..ed28c07 --- /dev/null +++ b/IblockElement.php @@ -0,0 +1,83 @@ + 0) { + $cache = Main\Application::getInstance()->getManagedCache(); + + if($cache->read($ttl, $cacheId)) { + return $cache->get($cacheId); + } + } + + return null; + } + + /** + * Write data to cache + * + * @param array $data + * @param int $ttl + * @param string $cacheId + * @param string $cacheDir + */ + public static function writeToCache($data, $ttl, $cacheId) + { + if ($ttl > 0) { + $cache = Main\Application::getInstance()->getManagedCache(); + $ret = $cache->set($cacheId, $data); + } + } + + public static function GetList() + { + $args = func_get_args(); + $cache = end($args); + + if (isset($cache['ttl'])) { + array_pop($args); + } else { + $cache = ['ttl' => 600, 'fetchMethod' => 'Fetch']; + } + + $cacheId = md5(__METHOD__ . serialize($args)); + $data = static::readFromCache($cache['ttl'], $cacheId); + + if ($data !== null) { echo 'dsf'; die; + return $data; + } + + $result = call_user_func_array(['\CIBlockElement', __METHOD__], $args); + $return = []; + + while($item = call_user_func([$result, $cache['fetchMethod'] ?: 'Fetch'])) { + $return[] = $item; + } + + static::writeToCache($return, $cache['ttl']); + + return $return; + } +} + + +$items = CIBlockElementCache::GetList( + $order = ['ID' => 'ASC'], + $filter = ['IBLOCK_ID' => 1] +); + +print_r($items); \ No newline at end of file From 6a6729e68bf0ac7c88afa2d03d280c6d2385a9f6 Mon Sep 17 00:00:00 2001 From: Alexandr Kozlov Date: Wed, 23 May 2018 22:01:19 +0200 Subject: [PATCH 2/2] add rss reader class --- getLastNews.php | 140 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 getLastNews.php diff --git a/getLastNews.php b/getLastNews.php new file mode 100644 index 0000000..c236850 --- /dev/null +++ b/getLastNews.php @@ -0,0 +1,140 @@ +url = $url; + } + + public function rewind() + { + $this->position = 0; + } + + public function key() + { + return $this->position; + } + + public function current() + { + return $this->items[$this->position]; + } + + public function next() + { + $this->position++; + } + + public function valid() + { + if (array_key_exists($this->position, $this->items)) { + return true; + } + + if ($item = $this->read()) { + $this->items[$this->position] = $item; + + return true; + } + + return false; + } + + /** + * Open rss chanel for read + * + * @throws Exception + * + * @return DomNodeList + */ + protected function getSource() + { + if ($this->source === null) + { + $dom = new DomDocument('1.0'); + + if (!$dom->load($this->url)) { + throw new Exception('Unable open rss chanel'); + } + + $xpath = new DomXPath($dom); + $this->source = $xpath->query('//*/item'); + } + + return $this->source; + } + + /** + * Read rss item from chanel + * + * @return array|false + */ + protected function read() + { + $items = $this->getSource(); + + if ($items->length <= $this->position) { + return false; + } + + $node = $items->item($this->position); + + return static::nodeToArray($node); + } + + /** + * Convert DomNode to array + * + * @param DomNode $node + * + * @return mixed + */ + protected static function nodeToArray($node) + { + return (array) simplexml_import_dom($node); + } +} + +$rss = new RssReader('https://lenta.ru/rss'); + +foreach ($rss as $index => $item) +{ + echo 'Название: '. $item['title'], PHP_EOL; + echo 'Ссылка: '. $item['link'], PHP_EOL; + echo $item['description'], PHP_EOL; + echo '----------------------------', PHP_EOL; + + if ($index >= 4) { + break; + } +} \ No newline at end of file