From 1d9253425da90b50d69f59e4362f5065a0960361 Mon Sep 17 00:00:00 2001 From: Lilliance Date: Thu, 3 May 2018 19:54:00 +0300 Subject: [PATCH 1/4] Created getLastNews.php --- getLastNews.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 getLastNews.php diff --git a/getLastNews.php b/getLastNews.php new file mode 100644 index 0000000..e69de29 From 1d2505fcc99990f075b789fca55242b387424dbe Mon Sep 17 00:00:00 2001 From: Lilliance Date: Thu, 3 May 2018 22:59:58 +0300 Subject: [PATCH 2/4] Base realization of last news getter. --- getLastNews.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/getLastNews.php b/getLastNews.php index e69de29..2c5f084 100644 --- a/getLastNews.php +++ b/getLastNews.php @@ -0,0 +1,40 @@ +news, [ + 'title' => (string)$title, + 'link' => (string)$link, + 'description' => (string)$description + ]); + } + + public function getAll() + { + return $this->news; + } +} + +$rss = simplexml_load_file(RSS_SOURCE); + +$obLastNews = new PARSED_NEWS(); +for ($i = 0; $i < NEWS_COUNT; $i++) { + $item = $rss->channel->item[$i]; + $obLastNews->addNew($item->title, $item->link, $item->description); +} + +print_r($obLastNews->getAll()); \ No newline at end of file From 481b868e72a414d7d282c5d961df1e9a28238d5f Mon Sep 17 00:00:00 2001 From: Lilliance Date: Fri, 4 May 2018 20:46:16 +0300 Subject: [PATCH 3/4] =?UTF-8?q?=D0=91=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BA=D0=B5=D1=88=D0=B8=D1=80=D1=83=D0=B5=D0=BC=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=20CIBlockElement:?= =?UTF-8?q?:GetList();?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CachedCIblockElement.php | 160 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 CachedCIblockElement.php diff --git a/CachedCIblockElement.php b/CachedCIblockElement.php new file mode 100644 index 0000000..fd6ac9c --- /dev/null +++ b/CachedCIblockElement.php @@ -0,0 +1,160 @@ + + + "ASC"), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array(), $cacheTime = 10, $fetchType = 'Fetch') + { + + $cacheID = md5(json_encode(array( + $arOrder, $arFilter, $arGroupBy, $arNavStartParams, $arSelectFields, $cacheTime, $fetchType + ))); + + return self::Caching($cacheID, $cacheTime, function() use ($arOrder, $arFilter, $arGroupBy, $arNavStartParams, $arSelectFields, $fetchType) { + $res = parent::GetList($arOrder, $arFilter, $arGroupBy, $arNavStartParams, $arSelectFields); + + return self::FetchByType($res, $fetchType); + }); + } + + /** + * Добавил для примера переопределение метода GetByID. + * После стандартного параметра $ID теперь можно передать параметр $cacheTime - время кеширования и $fetchType - тип разбора объекта CDBResult (Fetch, GetList, GetNextElement). + * @param bool $ID + * @param int $cacheTime + * @param string $fetchType + * @return array|bool|mixed + */ + public static function GetByID($ID = false, $cacheTime = 10, $fetchType = 'Fetch') + { + if($ID === false) return false; + + $cacheID = md5(json_encode(array( + $ID, $cacheTime + ))); + + return self::Caching($cacheID, $cacheTime, function() use ($ID, $fetchType) { + $res = parent::GetByID($ID); + + return self::FetchByType($res, $fetchType); + }); + } + + /** + * Метод для разбора объекта CDBResult в массив, в соответствии с одним из вариантов разбора: + * Fetch + * GetList + * GetNextElement + * + * https://dev.1c-bitrix.ru/api_help/iblock/classes/ciblockresult/index.php + * https://dev.1c-bitrix.ru/api_help/main/reference/cdbresult/index.php + * @param CDBResult $CDBResult + * @param string $fetchType + * @return array|bool + */ + protected static function FetchByType(CDBResult $CDBResult, $fetchType = 'Fetch') + { + if($CDBResult === false) return false; + + $result = array(); + switch ($fetchType) { + case 'GetNext': + + while ($elem = $CDBResult->GetNext()) { + $result[] = $elem; + } + + break; + + case 'GetNextElement': + + while ($obElem = $CDBResult->GetNextElement()) { + $result[] = array( + 'FIELDS' => $obElem->GetFields(), + 'PROPERTIES' => $obElem->GetProperties() + ); + } + + break; + + case 'Fetch': + default: + + while ($fetched = $CDBResult->Fetch()) { + $result[] = $fetched; + } + + break; + } + + return $result; + } + + /** + * Метод отвечающий за кеширование запросов. + * Принимает ИД кеша, время кеширования и callback функцию, результат которой нужно закешировать. + * + * В callback имеет смысл передавать родительский метод и парсинг результата его работы. + * @param $cacheID + * @param $cacheTime + * @param $callback + * @return array|mixed + */ + private static function Caching($cacheID, $cacheTime, $callback) + { + $obCache = new CPHPCache(); + $result = array(); + if($obCache->InitCache($cacheTime, $cacheID, '/bitrix/cache/')) { + $result = $obCache->GetVars()['DATA']; + } else { + $obCache->StartDataCache(); + + if(is_callable($callback)) { + $result = call_user_func($callback); + } + + $obCache->EndDataCache(array('DATA' => $result)); + } + + return $result; + } +} + +// Клиентский код. Примеры использования: + +//$data = CachedCIblockElement::GetList( +// array('ID' => 'ASC'), +// array('IBLOCK_ID' => 42), +// false, +// array('nTopCount' => 3), +// array('ID', 'NAME'), +// 10, +// 'GetNext' +//); + +//var_dump($data); + +//var_dump(CachedCIblockElement::GetByID(7020, 5)) + +?> \ No newline at end of file From fcbd21934e8bbe694958cdea23ea320680f26bd6 Mon Sep 17 00:00:00 2001 From: Vladislav Pasechnik Date: Mon, 7 May 2018 12:28:26 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=9F=D0=BE=D1=82=D0=B5=D1=80=D1=8F=D0=BB?= =?UTF-8?q?=20<=3Fphp.=20:(?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- getLastNews.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/getLastNews.php b/getLastNews.php index 2c5f084..69fbf27 100644 --- a/getLastNews.php +++ b/getLastNews.php @@ -1,4 +1,4 @@ -