data cache usage

This commit is contained in:
Yuri Kuznetsov
2020-09-21 11:40:26 +03:00
parent e17b88ca74
commit 79efc87ed2
2 changed files with 76 additions and 37 deletions

View File

@@ -37,6 +37,7 @@ use Espo\Core\{
Utils\Metadata,
Utils\Config,
Utils\Util,
Utils\DataCache,
};
/**
@@ -55,7 +56,7 @@ class HookManager
private $hooks;
protected $cacheFile = 'data/cache/application/hooks.php';
protected $cacheKey = 'hooks';
protected $ignoredMethodList = [
'__construct',
@@ -73,12 +74,14 @@ class HookManager
InjectableFactory $injectableFactory,
FileManager $fileManager,
Metadata $metadata,
Config $config
Config $config,
DataCache $dataCache
) {
$this->injectableFactory = $injectableFactory;
$this->fileManager = $fileManager;
$this->metadata = $metadata;
$this->config = $config;
$this->dataCache = $dataCache;
}
public function process(string $scope, string $hookName, $injection = null, array $options = [], array $hookData = [])
@@ -97,9 +100,14 @@ class HookManager
foreach ($hookList as $className) {
if (empty($this->hooks[$className])) {
$this->hooks[$className] = $this->createHookByClassName($className);
if (empty($this->hooks[$className])) continue;
if (empty($this->hooks[$className])) {
continue;
}
}
$hook = $this->hooks[$className];
$hook->$hookName($injection, $options, $hookData);
}
}
@@ -123,8 +131,9 @@ class HookManager
protected function loadHooks()
{
if ($this->config->get('useCache') && file_exists($this->cacheFile)) {
$this->data = $this->fileManager->getPhpContents($this->cacheFile);
if ($this->config->get('useCache') && $this->dataCache->has($this->cacheKey)) {
$this->data = $this->dataCache->get($this->cacheKey);
return;
}
@@ -134,6 +143,7 @@ class HookManager
foreach ($metadata->getModuleList() as $moduleName) {
$modulePath = str_replace('{*}', $moduleName, $this->paths['modulePath']);
$data = $this->getHookData($modulePath, $data);
}
@@ -142,13 +152,13 @@ class HookManager
$this->data = $this->sortHooks($data);
if ($this->config->get('useCache')) {
$this->fileManager->putPhpContents($this->cacheFile, $this->data);
$this->dataCache->store($this->cacheKey, $this->data);
}
}
protected function createHookByClassName(string $className) : object
{
if (!@class_exists($className)) {
if (!class_exists($className)) {
$GLOBALS['log']->error("Hook class '{$className}' does not exist.");
}
@@ -158,7 +168,7 @@ class HookManager
}
/**
* Get and merge hook data by checking the files exist in $hookDirs
* Get and merge hook data by checking the files exist in $hookDirs.
*
* @param $hookDirs - can be ['Espo/Hooks', 'Espo/Custom/Hooks', 'Espo/Modules/Crm/Hooks']
*/
@@ -172,6 +182,7 @@ class HookManager
if (!file_exists($hookDir)) {
continue;
}
$fileList = $this->fileManager->getFileList($hookDir, 1, '\.php$', true);
foreach ($fileList as $scopeName => $hookFiles) {
@@ -179,6 +190,7 @@ class HookManager
$normalizedScopeName = Util::normilizeScopeName($scopeName);
$scopeHooks = [];
foreach ($hookFiles as $hookFile) {
$hookFilePath = Util::concatPath($hookScopeDirPath, $hookFile);
$className = Util::getClassName($hookFilePath);
@@ -187,12 +199,16 @@ class HookManager
$hookMethods = array_diff($classMethods, $this->ignoredMethodList);
$hookMethods = array_filter($hookMethods, function ($item) {
if (strpos($item, 'set') === 0) return false;
if (strpos($item, 'set') === 0) {
return false;
}
return true;
});
foreach ($hookMethods as $hookType) {
$entityHookData = $hookData[$normalizedScopeName][$hookType] ?? [];
if (!$this->hookExists($className, $entityHookData)) {
$hookData[$normalizedScopeName][$hookType][] = [
'className' => $className,
@@ -241,6 +257,7 @@ class HookManager
}
$normalizedList = [];
foreach ($hookList as $hookData) {
$normalizedList[] = $hookData['className'];
}

View File

@@ -31,6 +31,15 @@ namespace tests\unit\Espo\Core;
use tests\unit\ReflectionHelper;
use Espo\Core\{
HookManager,
InjectableFactory,
Utils\Metadata,
Utils\Config,
Utils\File\Manager as FileManager,
Utils\DataCache,
};
class HookManagerTest extends \PHPUnit\Framework\TestCase
{
protected $object;
@@ -42,23 +51,26 @@ class HookManagerTest extends \PHPUnit\Framework\TestCase
protected function setUp() : void
{
$this->objects['metadata'] =
$this->getMockBuilder('Espo\\Core\\Utils\\Metadata')->disableOriginalConstructor()->getMock();
$this->metadata =
$this->getMockBuilder(Metadata::class)->disableOriginalConstructor()->getMock();
$this->objects['config'] =
$this->getMockBuilder('Espo\\Core\\Utils\\Config')->disableOriginalConstructor()->getMock();
$this->config =
$this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
$this->objects['injectableFactory'] =
$this->getMockBuilder('Espo\\Core\\InjectableFactory')->disableOriginalConstructor()->getMock();
$this->injectableFactory =
$this->getMockBuilder(InjectableFactory::class)->disableOriginalConstructor()->getMock();
$this->objects['fileManager'] = new \Espo\Core\Utils\File\Manager();
$this->dataCache =
$this->getMockBuilder(DataCache::class)->disableOriginalConstructor()->getMock();
$this->fileManager = new FileManager();
$this->object = new \Espo\Core\HookManager(
$this->objects['injectableFactory'],
$this->objects['fileManager'],
$this->objects['metadata'],
$this->objects['config']
$this->object = new HookManager(
$this->injectableFactory,
$this->fileManager,
$this->metadata,
$this->config,
$this->dataCache
);
$this->reflection = new ReflectionHelper($this->object);
@@ -73,9 +85,9 @@ class HookManagerTest extends \PHPUnit\Framework\TestCase
public function testIsHookExists()
{
$data = array (
'\\Espo\\Hooks\\Note\\Stream' => 8,
'\\Espo\\Hooks\\Note\\Mentions' => 9,
'\\Espo\\Hooks\\Note\\Notifications' => 14,
'Espo\\Hooks\\Note\\Stream' => 8,
'Espo\\Hooks\\Note\\Mentions' => 9,
'Espo\\Hooks\\Note\\Notifications' => 14,
);
$data = array (
@@ -93,11 +105,21 @@ class HookManagerTest extends \PHPUnit\Framework\TestCase
),
);
$this->assertTrue( $this->reflection->invokeMethod('hookExists', array('Espo\\Hooks\\Note\\Mentions', $data)) );
$this->assertTrue( $this->reflection->invokeMethod('hookExists', array('Espo\\Modules\\Crm\\Hooks\\Note\\Mentions', $data)) );
$this->assertTrue( $this->reflection->invokeMethod('hookExists', array('Espo\\Modules\\Test\\Hooks\\Note\\Mentions', $data)) );
$this->assertTrue( $this->reflection->invokeMethod('hookExists', array('Espo\\Modules\\Test\\Hooks\\Common\\Stream', $data)) );
$this->assertFalse( $this->reflection->invokeMethod('hookExists', array('Espo\\Hooks\\Note\\TestHook', $data)) );
$this->assertTrue(
$this->reflection->invokeMethod('hookExists', array('Espo\\Hooks\\Note\\Mentions', $data))
);
$this->assertTrue(
$this->reflection->invokeMethod('hookExists', array('Espo\\Modules\\Crm\\Hooks\\Note\\Mentions', $data))
);
$this->assertTrue(
$this->reflection->invokeMethod('hookExists', array('Espo\\Modules\\Test\\Hooks\\Note\\Mentions', $data))
);
$this->assertTrue(
$this->reflection->invokeMethod('hookExists', array('Espo\\Modules\\Test\\Hooks\\Common\\Stream', $data))
);
$this->assertFalse(
$this->reflection->invokeMethod('hookExists', array('Espo\\Hooks\\Note\\TestHook', $data))
);
}
public function testSortHooks()
@@ -219,12 +241,12 @@ class HookManagerTest extends \PHPUnit\Framework\TestCase
'customPath' => $this->filesPath . '/testCase1/custom/Espo/Custom/Hooks',
));
$this->objects['config']
$this->config
->expects($this->exactly(2))
->method('get')
->will($this->returnValue(false));
$this->objects['metadata']
$this->metadata
->expects($this->once())
->method('getModuleList')
->will($this->returnValue(array(
@@ -258,12 +280,12 @@ class HookManagerTest extends \PHPUnit\Framework\TestCase
'customPath' => $this->filesPath . '/testCase2/custom/Espo/Custom/Hooks',
));
$this->objects['config']
$this->config
->expects($this->exactly(2))
->method('get')
->will($this->returnValue(false));
$this->objects['metadata']
$this->metadata
->expects($this->once())
->method('getModuleList')
->will($this->returnValue(array(
@@ -298,12 +320,12 @@ class HookManagerTest extends \PHPUnit\Framework\TestCase
'customPath' => $this->filesPath . '/testCase2/custom/Espo/Custom/Hooks',
));
$this->objects['config']
$this->config
->expects($this->exactly(2))
->method('get')
->will($this->returnValue(false));
$this->objects['metadata']
$this->metadata
->expects($this->once())
->method('getModuleList')
->will($this->returnValue(array(
@@ -338,12 +360,12 @@ class HookManagerTest extends \PHPUnit\Framework\TestCase
'customPath' => $this->filesPath . '/testCase3/custom/Espo/Custom/Hooks',
));
$this->objects['config']
$this->config
->expects($this->exactly(2))
->method('get')
->will($this->returnValue(false));
$this->objects['metadata']
$this->metadata
->expects($this->at(0))
->method('getModuleList')
->will($this->returnValue(array(