/
EntityIndex.php
79 lines (63 loc) · 2.04 KB
/
EntityIndex.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace Concrete\Core\Express\Search\Index;
use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Application\ApplicationAwareTrait;
use Concrete\Core\Attribute\Category\PageCategory;
use Concrete\Core\Attribute\Key\SearchIndexer\SearchIndexerInterface;
use Concrete\Core\Entity\Attribute\Key\ExpressKey;
use Concrete\Core\Entity\Attribute\Key\PageKey;
use Concrete\Core\Entity\Express\Entity;
use Concrete\Core\Search\Index\AbstractIndex;
use Concrete\Core\Search\Index\Driver\IndexingDriverInterface;
use Doctrine\DBAL\Connection;
class EntityIndex extends AbstractIndex implements ApplicationAwareInterface
{
use ApplicationAwareTrait;
/**
* @var Entity
*/
protected $entity;
/**
* @var Connection
*/
protected $connection;
public function __construct(Connection $connection, Entity $entity)
{
$this->connection = $connection;
$this->entity = $entity;
}
/**
* @return IndexingDriverInterface
*/
protected function getIndexer()
{
if (!$this->indexDriver) {
$this->indexDriver = $this->app->make(EntryIndexer::class);
}
return $this->indexDriver;
}
/**
* Clear out all indexed items
* @return void
*/
public function clear()
{
$category = $this->entity->getAttributeKeyCategory();
$table = $category->getIndexedSearchTable();
/** @var ExpressKey $key */
if (!$this->connection->tableExists($table)) {
$indexer = $category->getSearchIndexer();
$indexer->createRepository($category);
}
foreach ($category->getList() as $key) {
/** @var SearchIndexerInterface $indexer */
$indexer = $key->getSearchIndexer();
// Update the key tables
$indexer->updateSearchIndexKeyColumns($category, $key);
}
// Truncate the existing search index
if ($table) {
$this->connection->Execute(sprintf('truncate table %s', $table));
}
}
}