Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/BitrixWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Arrilot\BitrixModels;


class BitrixWrapper
{
protected static $configProvider;
protected static $cacheManagerProvider;

public static function registerConfigProvider($provider)
{
static::$configProvider = $provider;
}

/**
* @return \COption
*/
public static function configProvider()
{
if (!static::$configProvider) {
static::$configProvider = new \COption();
}

return static::$configProvider;
}

public static function registerCacheManagerProvider($provider)
{
static::$cacheManagerProvider = $provider;
}

/**
* @return \CCacheManager
*/
public static function cacheManagerProvider()
{
if (!static::$cacheManagerProvider) {
global $CACHE_MANAGER;
static::$cacheManagerProvider = $CACHE_MANAGER;
}

return static::$cacheManagerProvider;
}
}
15 changes: 14 additions & 1 deletion src/Queries/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Arrilot\BitrixModels\Queries;

use Arrilot\BitrixModels\BitrixWrapper;
use Arrilot\BitrixModels\Models\BaseBitrixModel;
use BadMethodCallException;
use Bitrix\Main\Data\Cache;
Expand All @@ -15,6 +16,8 @@
abstract class BaseQuery
{
use BaseRelationQuery;

public static $cacheDir = '/bitrix-models';

/**
* Query select.
Expand Down Expand Up @@ -518,7 +521,7 @@ protected function rememberInCache($key, $minutes, Closure $callback)
}

$cache = Cache::createInstance();
if ($cache->initCache($minutes * 60, $key, '/bitrix-models')) {
if ($cache->initCache($minutes * 60, $key, static::$cacheDir)) {
$vars = $cache->getVars();
return !empty($vars['isCollection']) ? new Collection($vars['cache']) : $vars['cache'];
}
Expand Down Expand Up @@ -577,4 +580,14 @@ protected function prepareMultiFilter(&$key, &$value)
{

}

/**
* Проверка включен ли тегированный кеш
* @return bool
*/
protected function isManagedCacheOn()
{
$config = BitrixWrapper::configProvider();
return $config::GetOptionString('main', 'component_managed_cache_on', 'N') == 'Y';
}
}
11 changes: 11 additions & 0 deletions src/Queries/ElementQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Arrilot\BitrixModels\Queries;

use Arrilot\BitrixCacher\Cache;
use Arrilot\BitrixModels\BitrixWrapper;
use CIBlock;
use Illuminate\Support\Collection;
use Arrilot\BitrixModels\Models\ElementModel;
Expand Down Expand Up @@ -159,6 +160,11 @@ protected function loadModels()
list($select, $chunkQuery) = $this->multiplySelectForMaxJoinsRestrictionIfNeeded($select);

$callback = function() use ($sort, $filter, $groupBy, $navigation, $select, $chunkQuery) {
if (static::isManagedCacheOn()) {
BitrixWrapper::cacheManagerProvider()->StartTagCache(static::$cacheDir);
BitrixWrapper::cacheManagerProvider()->RegisterTag("iblock_id_new");
}

if ($chunkQuery) {
$itemsChunks = [];
foreach ($select as $chunkIndex => $selectForChunk) {
Expand All @@ -176,6 +182,11 @@ protected function loadModels()
$this->addItemToResultsUsingKeyBy($items, new $this->modelName($arItem['ID'], $arItem));
}
}

if (static::isManagedCacheOn()) {
BitrixWrapper::cacheManagerProvider()->EndTagCache();
}

return new Collection($items);
};

Expand Down
1 change: 1 addition & 0 deletions tests/ElementModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function setUp()
{
TestElement::$bxObject = m::mock('obj');
ElementQuery::$cIblockObject = m::mock('cIblockObject');
parent::setUp();
}

public function tearDown()
Expand Down
1 change: 1 addition & 0 deletions tests/SectionModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SectionModelTest extends TestCase
public function setUp()
{
TestSection::$bxObject = m::mock('obj');
parent::setUp();
}

public function tearDown()
Expand Down
23 changes: 23 additions & 0 deletions tests/Stubs/COption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Arrilot\Tests\BitrixModels\Stubs;


class COption
{
public static $config = [
'main' => [
'component_managed_cache_on' => 'Y',
]
];

public static function GetOptionInt($module_id, $name, $def = "", $site = false)
{
return intval(static::GetOptionString($module_id, $name, $def, $site));
}

public static function GetOptionString($module_id, $name, $def = "", $site = false)
{
return static::$config[$module_id][$name] ?: $def;
}
}
20 changes: 20 additions & 0 deletions tests/Stubs/CacheManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Arrilot\Tests\BitrixModels\Stubs;


class CacheManager
{
public function StartTagCache()
{

}
public function RegisterTag()
{

}
public function EndTagCache()
{

}
}
8 changes: 8 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

namespace Arrilot\Tests\BitrixModels;

use Arrilot\BitrixModels\BitrixWrapper;
use Arrilot\Tests\BitrixModels\Stubs\CacheManager;
use Arrilot\Tests\BitrixModels\Stubs\COption;
use Mockery;
use PHPUnit_Framework_TestCase;

abstract class TestCase extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
BitrixWrapper::registerConfigProvider(new COption());
BitrixWrapper::registerCacheManagerProvider(new CacheManager());
}
public function tearDown()
{
Mockery::close();
Expand Down
1 change: 1 addition & 0 deletions tests/UserModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class UserModelTest extends TestCase
public function setUp()
{
TestUser::$bxObject = m::mock('obj');
parent::setUp();
}

public function tearDown()
Expand Down